Skip to content

Commit 6dd4e5d

Browse files
committed
encoding/asn1: export tag and class constants
Fixes golang#9236 Change-Id: I744d7f071e945ea6e6e50203d931f4678c8b545d Reviewed-on: https://go-review.googlesource.com/17311 Reviewed-by: Adam Langley <[email protected]> Reviewed-by: Russ Cox <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 71d2fa8 commit 6dd4e5d

File tree

4 files changed

+90
-88
lines changed

4 files changed

+90
-88
lines changed

src/encoding/asn1/asn1.go

+33-33
Original file line numberDiff line numberDiff line change
@@ -530,17 +530,17 @@ func parseSequenceOf(bytes []byte, sliceType reflect.Type, elemType reflect.Type
530530
return
531531
}
532532
switch t.tag {
533-
case tagIA5String, tagGeneralString, tagT61String, tagUTF8String:
533+
case TagIA5String, TagGeneralString, TagT61String, TagUTF8String:
534534
// We pretend that various other string types are
535535
// PRINTABLE STRINGs so that a sequence of them can be
536536
// parsed into a []string.
537-
t.tag = tagPrintableString
538-
case tagGeneralizedTime, tagUTCTime:
537+
t.tag = TagPrintableString
538+
case TagGeneralizedTime, TagUTCTime:
539539
// Likewise, both time types are treated the same.
540-
t.tag = tagUTCTime
540+
t.tag = TagUTCTime
541541
}
542542

