| |
| |
| |
|
|
| |
| |
| |
| |
| |
| package asn1 |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import ( |
| "errors" |
| "fmt" |
| "internal/saferio" |
| "math" |
| "math/big" |
| "reflect" |
| "slices" |
| "strconv" |
| "strings" |
| "time" |
| "unicode/utf16" |
| "unicode/utf8" |
| ) |
|
|
| |
| |
| type StructuralError struct { |
| Msg string |
| } |
|
|
| func (e StructuralError) Error() string { return "asn1: structure error: " + e.Msg } |
|
|
| |
| type SyntaxError struct { |
| Msg string |
| } |
|
|
| func (e SyntaxError) Error() string { return "asn1: syntax error: " + e.Msg } |
|
|
| |
|
|
| |
|
|
| func parseBool(bytes []byte) (ret bool, err error) { |
| if len(bytes) != 1 { |
| err = SyntaxError{"invalid boolean"} |
| return |
| } |
|
|
| |
| |
| |
| switch bytes[0] { |
| case 0: |
| ret = false |
| case 0xff: |
| ret = true |
| default: |
| err = SyntaxError{"invalid boolean"} |
| } |
|
|
| return |
| } |
|
|
| |
|
|
| |
| |
| func checkInteger(bytes []byte) error { |
| if len(bytes) == 0 { |
| return StructuralError{"empty integer"} |
| } |
| if len(bytes) == 1 { |
| return nil |
| } |
| if (bytes[0] == 0 && bytes[1]&0x80 == 0) || (bytes[0] == 0xff && bytes[1]&0x80 == 0x80) { |
| return StructuralError{"integer not minimally-encoded"} |
| } |
| return nil |
| } |
|
|
| |
| |
| func parseInt64(bytes []byte) (ret int64, err error) { |
| err = checkInteger(bytes) |
| if err != nil { |
| return |
| } |
| if len(bytes) > 8 { |
| |
| err = StructuralError{"integer too large"} |
| return |
| } |
| for bytesRead := 0; bytesRead < len(bytes); bytesRead++ { |
| ret <<= 8 |
| ret |= int64(bytes[bytesRead]) |
| } |
|
|
| |
| ret <<= 64 - uint8(len(bytes))*8 |
| ret >>= 64 - uint8(len(bytes))*8 |
| return |
| } |
|
|
| |
| |
| func parseInt32(bytes []byte) (int32, error) { |
| if err := checkInteger(bytes); err != nil { |
| return 0, err |
| } |
| ret64, err := parseInt64(bytes) |
| if err != nil { |
| return 0, err |
| } |
| if ret64 != int64(int32(ret64)) { |
| return 0, StructuralError{"integer too large"} |
| } |
| return int32(ret64), nil |
| } |
|
|
| var bigOne = big.NewInt(1) |
|
|
| |
| |
| func parseBigInt(bytes []byte) (*big.Int, error) { |
| if err := checkInteger(bytes); err != nil { |
| return nil, err |
| } |
| ret := new(big.Int) |
| if len(bytes) > 0 && bytes[0]&0x80 == 0x80 { |
| |
| notBytes := make([]byte, len(bytes)) |
| for i := range notBytes { |
| notBytes[i] = ^bytes[i] |
| } |
| ret.SetBytes(notBytes) |
| ret.Add(ret, bigOne) |
| ret.Neg(ret) |
| return ret, nil |
| } |
| ret.SetBytes(bytes) |
| return ret, nil |
| } |
|
|
| |
|
|
| |
| |
| |
| type BitString struct { |
| Bytes []byte |
| BitLength int |
| } |
|
|
| |
| |
| func (b BitString) At(i int) int { |
| if i < 0 || i >= b.BitLength { |
| return 0 |
| } |
| x := i / 8 |
| y := 7 - uint(i%8) |
| return int(b.Bytes[x]>>y) & 1 |
| } |
|
|
| |
| |
| func (b BitString) RightAlign() []byte { |
| shift := uint(8 - (b.BitLength % 8)) |
| if shift == 8 || len(b.Bytes) == 0 { |
| return b.Bytes |
| } |
|
|
| a := make([]byte, len(b.Bytes)) |
| a[0] = b.Bytes[0] >> shift |
| for i := 1; i < len(b.Bytes); i++ { |
| a[i] = b.Bytes[i-1] << (8 - shift) |
| a[i] |= b.Bytes[i] >> shift |
| } |
|
|
| return a |
| } |
|
|
| |
| func parseBitString(bytes []byte) (ret BitString, err error) { |
| if len(bytes) == 0 { |
| err = SyntaxError{"zero length BIT STRING"} |
| return |
| } |
| paddingBits := int(bytes[0]) |
| if paddingBits > 7 || |
| len(bytes) == 1 && paddingBits > 0 || |
| bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 { |
| err = SyntaxError{"invalid padding bits in BIT STRING"} |
| return |
| } |
| ret.BitLength = (len(bytes)-1)*8 - paddingBits |
| ret.Bytes = bytes[1:] |
| return |
| } |
|
|
| |
|
|
| |
| var NullRawValue = RawValue{Tag: TagNull} |
|
|
| |
| var NullBytes = []byte{TagNull, 0} |
|
|
| |
|
|
| |
| type ObjectIdentifier []int |
|
|
| |
| func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool { |
| return slices.Equal(oi, other) |
| } |
|
|
| func (oi ObjectIdentifier) String() string { |
| var s strings.Builder |
| s.Grow(32) |
|
|
| buf := make([]byte, 0, 19) |
| for i, v := range oi { |
| if i > 0 { |
| s.WriteByte('.') |
| } |
| s.Write(strconv.AppendInt(buf, int64(v), 10)) |
| } |
|
|
| return s.String() |
| } |
|
|
| |
| |
| |
| func parseObjectIdentifier(bytes []byte) (s ObjectIdentifier, err error) { |
| if len(bytes) == 0 { |
| err = SyntaxError{"zero length OBJECT IDENTIFIER"} |
| return |
| } |
|
|
| |
| |
| s = make([]int, len(bytes)+1) |
|
|
| |
| |
| |
| |
| v, offset, err := parseBase128Int(bytes, 0) |
| if err != nil { |
| return |
| } |
| if v < 80 { |
| s[0] = v / 40 |
| s[1] = v % 40 |
| } else { |
| s[0] = 2 |
| s[1] = v - 80 |
| } |
|
|
| i := 2 |
| for ; offset < len(bytes); i++ { |
| v, offset, err = parseBase128Int(bytes, offset) |
| if err != nil { |
| return |
| } |
| s[i] = v |
| } |
| s = s[0:i] |
| return |
| } |
|
|
| |
|
|
| |
| type Enumerated int |
|
|
| |
|
|
| |
| type Flag bool |
|
|
| |
| |
| func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err error) { |
| offset = initOffset |
| var ret64 int64 |
| for shifted := 0; offset < len(bytes); shifted++ { |
| |
| |
| if shifted == 5 { |
| err = StructuralError{"base 128 integer too large"} |
| return |
| } |
| ret64 <<= 7 |
| b := bytes[offset] |
| |
| |
| if shifted == 0 && b == 0x80 { |
| err = SyntaxError{"integer is not minimally encoded"} |
| return |
| } |
| ret64 |= int64(b & 0x7f) |
| offset++ |
| if b&0x80 == 0 { |
| ret = int(ret64) |
| |
| if ret64 > math.MaxInt32 { |
| err = StructuralError{"base 128 integer too large"} |
| } |
| return |
| } |
| } |
| err = SyntaxError{"truncated base 128 integer"} |
| return |
| } |
|
|
| |
|
|
| func parseUTCTime(bytes []byte) (ret time.Time, err error) { |
| s := string(bytes) |
|
|
| formatStr := "0601021504Z0700" |
| ret, err = time.Parse(formatStr, s) |
| if err != nil { |
| formatStr = "060102150405Z0700" |
| ret, err = time.Parse(formatStr, s) |
| } |
| if err != nil { |
| return |
| } |
|
|
| if serialized := ret.Format(formatStr); serialized != s { |
| 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) |
| return |
| } |
|
|
| if ret.Year() >= 2050 { |
| |
| ret = ret.AddDate(-100, 0, 0) |
| } |
|
|
| return |
| } |
|
|
| |
| |
| func parseGeneralizedTime(bytes []byte) (ret time.Time, err error) { |
| const formatStr = "20060102150405.999999999Z0700" |
| s := string(bytes) |
|
|
| if ret, err = time.Parse(formatStr, s); err != nil { |
| return |
| } |
|
|
| if serialized := ret.Format(formatStr); serialized != s { |
| 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) |
| } |
|
|
| return |
| } |
|
|
| |
|
|
| |
| |
| func parseNumericString(bytes []byte) (ret string, err error) { |
| for _, b := range bytes { |
| if !isNumeric(b) { |
| return "", SyntaxError{"NumericString contains invalid character"} |
| } |
| } |
| return string(bytes), nil |
| } |
|
|
| |
| func isNumeric(b byte) bool { |
| return '0' <= b && b <= '9' || |
| b == ' ' |
| } |
|
|
| |
|
|
| |
| |
| func parsePrintableString(bytes []byte) (ret string, err error) { |
| for _, b := range bytes { |
| if !isPrintable(b, allowAsterisk, allowAmpersand) { |
| err = SyntaxError{"PrintableString contains invalid character"} |
| return |
| } |
| } |
| ret = string(bytes) |
| return |
| } |
|
|
| type asteriskFlag bool |
| type ampersandFlag bool |
|
|
| const ( |
| allowAsterisk asteriskFlag = true |
| rejectAsterisk asteriskFlag = false |
|
|
| allowAmpersand ampersandFlag = true |
| rejectAmpersand ampersandFlag = false |
| ) |
|
|
| |
| |
| |
| func isPrintable(b byte, asterisk asteriskFlag, ampersand ampersandFlag) bool { |
| return 'a' <= b && b <= 'z' || |
| 'A' <= b && b <= 'Z' || |
| '0' <= b && b <= '9' || |
| '\'' <= b && b <= ')' || |
| '+' <= b && b <= '/' || |
| b == ' ' || |
| b == ':' || |
| b == '=' || |
| b == '?' || |
| |
| |
| |
| (bool(asterisk) && b == '*') || |
| |
| |
| |
| |
| (bool(ampersand) && b == '&') |
| } |
|
|
| |
|
|
| |
| |
| func parseIA5String(bytes []byte) (ret string, err error) { |
| for _, b := range bytes { |
| if b >= utf8.RuneSelf { |
| err = SyntaxError{"IA5String contains invalid character"} |
| return |
| } |
| } |
| ret = string(bytes) |
| return |
| } |
|
|
| |
|
|
| |
| |
| func parseT61String(bytes []byte) (ret string, err error) { |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| buf := make([]byte, 0, len(bytes)) |
| for _, v := range bytes { |
| |
| buf = utf8.AppendRune(buf, rune(v)) |
| } |
| return string(buf), nil |
| } |
|
|
| |
|
|
| |
| |
| func parseUTF8String(bytes []byte) (ret string, err error) { |
| if !utf8.Valid(bytes) { |
| return "", errors.New("asn1: invalid UTF-8 string") |
| } |
| return string(bytes), nil |
| } |
|
|
| |
|
|
| |
| |
| func parseBMPString(bmpString []byte) (string, error) { |
| |
| |
| |
| |
| |
| |
| |
|
|
| if len(bmpString)%2 != 0 { |
| return "", errors.New("invalid BMPString") |
| } |
|
|
| |
| if l := len(bmpString); l >= 2 && bmpString[l-1] == 0 && bmpString[l-2] == 0 { |
| bmpString = bmpString[:l-2] |
| } |
|
|
| s := make([]uint16, 0, len(bmpString)/2) |
| for len(bmpString) > 0 { |
| point := uint16(bmpString[0])<<8 + uint16(bmpString[1]) |
| |
| |
| |
| if point == 0xfffe || point == 0xffff || |
| (point >= 0xfdd0 && point <= 0xfdef) || |
| (point >= 0xd800 && point <= 0xdfff) { |
| return "", errors.New("invalid BMPString") |
| } |
| s = append(s, point) |
| bmpString = bmpString[2:] |
| } |
|
|
| return string(utf16.Decode(s)), nil |
| } |
|
|
| |
| type RawValue struct { |
| Class, Tag int |
| IsCompound bool |
| Bytes []byte |
| FullBytes []byte |
| } |
|
|
| |
| |
| |
| type RawContent []byte |
|
|
| |
|
|
| |
| |
| |
| |
| func parseTagAndLength(bytes []byte, initOffset int) (ret tagAndLength, offset int, err error) { |
| offset = initOffset |
| |
| |
| if offset >= len(bytes) { |
| err = errors.New("asn1: internal error in parseTagAndLength") |
| return |
| } |
| b := bytes[offset] |
| offset++ |
| ret.class = int(b >> 6) |
| ret.isCompound = b&0x20 == 0x20 |
| ret.tag = int(b & 0x1f) |
|
|
| |
| |
| if ret.tag == 0x1f { |
| ret.tag, offset, err = parseBase128Int(bytes, offset) |
| if err != nil { |
| return |
| } |
| |
| if ret.tag < 0x1f { |
| err = SyntaxError{"non-minimal tag"} |
| return |
| } |
| } |
| if offset >= len(bytes) { |
| err = SyntaxError{"truncated tag or length"} |
| return |
| } |
| b = bytes[offset] |
| offset++ |
| if b&0x80 == 0 { |
| |
| ret.length = int(b & 0x7f) |
| } else { |
| |
| numBytes := int(b & 0x7f) |
| if numBytes == 0 { |
| err = SyntaxError{"indefinite length found (not DER)"} |
| return |
| } |
| ret.length = 0 |
| for i := 0; i < numBytes; i++ { |
| if offset >= len(bytes) { |
| err = SyntaxError{"truncated tag or length"} |
| return |
| } |
| b = bytes[offset] |
| offset++ |
| if ret.length >= 1<<23 { |
| |
| |
| err = StructuralError{"length too large"} |
| return |
| } |
| ret.length <<= 8 |
| ret.length |= int(b) |
| if ret.length == 0 { |
| |
| err = StructuralError{"superfluous leading zeros in length"} |
| return |
| } |
| } |
| |
| if ret.length < 0x80 { |
| err = StructuralError{"non-minimal length"} |
| return |
| } |
| } |
|
|
| return |
| } |
|
|
| |
| |
| |
| func parseSequenceOf(bytes []byte, sliceType reflect.Type, elemType reflect.Type) (ret reflect.Value, err error) { |
| matchAny, expectedTag, compoundType, ok := getUniversalType(elemType) |
| if !ok { |
| err = StructuralError{"unknown Go type for slice"} |
| return |
| } |
|
|
| |
| |
| numElements := 0 |
| for offset := 0; offset < len(bytes); { |
| var t tagAndLength |
| t, offset, err = parseTagAndLength(bytes, offset) |
| if err != nil { |
| return |
| } |
| switch t.tag { |
| case TagIA5String, TagGeneralString, TagT61String, TagUTF8String, TagNumericString, TagBMPString: |
| |
| |
| |
| t.tag = TagPrintableString |
| case TagGeneralizedTime, TagUTCTime: |
| |
| t.tag = TagUTCTime |
| } |
|
|
| if !matchAny && (t.class != ClassUniversal || t.isCompound != compoundType || t.tag != expectedTag) { |
| err = StructuralError{"sequence tag mismatch"} |
| return |
| } |
| if invalidLength(offset, t.length, len(bytes)) { |
| err = SyntaxError{"truncated sequence"} |
| return |
| } |
| offset += t.length |
| numElements++ |
| } |
| elemSize := uint64(elemType.Size()) |
| safeCap := saferio.SliceCapWithSize(elemSize, uint64(numElements)) |
| if safeCap < 0 { |
| err = SyntaxError{fmt.Sprintf("%s slice too big: %d elements of %d bytes", elemType.Kind(), numElements, elemSize)} |
| return |
| } |
| ret = reflect.MakeSlice(sliceType, 0, safeCap) |
| params := fieldParameters{} |
| offset := 0 |
| for i := 0; i < numElements; i++ { |
| ret = reflect.Append(ret, reflect.Zero(elemType)) |
| offset, err = parseField(ret.Index(i), bytes, offset, params) |
| if err != nil { |
| return |
| } |
| } |
| return |
| } |
|
|
| var ( |
| bitStringType = reflect.TypeFor[BitString]() |
| objectIdentifierType = reflect.TypeFor[ObjectIdentifier]() |
| enumeratedType = reflect.TypeFor[Enumerated]() |
| flagType = reflect.TypeFor[Flag]() |
| timeType = reflect.TypeFor[time.Time]() |
| rawValueType = reflect.TypeFor[RawValue]() |
| rawContentsType = reflect.TypeFor[RawContent]() |
| bigIntType = reflect.TypeFor[*big.Int]() |
| ) |
|
|
| |
| |
| func invalidLength(offset, length, sliceLength int) bool { |
| return offset+length < offset || offset+length > sliceLength |
| } |
|
|
| |
| |
| |
| func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParameters) (offset int, err error) { |
| offset = initOffset |
| fieldType := v.Type() |
|
|
| |
| if offset == len(bytes) { |
| if !setDefaultValue(v, params) { |
| err = SyntaxError{"sequence truncated"} |
| } |
| return |
| } |
|
|
| |
| if ifaceType := fieldType; ifaceType.Kind() == reflect.Interface && ifaceType.NumMethod() == 0 { |
| var t tagAndLength |
| t, offset, err = parseTagAndLength(bytes, offset) |
| if err != nil { |
| return |
| } |
| if invalidLength(offset, t.length, len(bytes)) { |
| err = SyntaxError{"data truncated"} |
| return |
| } |
| var result any |
| if !t.isCompound && t.class == ClassUniversal { |
| innerBytes := bytes[offset : offset+t.length] |
| switch t.tag { |
| case TagBoolean: |
| result, err = parseBool(innerBytes) |
| case TagPrintableString: |
| result, err = parsePrintableString(innerBytes) |
| case TagNumericString: |
| result, err = parseNumericString(innerBytes) |
| case TagIA5String: |
| result, err = parseIA5String(innerBytes) |
| case TagT61String: |
| result, err = parseT61String(innerBytes) |
| case TagUTF8String: |
| result, err = parseUTF8String(innerBytes) |
| case TagInteger: |
| result, err = parseInt64(innerBytes) |
| case TagBitString: |
| result, err = parseBitString(innerBytes) |
| case TagOID: |
| result, err = parseObjectIdentifier(innerBytes) |
| case TagUTCTime: |
| result, err = parseUTCTime(innerBytes) |
| case TagGeneralizedTime: |
| result, err = parseGeneralizedTime(innerBytes) |
| case TagOctetString: |
| result = innerBytes |
| case TagBMPString: |
| result, err = parseBMPString(innerBytes) |
| default: |
| |
| } |
| } |
| offset += t.length |
| if err != nil { |
| return |
| } |
| if result != nil { |
| v.Set(reflect.ValueOf(result)) |
| } |
| return |
| } |
|
|
| t, offset, err := parseTagAndLength(bytes, offset) |
| if err != nil { |
| return |
| } |
| if params.explicit { |
| expectedClass := ClassContextSpecific |
| if params.application { |
| expectedClass = ClassApplication |
| } |
| if offset == len(bytes) { |
| err = StructuralError{"explicit tag has no child"} |
| return |
| } |
| if t.class == expectedClass && t.tag == *params.tag && (t.length == 0 || t.isCompound) { |
| if fieldType == rawValueType { |
| |
| } else if t.length > 0 { |
| t, offset, err = parseTagAndLength(bytes, offset) |
| if err != nil { |
| return |
| } |
| } else { |
| if fieldType != flagType { |
| err = StructuralError{"zero length explicit tag was not an asn1.Flag"} |
| return |
| } |
| v.SetBool(true) |
| return |
| } |
| } else { |
| |
| ok := setDefaultValue(v, params) |
| if ok { |
| offset = initOffset |
| } else { |
| err = StructuralError{"explicitly tagged member didn't match"} |
| } |
| return |
| } |
| } |
|
|
| matchAny, universalTag, compoundType, ok1 := getUniversalType(fieldType) |
| if !ok1 { |
| err = StructuralError{fmt.Sprintf("unknown Go type: %v", fieldType)} |
| return |
| } |
|
|
| |
| |
| |
| |
| if universalTag == TagPrintableString { |
| if t.class == ClassUniversal { |
| switch t.tag { |
| case TagIA5String, TagGeneralString, TagT61String, TagUTF8String, TagNumericString, TagBMPString: |
| universalTag = t.tag |
| } |
| } else if params.stringType != 0 { |
| universalTag = params.stringType |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| if universalTag == TagUTCTime { |
| if t.class == ClassUniversal { |
| if t.tag == TagGeneralizedTime { |
| universalTag = t.tag |
| } |
| } else if params.timeType != 0 { |
| universalTag = params.timeType |
| } |
| } |
|
|
| if params.set { |
| universalTag = TagSet |
| } |
|
|
| matchAnyClassAndTag := matchAny |
| expectedClass := ClassUniversal |
| expectedTag := universalTag |
|
|
| if !params.explicit && params.tag != nil { |
| expectedClass = ClassContextSpecific |
| expectedTag = *params.tag |
| matchAnyClassAndTag = false |
| } |
|
|
| if !params.explicit && params.application && params.tag != nil { |
| expectedClass = ClassApplication |
| expectedTag = *params.tag |
| matchAnyClassAndTag = false |
| } |
|
|
| if !params.explicit && params.private && params.tag != nil { |
| expectedClass = ClassPrivate |
| expectedTag = *params.tag |
| matchAnyClassAndTag = false |
| } |
|
|
| |
| if !matchAnyClassAndTag && (t.class != expectedClass || t.tag != expectedTag) || |
| (!matchAny && t.isCompound != compoundType) { |
| |
| ok := setDefaultValue(v, params) |
| if ok { |
| offset = initOffset |
| } else { |
| err = StructuralError{fmt.Sprintf("tags don't match (%d vs %+v) %+v %s @%d", expectedTag, t, params, fieldType.Name(), offset)} |
| } |
| return |
| } |
| if invalidLength(offset, t.length, len(bytes)) { |
| err = SyntaxError{"data truncated"} |
| return |
| } |
| innerBytes := bytes[offset : offset+t.length] |
| offset += t.length |
|
|
| |
| switch v := v.Addr().Interface().(type) { |
| case *RawValue: |
| *v = RawValue{t.class, t.tag, t.isCompound, innerBytes, bytes[initOffset:offset]} |
| return |
| case *ObjectIdentifier: |
| *v, err = parseObjectIdentifier(innerBytes) |
| return |
| case *BitString: |
| *v, err = parseBitString(innerBytes) |
| return |
| case *time.Time: |
| if universalTag == TagUTCTime { |
| *v, err = parseUTCTime(innerBytes) |
| return |
| } |
| *v, err = parseGeneralizedTime(innerBytes) |
| return |
| case *Enumerated: |
| parsedInt, err1 := parseInt32(innerBytes) |
| if err1 == nil { |
| *v = Enumerated(parsedInt) |
| } |
| err = err1 |
| return |
| case *Flag: |
| *v = true |
| return |
| case **big.Int: |
| parsedInt, err1 := parseBigInt(innerBytes) |
| if err1 == nil { |
| *v = parsedInt |
| } |
| err = err1 |
| return |
| } |
| switch val := v; val.Kind() { |
| case reflect.Bool: |
| parsedBool, err1 := parseBool(innerBytes) |
| if err1 == nil { |
| val.SetBool(parsedBool) |
| } |
| err = err1 |
| return |
| case reflect.Int, reflect.Int32, reflect.Int64: |
| if val.Type().Size() == 4 { |
| parsedInt, err1 := parseInt32(innerBytes) |
| if err1 == nil { |
| val.SetInt(int64(parsedInt)) |
| } |
| err = err1 |
| } else { |
| parsedInt, err1 := parseInt64(innerBytes) |
| if err1 == nil { |
| val.SetInt(parsedInt) |
| } |
| err = err1 |
| } |
| return |
| |
| case reflect.Struct: |
| structType := fieldType |
|
|
| for i := 0; i < structType.NumField(); i++ { |
| if !structType.Field(i).IsExported() { |
| err = StructuralError{"struct contains unexported fields"} |
| return |
| } |
| } |
|
|
| if structType.NumField() > 0 && |
| structType.Field(0).Type == rawContentsType { |
| bytes := bytes[initOffset:offset] |
| val.Field(0).Set(reflect.ValueOf(RawContent(bytes))) |
| } |
|
|
| innerOffset := 0 |
| for i := 0; i < structType.NumField(); i++ { |
| field := structType.Field(i) |
| if i == 0 && field.Type == rawContentsType { |
| continue |
| } |
| innerOffset, err = parseField(val.Field(i), innerBytes, innerOffset, parseFieldParameters(field.Tag.Get("asn1"))) |
| if err != nil { |
| return |
| } |
| } |
| |
| |
| |
| return |
| case reflect.Slice: |
| sliceType := fieldType |
| if sliceType.Elem().Kind() == reflect.Uint8 { |
| val.Set(reflect.MakeSlice(sliceType, len(innerBytes), len(innerBytes))) |
| reflect.Copy(val, reflect.ValueOf(innerBytes)) |
| return |
| } |
| newSlice, err1 := parseSequenceOf(innerBytes, sliceType, sliceType.Elem()) |
| if err1 == nil { |
| val.Set(newSlice) |
| } |
| err = err1 |
| return |
| case reflect.String: |
| var v string |
| switch universalTag { |
| case TagPrintableString: |
| v, err = parsePrintableString(innerBytes) |
| case TagNumericString: |
| v, err = parseNumericString(innerBytes) |
| case TagIA5String: |
| v, err = parseIA5String(innerBytes) |
| case TagT61String: |
| v, err = parseT61String(innerBytes) |
| case TagUTF8String: |
| v, err = parseUTF8String(innerBytes) |
| case TagGeneralString: |
| |
| |
| |
| |
| v, err = parseT61String(innerBytes) |
| case TagBMPString: |
| v, err = parseBMPString(innerBytes) |
|
|
| default: |
| err = SyntaxError{fmt.Sprintf("internal error: unknown string type %d", universalTag)} |
| } |
| if err == nil { |
| val.SetString(v) |
| } |
| return |
| } |
| err = StructuralError{"unsupported: " + v.Type().String()} |
| return |
| } |
|
|
| |
| |
| func canHaveDefaultValue(k reflect.Kind) bool { |
| switch k { |
| case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| return true |
| } |
|
|
| return false |
| } |
|
|
| |
| |
| |
| func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) { |
| if !params.optional { |
| return |
| } |
| ok = true |
| if params.defaultValue == nil { |
| return |
| } |
| if canHaveDefaultValue(v.Kind()) { |
| v.SetInt(*params.defaultValue) |
| } |
| return |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| func Unmarshal(b []byte, val any) (rest []byte, err error) { |
| return UnmarshalWithParams(b, val, "") |
| } |
|
|
| |
| |
| type invalidUnmarshalError struct { |
| Type reflect.Type |
| } |
|
|
| func (e *invalidUnmarshalError) Error() string { |
| if e.Type == nil { |
| return "asn1: Unmarshal recipient value is nil" |
| } |
|
|
| if e.Type.Kind() != reflect.Pointer { |
| return "asn1: Unmarshal recipient value is non-pointer " + e.Type.String() |
| } |
| return "asn1: Unmarshal recipient value is nil " + e.Type.String() |
| } |
|
|
| |
| |
| func UnmarshalWithParams(b []byte, val any, params string) (rest []byte, err error) { |
| v := reflect.ValueOf(val) |
| if v.Kind() != reflect.Pointer || v.IsNil() { |
| return nil, &invalidUnmarshalError{reflect.TypeOf(val)} |
| } |
| offset, err := parseField(v.Elem(), b, 0, parseFieldParameters(params)) |
| if err != nil { |
| return nil, err |
| } |
| return b[offset:], nil |
| } |
|
|