OLD | NEW |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // The asn1 package implements parsing of DER-encoded ASN.1 data structures, | 5 // The asn1 package implements parsing of DER-encoded ASN.1 data structures, |
6 // as defined in ITU-T Rec X.690. | 6 // as defined in ITU-T Rec X.690. |
7 // | 7 // |
8 // See also ``A Layman's Guide to a Subset of ASN.1, BER, and DER,'' | 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. | 9 // http://luca.ntop.org/Teaching/Appunti/asn1.html. |
10 package asn1 | 10 package asn1 |
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 ret.length |= int(b) | 366 ret.length |= int(b) |
367 } | 367 } |
368 } | 368 } |
369 | 369 |
370 return | 370 return |
371 } | 371 } |
372 | 372 |
373 // parseSequenceOf is used for SEQUENCE OF and SET OF values. It tries to parse | 373 // parseSequenceOf is used for SEQUENCE OF and SET OF values. It tries to parse |
374 // a number of ASN.1 values from the given byte array and returns them as a | 374 // a number of ASN.1 values from the given byte array and returns them as a |
375 // slice of Go values of the given type. | 375 // slice of Go values of the given type. |
376 func parseSequenceOf(bytes []byte, sliceType *reflect.SliceType, elemType reflec
t.Type) (ret *reflect.SliceValue, err os.Error) { | 376 func parseSequenceOf(bytes []byte, sliceType reflect.Type, elemType reflect.Type
) (ret *reflect.SliceValue, err os.Error) { |
377 expectedTag, compoundType, ok := getUniversalType(elemType) | 377 expectedTag, compoundType, ok := getUniversalType(elemType) |
378 if !ok { | 378 if !ok { |
379 err = StructuralError{"unknown Go type for slice"} | 379 err = StructuralError{"unknown Go type for slice"} |
380 return | 380 return |
381 } | 381 } |
382 | 382 |
383 // First we iterate over the input and count the number of elements, | 383 // First we iterate over the input and count the number of elements, |
384 // checking that the types are correct in each case. | 384 // checking that the types are correct in each case. |
385 numElements := 0 | 385 numElements := 0 |
386 for offset := 0; offset < len(bytes); { | 386 for offset := 0; offset < len(bytes); { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
454 err = SyntaxError{"data truncated"} | 454 err = SyntaxError{"data truncated"} |
455 return | 455 return |
456 } | 456 } |
457 result := RawValue{t.class, t.tag, t.isCompound, bytes[offset :
offset+t.length], bytes[initOffset : offset+t.length]} | 457 result := RawValue{t.class, t.tag, t.isCompound, bytes[offset :
offset+t.length], bytes[initOffset : offset+t.length]} |
458 offset += t.length | 458 offset += t.length |
459 v.(*reflect.StructValue).Set(reflect.NewValue(result).(*reflect.
StructValue)) | 459 v.(*reflect.StructValue).Set(reflect.NewValue(result).(*reflect.
StructValue)) |
460 return | 460 return |
461 } | 461 } |
462 | 462 |
463 // Deal with the ANY type. | 463 // Deal with the ANY type. |
464 » if ifaceType, ok := fieldType.(*reflect.InterfaceType); ok && ifaceType.
NumMethod() == 0 { | 464 » if ifaceType := fieldType; ifaceType.Kind() == reflect.Interface && ifac
eType.NumMethod() == 0 { |
465 ifaceValue := v.(*reflect.InterfaceValue) | 465 ifaceValue := v.(*reflect.InterfaceValue) |
466 var t tagAndLength | 466 var t tagAndLength |
467 t, offset, err = parseTagAndLength(bytes, offset) | 467 t, offset, err = parseTagAndLength(bytes, offset) |
468 if err != nil { | 468 if err != nil { |
469 return | 469 return |
470 } | 470 } |
471 if invalidLength(offset, t.length, len(bytes)) { | 471 if invalidLength(offset, t.length, len(bytes)) { |
472 err = SyntaxError{"data truncated"} | 472 err = SyntaxError{"data truncated"} |
473 return | 473 return |
474 } | 474 } |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
582 return | 582 return |
583 } | 583 } |
584 innerBytes := bytes[offset : offset+t.length] | 584 innerBytes := bytes[offset : offset+t.length] |
585 offset += t.length | 585 offset += t.length |
586 | 586 |
587 // We deal with the structures defined in this package first. | 587 // We deal with the structures defined in this package first. |
588 switch fieldType { | 588 switch fieldType { |
589 case objectIdentifierType: | 589 case objectIdentifierType: |
590 newSlice, err1 := parseObjectIdentifier(innerBytes) | 590 newSlice, err1 := parseObjectIdentifier(innerBytes) |
591 sliceValue := v.(*reflect.SliceValue) | 591 sliceValue := v.(*reflect.SliceValue) |
592 » » sliceValue.Set(reflect.MakeSlice(sliceValue.Type().(*reflect.Sli
ceType), len(newSlice), len(newSlice))) | 592 » » sliceValue.Set(reflect.MakeSlice(sliceValue.Type(), len(newSlice
), len(newSlice))) |
593 if err1 == nil { | 593 if err1 == nil { |
594 reflect.Copy(sliceValue, reflect.NewValue(newSlice).(ref
lect.ArrayOrSliceValue)) | 594 reflect.Copy(sliceValue, reflect.NewValue(newSlice).(ref
lect.ArrayOrSliceValue)) |
595 } | 595 } |
596 err = err1 | 596 err = err1 |
597 return | 597 return |
598 case bitStringType: | 598 case bitStringType: |
599 structValue := v.(*reflect.StructValue) | 599 structValue := v.(*reflect.StructValue) |
600 bs, err1 := parseBitString(innerBytes) | 600 bs, err1 := parseBitString(innerBytes) |
601 if err1 == nil { | 601 if err1 == nil { |
602 structValue.Set(reflect.NewValue(bs).(*reflect.StructVal
ue)) | 602 structValue.Set(reflect.NewValue(bs).(*reflect.StructVal
ue)) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
649 return | 649 return |
650 case reflect.Int64: | 650 case reflect.Int64: |
651 parsedInt, err1 := parseInt64(innerBytes) | 651 parsedInt, err1 := parseInt64(innerBytes) |
652 if err1 == nil { | 652 if err1 == nil { |
653 val.Set(parsedInt) | 653 val.Set(parsedInt) |
654 } | 654 } |
655 err = err1 | 655 err = err1 |
656 return | 656 return |
657 } | 657 } |
658 case *reflect.StructValue: | 658 case *reflect.StructValue: |
659 » » structType := fieldType.(*reflect.StructType) | 659 » » structType := fieldType |
660 | 660 |
661 if structType.NumField() > 0 && | 661 if structType.NumField() > 0 && |
662 structType.Field(0).Type == rawContentsType { | 662 structType.Field(0).Type == rawContentsType { |
663 bytes := bytes[initOffset:offset] | 663 bytes := bytes[initOffset:offset] |
664 val.Field(0).SetValue(reflect.NewValue(RawContent(bytes)
)) | 664 val.Field(0).SetValue(reflect.NewValue(RawContent(bytes)
)) |
665 } | 665 } |
666 | 666 |
667 innerOffset := 0 | 667 innerOffset := 0 |
668 for i := 0; i < structType.NumField(); i++ { | 668 for i := 0; i < structType.NumField(); i++ { |
669 field := structType.Field(i) | 669 field := structType.Field(i) |
670 if i == 0 && field.Type == rawContentsType { | 670 if i == 0 && field.Type == rawContentsType { |
671 continue | 671 continue |
672 } | 672 } |
673 innerOffset, err = parseField(val.Field(i), innerBytes,
innerOffset, parseFieldParameters(field.Tag)) | 673 innerOffset, err = parseField(val.Field(i), innerBytes,
innerOffset, parseFieldParameters(field.Tag)) |
674 if err != nil { | 674 if err != nil { |
675 return | 675 return |
676 } | 676 } |
677 } | 677 } |
678 // We allow extra bytes at the end of the SEQUENCE because | 678 // We allow extra bytes at the end of the SEQUENCE because |
679 // adding elements to the end has been used in X.509 as the | 679 // adding elements to the end has been used in X.509 as the |
680 // version numbers have increased. | 680 // version numbers have increased. |
681 return | 681 return |
682 case *reflect.SliceValue: | 682 case *reflect.SliceValue: |
683 » » sliceType := fieldType.(*reflect.SliceType) | 683 » » sliceType := fieldType |
684 if sliceType.Elem().Kind() == reflect.Uint8 { | 684 if sliceType.Elem().Kind() == reflect.Uint8 { |
685 val.Set(reflect.MakeSlice(sliceType, len(innerBytes), le
n(innerBytes))) | 685 val.Set(reflect.MakeSlice(sliceType, len(innerBytes), le
n(innerBytes))) |
686 reflect.Copy(val, reflect.NewValue(innerBytes).(reflect.
ArrayOrSliceValue)) | 686 reflect.Copy(val, reflect.NewValue(innerBytes).(reflect.
ArrayOrSliceValue)) |
687 return | 687 return |
688 } | 688 } |
689 newSlice, err1 := parseSequenceOf(innerBytes, sliceType, sliceTy
pe.Elem()) | 689 newSlice, err1 := parseSequenceOf(innerBytes, sliceType, sliceTy
pe.Elem()) |
690 if err1 == nil { | 690 if err1 == nil { |
691 val.Set(newSlice) | 691 val.Set(newSlice) |
692 } | 692 } |
693 err = err1 | 693 err = err1 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
776 // Other ASN.1 types are not supported; if it encounters them, | 776 // Other ASN.1 types are not supported; if it encounters them, |
777 // Unmarshal returns a parse error. | 777 // Unmarshal returns a parse error. |
778 func Unmarshal(b []byte, val interface{}) (rest []byte, err os.Error) { | 778 func Unmarshal(b []byte, val interface{}) (rest []byte, err os.Error) { |
779 v := reflect.NewValue(val).(*reflect.PtrValue).Elem() | 779 v := reflect.NewValue(val).(*reflect.PtrValue).Elem() |
780 offset, err := parseField(v, b, 0, fieldParameters{}) | 780 offset, err := parseField(v, b, 0, fieldParameters{}) |
781 if err != nil { | 781 if err != nil { |
782 return nil, err | 782 return nil, err |
783 } | 783 } |
784 return b[offset:], nil | 784 return b[offset:], nil |
785 } | 785 } |
OLD | NEW |