1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
5 // Package asn1 implements parsing of DER-encoded ASN.1 data structures,
6 // as defined in ITU-T Rec X.690.
8 // See also ``A Layman's Guide to a Subset of ASN.1, BER, and DER,''
9 // http://luca.ntop.org/Teaching/Appunti/asn1.html.
12 // ASN.1 is a syntax for specifying abstract objects and BER, DER, PER, XER etc
13 // are different encoding formats for those objects. Here, we'll be dealing
14 // with DER, the Distinguished Encoding Rules. DER is used in X.509 because
15 // it's fast to parse and, unlike BER, has a unique encoding for every object.
16 // When calculating hashes over objects, it's important that the resulting
17 // bytes be the same at both ends and DER removes this margin of error.
19 // ASN.1 is very complex and this package doesn't attempt to implement
20 // everything by any means.
29 // A StructuralError suggests that the ASN.1 data is valid, but the Go type
30 // which is receiving it doesn't match.
31 type StructuralError struct {
35 func (e StructuralError) Error() string { return "ASN.1 structure error: " + e.Msg }
37 // A SyntaxError suggests that the ASN.1 data is invalid.
38 type SyntaxError struct {
42 func (e SyntaxError) Error() string { return "ASN.1 syntax error: " + e.Msg }
44 // We start by dealing with each of the primitive types in turn.
48 func parseBool(bytes []byte) (ret bool, err error) {
50 err = SyntaxError{"invalid boolean"}
54 return bytes[0] != 0, nil
59 // parseInt64 treats the given bytes as a big-endian, signed integer and
60 // returns the result.
61 func parseInt64(bytes []byte) (ret int64, err error) {
63 // We'll overflow an int64 in this case.
64 err = StructuralError{"integer too large"}
67 for bytesRead := 0; bytesRead < len(bytes); bytesRead++ {
69 ret |= int64(bytes[bytesRead])
72 // Shift up and down in order to sign extend the result.
73 ret <<= 64 - uint8(len(bytes))*8
74 ret >>= 64 - uint8(len(bytes))*8
78 // parseInt treats the given bytes as a big-endian, signed integer and returns
80 func parseInt(bytes []byte) (int, error) {
81 ret64, err := parseInt64(bytes)
85 if ret64 != int64(int(ret64)) {
86 return 0, StructuralError{"integer too large"}
88 return int(ret64), nil
91 var bigOne = big.NewInt(1)
93 // parseBigInt treats the given bytes as a big-endian, signed integer and returns
95 func parseBigInt(bytes []byte) *big.Int {
97 if len(bytes) > 0 && bytes[0]&0x80 == 0x80 {
98 // This is a negative number.
99 notBytes := make([]byte, len(bytes))
100 for i := range notBytes {
101 notBytes[i] = ^bytes[i]
103 ret.SetBytes(notBytes)
114 // BitString is the structure to use when you want an ASN.1 BIT STRING type. A
115 // bit string is padded up to the nearest byte in memory and the number of
116 // valid bits is recorded. Padding bits will be zero.
117 type BitString struct {
118 Bytes []byte // bits packed into bytes.
119 BitLength int // length in bits.
122 // At returns the bit at the given index. If the index is out of range it
124 func (b BitString) At(i int) int {
125 if i < 0 || i >= b.BitLength {
130 return int(b.Bytes[x]>>y) & 1
133 // RightAlign returns a slice where the padding bits are at the beginning. The
134 // slice may share memory with the BitString.
135 func (b BitString) RightAlign() []byte {
136 shift := uint(8 - (b.BitLength % 8))
137 if shift == 8 || len(b.Bytes) == 0 {
141 a := make([]byte, len(b.Bytes))
142 a[0] = b.Bytes[0] >> shift
143 for i := 1; i < len(b.Bytes); i++ {
144 a[i] = b.Bytes[i-1] << (8 - shift)
145 a[i] |= b.Bytes[i] >> shift
151 // parseBitString parses an ASN.1 bit string from the given byte slice and returns it.
152 func parseBitString(bytes []byte) (ret BitString, err error) {
154 err = SyntaxError{"zero length BIT STRING"}
157 paddingBits := int(bytes[0])
158 if paddingBits > 7 ||
159 len(bytes) == 1 && paddingBits > 0 ||
160 bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 {
161 err = SyntaxError{"invalid padding bits in BIT STRING"}
164 ret.BitLength = (len(bytes)-1)*8 - paddingBits
165 ret.Bytes = bytes[1:]
171 // An ObjectIdentifier represents an ASN.1 OBJECT IDENTIFIER.
172 type ObjectIdentifier []int
174 // Equal returns true iff oi and other represent the same identifier.
175 func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool {
176 if len(oi) != len(other) {
179 for i := 0; i < len(oi); i++ {
180 if oi[i] != other[i] {
188 // parseObjectIdentifier parses an OBJECT IDENTIFIER from the given bytes and
189 // returns it. An object identifier is a sequence of variable length integers
190 // that are assigned in a hierarchy.
191 func parseObjectIdentifier(bytes []byte) (s []int, err error) {
193 err = SyntaxError{"zero length OBJECT IDENTIFIER"}
197 // In the worst case, we get two elements from the first byte (which is
198 // encoded differently) and then every varint is a single byte long.
199 s = make([]int, len(bytes)+1)
201 // The first byte is 40*value1 + value2:
202 s[0] = int(bytes[0]) / 40
203 s[1] = int(bytes[0]) % 40
205 for offset := 1; offset < len(bytes); i++ {
207 v, offset, err = parseBase128Int(bytes, offset)
219 // An Enumerated is represented as a plain int.
224 // A Flag accepts any data and is set to true if present.
227 // parseBase128Int parses a base-128 encoded int from the given offset in the
228 // given byte slice. It returns the value and the new offset.
229 func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err error) {
231 for shifted := 0; offset < len(bytes); shifted++ {
233 err = StructuralError{"base 128 integer too large"}
244 err = SyntaxError{"truncated base 128 integer"}
250 func parseUTCTime(bytes []byte) (ret time.Time, err error) {
252 ret, err = time.Parse("0601021504Z0700", s)
256 ret, err = time.Parse("060102150405Z0700", s)
260 // parseGeneralizedTime parses the GeneralizedTime from the given byte slice
261 // and returns the resulting time.
262 func parseGeneralizedTime(bytes []byte) (ret time.Time, err error) {
263 return time.Parse("20060102150405Z0700", string(bytes))
268 // parsePrintableString parses a ASN.1 PrintableString from the given byte
269 // array and returns it.
270 func parsePrintableString(bytes []byte) (ret string, err error) {
271 for _, b := range bytes {
273 err = SyntaxError{"PrintableString contains invalid character"}
281 // isPrintable returns true iff the given b is in the ASN.1 PrintableString set.
282 func isPrintable(b byte) bool {
283 return 'a' <= b && b <= 'z' ||
284 'A' <= b && b <= 'Z' ||
285 '0' <= b && b <= '9' ||
286 '\'' <= b && b <= ')' ||
287 '+' <= b && b <= '/' ||
292 // This is technically not allowed in a PrintableString.
293 // However, x509 certificates with wildcard strings don't
294 // always use the correct string type so we permit it.
300 // parseIA5String parses a ASN.1 IA5String (ASCII string) from the given
301 // byte slice and returns it.
302 func parseIA5String(bytes []byte) (ret string, err error) {
303 for _, b := range bytes {
305 err = SyntaxError{"IA5String contains invalid character"}
315 // parseT61String parses a ASN.1 T61String (8-bit clean string) from the given
316 // byte slice and returns it.
317 func parseT61String(bytes []byte) (ret string, err error) {
318 return string(bytes), nil
323 // parseUTF8String parses a ASN.1 UTF8String (raw UTF-8) from the given byte
324 // array and returns it.
325 func parseUTF8String(bytes []byte) (ret string, err error) {
326 return string(bytes), nil
329 // A RawValue represents an undecoded ASN.1 object.
330 type RawValue struct {
334 FullBytes []byte // includes the tag and length
337 // RawContent is used to signal that the undecoded, DER data needs to be
338 // preserved for a struct. To use it, the first field of the struct must have
339 // this type. It's an error for any of the other fields to have this type.
340 type RawContent []byte
344 // parseTagAndLength parses an ASN.1 tag and length pair from the given offset
345 // into a byte slice. It returns the parsed data and the new offset. SET and
346 // SET OF (tag 17) are mapped to SEQUENCE and SEQUENCE OF (tag 16) since we
347 // don't distinguish between ordered and unordered objects in this code.
348 func parseTagAndLength(bytes []byte, initOffset int) (ret tagAndLength, offset int, err error) {
352 ret.class = int(b >> 6)
353 ret.isCompound = b&0x20 == 0x20
354 ret.tag = int(b & 0x1f)
356 // If the bottom five bits are set, then the tag number is actually base 128
357 // encoded afterwards
359 ret.tag, offset, err = parseBase128Int(bytes, offset)
364 if offset >= len(bytes) {
365 err = SyntaxError{"truncated tag or length"}
371 // The length is encoded in the bottom 7 bits.
372 ret.length = int(b & 0x7f)
374 // Bottom 7 bits give the number of length bytes to follow.
375 numBytes := int(b & 0x7f)
376 // We risk overflowing a signed 32-bit number if we accept more than 3 bytes.
378 err = StructuralError{"length too large"}
382 err = SyntaxError{"indefinite length found (not DER)"}
386 for i := 0; i < numBytes; i++ {
387 if offset >= len(bytes) {
388 err = SyntaxError{"truncated tag or length"}
401 // parseSequenceOf is used for SEQUENCE OF and SET OF values. It tries to parse
402 // a number of ASN.1 values from the given byte slice and returns them as a
403 // slice of Go values of the given type.
404 func parseSequenceOf(bytes []byte, sliceType reflect.Type, elemType reflect.Type) (ret reflect.Value, err error) {
405 expectedTag, compoundType, ok := getUniversalType(elemType)
407 err = StructuralError{"unknown Go type for slice"}
411 // First we iterate over the input and count the number of elements,
412 // checking that the types are correct in each case.
414 for offset := 0; offset < len(bytes); {
416 t, offset, err = parseTagAndLength(bytes, offset)
420 // We pretend that GENERAL STRINGs are PRINTABLE STRINGs so
421 // that a sequence of them can be parsed into a []string.
422 if t.tag == tagGeneralString {
423 t.tag = tagPrintableString
425 if t.class != classUniversal || t.isCompound != compoundType || t.tag != expectedTag {
426 err = StructuralError{"sequence tag mismatch"}
429 if invalidLength(offset, t.length, len(bytes)) {
430 err = SyntaxError{"truncated sequence"}
436 ret = reflect.MakeSlice(sliceType, numElements, numElements)
437 params := fieldParameters{}
439 for i := 0; i < numElements; i++ {
440 offset, err = parseField(ret.Index(i), bytes, offset, params)
449 bitStringType = reflect.TypeOf(BitString{})
450 objectIdentifierType = reflect.TypeOf(ObjectIdentifier{})
451 enumeratedType = reflect.TypeOf(Enumerated(0))
452 flagType = reflect.TypeOf(Flag(false))
453 timeType = reflect.TypeOf(time.Time{})
454 rawValueType = reflect.TypeOf(RawValue{})
455 rawContentsType = reflect.TypeOf(RawContent(nil))
456 bigIntType = reflect.TypeOf(new(big.Int))
459 // invalidLength returns true iff offset + length > sliceLength, or if the
460 // addition would overflow.
461 func invalidLength(offset, length, sliceLength int) bool {
462 return offset+length < offset || offset+length > sliceLength
465 // parseField is the main parsing function. Given a byte slice and an offset
466 // into the array, it will try to parse a suitable ASN.1 value out and store it
467 // in the given Value.
468 func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParameters) (offset int, err error) {
470 fieldType := v.Type()
472 // If we have run out of data, it may be that there are optional elements at the end.
473 if offset == len(bytes) {
474 if !setDefaultValue(v, params) {
475 err = SyntaxError{"sequence truncated"}
480 // Deal with raw values.
481 if fieldType == rawValueType {
483 t, offset, err = parseTagAndLength(bytes, offset)
487 if invalidLength(offset, t.length, len(bytes)) {
488 err = SyntaxError{"data truncated"}
491 result := RawValue{t.class, t.tag, t.isCompound, bytes[offset : offset+t.length], bytes[initOffset : offset+t.length]}
493 v.Set(reflect.ValueOf(result))
497 // Deal with the ANY type.
498 if ifaceType := fieldType; ifaceType.Kind() == reflect.Interface && ifaceType.NumMethod() == 0 {
500 t, offset, err = parseTagAndLength(bytes, offset)
504 if invalidLength(offset, t.length, len(bytes)) {
505 err = SyntaxError{"data truncated"}
508 var result interface{}
509 if !t.isCompound && t.class == classUniversal {
510 innerBytes := bytes[offset : offset+t.length]
512 case tagPrintableString:
513 result, err = parsePrintableString(innerBytes)
515 result, err = parseIA5String(innerBytes)
517 result, err = parseT61String(innerBytes)
519 result, err = parseUTF8String(innerBytes)
521 result, err = parseInt64(innerBytes)
523 result, err = parseBitString(innerBytes)
525 result, err = parseObjectIdentifier(innerBytes)
527 result, err = parseUTCTime(innerBytes)
531 // If we don't know how to handle the type, we just leave Value as nil.
539 v.Set(reflect.ValueOf(result))
543 universalTag, compoundType, ok1 := getUniversalType(fieldType)
545 err = StructuralError{fmt.Sprintf("unknown Go type: %v", fieldType)}
549 t, offset, err := parseTagAndLength(bytes, offset)
554 expectedClass := classContextSpecific
555 if params.application {
556 expectedClass = classApplication
558 if t.class == expectedClass && t.tag == *params.tag && (t.length == 0 || t.isCompound) {
560 t, offset, err = parseTagAndLength(bytes, offset)
565 if fieldType != flagType {
566 err = StructuralError{"Zero length explicit tag was not an asn1.Flag"}
573 // The tags didn't match, it might be an optional element.
574 ok := setDefaultValue(v, params)
578 err = StructuralError{"explicitly tagged member didn't match"}
584 // Special case for strings: all the ASN.1 string types map to the Go
585 // type string. getUniversalType returns the tag for PrintableString
586 // when it sees a string, so if we see a different string type on the
587 // wire, we change the universal type to match.
588 if universalTag == tagPrintableString {
590 case tagIA5String, tagGeneralString, tagT61String, tagUTF8String:
595 // Special case for time: UTCTime and GeneralizedTime both map to the
596 // Go type time.Time.
597 if universalTag == tagUTCTime && t.tag == tagGeneralizedTime {
598 universalTag = tagGeneralizedTime
601 expectedClass := classUniversal
602 expectedTag := universalTag
604 if !params.explicit && params.tag != nil {
605 expectedClass = classContextSpecific
606 expectedTag = *params.tag
609 if !params.explicit && params.application && params.tag != nil {
610 expectedClass = classApplication
611 expectedTag = *params.tag
614 // We have unwrapped any explicit tagging at this point.
615 if t.class != expectedClass || t.tag != expectedTag || t.isCompound != compoundType {
616 // Tags don't match. Again, it could be an optional element.
617 ok := setDefaultValue(v, params)
621 err = StructuralError{fmt.Sprintf("tags don't match (%d vs %+v) %+v %s @%d", expectedTag, t, params, fieldType.Name(), offset)}
625 if invalidLength(offset, t.length, len(bytes)) {
626 err = SyntaxError{"data truncated"}
629 innerBytes := bytes[offset : offset+t.length]
632 // We deal with the structures defined in this package first.
634 case objectIdentifierType:
635 newSlice, err1 := parseObjectIdentifier(innerBytes)
636 v.Set(reflect.MakeSlice(v.Type(), len(newSlice), len(newSlice)))
638 reflect.Copy(v, reflect.ValueOf(newSlice))
643 bs, err1 := parseBitString(innerBytes)
645 v.Set(reflect.ValueOf(bs))
652 if universalTag == tagUTCTime {
653 time, err1 = parseUTCTime(innerBytes)
655 time, err1 = parseGeneralizedTime(innerBytes)
658 v.Set(reflect.ValueOf(time))
663 parsedInt, err1 := parseInt(innerBytes)
665 v.SetInt(int64(parsedInt))
673 parsedInt := parseBigInt(innerBytes)
674 v.Set(reflect.ValueOf(parsedInt))
677 switch val := v; val.Kind() {
679 parsedBool, err1 := parseBool(innerBytes)
681 val.SetBool(parsedBool)
685 case reflect.Int, reflect.Int32:
686 parsedInt, err1 := parseInt(innerBytes)
688 val.SetInt(int64(parsedInt))
693 parsedInt, err1 := parseInt64(innerBytes)
695 val.SetInt(parsedInt)
699 // TODO(dfc) Add support for the remaining integer types
701 structType := fieldType
703 if structType.NumField() > 0 &&
704 structType.Field(0).Type == rawContentsType {
705 bytes := bytes[initOffset:offset]
706 val.Field(0).Set(reflect.ValueOf(RawContent(bytes)))
710 for i := 0; i < structType.NumField(); i++ {
711 field := structType.Field(i)
712 if i == 0 && field.Type == rawContentsType {
715 innerOffset, err = parseField(val.Field(i), innerBytes, innerOffset, parseFieldParameters(field.Tag.Get("asn1")))
720 // We allow extra bytes at the end of the SEQUENCE because
721 // adding elements to the end has been used in X.509 as the
722 // version numbers have increased.
725 sliceType := fieldType
726 if sliceType.Elem().Kind() == reflect.Uint8 {
727 val.Set(reflect.MakeSlice(sliceType, len(innerBytes), len(innerBytes)))
728 reflect.Copy(val, reflect.ValueOf(innerBytes))
731 newSlice, err1 := parseSequenceOf(innerBytes, sliceType, sliceType.Elem())
739 switch universalTag {
740 case tagPrintableString:
741 v, err = parsePrintableString(innerBytes)
743 v, err = parseIA5String(innerBytes)
745 v, err = parseT61String(innerBytes)
747 v, err = parseUTF8String(innerBytes)
748 case tagGeneralString:
749 // GeneralString is specified in ISO-2022/ECMA-35,
750 // A brief review suggests that it includes structures
751 // that allow the encoding to change midstring and
752 // such. We give up and pass it as an 8-bit string.
753 v, err = parseT61String(innerBytes)
755 err = SyntaxError{fmt.Sprintf("internal error: unknown string type %d", universalTag)}
762 err = StructuralError{"unsupported: " + v.Type().String()}
766 // setDefaultValue is used to install a default value, from a tag string, into
767 // a Value. It is successful is the field was optional, even if a default value
768 // wasn't provided or it failed to install it into the Value.
769 func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) {
770 if !params.optional {
774 if params.defaultValue == nil {
777 switch val := v; val.Kind() {
778 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
779 val.SetInt(*params.defaultValue)
784 // Unmarshal parses the DER-encoded ASN.1 data structure b
785 // and uses the reflect package to fill in an arbitrary value pointed at by val.
786 // Because Unmarshal uses the reflect package, the structs
787 // being written to must use upper case field names.
789 // An ASN.1 INTEGER can be written to an int, int32, int64,
790 // or *big.Int (from the math/big package).
791 // If the encoded value does not fit in the Go type,
792 // Unmarshal returns a parse error.
794 // An ASN.1 BIT STRING can be written to a BitString.
796 // An ASN.1 OCTET STRING can be written to a []byte.
798 // An ASN.1 OBJECT IDENTIFIER can be written to an
801 // An ASN.1 ENUMERATED can be written to an Enumerated.
803 // An ASN.1 UTCTIME or GENERALIZEDTIME can be written to a time.Time.
805 // An ASN.1 PrintableString or IA5String can be written to a string.
807 // Any of the above ASN.1 values can be written to an interface{}.
808 // The value stored in the interface has the corresponding Go type.
809 // For integers, that type is int64.
811 // An ASN.1 SEQUENCE OF x or SET OF x can be written
812 // to a slice if an x can be written to the slice's element type.
814 // An ASN.1 SEQUENCE or SET can be written to a struct
815 // if each of the elements in the sequence can be
816 // written to the corresponding element in the struct.
818 // The following tags on struct fields have special meaning to Unmarshal:
820 // optional marks the field as ASN.1 OPTIONAL
821 // [explicit] tag:x specifies the ASN.1 tag number; implies ASN.1 CONTEXT SPECIFIC
822 // default:x sets the default value for optional integer fields
824 // If the type of the first field of a structure is RawContent then the raw
825 // ASN1 contents of the struct will be stored in it.
827 // Other ASN.1 types are not supported; if it encounters them,
828 // Unmarshal returns a parse error.
829 func Unmarshal(b []byte, val interface{}) (rest []byte, err error) {
830 return UnmarshalWithParams(b, val, "")
833 // UnmarshalWithParams allows field parameters to be specified for the
834 // top-level element. The form of the params is the same as the field tags.
835 func UnmarshalWithParams(b []byte, val interface{}, params string) (rest []byte, err error) {
836 v := reflect.ValueOf(val).Elem()
837 offset, err := parseField(v, b, 0, parseFieldParameters(params))
841 return b[offset:], nil