github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/encoding/asn1/asn1.go (about) 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. 4 5 // Package asn1 implements parsing of DER-encoded ASN.1 data structures, 6 // as defined in ITU-T Rec X.690. 7 // 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. 10 package asn1 11 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. 18 // 19 // ASN.1 is very complex and this package doesn't attempt to implement 20 // everything by any means. 21 22 import ( 23 "errors" 24 "fmt" 25 "math" 26 "math/big" 27 "reflect" 28 "strconv" 29 "time" 30 "unicode/utf16" 31 "unicode/utf8" 32 ) 33 34 // A StructuralError suggests that the ASN.1 data is valid, but the Go type 35 // which is receiving it doesn't match. 36 type StructuralError struct { 37 Msg string 38 } 39 40 func (e StructuralError) Error() string { return "asn1: structure error: " + e.Msg } 41 42 // A SyntaxError suggests that the ASN.1 data is invalid. 43 type SyntaxError struct { 44 Msg string 45 } 46 47 func (e SyntaxError) Error() string { return "asn1: syntax error: " + e.Msg } 48 49 // We start by dealing with each of the primitive types in turn. 50 51 // BOOLEAN 52 53 func parseBool(bytes []byte) (ret bool, err error) { 54 if len(bytes) != 1 { 55 err = SyntaxError{"invalid boolean"} 56 return 57 } 58 59 // DER demands that "If the encoding represents the boolean value TRUE, 60 // its single contents octet shall have all eight bits set to one." 61 // Thus only 0 and 255 are valid encoded values. 62 switch bytes[0] { 63 case 0: 64 ret = false 65 case 0xff: 66 ret = true 67 default: 68 err = SyntaxError{"invalid boolean"} 69 } 70 71 return 72 } 73 74 // INTEGER 75 76 // checkInteger returns nil if the given bytes are a valid DER-encoded 77 // INTEGER and an error otherwise. 78 func checkInteger(bytes []byte) error { 79 if len(bytes) == 0 { 80 return StructuralError{"empty integer"} 81 } 82 if len(bytes) == 1 { 83 return nil 84 } 85 if (bytes[0] == 0 && bytes[1]&0x80 == 0) || (bytes[0] == 0xff && bytes[1]&0x80 == 0x80) { 86 return StructuralError{"integer not minimally-encoded"} 87 } 88 return nil 89 } 90 91 // parseInt64 treats the given bytes as a big-endian, signed integer and 92 // returns the result. 93 func parseInt64(bytes []byte) (ret int64, err error) { 94 err = checkInteger(bytes) 95 if err != nil { 96 return 97 } 98 if len(bytes) > 8 { 99 // We'll overflow an int64 in this case. 100 err = StructuralError{"integer too large"} 101 return 102 } 103 for bytesRead := 0; bytesRead < len(bytes); bytesRead++ { 104 ret <<= 8 105 ret |= int64(bytes[bytesRead]) 106 } 107 108 // Shift up and down in order to sign extend the result. 109 ret <<= 64 - uint8(len(bytes))*8 110 ret >>= 64 - uint8(len(bytes))*8 111 return 112 } 113 114 // parseInt treats the given bytes as a big-endian, signed integer and returns 115 // the result. 116 func parseInt32(bytes []byte) (int32, error) { 117 if err := checkInteger(bytes); err != nil { 118 return 0, err 119 } 120 ret64, err := parseInt64(bytes) 121 if err != nil { 122 return 0, err 123 } 124 if ret64 != int64(int32(ret64)) { 125 return 0, StructuralError{"integer too large"} 126 } 127 return int32(ret64), nil 128 } 129 130 var bigOne = big.NewInt(1) 131 132 // parseBigInt treats the given bytes as a big-endian, signed integer and returns 133 // the result. 134 func parseBigInt(bytes []byte) (*big.Int, error) { 135 if err := checkInteger(bytes); err != nil { 136 return nil, err 137 } 138 ret := new(big.Int) 139 if len(bytes) > 0 && bytes[0]&0x80 == 0x80 { 140 // This is a negative number. 141 notBytes := make([]byte, len(bytes)) 142 for i := range notBytes { 143 notBytes[i] = ^bytes[i] 144 } 145 ret.SetBytes(notBytes) 146 ret.Add(ret, bigOne) 147 ret.Neg(ret) 148 return ret, nil 149 } 150 ret.SetBytes(bytes) 151 return ret, nil 152 } 153 154 // BIT STRING 155 156 // BitString is the structure to use when you want an ASN.1 BIT STRING type. A 157 // bit string is padded up to the nearest byte in memory and the number of 158 // valid bits is recorded. Padding bits will be zero. 159 type BitString struct { 160 Bytes []byte // bits packed into bytes. 161 BitLength int // length in bits. 162 } 163 164 // At returns the bit at the given index. If the index is out of range it 165 // returns false. 166 func (b BitString) At(i int) int { 167 if i < 0 || i >= b.BitLength { 168 return 0 169 } 170 x := i / 8 171 y := 7 - uint(i%8) 172 return int(b.Bytes[x]>>y) & 1 173 } 174 175 // RightAlign returns a slice where the padding bits are at the beginning. The 176 // slice may share memory with the BitString. 177 func (b BitString) RightAlign() []byte { 178 shift := uint(8 - (b.BitLength % 8)) 179 if shift == 8 || len(b.Bytes) == 0 { 180 return b.Bytes 181 } 182 183 a := make([]byte, len(b.Bytes)) 184 a[0] = b.Bytes[0] >> shift 185 for i := 1; i < len(b.Bytes); i++ { 186 a[i] = b.Bytes[i-1] << (8 - shift) 187 a[i] |= b.Bytes[i] >> shift 188 } 189 190 return a 191 } 192 193 // parseBitString parses an ASN.1 bit string from the given byte slice and returns it. 194 func parseBitString(bytes []byte) (ret BitString, err error) { 195 if len(bytes) == 0 { 196 err = SyntaxError{"zero length BIT STRING"} 197 return 198 } 199 paddingBits := int(bytes[0]) 200 if paddingBits > 7 || 201 len(bytes) == 1 && paddingBits > 0 || 202 bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 { 203 err = SyntaxError{"invalid padding bits in BIT STRING"} 204 return 205 } 206 ret.BitLength = (len(bytes)-1)*8 - paddingBits 207 ret.Bytes = bytes[1:] 208 return 209 } 210 211 // NULL 212 213 // NullRawValue is a RawValue with its Tag set to the ASN.1 NULL type tag (5). 214 var NullRawValue = RawValue{Tag: TagNull} 215 216 // NullBytes contains bytes representing the DER-encoded ASN.1 NULL type. 217 var NullBytes = []byte{TagNull, 0} 218 219 // OBJECT IDENTIFIER 220 221 // An ObjectIdentifier represents an ASN.1 OBJECT IDENTIFIER. 222 type ObjectIdentifier []int 223 224 // Equal reports whether oi and other represent the same identifier. 225 func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool { 226 if len(oi) != len(other) { 227 return false 228 } 229 for i := 0; i < len(oi); i++ { 230 if oi[i] != other[i] { 231 return false 232 } 233 } 234 235 return true 236 } 237 238 func (oi ObjectIdentifier) String() string { 239 var s string 240 241 for i, v := range oi { 242 if i > 0 { 243 s += "." 244 } 245 s += strconv.Itoa(v) 246 } 247 248 return s 249 } 250 251 // parseObjectIdentifier parses an OBJECT IDENTIFIER from the given bytes and 252 // returns it. An object identifier is a sequence of variable length integers 253 // that are assigned in a hierarchy. 254 func parseObjectIdentifier(bytes []byte) (s ObjectIdentifier, err error) { 255 if len(bytes) == 0 { 256 err = SyntaxError{"zero length OBJECT IDENTIFIER"} 257 return 258 } 259 260 // In the worst case, we get two elements from the first byte (which is 261 // encoded differently) and then every varint is a single byte long. 262 s = make([]int, len(bytes)+1) 263 264 // The first varint is 40*value1 + value2: 265 // According to this packing, value1 can take the values 0, 1 and 2 only. 266 // When value1 = 0 or value1 = 1, then value2 is <= 39. When value1 = 2, 267 // then there are no restrictions on value2. 268 v, offset, err := parseBase128Int(bytes, 0) 269 if err != nil { 270 return 271 } 272 if v < 80 { 273 s[0] = v / 40 274 s[1] = v % 40 275 } else { 276 s[0] = 2 277 s[1] = v - 80 278 } 279 280 i := 2 281 for ; offset < len(bytes); i++ { 282 v, offset, err = parseBase128Int(bytes, offset) 283 if err != nil { 284 return 285 } 286 s[i] = v 287 } 288 s = s[0:i] 289 return 290 } 291 292 // ENUMERATED 293 294 // An Enumerated is represented as a plain int. 295 type Enumerated int 296 297 // FLAG 298 299 // A Flag accepts any data and is set to true if present. 300 type Flag bool 301 302 // parseBase128Int parses a base-128 encoded int from the given offset in the 303 // given byte slice. It returns the value and the new offset. 304 func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err error) { 305 offset = initOffset 306 var ret64 int64 307 for shifted := 0; offset < len(bytes); shifted++ { 308 // 5 * 7 bits per byte == 35 bits of data 309 // Thus the representation is either non-minimal or too large for an int32 310 if shifted == 5 { 311 err = StructuralError{"base 128 integer too large"} 312 return 313 } 314 ret64 <<= 7 315 b := bytes[offset] 316 ret64 |= int64(b & 0x7f) 317 offset++ 318 if b&0x80 == 0 { 319 ret = int(ret64) 320 // Ensure that the returned value fits in an int on all platforms 321 if ret64 > math.MaxInt32 { 322 err = StructuralError{"base 128 integer too large"} 323 } 324 return 325 } 326 } 327 err = SyntaxError{"truncated base 128 integer"} 328 return 329 } 330 331 // UTCTime 332 333 func parseUTCTime(bytes []byte) (ret time.Time, err error) { 334 s := string(bytes) 335 336 formatStr := "0601021504Z0700" 337 ret, err = time.Parse(formatStr, s) 338 if err != nil { 339 formatStr = "060102150405Z0700" 340 ret, err = time.Parse(formatStr, s) 341 } 342 if err != nil { 343 return 344 } 345 346 if serialized := ret.Format(formatStr); serialized != s { 347 err = fmt.Errorf("asn1: time did not serialize back to the original value and may be invalid: given %q, but serialized as %q", s, serialized) 348 return 349 } 350 351 if ret.Year() >= 2050 { 352 // UTCTime only encodes times prior to 2050. See https://tools.ietf.org/html/rfc5280#section-4.1.2.5.1 353 ret = ret.AddDate(-100, 0, 0) 354 } 355 356 return 357 } 358 359 // parseGeneralizedTime parses the GeneralizedTime from the given byte slice 360 // and returns the resulting time. 361 func parseGeneralizedTime(bytes []byte) (ret time.Time, err error) { 362 const formatStr = "20060102150405Z0700" 363 s := string(bytes) 364 365 if ret, err = time.Parse(formatStr, s); err != nil { 366 return 367 } 368 369 if serialized := ret.Format(formatStr); serialized != s { 370 err = fmt.Errorf("asn1: time did not serialize back to the original value and may be invalid: given %q, but serialized as %q", s, serialized) 371 } 372 373 return 374 } 375 376 // NumericString 377 378 // parseNumericString parses an ASN.1 NumericString from the given byte array 379 // and returns it. 380 func parseNumericString(bytes []byte) (ret string, err error) { 381 for _, b := range bytes { 382 if !isNumeric(b) { 383 return "", SyntaxError{"NumericString contains invalid character"} 384 } 385 } 386 return string(bytes), nil 387 } 388 389 // isNumeric reports whether the given b is in the ASN.1 NumericString set. 390 func isNumeric(b byte) bool { 391 return '0' <= b && b <= '9' || 392 b == ' ' 393 } 394 395 // PrintableString 396 397 // parsePrintableString parses an ASN.1 PrintableString from the given byte 398 // array and returns it. 399 func parsePrintableString(bytes []byte) (ret string, err error) { 400 for _, b := range bytes { 401 if !isPrintable(b, allowAsterisk, allowAmpersand) { 402 err = SyntaxError{"PrintableString contains invalid character"} 403 return 404 } 405 } 406 ret = string(bytes) 407 return 408 } 409 410 type asteriskFlag bool 411 type ampersandFlag bool 412 413 const ( 414 allowAsterisk asteriskFlag = true 415 rejectAsterisk asteriskFlag = false 416 417 allowAmpersand ampersandFlag = true 418 rejectAmpersand ampersandFlag = false 419 ) 420 421 // isPrintable reports whether the given b is in the ASN.1 PrintableString set. 422 // If asterisk is allowAsterisk then '*' is also allowed, reflecting existing 423 // practice. If ampersand is allowAmpersand then '&' is allowed as well. 424 func isPrintable(b byte, asterisk asteriskFlag, ampersand ampersandFlag) bool { 425 return 'a' <= b && b <= 'z' || 426 'A' <= b && b <= 'Z' || 427 '0' <= b && b <= '9' || 428 '\'' <= b && b <= ')' || 429 '+' <= b && b <= '/' || 430 b == ' ' || 431 b == ':' || 432 b == '=' || 433 b == '?' || 434 // This is technically not allowed in a PrintableString. 435 // However, x509 certificates with wildcard strings don't 436 // always use the correct string type so we permit it. 437 (bool(asterisk) && b == '*') || 438 // This is not technically allowed either. However, not 439 // only is it relatively common, but there are also a 440 // handful of CA certificates that contain it. At least 441 // one of which will not expire until 2027. 442 (bool(ampersand) && b == '&') 443 } 444 445 // IA5String 446 447 // parseIA5String parses an ASN.1 IA5String (ASCII string) from the given 448 // byte slice and returns it. 449 func parseIA5String(bytes []byte) (ret string, err error) { 450 for _, b := range bytes { 451 if b >= utf8.RuneSelf { 452 err = SyntaxError{"IA5String contains invalid character"} 453 return 454 } 455 } 456 ret = string(bytes) 457 return 458 } 459 460 // T61String 461 462 // parseT61String parses an ASN.1 T61String (8-bit clean string) from the given 463 // byte slice and returns it. 464 func parseT61String(bytes []byte) (ret string, err error) { 465 return string(bytes), nil 466 } 467 468 // UTF8String 469 470 // parseUTF8String parses an ASN.1 UTF8String (raw UTF-8) from the given byte 471 // array and returns it. 472 func parseUTF8String(bytes []byte) (ret string, err error) { 473 if !utf8.Valid(bytes) { 474 return "", errors.New("asn1: invalid UTF-8 string") 475 } 476 return string(bytes), nil 477 } 478 479 // BMPString 480 481 // parseBMPString parses an ASN.1 BMPString (Basic Multilingual Plane of 482 // ISO/IEC/ITU 10646-1) from the given byte slice and returns it. 483 func parseBMPString(bmpString []byte) (string, error) { 484 if len(bmpString)%2 != 0 { 485 return "", errors.New("pkcs12: odd-length BMP string") 486 } 487 488 // Strip terminator if present. 489 if l := len(bmpString); l >= 2 && bmpString[l-1] == 0 && bmpString[l-2] == 0 { 490 bmpString = bmpString[:l-2] 491 } 492 493 s := make([]uint16, 0, len(bmpString)/2) 494 for len(bmpString) > 0 { 495 s = append(s, uint16(bmpString[0])<<8+uint16(bmpString[1])) 496 bmpString = bmpString[2:] 497 } 498 499 return string(utf16.Decode(s)), nil 500 } 501 502 // A RawValue represents an undecoded ASN.1 object. 503 type RawValue struct { 504 Class, Tag int 505 IsCompound bool 506 Bytes []byte 507 FullBytes []byte // includes the tag and length 508 } 509 510 // RawContent is used to signal that the undecoded, DER data needs to be 511 // preserved for a struct. To use it, the first field of the struct must have 512 // this type. It's an error for any of the other fields to have this type. 513 type RawContent []byte 514 515 // Tagging 516 517 // parseTagAndLength parses an ASN.1 tag and length pair from the given offset 518 // into a byte slice. It returns the parsed data and the new offset. SET and 519 // SET OF (tag 17) are mapped to SEQUENCE and SEQUENCE OF (tag 16) since we 520 // don't distinguish between ordered and unordered objects in this code. 521 func parseTagAndLength(bytes []byte, initOffset int) (ret tagAndLength, offset int, err error) { 522 offset = initOffset 523 // parseTagAndLength should not be called without at least a single 524 // byte to read. Thus this check is for robustness: 525 if offset >= len(bytes) { 526 err = errors.New("asn1: internal error in parseTagAndLength") 527 return 528 } 529 b := bytes[offset] 530 offset++ 531 ret.class = int(b >> 6) 532 ret.isCompound = b&0x20 == 0x20 533 ret.tag = int(b & 0x1f) 534 535 // If the bottom five bits are set, then the tag number is actually base 128 536 // encoded afterwards 537 if ret.tag == 0x1f { 538 ret.tag, offset, err = parseBase128Int(bytes, offset) 539 if err != nil { 540 return 541 } 542 // Tags should be encoded in minimal form. 543 if ret.tag < 0x1f { 544 err = SyntaxError{"non-minimal tag"} 545 return 546 } 547 } 548 if offset >= len(bytes) { 549 err = SyntaxError{"truncated tag or length"} 550 return 551 } 552 b = bytes[offset] 553 offset++ 554 if b&0x80 == 0 { 555 // The length is encoded in the bottom 7 bits. 556 ret.length = int(b & 0x7f) 557 } else { 558 // Bottom 7 bits give the number of length bytes to follow. 559 numBytes := int(b & 0x7f) 560 if numBytes == 0 { 561 err = SyntaxError{"indefinite length found (not DER)"} 562 return 563 } 564 ret.length = 0 565 for i := 0; i < numBytes; i++ { 566 if offset >= len(bytes) { 567 err = SyntaxError{"truncated tag or length"} 568 return 569 } 570 b = bytes[offset] 571 offset++ 572 if ret.length >= 1<<23 { 573 // We can't shift ret.length up without 574 // overflowing. 575 err = StructuralError{"length too large"} 576 return 577 } 578 ret.length <<= 8 579 ret.length |= int(b) 580 if ret.length == 0 { 581 // DER requires that lengths be minimal. 582 err = StructuralError{"superfluous leading zeros in length"} 583 return 584 } 585 } 586 // Short lengths must be encoded in short form. 587 if ret.length < 0x80 { 588 err = StructuralError{"non-minimal length"} 589 return 590 } 591 } 592 593 return 594 } 595 596 // parseSequenceOf is used for SEQUENCE OF and SET OF values. It tries to parse 597 // a number of ASN.1 values from the given byte slice and returns them as a 598 // slice of Go values of the given type. 599 func parseSequenceOf(bytes []byte, sliceType reflect.Type, elemType reflect.Type) (ret reflect.Value, err error) { 600 matchAny, expectedTag, compoundType, ok := getUniversalType(elemType) 601 if !ok { 602 err = StructuralError{"unknown Go type for slice"} 603 return 604 } 605 606 // First we iterate over the input and count the number of elements, 607 // checking that the types are correct in each case. 608 numElements := 0 609 for offset := 0; offset < len(bytes); { 610 var t tagAndLength 611 t, offset, err = parseTagAndLength(bytes, offset) 612 if err != nil { 613 return 614 } 615 switch t.tag { 616 case TagIA5String, TagGeneralString, TagT61String, TagUTF8String, TagNumericString, TagBMPString: 617 // We pretend that various other string types are 618 // PRINTABLE STRINGs so that a sequence of them can be 619 // parsed into a []string. 620 t.tag = TagPrintableString 621 case TagGeneralizedTime, TagUTCTime: 622 // Likewise, both time types are treated the same. 623 t.tag = TagUTCTime 624 } 625 626 if !matchAny && (t.class != ClassUniversal || t.isCompound != compoundType || t.tag != expectedTag) { 627 err = StructuralError{"sequence tag mismatch"} 628 return 629 } 630 if invalidLength(offset, t.length, len(bytes)) { 631 err = SyntaxError{"truncated sequence"} 632 return 633 } 634 offset += t.length 635 numElements++ 636 } 637 ret = reflect.MakeSlice(sliceType, numElements, numElements) 638 params := fieldParameters{} 639 offset := 0 640 for i := 0; i < numElements; i++ { 641 offset, err = parseField(ret.Index(i), bytes, offset, params) 642 if err != nil { 643 return 644 } 645 } 646 return 647 } 648 649 var ( 650 bitStringType = reflect.TypeOf(BitString{}) 651 objectIdentifierType = reflect.TypeOf(ObjectIdentifier{}) 652 enumeratedType = reflect.TypeOf(Enumerated(0)) 653 flagType = reflect.TypeOf(Flag(false)) 654 timeType = reflect.TypeOf(time.Time{}) 655 rawValueType = reflect.TypeOf(RawValue{}) 656 rawContentsType = reflect.TypeOf(RawContent(nil)) 657 bigIntType = reflect.TypeOf(new(big.Int)) 658 ) 659 660 // invalidLength reports whether offset + length > sliceLength, or if the 661 // addition would overflow. 662 func invalidLength(offset, length, sliceLength int) bool { 663 return offset+length < offset || offset+length > sliceLength 664 } 665 666 // parseField is the main parsing function. Given a byte slice and an offset 667 // into the array, it will try to parse a suitable ASN.1 value out and store it 668 // in the given Value. 669 func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParameters) (offset int, err error) { 670 offset = initOffset 671 fieldType := v.Type() 672 673 // If we have run out of data, it may be that there are optional elements at the end. 674 if offset == len(bytes) { 675 if !setDefaultValue(v, params) { 676 err = SyntaxError{"sequence truncated"} 677 } 678 return 679 } 680 681 // Deal with the ANY type. 682 if ifaceType := fieldType; ifaceType.Kind() == reflect.Interface && ifaceType.NumMethod() == 0 { 683 var t tagAndLength 684 t, offset, err = parseTagAndLength(bytes, offset) 685 if err != nil { 686 return 687 } 688 if invalidLength(offset, t.length, len(bytes)) { 689 err = SyntaxError{"data truncated"} 690 return 691 } 692 var result interface{} 693 if !t.isCompound && t.class == ClassUniversal { 694 innerBytes := bytes[offset : offset+t.length] 695 switch t.tag { 696 case TagPrintableString: 697 result, err = parsePrintableString(innerBytes) 698 case TagNumericString: 699 result, err = parseNumericString(innerBytes) 700 case TagIA5String: 701 result, err = parseIA5String(innerBytes) 702 case TagT61String: 703 result, err = parseT61String(innerBytes) 704 case TagUTF8String: 705 result, err = parseUTF8String(innerBytes) 706 case TagInteger: 707 result, err = parseInt64(innerBytes) 708 case TagBitString: 709 result, err = parseBitString(innerBytes) 710 case TagOID: 711 result, err = parseObjectIdentifier(innerBytes) 712 case TagUTCTime: 713 result, err = parseUTCTime(innerBytes) 714 case TagGeneralizedTime: 715 result, err = parseGeneralizedTime(innerBytes) 716 case TagOctetString: 717 result = innerBytes 718 case TagBMPString: 719 result, err = parseBMPString(innerBytes) 720 default: 721 // If we don't know how to handle the type, we just leave Value as nil. 722 } 723 } 724 offset += t.length 725 if err != nil { 726 return 727 } 728 if result != nil { 729 v.Set(reflect.ValueOf(result)) 730 } 731 return 732 } 733 734 t, offset, err := parseTagAndLength(bytes, offset) 735 if err != nil { 736 return 737 } 738 if params.explicit { 739 expectedClass := ClassContextSpecific 740 if params.application { 741 expectedClass = ClassApplication 742 } 743 if offset == len(bytes) { 744 err = StructuralError{"explicit tag has no child"} 745 return 746 } 747 if t.class == expectedClass && t.tag == *params.tag && (t.length == 0 || t.isCompound) { 748 if fieldType == rawValueType { 749 // The inner element should not be parsed for RawValues. 750 } else if t.length > 0 { 751 t, offset, err = parseTagAndLength(bytes, offset) 752 if err != nil { 753 return 754 } 755 } else { 756 if fieldType != flagType { 757 err = StructuralError{"zero length explicit tag was not an asn1.Flag"} 758 return 759 } 760 v.SetBool(true) 761 return 762 } 763 } else { 764 // The tags didn't match, it might be an optional element. 765 ok := setDefaultValue(v, params) 766 if ok { 767 offset = initOffset 768 } else { 769 err = StructuralError{"explicitly tagged member didn't match"} 770 } 771 return 772 } 773 } 774 775 matchAny, universalTag, compoundType, ok1 := getUniversalType(fieldType) 776 if !ok1 { 777 err = StructuralError{fmt.Sprintf("unknown Go type: %v", fieldType)} 778 return 779 } 780 781 // Special case for strings: all the ASN.1 string types map to the Go 782 // type string. getUniversalType returns the tag for PrintableString 783 // when it sees a string, so if we see a different string type on the 784 // wire, we change the universal type to match. 785 if universalTag == TagPrintableString { 786 if t.class == ClassUniversal { 787 switch t.tag { 788 case TagIA5String, TagGeneralString, TagT61String, TagUTF8String, TagNumericString, TagBMPString: 789 universalTag = t.tag 790 } 791 } else if params.stringType != 0 { 792 universalTag = params.stringType 793 } 794 } 795 796 // Special case for time: UTCTime and GeneralizedTime both map to the 797 // Go type time.Time. 798 if universalTag == TagUTCTime && t.tag == TagGeneralizedTime && t.class == ClassUniversal { 799 universalTag = TagGeneralizedTime 800 } 801 802 if params.set { 803 universalTag = TagSet 804 } 805 806 matchAnyClassAndTag := matchAny 807 expectedClass := ClassUniversal 808 expectedTag := universalTag 809 810 if !params.explicit && params.tag != nil { 811 expectedClass = ClassContextSpecific 812 expectedTag = *params.tag 813 matchAnyClassAndTag = false 814 } 815 816 if !params.explicit && params.application && params.tag != nil { 817 expectedClass = ClassApplication 818 expectedTag = *params.tag 819 matchAnyClassAndTag = false 820 } 821 822 if !params.explicit && params.private && params.tag != nil { 823 expectedClass = ClassPrivate 824 expectedTag = *params.tag 825 matchAnyClassAndTag = false 826 } 827 828 // We have unwrapped any explicit tagging at this point. 829 if !matchAnyClassAndTag && (t.class != expectedClass || t.tag != expectedTag) || 830 (!matchAny && t.isCompound != compoundType) { 831 // Tags don't match. Again, it could be an optional element. 832 ok := setDefaultValue(v, params) 833 if ok { 834 offset = initOffset 835 } else { 836 err = StructuralError{fmt.Sprintf("tags don't match (%d vs %+v) %+v %s @%d", expectedTag, t, params, fieldType.Name(), offset)} 837 } 838 return 839 } 840 if invalidLength(offset, t.length, len(bytes)) { 841 err = SyntaxError{"data truncated"} 842 return 843 } 844 innerBytes := bytes[offset : offset+t.length] 845 offset += t.length 846 847 // We deal with the structures defined in this package first. 848 switch fieldType { 849 case rawValueType: 850 result := RawValue{t.class, t.tag, t.isCompound, innerBytes, bytes[initOffset:offset]} 851 v.Set(reflect.ValueOf(result)) 852 return 853 case objectIdentifierType: 854 newSlice, err1 := parseObjectIdentifier(innerBytes) 855 v.Set(reflect.MakeSlice(v.Type(), len(newSlice), len(newSlice))) 856 if err1 == nil { 857 reflect.Copy(v, reflect.ValueOf(newSlice)) 858 } 859 err = err1 860 return 861 case bitStringType: 862 bs, err1 := parseBitString(innerBytes) 863 if err1 == nil { 864 v.Set(reflect.ValueOf(bs)) 865 } 866 err = err1 867 return 868 case timeType: 869 var time time.Time 870 var err1 error 871 if universalTag == TagUTCTime { 872 time, err1 = parseUTCTime(innerBytes) 873 } else { 874 time, err1 = parseGeneralizedTime(innerBytes) 875 } 876 if err1 == nil { 877 v.Set(reflect.ValueOf(time)) 878 } 879 err = err1 880 return 881 case enumeratedType: 882 parsedInt, err1 := parseInt32(innerBytes) 883 if err1 == nil { 884 v.SetInt(int64(parsedInt)) 885 } 886 err = err1 887 return 888 case flagType: 889 v.SetBool(true) 890 return 891 case bigIntType: 892 parsedInt, err1 := parseBigInt(innerBytes) 893 if err1 == nil { 894 v.Set(reflect.ValueOf(parsedInt)) 895 } 896 err = err1 897 return 898 } 899 switch val := v; val.Kind() { 900 case reflect.Bool: 901 parsedBool, err1 := parseBool(innerBytes) 902 if err1 == nil { 903 val.SetBool(parsedBool) 904 } 905 err = err1 906 return 907 case reflect.Int, reflect.Int32, reflect.Int64: 908 if val.Type().Size() == 4 { 909 parsedInt, err1 := parseInt32(innerBytes) 910 if err1 == nil { 911 val.SetInt(int64(parsedInt)) 912 } 913 err = err1 914 } else { 915 parsedInt, err1 := parseInt64(innerBytes) 916 if err1 == nil { 917 val.SetInt(parsedInt) 918 } 919 err = err1 920 } 921 return 922 // TODO(dfc) Add support for the remaining integer types 923 case reflect.Struct: 924 structType := fieldType 925 926 for i := 0; i < structType.NumField(); i++ { 927 if structType.Field(i).PkgPath != "" { 928 err = StructuralError{"struct contains unexported fields"} 929 return 930 } 931 } 932 933 if structType.NumField() > 0 && 934 structType.Field(0).Type == rawContentsType { 935 bytes := bytes[initOffset:offset] 936 val.Field(0).Set(reflect.ValueOf(RawContent(bytes))) 937 } 938 939 innerOffset := 0 940 for i := 0; i < structType.NumField(); i++ { 941 field := structType.Field(i) 942 if i == 0 && field.Type == rawContentsType { 943 continue 944 } 945 innerOffset, err = parseField(val.Field(i), innerBytes, innerOffset, parseFieldParameters(field.Tag.Get("asn1"))) 946 if err != nil { 947 return 948 } 949 } 950 // We allow extra bytes at the end of the SEQUENCE because 951 // adding elements to the end has been used in X.509 as the 952 // version numbers have increased. 953 return 954 case reflect.Slice: 955 sliceType := fieldType 956 if sliceType.Elem().Kind() == reflect.Uint8 { 957 val.Set(reflect.MakeSlice(sliceType, len(innerBytes), len(innerBytes))) 958 reflect.Copy(val, reflect.ValueOf(innerBytes)) 959 return 960 } 961 newSlice, err1 := parseSequenceOf(innerBytes, sliceType, sliceType.Elem()) 962 if err1 == nil { 963 val.Set(newSlice) 964 } 965 err = err1 966 return 967 case reflect.String: 968 var v string 969 switch universalTag { 970 case TagPrintableString: 971 v, err = parsePrintableString(innerBytes) 972 case TagNumericString: 973 v, err = parseNumericString(innerBytes) 974 case TagIA5String: 975 v, err = parseIA5String(innerBytes) 976 case TagT61String: 977 v, err = parseT61String(innerBytes) 978 case TagUTF8String: 979 v, err = parseUTF8String(innerBytes) 980 case TagGeneralString: 981 // GeneralString is specified in ISO-2022/ECMA-35, 982 // A brief review suggests that it includes structures 983 // that allow the encoding to change midstring and 984 // such. We give up and pass it as an 8-bit string. 985 v, err = parseT61String(innerBytes) 986 case TagBMPString: 987 v, err = parseBMPString(innerBytes) 988 989 default: 990 err = SyntaxError{fmt.Sprintf("internal error: unknown string type %d", universalTag)} 991 } 992 if err == nil { 993 val.SetString(v) 994 } 995 return 996 } 997 err = StructuralError{"unsupported: " + v.Type().String()} 998 return 999 } 1000 1001 // canHaveDefaultValue reports whether k is a Kind that we will set a default 1002 // value for. (A signed integer, essentially.) 1003 func canHaveDefaultValue(k reflect.Kind) bool { 1004 switch k { 1005 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 1006 return true 1007 } 1008 1009 return false 1010 } 1011 1012 // setDefaultValue is used to install a default value, from a tag string, into 1013 // a Value. It is successful if the field was optional, even if a default value 1014 // wasn't provided or it failed to install it into the Value. 1015 func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) { 1016 if !params.optional { 1017 return 1018 } 1019 ok = true 1020 if params.defaultValue == nil { 1021 return 1022 } 1023 if canHaveDefaultValue(v.Kind()) { 1024 v.SetInt(*params.defaultValue) 1025 } 1026 return 1027 } 1028 1029 // Unmarshal parses the DER-encoded ASN.1 data structure b 1030 // and uses the reflect package to fill in an arbitrary value pointed at by val. 1031 // Because Unmarshal uses the reflect package, the structs 1032 // being written to must use upper case field names. 1033 // 1034 // An ASN.1 INTEGER can be written to an int, int32, int64, 1035 // or *big.Int (from the math/big package). 1036 // If the encoded value does not fit in the Go type, 1037 // Unmarshal returns a parse error. 1038 // 1039 // An ASN.1 BIT STRING can be written to a BitString. 1040 // 1041 // An ASN.1 OCTET STRING can be written to a []byte. 1042 // 1043 // An ASN.1 OBJECT IDENTIFIER can be written to an 1044 // ObjectIdentifier. 1045 // 1046 // An ASN.1 ENUMERATED can be written to an Enumerated. 1047 // 1048 // An ASN.1 UTCTIME or GENERALIZEDTIME can be written to a time.Time. 1049 // 1050 // An ASN.1 PrintableString, IA5String, or NumericString can be written to a string. 1051 // 1052 // Any of the above ASN.1 values can be written to an interface{}. 1053 // The value stored in the interface has the corresponding Go type. 1054 // For integers, that type is int64. 1055 // 1056 // An ASN.1 SEQUENCE OF x or SET OF x can be written 1057 // to a slice if an x can be written to the slice's element type. 1058 // 1059 // An ASN.1 SEQUENCE or SET can be written to a struct 1060 // if each of the elements in the sequence can be 1061 // written to the corresponding element in the struct. 1062 // 1063 // The following tags on struct fields have special meaning to Unmarshal: 1064 // 1065 // application specifies that an APPLICATION tag is used 1066 // private specifies that a PRIVATE tag is used 1067 // default:x sets the default value for optional integer fields (only used if optional is also present) 1068 // explicit specifies that an additional, explicit tag wraps the implicit one 1069 // optional marks the field as ASN.1 OPTIONAL 1070 // set causes a SET, rather than a SEQUENCE type to be expected 1071 // tag:x specifies the ASN.1 tag number; implies ASN.1 CONTEXT SPECIFIC 1072 // 1073 // If the type of the first field of a structure is RawContent then the raw 1074 // ASN1 contents of the struct will be stored in it. 1075 // 1076 // If the type name of a slice element ends with "SET" then it's treated as if 1077 // the "set" tag was set on it. This can be used with nested slices where a 1078 // struct tag cannot be given. 1079 // 1080 // Other ASN.1 types are not supported; if it encounters them, 1081 // Unmarshal returns a parse error. 1082 func Unmarshal(b []byte, val interface{}) (rest []byte, err error) { 1083 return UnmarshalWithParams(b, val, "") 1084 } 1085 1086 // UnmarshalWithParams allows field parameters to be specified for the 1087 // top-level element. The form of the params is the same as the field tags. 1088 func UnmarshalWithParams(b []byte, val interface{}, params string) (rest []byte, err error) { 1089 v := reflect.ValueOf(val).Elem() 1090 offset, err := parseField(v, b, 0, parseFieldParameters(params)) 1091 if err != nil { 1092 return nil, err 1093 } 1094 return b[offset:], nil 1095 }