543-
if t.class != classUniversal || t.isCompound != compoundType || t.tag != expectedTag {
543+
if t.class != ClassUniversal || t.isCompound != compoundType || t.tag != expectedTag {
544544
err = StructuralError{"sequence tag mismatch"}
545545
return
546546
}
@@ -624,28 +624,28 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
624624
return
625625
}
626626
var result interface{}
627-
if !t.isCompound && t.class == classUniversal {
627+
if !t.isCompound && t.class == ClassUniversal {
628628
innerBytes := bytes[offset : offset+t.length]
629629
switch t.tag {
630-
case tagPrintableString:
630+
case TagPrintableString:
631631
result, err = parsePrintableString(innerBytes)
632-
case tagIA5String:
632+
case TagIA5String:
633633
result, err = parseIA5String(innerBytes)
634-
case tagT61String:
634+
case TagT61String:
635635
result, err = parseT61String(innerBytes)
636-
case tagUTF8String:
636+
case TagUTF8String:
637637
result, err = parseUTF8String(innerBytes)
638-
case tagInteger:
638+
case TagInteger:
639639
result, err = parseInt64(innerBytes)
640-
case tagBitString:
640+
case TagBitString:
641641
result, err = parseBitString(innerBytes)
642-
case tagOID:
642+
case TagOID:
643643
result, err = parseObjectIdentifier(innerBytes)
644-
case tagUTCTime:
644+
case TagUTCTime:
645645
result, err = parseUTCTime(innerBytes)
646-
case tagGeneralizedTime:
646+
case TagGeneralizedTime:
647647
result, err = parseGeneralizedTime(innerBytes)
648-
case tagOctetString:
648+
case TagOctetString:
649649
result = innerBytes
650650
default:
651651
// If we don't know how to handle the type, we just leave Value as nil.
@@ -671,9 +671,9 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
671671
return
672672
}
673673
if params.explicit {
674-
expectedClass := classContextSpecific
674+
expectedClass := ClassContextSpecific
675675
if params.application {
676-
expectedClass = classApplication
676+
expectedClass = ClassApplication
677677
}
678678
if offset == len(bytes) {
679679
err = StructuralError{"explicit tag has no child"}
@@ -709,10 +709,10 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
709709
// type string. getUniversalType returns the tag for PrintableString
710710
// when it sees a string, so if we see a different string type on the
711711
// wire, we change the universal type to match.
712-
if universalTag == tagPrintableString {
713-
if t.class == classUniversal {
712+
if universalTag == TagPrintableString {
713+
if t.class == ClassUniversal {
714714
switch t.tag {
715-
case tagIA5String, tagGeneralString, tagT61String, tagUTF8String:
715+
case TagIA5String, TagGeneralString, TagT61String, TagUTF8String:
716716
universalTag = t.tag
717717
}
718718
} else if params.stringType != 0 {
@@ -722,24 +722,24 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
722722

723723
// Special case for time: UTCTime and GeneralizedTime both map to the
724724
// Go type time.Time.
725-
if universalTag == tagUTCTime && t.tag == tagGeneralizedTime && t.class == classUniversal {
726-
universalTag = tagGeneralizedTime
725+
if universalTag == TagUTCTime && t.tag == TagGeneralizedTime && t.class == ClassUniversal {
726+
universalTag = TagGeneralizedTime
727727
}
728728

729729
if params.set {
730-
universalTag = tagSet
730+
universalTag = TagSet
731731
}
732732

733-
expectedClass := classUniversal
733+
expectedClass := ClassUniversal
734734
expectedTag := universalTag
735735

736736
if !params.explicit && params.tag != nil {
737-
expectedClass = classContextSpecific
737+
expectedClass = ClassContextSpecific
738738
expectedTag = *params.tag
739739
}
740740

741741
if !params.explicit && params.application && params.tag != nil {
742-
expectedClass = classApplication
742+
expectedClass = ClassApplication
743743
expectedTag = *params.tag
744744
}
745745

@@ -781,7 +781,7 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
781781
case timeType:
782782
var time time.Time
783783
var err1 error
784-
if universalTag == tagUTCTime {
784+
if universalTag == TagUTCTime {
785785
time, err1 = parseUTCTime(innerBytes)
786786
} else {
787787
time, err1 = parseGeneralizedTime(innerBytes)
@@ -873,15 +873,15 @@ func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParam
873873
case reflect.String:
874874
var v string
875875
switch universalTag {
876-
case tagPrintableString:
876+
case TagPrintableString:
877877
v, err = parsePrintableString(innerBytes)
878-
case tagIA5String:
878+
case TagIA5String:
879879
v, err = parseIA5String(innerBytes)
880-
case tagT61String:
880+
case TagT61String:
881881
v, err = parseT61String(innerBytes)
882-
case tagUTF8String:
882+
case TagUTF8String:
883883
v, err = parseUTF8String(innerBytes)
884-
case tagGeneralString:
884+
case TagGeneralString:
885885
// GeneralString is specified in ISO-2022/ECMA-35,
886886
// A brief review suggests that it includes structures
887887
// that allow the encoding to change midstring and

src/encoding/asn1/asn1_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -409,10 +409,10 @@ func newBool(b bool) *bool { return &b }
409409

410410
var parseFieldParametersTestData []parseFieldParametersTest = []parseFieldParametersTest{
411411
{"", fieldParameters{}},
412-
{"ia5", fieldParameters{stringType: tagIA5String}},
413-
{"generalized", fieldParameters{timeType: tagGeneralizedTime}},
414-
{"utc", fieldParameters{timeType: tagUTCTime}},
415-
{"printable", fieldParameters{stringType: tagPrintableString}},
412+
{"ia5", fieldParameters{stringType: TagIA5String}},
413+
{"generalized", fieldParameters{timeType: TagGeneralizedTime}},
414+
{"utc", fieldParameters{timeType: TagUTCTime}},
415+
{"printable", fieldParameters{stringType: TagPrintableString}},
416416
{"optional", fieldParameters{optional: true}},
417417
{"explicit", fieldParameters{explicit: true, tag: new(int)}},
418418
{"application", fieldParameters{application: true, tag: new(int)}},

src/encoding/asn1/common.go

+38-36
Original file line numberDiff line numberDiff line change
@@ -18,29 +18,31 @@ import (
1818

1919
// Here are some standard tags and classes
2020

21+
// ASN.1 tags represent the type of the following object.
2122
const (
22-
tagBoolean = 1
23-
tagInteger = 2
24-
tagBitString = 3
25-
tagOctetString = 4
26-
tagOID = 6
27-
tagEnum = 10
28-
tagUTF8String = 12
29-
tagSequence = 16
30-
tagSet = 17
31-
tagPrintableString = 19
32-
tagT61String = 20
33-
tagIA5String = 22
34-
tagUTCTime = 23
35-
tagGeneralizedTime = 24
36-
tagGeneralString = 27
23+
TagBoolean = 1
24+
TagInteger = 2
25+
TagBitString = 3
26+
TagOctetString = 4
27+
TagOID = 6
28+
TagEnum = 10
29+
TagUTF8String = 12
30+
TagSequence = 16
31+
TagSet = 17
32+
TagPrintableString = 19
33+
TagT61String = 20
34+
TagIA5String = 22
35+
TagUTCTime = 23
36+
TagGeneralizedTime = 24
37+
TagGeneralString = 27
3738
)
3839

40+
// ASN.1 class types represent the namespace of the tag.
3941
const (
40-
classUniversal = 0
41-
classApplication = 1
42-
classContextSpecific = 2
43-
classPrivate = 3
42+
ClassUniversal = 0
43+
ClassApplication = 1
44+
ClassContextSpecific = 2
45+
ClassPrivate = 3
4446
)
4547

4648
type tagAndLength struct {
@@ -96,15 +98,15 @@ func parseFieldParameters(str string) (ret fieldParameters) {
9698
ret.tag = new(int)
9799
}
98100
case part == "generalized":
99-
ret.timeType = tagGeneralizedTime
101+
ret.timeType = TagGeneralizedTime
100102
case part == "utc":
101-
ret.timeType = tagUTCTime
103+
ret.timeType = TagUTCTime
102104
case part == "ia5":
103-
ret.stringType = tagIA5String
105+
ret.stringType = TagIA5String
104106
case part == "printable":
105-
ret.stringType = tagPrintableString
107+
ret.stringType = TagPrintableString
106108
case part == "utf8":
107-
ret.stringType = tagUTF8String
109+
ret.stringType = TagUTF8String
108110
case strings.HasPrefix(part, "default:"):
109111
i, err := strconv.ParseInt(part[8:], 10, 64)
110112
if err == nil {
@@ -136,33 +138,33 @@ func parseFieldParameters(str string) (ret fieldParameters) {
136138
func getUniversalType(t reflect.Type) (tagNumber int, isCompound, ok bool) {
137139
switch t {
138140
case objectIdentifierType:
139-
return tagOID, false, true
141+
return TagOID, false, true
140142
case bitStringType:
141-
return tagBitString, false, true
143+
return TagBitString, false, true
142144
case timeType:
143-
return tagUTCTime, false, true
145+
return TagUTCTime, false, true
144146
case enumeratedType:
145-
return tagEnum, false, true
147+
return TagEnum, false, true
146148
case bigIntType:
147-
return tagInteger, false, true
149+
return TagInteger, false, true
148150
}
149151
switch t.Kind() {
150152
case reflect.Bool:
151-
return tagBoolean, false, true
153+
return TagBoolean, false, true
152154
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
153-
return tagInteger, false, true
155+
return TagInteger, false, true
154156
case reflect.Struct:
155-
return tagSequence, true, true
157+
return TagSequence, true, true
156158
case reflect.Slice:
157159
if t.Elem().Kind() == reflect.Uint8 {
158-
return tagOctetString, false, true
160+
return TagOctetString, false, true
159161
}
160162
if strings.HasSuffix(t.Name(), "SET") {
161-
return tagSet, true, true
163+
return TagSet, true, true
162164
}
163-
return tagSequence, true, true
165+
return TagSequence, true, true
164166
case reflect.String:
165-
return tagPrintableString, false, true
167+
return TagPrintableString, false, true
166168
}
167169
return 0, false, false
168170
}

src/encoding/asn1/marshal.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func marshalBody(out *forkableWriter, value reflect.Value, params fieldParameter
414414
return nil
415415
case timeType:
416416
t := value.Interface().(time.Time)
417-
if params.timeType == tagGeneralizedTime || outsideUTCRange(t) {
417+
if params.timeType == TagGeneralizedTime || outsideUTCRange(t) {
418418
return marshalGeneralizedTime(out, t)
419419
} else {
420420
return marshalUTCTime(out, t)
@@ -493,9 +493,9 @@ func marshalBody(out *forkableWriter, value reflect.Value, params fieldParameter
493493
return
494494
case reflect.String:
495495
switch params.stringType {
496-
case tagIA5String:
496+
case TagIA5String:
497497
return marshalIA5String(out, v.String())
498-
case tagPrintableString:
498+
case TagPrintableString:
499499
return marshalPrintableString(out, v.String())
500500
default:
501501
return marshalUTF8String(out, v.String())
@@ -555,18 +555,18 @@ func marshalField(out *forkableWriter, v reflect.Value, params fieldParameters)
555555
err = StructuralError{fmt.Sprintf("unknown Go type: %v", v.Type())}
556556
return
557557
}
558-
class := classUniversal
558+
class := ClassUniversal
559559

560-
if params.timeType != 0 && tag != tagUTCTime {
560+
if params.timeType != 0 && tag != TagUTCTime {
561561
return StructuralError{"explicit time type given to non-time member"}
562562
}
563563

564-
if params.stringType != 0 && tag != tagPrintableString {
564+
if params.stringType != 0 && tag != TagPrintableString {
565565
return StructuralError{"explicit string type given to non-string member"}
566566
}
567567

568568
switch tag {
569-
case tagPrintableString:
569+
case TagPrintableString:
570570
if params.stringType == 0 {
571571
// This is a string without an explicit string type. We'll use
572572
// a PrintableString if the character set in the string is
@@ -576,24 +576,24 @@ func marshalField(out *forkableWriter, v reflect.Value, params fieldParameters)
576576
if !utf8.ValidString(v.String()) {
577577
return errors.New("asn1: string not valid UTF-8")
578578
}
579-
tag = tagUTF8String
579+
tag = TagUTF8String
580580
break
581581
}
582582
}
583583
} else {
584584
tag = params.stringType
585585
}
586-
case tagUTCTime:
587-
if params.timeType == tagGeneralizedTime || outsideUTCRange(v.Interface().(time.Time)) {
588-
tag = tagGeneralizedTime
586+
case TagUTCTime:
587+
if params.timeType == TagGeneralizedTime || outsideUTCRange(v.Interface().(time.Time)) {
588+
tag = TagGeneralizedTime
589589
}
590590
}
591591

592592
if params.set {
593-
if tag != tagSequence {
593+
if tag != TagSequence {
594594
return StructuralError{"non sequence tagged as set"}
595595
}
596-
tag = tagSet
596+
tag = TagSet
597597
}
598598

599599
tags, body := out.fork()
@@ -613,7 +613,7 @@ func marshalField(out *forkableWriter, v reflect.Value, params fieldParameters)
613613
if !params.explicit && params.tag != nil {
614614
// implicit tag.
615615
tag = *params.tag
616-
class = classContextSpecific
616+
class = ClassContextSpecific
617617
}
618618

619619
err = marshalTagAndLength(tags, tagAndLength{class, tag, bodyLen, isCompound})
@@ -623,7 +623,7 @@ func marshalField(out *forkableWriter, v reflect.Value, params fieldParameters)
623623

624624
if params.explicit {
625625
err = marshalTagAndLength(explicitTag, tagAndLength{
626-
class: classContextSpecific,
626+
class: ClassContextSpecific,
627627
tag: *params.tag,
628628
length: bodyLen + tags.Len(),
629629
isCompound: true,

0 commit comments

Comments
 (0)