Skip to content

Commit 9694c65

Browse files
authored
Use utf8 string for private key subject with non-printable characters (#710)
1 parent 26e1ce2 commit 9694c65

File tree

7 files changed

+103
-46
lines changed

7 files changed

+103
-46
lines changed

internal/utils/asn1.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package utils
2+
3+
// IsPrintableString reports whether the given s is a valid ASN.1 PrintableString.
4+
// If asterisk is allowAsterisk then '*' is also allowed, reflecting existing
5+
// practice. If ampersand is allowAmpersand then '&' is allowed as well.
6+
func IsPrintableString(s string, asterisk, ampersand bool) bool {
7+
for _, b := range s {
8+
valid := 'a' <= b && b <= 'z' ||
9+
'A' <= b && b <= 'Z' ||
10+
'0' <= b && b <= '9' ||
11+
'\'' <= b && b <= ')' ||
12+
'+' <= b && b <= '/' ||
13+
b == ' ' ||
14+
b == ':' ||
15+
b == '=' ||
16+
b == '?' ||
17+
// This is technically not allowed in a PrintableString.
18+
// However, x509 certificates with wildcard strings don't
19+
// always use the correct string type so we permit it.
20+
(asterisk && b == '*') ||
21+
// This is not technically allowed either. However, not
22+
// only is it relatively common, but there are also a
23+
// handful of CA certificates that contain it. At least
24+
// one of which will not expire until 2027.
25+
(ampersand && b == '&')
26+
27+
if !valid {
28+
return false
29+
}
30+
}
31+
32+
return true
33+
}

internal/utils/asn1_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestIsPrintableString(t *testing.T) {
10+
type args struct {
11+
s string
12+
asterisk bool
13+
ampersand bool
14+
}
15+
tests := []struct {
16+
name string
17+
args args
18+
want bool
19+
}{
20+
{"empty", args{"", false, false}, true},
21+
{"a", args{"a", false, false}, true},
22+
{"spaces and caps", args{"My Leaf", false, false}, true},
23+
{"default allowed punctuation", args{`(Hi+,-./):=?`, false, false}, true},
24+
{"asterisk not allowed", args{"*", false, false}, false},
25+
{"ampersand not allowed", args{"&", false, false}, false},
26+
{"asterisk allowed", args{"*", true, false}, true},
27+
{"ampersand allowed", args{"&", false, true}, true},
28+
}
29+
for _, tt := range tests {
30+
t.Run(tt.name, func(t *testing.T) {
31+
assert.Equal(t, tt.want, IsPrintableString(tt.args.s, tt.args.asterisk, tt.args.ampersand))
32+
})
33+
}
34+
}

nssdb/keys.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"fmt"
1111
"math/big"
1212

13+
"go.step.sm/crypto/internal/utils"
1314
"golang.org/x/crypto/cryptobyte"
1415
)
1516

@@ -99,8 +100,13 @@ type certCN struct {
99100
CommonName string `asn1:"printable"`
100101
}
101102

103+
type certCNUTF8 struct {
104+
OID asn1.ObjectIdentifier
105+
CommonName string `asn1:"utf8"`
106+
}
107+
102108
type privateKeySubject struct {
103-
List []certCN `asn1:"set"`
109+
List []any `asn1:"set"`
104110
}
105111

