-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use utf8 string for private key subject with non-printable characters (…
- Loading branch information
Showing
7 changed files
with
103 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package utils | ||
|
||
// IsPrintableString reports whether the given s is a valid ASN.1 PrintableString. | ||
// If asterisk is allowAsterisk then '*' is also allowed, reflecting existing | ||
// practice. If ampersand is allowAmpersand then '&' is allowed as well. | ||
func IsPrintableString(s string, asterisk, ampersand bool) bool { | ||
for _, b := range s { | ||
valid := 'a' <= b && b <= 'z' || | ||
'A' <= b && b <= 'Z' || | ||
'0' <= b && b <= '9' || | ||
'\'' <= b && b <= ')' || | ||
'+' <= b && b <= '/' || | ||
b == ' ' || | ||
b == ':' || | ||
b == '=' || | ||
b == '?' || | ||
// This is technically not allowed in a PrintableString. | ||
// However, x509 certificates with wildcard strings don't | ||
// always use the correct string type so we permit it. | ||
(asterisk && b == '*') || | ||
// This is not technically allowed either. However, not | ||
// only is it relatively common, but there are also a | ||
// handful of CA certificates that contain it. At least | ||
// one of which will not expire until 2027. | ||
(ampersand && b == '&') | ||
|
||
if !valid { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsPrintableString(t *testing.T) { | ||
type args struct { | ||
s string | ||
asterisk bool | ||
ampersand bool | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{"empty", args{"", false, false}, true}, | ||
{"a", args{"a", false, false}, true}, | ||
{"spaces and caps", args{"My Leaf", false, false}, true}, | ||
{"default allowed punctuation", args{`(Hi+,-./):=?`, false, false}, true}, | ||
{"asterisk not allowed", args{"*", false, false}, false}, | ||
{"ampersand not allowed", args{"&", false, false}, false}, | ||
{"asterisk allowed", args{"*", true, false}, true}, | ||
{"ampersand allowed", args{"&", false, true}, true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert.Equal(t, tt.want, IsPrintableString(tt.args.s, tt.args.asterisk, tt.args.ampersand)) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,12 +67,19 @@ func TestEcPrivKeyToObject(t *testing.T) { | |
ecdsaPrivKey, ok := privKey.(*ecdsa.PrivateKey) | ||
require.True(t, ok) | ||
|
||
obj, err := ecPrivKeyToObject(ecdsaPrivKey, "leafkey", []byte{7}, "leaf") | ||
require.NoError(t, err) | ||
assert.NoError(t, obj.ValidateULong("CKA_CLASS", CKO_PRIVATE_KEY)) | ||
sub, err := hex.DecodeString("300f310d300b060355040313046c656166") | ||
require.NoError(t, err) | ||
assert.NoError(t, obj.Validate("CKA_SUBJECT", sub)) | ||
t.Run("printable subject", func(t *testing.T) { | ||
obj, err := ecPrivKeyToObject(ecdsaPrivKey, "leafkey", []byte{7}, "leaf") | ||
require.NoError(t, err) | ||
assert.NoError(t, obj.ValidateULong("CKA_CLASS", CKO_PRIVATE_KEY)) | ||
sub, err := hex.DecodeString("300f310d300b060355040313046c656166") | ||
require.NoError(t, err) | ||
assert.NoError(t, obj.Validate("CKA_SUBJECT", sub)) | ||
}) | ||
|
||
t.Run("utf8 subject", func(t *testing.T) { | ||
_, err := ecPrivKeyToObject(ecdsaPrivKey, "leafkey", []byte{7}, "[email protected]") | ||
require.NoError(t, err) | ||
}) | ||
} | ||
|
||
func TestNSSDB_AddPrivateKey(t *testing.T) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters