Skip to content

Commit 85b7529

Browse files
authored
Merge pull request #346 from smallstep/mariano/bool
Allow to encode boolean types
2 parents 5a154e6 + 96d8185 commit 85b7529

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

x509util/extensions.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,8 @@ func parseFieldParameters(str string) (p asn1Params) {
495495
p.Type = part
496496
params = append(params, part)
497497
// types that are parsed from the string.
498-
// int and oid are not a type that can be set in a tag.
499-
case "int", "oid":
498+
// int, oid, and bool are not a type that can be set in a tag.
499+
case "int", "oid", "bool", "boolean":
500500
p.Type = part
501501
// types parsed from the string as a time
502502
case "utc", "generalized":
@@ -574,6 +574,12 @@ func marshalValue(value, params string) ([]byte, error) {
574574
}
575575
}
576576
return asn1.MarshalWithParams(t, p.Params)
577+
case "bool", "boolean":
578+
b, err := strconv.ParseBool(value)
579+
if err != nil {
580+
return nil, errors.Wrap(err, "invalid bool value")
581+
}
582+
return asn1.MarshalWithParams(b, p.Params)
577583
default: // if it's an unknown type, default to printable
578584
if !isPrintableString(value, true, true) {
579585
return nil, fmt.Errorf("invalid printable value")

x509util/extensions_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,12 @@ func TestSubjectAlternativeName_RawValue(t *testing.T) {
370370
{"otherName whitespaces", fields{"1.2.3.4", ",,printable:abc1234", nil}, asn1.RawValue{
371371
FullBytes: append([]byte{160, 16, 6, 3, 42, 3, 4, 160, 9, 19, 7}, []byte("abc1234")...),
372372
}, false},
373+
{"otherName bool:true", fields{"1.2.3.4", "bool:true", nil}, asn1.RawValue{
374+
FullBytes: []byte{160, 10, 6, 3, 42, 3, 4, 160, 3, 1, 1, 255},
375+
}, false},
376+
{"otherName boolean:false", fields{"1.2.3.4", "boolean:false", nil}, asn1.RawValue{
377+
FullBytes: []byte{160, 10, 6, 3, 42, 3, 4, 160, 3, 1, 1, 0},
378+
}, false},
373379
{"fail dn", fields{"dn", "1234", nil}, asn1.RawValue{}, true},
374380
{"fail x400Address", fields{"x400Address", "1234", nil}, asn1.RawValue{}, true},
375381
{"fail ediPartyName", fields{"ediPartyName", "1234", nil}, asn1.RawValue{}, true},

x509util/options_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func TestWithTemplate(t *testing.T) {
7979
{"id": "1.2.3.4", "value": {{ asn1Enc (first .Insecure.CR.DNSNames) | toJson }}},
8080
{"id": "1.2.3.5", "value": {{ asn1Marshal (first .Insecure.CR.DNSNames) | toJson }}},
8181
{"id": "1.2.3.6", "value": {{ asn1Seq (asn1Enc (first .Insecure.CR.DNSNames)) (asn1Enc "int:123456") | toJson }}},
82-
{"id": "1.2.3.7", "value": {{ asn1Set (asn1Marshal (first .Insecure.CR.DNSNames) "utf8") (asn1Enc "int:123456") | toJson }}}
82+
{"id": "1.2.3.7", "value": {{ asn1Set (asn1Marshal (first .Insecure.CR.DNSNames) "utf8") (asn1Enc "bool:true") | toJson }}}
8383
]
8484
}`
8585

@@ -181,7 +181,7 @@ func TestWithTemplate(t *testing.T) {
181181
{"id": "1.2.3.4", "value": "Ewdmb28uY29t"},
182182
{"id": "1.2.3.5", "value": "Ewdmb28uY29t"},
183183
{"id": "1.2.3.6", "value": "MA4TB2Zvby5jb20CAwHiQA=="},
184-
{"id": "1.2.3.7", "value": "MQ4MB2Zvby5jb20CAwHiQA=="}
184+
{"id": "1.2.3.7", "value": "MQwMB2Zvby5jb20BAf8="}
185185
]
186186
}`),
187187
}, false},
@@ -340,9 +340,11 @@ func Test_asn1Encode(t *testing.T) {
340340
{"ok generalized", args{"generalized:" + now.Format(time.RFC3339)}, mustMarshal(t, now, "generalized"), false},
341341
{"ok int", args{"int:1234"}, mustMarshal(t, 1234, ""), false},
342342
{"ok numeric", args{"numeric:1234"}, mustMarshal(t, "1234", "numeric"), false},
343+
{"ok bool", args{"bool:true"}, mustMarshal(t, true, ""), false},
343344
{"ok raw", args{"raw:" + mustMarshal(t, 1234, "")}, mustMarshal(t, 1234, ""), false},
344345
{"fail numeric", args{"numeric:not-a-number"}, "", true},
345346
{"fail time", args{"utc:not-a-time"}, "", true},
347+
{"fail bool", args{"bool:untrue"}, "", true},
346348
}
347349
for _, tt := range tests {
348350
t.Run(tt.name, func(t *testing.T) {
@@ -375,6 +377,7 @@ func Test_asn1Marshal(t *testing.T) {
375377
{"ok time", args{now, nil}, mustMarshal(t, now, "utc"), false},
376378
{"ok seq", args{[]any{"string", 1234}, nil}, mustMarshal(t, []any{"string", 1234}, ""), false},
377379
{"ok set", args{[]any{"string", 1234}, []string{"set"}}, mustMarshal(t, []any{"string", 1234}, "set"), false},
380+
{"ok bool", args{false, nil}, mustMarshal(t, false, ""), false},
378381
{"fail numeric", args{"string", []string{"numeric"}}, "", true},
379382
}
380383
for _, tt := range tests {

0 commit comments

Comments
 (0)