106112
func ecPrivKeyToObject(priv *ecdsa.PrivateKey, name string, id []byte, certCNs ...string) (*Object, error) {
@@ -141,10 +147,17 @@ func ecPrivKeyToObject(priv *ecdsa.PrivateKey, name string, id []byte, certCNs .
141147
if len(certCNs) > 0 {
142148
sub := privateKeySubject{}
143149
for _, cn := range certCNs {
144-
sub.List = append(sub.List, certCN{
145-
OID: asn1.ObjectIdentifier{2, 5, 4, 3},
146-
CommonName: cn,
147-
})
150+
if utils.IsPrintableString(cn, false, false) {
151+
sub.List = append(sub.List, certCN{
152+
OID: asn1.ObjectIdentifier{2, 5, 4, 3},
153+
CommonName: cn,
154+
})
155+
} else {
156+
sub.List = append(sub.List, certCNUTF8{
157+
OID: asn1.ObjectIdentifier{2, 5, 4, 3},
158+
CommonName: cn,
159+
})
160+
}
148161
}
149162
subASN1, err := asn1.Marshal(sub)
150163
if err != nil {

nssdb/keys_test.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,19 @@ func TestEcPrivKeyToObject(t *testing.T) {
6767
ecdsaPrivKey, ok := privKey.(*ecdsa.PrivateKey)
6868
require.True(t, ok)
6969

70-
obj, err := ecPrivKeyToObject(ecdsaPrivKey, "leafkey", []byte{7}, "leaf")
71-
require.NoError(t, err)
72-
assert.NoError(t, obj.ValidateULong("CKA_CLASS", CKO_PRIVATE_KEY))
73-
sub, err := hex.DecodeString("300f310d300b060355040313046c656166")
74-
require.NoError(t, err)
75-
assert.NoError(t, obj.Validate("CKA_SUBJECT", sub))
70+
t.Run("printable subject", func(t *testing.T) {
71+
obj, err := ecPrivKeyToObject(ecdsaPrivKey, "leafkey", []byte{7}, "leaf")
72+
require.NoError(t, err)
73+
assert.NoError(t, obj.ValidateULong("CKA_CLASS", CKO_PRIVATE_KEY))
74+
sub, err := hex.DecodeString("300f310d300b060355040313046c656166")
75+
require.NoError(t, err)
76+
assert.NoError(t, obj.Validate("CKA_SUBJECT", sub))
77+
})
78+
79+
t.Run("utf8 subject", func(t *testing.T) {
80+
_, err := ecPrivKeyToObject(ecdsaPrivKey, "leafkey", []byte{7}, "[email protected]")
81+
require.NoError(t, err)
82+
})
7683
}
7784

7885
func TestNSSDB_AddPrivateKey(t *testing.T) {

x509util/certificate_request.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"encoding/json"
1111

1212
"github.com/pkg/errors"
13+
"go.step.sm/crypto/internal/utils"
1314
"golang.org/x/crypto/cryptobyte"
1415
cryptobyte_asn1 "golang.org/x/crypto/cryptobyte/asn1"
1516
)
@@ -176,7 +177,7 @@ func (c *CertificateRequest) addChallengePassword(asn1Data []byte) ([]byte, erro
176177
child.AddASN1ObjectIdentifier(oidChallengePassword)
177178
child.AddASN1(cryptobyte_asn1.SET, func(value *cryptobyte.Builder) {
178179
switch {
179-
case isPrintableString(c.ChallengePassword, true, true):
180+
case utils.IsPrintableString(c.ChallengePassword, true, true):
180181
value.AddASN1(cryptobyte_asn1.PrintableString, func(s *cryptobyte.Builder) {
181182
s.AddBytes([]byte(c.ChallengePassword))
182183
})

x509util/extensions.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"time"
1717

1818
"github.com/pkg/errors"
19+
"go.step.sm/crypto/internal/utils"
1920
)
2021

2122
func convertName(s string) string {
@@ -559,7 +560,7 @@ func marshalValue(value, params string) ([]byte, error) {
559560
}
560561
return asn1.MarshalWithParams(value, p.Params)
561562
case "printable":
562-
if !isPrintableString(value, true, true) {
563+
if !utils.IsPrintableString(value, true, true) {
563564
return nil, fmt.Errorf("invalid printable value")
564565
}
565566
return asn1.MarshalWithParams(value, p.Params)
@@ -581,7 +582,7 @@ func marshalValue(value, params string) ([]byte, error) {
581582
}
582583
return asn1.MarshalWithParams(b, p.Params)
583584
default: // if it's an unknown type, default to printable
584-
if !isPrintableString(value, true, true) {
585+
if !utils.IsPrintableString(value, true, true) {
585586
return nil, fmt.Errorf("invalid printable value")
586587
}
587588
return asn1.MarshalWithParams(value, p.Params)

x509util/utils.go

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -179,35 +179,3 @@ func isNumericString(s string) bool {
179179

180180
return true
181181
}
182-
183-
// isPrintableString reports whether the given s is a valid ASN.1 PrintableString.
184-
// If asterisk is allowAsterisk then '*' is also allowed, reflecting existing
185-
// practice. If ampersand is allowAmpersand then '&' is allowed as well.
186-
func isPrintableString(s string, asterisk, ampersand bool) bool {
187-
for _, b := range s {
188-
valid := 'a' <= b && b <= 'z' ||
189-
'A' <= b && b <= 'Z' ||
190-
'0' <= b && b <= '9' ||
191-
'\'' <= b && b <= ')' ||
192-
'+' <= b && b <= '/' ||
193-
b == ' ' ||
194-
b == ':' ||
195-
b == '=' ||
196-
b == '?' ||
197-
// This is technically not allowed in a PrintableString.
198-
// However, x509 certificates with wildcard strings don't
199-
// always use the correct string type so we permit it.
200-
(asterisk && b == '*') ||
201-
// This is not technically allowed either. However, not
202-
// only is it relatively common, but there are also a
203-
// handful of CA certificates that contain it. At least
204-
// one of which will not expire until 2027.
205-
(ampersand && b == '&')
206-
207-
if !valid {
208-
return false
209-
}
210-
}
211-
212-
return true
213-
}

0 commit comments

Comments
 (0)