Skip to content

Commit 0effb85

Browse files
authored
Merge pull request #867 from smallstep/mariano/empty-sans
Skip empty SANs
2 parents a22a9a9 + 067c99c commit 0effb85

File tree

5 files changed

+43
-8
lines changed

5 files changed

+43
-8
lines changed

x509util/certificate_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ func TestNewCertificate(t *testing.T) {
146146
customSANsData.Set(SANsKey, []SubjectAlternativeName{
147147
{Type: PermanentIdentifierType, Value: "123456"},
148148
{Type: "1.2.3.4", Value: "utf8:otherName"},
149+
// An empty SAN won't be encoded
150+
{Type: "auto", Value: ""},
149151
})
150152
badCustomSANsData := CreateTemplateData("commonName", nil)
151153
badCustomSANsData.Set(SANsKey, []SubjectAlternativeName{
@@ -202,6 +204,7 @@ func TestNewCertificate(t *testing.T) {
202204
SANs: []SubjectAlternativeName{
203205
{Type: PermanentIdentifierType, Value: "123456"},
204206
{Type: "1.2.3.4", Value: "utf8:otherName"},
207+
{Type: "auto", Value: ""},
205208
},
206209
Extensions: []Extension{{
207210
ID: ObjectIdentifier{2, 5, 29, 17},
@@ -378,6 +381,7 @@ func TestNewCertificateTemplate(t *testing.T) {
378381
(dict "type" "hardwareModuleName" "value" .Insecure.User.hmn)
379382
(dict "type" "userPrincipalName" "value" .Token.upn)
380383
(dict "type" "1.2.3.4" "value" (printf "int:%s" .Insecure.User.id))
384+
(dict "type" "auto" "value" "")
381385
) | toJson }},
382386
"notBefore": "{{ .Token.nbf | formatTime }}",
383387
"notAfter": {{ now | dateModify "24h" | toJson }},

x509util/extensions.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,13 @@ type SubjectAlternativeName struct {
339339
ASN1Value json.RawMessage `json:"asn1Value,omitempty"`
340340
}
341341

342-
// Set sets the subject alternative name in the given x509.Certificate.
342+
// Set sets the subject alternative name in the given x509.Certificate. This
343+
// method will not set any SAN if the value is empty.
343344
func (s SubjectAlternativeName) Set(c *x509.Certificate) {
345+
if s.Value == "" {
346+
return
347+
}
348+
344349
switch strings.ToLower(s.Type) {
345350
case DNSType:
346351
c.DNSNames = append(c.DNSNames, s.Value)
@@ -1215,6 +1220,10 @@ func createSubjectAltNameExtension(dnsNames, emailAddresses MultiString, ipAddre
12151220

12161221
var rawValues []asn1.RawValue
12171222
for _, dnsName := range dnsNames {
1223+
if dnsName == "" {
1224+
continue
1225+
}
1226+
12181227
rawValue, err := SubjectAlternativeName{
12191228
Type: DNSType, Value: dnsName,
12201229
}.RawValue()
@@ -1226,6 +1235,10 @@ func createSubjectAltNameExtension(dnsNames, emailAddresses MultiString, ipAddre
12261235
}
12271236

12281237
for _, emailAddress := range emailAddresses {
1238+
if emailAddress == "" {
1239+
continue
1240+
}
1241+
12291242
rawValue, err := SubjectAlternativeName{
12301243
Type: EmailType, Value: emailAddress,
12311244
}.RawValue()
@@ -1237,6 +1250,10 @@ func createSubjectAltNameExtension(dnsNames, emailAddresses MultiString, ipAddre
12371250
}
12381251

12391252
for _, ip := range ipAddresses {
1253+
if len(ip) == 0 {
1254+
continue
1255+
}
1256+
12401257
rawValue, err := SubjectAlternativeName{
12411258
Type: IPType, Value: ip.String(),
12421259
}.RawValue()
@@ -1248,8 +1265,13 @@ func createSubjectAltNameExtension(dnsNames, emailAddresses MultiString, ipAddre
12481265
}
12491266

12501267
for _, uri := range uris {
1268+
v := uri.String()
1269+
if v == "" {
1270+
continue
1271+
}
1272+
12511273
rawValue, err := SubjectAlternativeName{
1252-
Type: URIType, Value: uri.String(),
1274+
Type: URIType, Value: v,
12531275
}.RawValue()
12541276
if err != nil {
12551277
return zero, err
@@ -1259,6 +1281,10 @@ func createSubjectAltNameExtension(dnsNames, emailAddresses MultiString, ipAddre
12591281
}
12601282

12611283
for _, san := range sans {
1284+
if san.Value == "" && len(san.ASN1Value) == 0 {
1285+
continue
1286+
}
1287+
12621288
rawValue, err := san.RawValue()
12631289
if err != nil {
12641290
return zero, err

x509util/extensions_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ func TestSubjectAlternativeName_Set(t *testing.T) {
243243
{"AutoIPAdd", fields{"", "::1"}, args{&x509.Certificate{IPAddresses: []net.IP{net.ParseIP("127.0.0.1")}}}, &x509.Certificate{IPAddresses: []net.IP{net.ParseIP("127.0.0.1"), net.ParseIP("::1")}}},
244244
{"AutoURI", fields{"Auto", "https://foo.com"}, args{&x509.Certificate{}}, &x509.Certificate{URIs: []*url.URL{{Scheme: "https", Host: "foo.com"}}}},
245245
{"AutoURIAdd", fields{"", "uri:foo:bar"}, args{&x509.Certificate{URIs: []*url.URL{{Scheme: "https", Host: "foo.com"}}}}, &x509.Certificate{URIs: []*url.URL{{Scheme: "https", Host: "foo.com"}, {Scheme: "uri", Opaque: "foo:bar"}}}},
246+
{"skipEmpty", fields{"auto", ""}, args{&x509.Certificate{}}, &x509.Certificate{}},
246247
{"panic", fields{"panic", "foo.com"}, args{&x509.Certificate{}}, &x509.Certificate{DNSNames: []string{"foo.com"}}},
247248
{"panicAdd", fields{"panic", "bar.com"}, args{&x509.Certificate{DNSNames: []string{"foo.com"}}}, &x509.Certificate{DNSNames: []string{"foo.com"}}},
248249
}
@@ -1249,7 +1250,7 @@ func Test_createSubjectAltNameExtension(t *testing.T) {
12491250
wantErr bool
12501251
}{
12511252
{"ok dns", args{Certificate{
1252-
DNSNames: []string{"foo.com"},
1253+
DNSNames: []string{"foo.com", ""},
12531254
}, false}, Extension{
12541255
ID: oidExtensionSubjectAltName,
12551256
Critical: false,
@@ -1263,21 +1264,21 @@ func Test_createSubjectAltNameExtension(t *testing.T) {
12631264
Value: append([]byte{0x30, 9, 0x80 | nameTypeDNS, 7}, []byte("foo.com")...),
12641265
}, false},
12651266
{"ok email", args{Certificate{
1266-
EmailAddresses: []string{"[email protected]"},
1267+
EmailAddresses: []string{"[email protected]", ""},
12671268
}, false}, Extension{
12681269
ID: oidExtensionSubjectAltName,
12691270
Critical: false,
12701271
Value: append([]byte{0x30, 13, 0x80 | nameTypeEmail, 11}, []byte("[email protected]")...),
12711272
}, false},
12721273
{"ok uri", args{Certificate{
1273-
URIs: []*url.URL{{Scheme: "urn", Opaque: "foo:bar"}},
1274+
URIs: []*url.URL{{Scheme: "urn", Opaque: "foo:bar"}, {}},
12741275
}, false}, Extension{
12751276
ID: oidExtensionSubjectAltName,
12761277
Critical: false,
12771278
Value: append([]byte{0x30, 13, 0x80 | nameTypeURI, 11}, []byte("urn:foo:bar")...),
12781279
}, false},
12791280
{"ok ip", args{Certificate{
1280-
IPAddresses: []net.IP{net.ParseIP("1.2.3.4")},
1281+
IPAddresses: []net.IP{net.ParseIP("1.2.3.4"), {}},
12811282
}, false}, Extension{
12821283
ID: oidExtensionSubjectAltName,
12831284
Critical: false,
@@ -1289,6 +1290,7 @@ func Test_createSubjectAltNameExtension(t *testing.T) {
12891290
{Type: "email", Value: "[email protected]"},
12901291
{Type: "uri", Value: "urn:foo:bar"},
12911292
{Type: "ip", Value: "1.2.3.4"},
1293+
{Type: "auto", Value: ""},
12921294
},
12931295
}, false}, Extension{
12941296
ID: oidExtensionSubjectAltName,
@@ -1322,7 +1324,7 @@ func Test_createSubjectAltNameExtension(t *testing.T) {
13221324
}, nil),
13231325
}, false},
13241326
{"fail dns", args{Certificate{
1325-
DNSNames: []string{""},
1327+
DNSNames: []string{"xn--bücher.example.com"},
13261328
}, false}, Extension{}, true},
13271329
{"fail email", args{Certificate{
13281330
EmailAddresses: []string{"nö[email protected]"},

x509util/utils.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ func SplitSANs(sans []string) (dnsNames []string, ips []net.IP, emails []string,
5555
emails = []string{}
5656
uris = []*url.URL{}
5757
for _, san := range sans {
58+
if san == "" {
59+
continue
60+
}
5861
ip := net.ParseIP(san)
5962
u, err := url.Parse(san)
6063
switch {

x509util/utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func TestSplitSANs(t *testing.T) {
6161
{Scheme: "mailto", Opaque: "[email protected]"},
6262
{Scheme: "urn", Opaque: "uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959"},
6363
}},
64-
{"mixed", args{[]string{
64+
{"mixed", args{[]string{"",
6565
"foo.internal", "https://ca.smallstep.com", "[email protected]",
6666
"urn:uuid:ddfe62ba-7e99-4bc1-83b3-8f57fe3e9959", "[email protected]",
6767
"1.1.1.1", "bar.internal", "https://google.com/index.html",

0 commit comments

Comments
 (0)