2009-10-13 15:37:48 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2009-11-10 11:55:53 -07:00
|
|
|
// The asn1 package implements parsing of DER-encoded ASN.1 data structures,
|
|
|
|
// as defined in ITU-T Rec X.690.
|
2009-10-13 15:37:48 -06:00
|
|
|
//
|
|
|
|
// See also ``A Layman's Guide to a Subset of ASN.1, BER, and DER,''
|
|
|
|
// http://luca.ntop.org/Teaching/Appunti/asn1.html.
|
|
|
|
package asn1
|
|
|
|
|
|
|
|
// ASN.1 is a syntax for specifying abstract objects and BER, DER, PER, XER etc
|
|
|
|
// are different encoding formats for those objects. Here, we'll be dealing
|
|
|
|
// with DER, the Distinguished Encoding Rules. DER is used in X.509 because
|
|
|
|
// it's fast to parse and, unlike BER, has a unique encoding for every object.
|
|
|
|
// When calculating hashes over objects, it's important that the resulting
|
|
|
|
// bytes be the same at both ends and DER removes this margin of error.
|
|
|
|
//
|
|
|
|
// ASN.1 is very complex and this package doesn't attempt to implement
|
|
|
|
// everything by any means.
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:33:31 -07:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"reflect"
|
|
|
|
"time"
|
2009-10-13 15:37:48 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// A StructuralError suggests that the ASN.1 data is valid, but the Go type
|
|
|
|
// which is receiving it doesn't match.
|
|
|
|
type StructuralError struct {
|
2009-12-15 16:33:31 -07:00
|
|
|
Msg string
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
func (e StructuralError) String() string { return "ASN.1 structure error: " + e.Msg }
|
2009-10-13 15:37:48 -06:00
|
|
|
|
|
|
|
// A SyntaxError suggests that the ASN.1 data is invalid.
|
|
|
|
type SyntaxError struct {
|
2009-12-15 16:33:31 -07:00
|
|
|
Msg string
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
func (e SyntaxError) String() string { return "ASN.1 syntax error: " + e.Msg }
|
2009-10-13 15:37:48 -06:00
|
|
|
|
|
|
|
// We start by dealing with each of the primitive types in turn.
|
|
|
|
|
|
|
|
// BOOLEAN
|
|
|
|
|
|
|
|
func parseBool(bytes []byte) (ret bool, err os.Error) {
|
|
|
|
if len(bytes) != 1 {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"invalid boolean"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
return bytes[0] != 0, nil
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// INTEGER
|
|
|
|
|
|
|
|
// parseInt64 treats the given bytes as a big-endian, signed integer and
|
|
|
|
// returns the result.
|
|
|
|
func parseInt64(bytes []byte) (ret int64, err os.Error) {
|
|
|
|
if len(bytes) > 8 {
|
|
|
|
// We'll overflow an int64 in this case.
|
2009-12-15 16:33:31 -07:00
|
|
|
err = StructuralError{"integer too large"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
for bytesRead := 0; bytesRead < len(bytes); bytesRead++ {
|
2009-12-15 16:33:31 -07:00
|
|
|
ret <<= 8
|
|
|
|
ret |= int64(bytes[bytesRead])
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Shift up and down in order to sign extend the result.
|
2009-12-15 16:33:31 -07:00
|
|
|
ret <<= 64 - uint8(len(bytes))*8
|
|
|
|
ret >>= 64 - uint8(len(bytes))*8
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseInt treats the given bytes as a big-endian, signed integer and returns
|
|
|
|
// the result.
|
|
|
|
func parseInt(bytes []byte) (int, os.Error) {
|
2009-12-15 16:33:31 -07:00
|
|
|
ret64, err := parseInt64(bytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, err
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
if ret64 != int64(int(ret64)) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, StructuralError{"integer too large"}
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return int(ret64), nil
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// BIT STRING
|
|
|
|
|
|
|
|
// BitString is the structure to use when you want an ASN.1 BIT STRING type. A
|
|
|
|
// bit string is padded up to the nearest byte in memory and the number of
|
|
|
|
// valid bits is recorded. Padding bits will be zero.
|
|
|
|
type BitString struct {
|
2009-12-15 16:33:31 -07:00
|
|
|
Bytes []byte // bits packed into bytes.
|
|
|
|
BitLength int // length in bits.
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// At returns the bit at the given index. If the index is out of range it
|
|
|
|
// returns false.
|
|
|
|
func (b BitString) At(i int) int {
|
|
|
|
if i < 0 || i >= b.BitLength {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
x := i / 8
|
|
|
|
y := 7 - uint(i%8)
|
|
|
|
return int(b.Bytes[x]>>y) & 1
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
2009-11-17 19:09:41 -07:00
|
|
|
// RightAlign returns a slice where the padding bits are at the beginning. The
|
|
|
|
// slice may share memory with the BitString.
|
|
|
|
func (b BitString) RightAlign() []byte {
|
2009-12-15 16:33:31 -07:00
|
|
|
shift := uint(8 - (b.BitLength % 8))
|
2009-11-17 19:09:41 -07:00
|
|
|
if shift == 8 || len(b.Bytes) == 0 {
|
|
|
|
return b.Bytes
|
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
a := make([]byte, len(b.Bytes))
|
|
|
|
a[0] = b.Bytes[0] >> shift
|
2009-11-17 19:09:41 -07:00
|
|
|
for i := 1; i < len(b.Bytes); i++ {
|
2009-12-15 16:33:31 -07:00
|
|
|
a[i] = b.Bytes[i-1] << (8 - shift)
|
|
|
|
a[i] |= b.Bytes[i] >> shift
|
2009-11-17 19:09:41 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
return a
|
2009-11-17 19:09:41 -07:00
|
|
|
}
|
|
|
|
|
2009-10-13 15:37:48 -06:00
|
|
|
// parseBitString parses an ASN.1 bit string from the given byte array and returns it.
|
|
|
|
func parseBitString(bytes []byte) (ret BitString, err os.Error) {
|
|
|
|
if len(bytes) == 0 {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"zero length BIT STRING"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
paddingBits := int(bytes[0])
|
2009-10-13 15:37:48 -06:00
|
|
|
if paddingBits > 7 ||
|
2009-11-04 18:03:08 -07:00
|
|
|
len(bytes) == 1 && paddingBits > 0 ||
|
|
|
|
bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"invalid padding bits in BIT STRING"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
ret.BitLength = (len(bytes)-1)*8 - paddingBits
|
|
|
|
ret.Bytes = bytes[1:]
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// OBJECT IDENTIFIER
|
|
|
|
|
|
|
|
// An ObjectIdentifier represents an ASN.1 OBJECT IDENTIFIER.
|
|
|
|
type ObjectIdentifier []int
|
|
|
|
|
|
|
|
// parseObjectIdentifier parses an OBJECT IDENTIFER from the given bytes and
|
|
|
|
// returns it. An object identifer is a sequence of variable length integers
|
|
|
|
// that are assigned in a hierarachy.
|
|
|
|
func parseObjectIdentifier(bytes []byte) (s []int, err os.Error) {
|
|
|
|
if len(bytes) == 0 {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"zero length OBJECT IDENTIFIER"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// In the worst case, we get two elements from the first byte (which is
|
|
|
|
// encoded differently) and then every varint is a single byte long.
|
2009-12-15 16:33:31 -07:00
|
|
|
s = make([]int, len(bytes)+1)
|
2009-10-13 15:37:48 -06:00
|
|
|
|
|
|
|
// The first byte is 40*value1 + value2:
|
2009-12-15 16:33:31 -07:00
|
|
|
s[0] = int(bytes[0]) / 40
|
|
|
|
s[1] = int(bytes[0]) % 40
|
|
|
|
i := 2
|
2009-10-13 15:37:48 -06:00
|
|
|
for offset := 1; offset < len(bytes); i++ {
|
2009-12-15 16:33:31 -07:00
|
|
|
var v int
|
|
|
|
v, offset, err = parseBase128Int(bytes, offset)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
s[i] = v
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
s = s[0:i]
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseBase128Int parses a base-128 encoded int from the given offset in the
|
|
|
|
// given byte array. It returns the value and the new offset.
|
|
|
|
func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err os.Error) {
|
2009-12-15 16:33:31 -07:00
|
|
|
offset = initOffset
|
2009-10-13 15:37:48 -06:00
|
|
|
for shifted := 0; offset < len(bytes); shifted++ {
|
|
|
|
if shifted > 4 {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = StructuralError{"base 128 integer too large"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
ret <<= 7
|
|
|
|
b := bytes[offset]
|
|
|
|
ret |= int(b & 0x7f)
|
|
|
|
offset++
|
2009-10-13 15:37:48 -06:00
|
|
|
if b&0x80 == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"truncated base 128 integer"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// UTCTime
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
func isDigit(b byte) bool { return '0' <= b && b <= '9' }
|
2009-10-13 15:37:48 -06:00
|
|
|
|
|
|
|
// twoDigits returns the value of two, base 10 digits.
|
|
|
|
func twoDigits(bytes []byte, max int) (int, bool) {
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
if !isDigit(bytes[i]) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, false
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
value := (int(bytes[0])-'0')*10 + int(bytes[1]-'0')
|
2009-10-13 15:37:48 -06:00
|
|
|
if value > max {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, false
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return value, true
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseUTCTime parses the UTCTime from the given byte array and returns the
|
|
|
|
// resulting time.
|
2009-11-17 19:09:41 -07:00
|
|
|
func parseUTCTime(bytes []byte) (ret *time.Time, err os.Error) {
|
2009-10-13 15:37:48 -06:00
|
|
|
// A UTCTime can take the following formats:
|
|
|
|
//
|
|
|
|
// 1111111
|
|
|
|
// 01234567890123456
|
|
|
|
//
|
|
|
|
// YYMMDDhhmmZ
|
|
|
|
// YYMMDDhhmm+hhmm
|
|
|
|
// YYMMDDhhmm-hhmm
|
|
|
|
// YYMMDDhhmmssZ
|
|
|
|
// YYMMDDhhmmss+hhmm
|
|
|
|
// YYMMDDhhmmss-hhmm
|
|
|
|
if len(bytes) < 11 {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"UTCTime too short"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
ret = new(time.Time)
|
2009-11-17 19:09:41 -07:00
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
var ok1, ok2, ok3, ok4, ok5 bool
|
|
|
|
year, ok1 := twoDigits(bytes[0:2], 99)
|
2009-10-13 15:37:48 -06:00
|
|
|
// RFC 5280, section 5.1.2.4 says that years 2050 or later use another date
|
|
|
|
// scheme.
|
2009-11-17 19:09:41 -07:00
|
|
|
if year >= 50 {
|
2009-11-09 22:09:34 -07:00
|
|
|
ret.Year = 1900 + int64(year)
|
2009-10-13 15:37:48 -06:00
|
|
|
} else {
|
2009-11-09 22:09:34 -07:00
|
|
|
ret.Year = 2000 + int64(year)
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
ret.Month, ok2 = twoDigits(bytes[2:4], 12)
|
|
|
|
ret.Day, ok3 = twoDigits(bytes[4:6], 31)
|
|
|
|
ret.Hour, ok4 = twoDigits(bytes[6:8], 23)
|
|
|
|
ret.Minute, ok5 = twoDigits(bytes[8:10], 59)
|
2009-10-13 15:37:48 -06:00
|
|
|
if !ok1 || !ok2 || !ok3 || !ok4 || !ok5 {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto Error
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
bytes = bytes[10:]
|
2009-10-13 15:37:48 -06:00
|
|
|
switch bytes[0] {
|
|
|
|
case '0', '1', '2', '3', '4', '5', '6':
|
|
|
|
if len(bytes) < 3 {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto Error
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
ret.Second, ok1 = twoDigits(bytes[0:2], 60) // 60, not 59, because of leap seconds.
|
2009-10-13 15:37:48 -06:00
|
|
|
if !ok1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto Error
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
bytes = bytes[2:]
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
if len(bytes) == 0 {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto Error
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
switch bytes[0] {
|
|
|
|
case 'Z':
|
|
|
|
if len(bytes) != 1 {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto Error
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
case '-', '+':
|
|
|
|
if len(bytes) != 5 {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto Error
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
hours, ok1 := twoDigits(bytes[1:3], 12)
|
|
|
|
minutes, ok2 := twoDigits(bytes[3:5], 59)
|
2009-10-13 15:37:48 -06:00
|
|
|
if !ok1 || !ok2 {
|
2009-11-09 13:07:39 -07:00
|
|
|
goto Error
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
sign := 1
|
2009-10-13 15:37:48 -06:00
|
|
|
if bytes[0] == '-' {
|
2009-11-09 13:07:39 -07:00
|
|
|
sign = -1
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
ret.ZoneOffset = sign * (60 * (hours*60 + minutes))
|
2009-10-13 15:37:48 -06:00
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
goto Error
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
|
|
|
|
Error:
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"invalid UTCTime"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// PrintableString
|
|
|
|
|
|
|
|
// parsePrintableString parses a ASN.1 PrintableString from the given byte
|
|
|
|
// array and returns it.
|
|
|
|
func parsePrintableString(bytes []byte) (ret string, err os.Error) {
|
|
|
|
for _, b := range bytes {
|
|
|
|
if !isPrintable(b) {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"PrintableString contains invalid character"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
ret = string(bytes)
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// isPrintable returns true iff the given b is in the ASN.1 PrintableString set.
|
|
|
|
func isPrintable(b byte) bool {
|
|
|
|
return 'a' <= b && b <= 'z' ||
|
2009-11-04 18:03:08 -07:00
|
|
|
'A' <= b && b <= 'Z' ||
|
|
|
|
'0' <= b && b <= '9' ||
|
|
|
|
'\'' <= b && b <= ')' ||
|
|
|
|
'+' <= b && b <= '/' ||
|
|
|
|
b == ' ' ||
|
|
|
|
b == ':' ||
|
|
|
|
b == '=' ||
|
2009-11-09 13:07:39 -07:00
|
|
|
b == '?'
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// IA5String
|
|
|
|
|
|
|
|
// parseIA5String parses a ASN.1 IA5String (ASCII string) from the given
|
|
|
|
// byte array and returns it.
|
|
|
|
func parseIA5String(bytes []byte) (ret string, err os.Error) {
|
|
|
|
for _, b := range bytes {
|
|
|
|
if b >= 0x80 {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"IA5String contains invalid character"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
ret = string(bytes)
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// A RawValue represents an undecoded ASN.1 object.
|
|
|
|
type RawValue struct {
|
2009-12-15 16:33:31 -07:00
|
|
|
Class, Tag int
|
|
|
|
IsCompound bool
|
|
|
|
Bytes []byte
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
2009-11-18 17:32:44 -07:00
|
|
|
// RawContent is used to signal that the undecoded, DER data needs to be
|
|
|
|
// preserved for a struct. To use it, the first field of the struct must have
|
|
|
|
// this type. It's an error for any of the other fields to have this type.
|
|
|
|
type RawContent []byte
|
|
|
|
|
2009-10-13 15:37:48 -06:00
|
|
|
// Tagging
|
|
|
|
|
|
|
|
// parseTagAndLength parses an ASN.1 tag and length pair from the given offset
|
|
|
|
// into a byte array. It returns the parsed data and the new offset. SET and
|
|
|
|
// SET OF (tag 17) are mapped to SEQUENCE and SEQUENCE OF (tag 16) since we
|
|
|
|
// don't distinguish between ordered and unordered objects in this code.
|
|
|
|
func parseTagAndLength(bytes []byte, initOffset int) (ret tagAndLength, offset int, err os.Error) {
|
2009-12-15 16:33:31 -07:00
|
|
|
offset = initOffset
|
|
|
|
b := bytes[offset]
|
|
|
|
offset++
|
|
|
|
ret.class = int(b >> 6)
|
|
|
|
ret.isCompound = b&0x20 == 0x20
|
|
|
|
ret.tag = int(b & 0x1f)
|
2009-10-13 15:37:48 -06:00
|
|
|
|
|
|
|
// If the bottom five bits are set, then the tag number is actually base 128
|
|
|
|
// encoded afterwards
|
|
|
|
if ret.tag == 0x1f {
|
2009-12-15 16:33:31 -07:00
|
|
|
ret.tag, offset, err = parseBase128Int(bytes, offset)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if offset >= len(bytes) {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"truncated tag or length"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
b = bytes[offset]
|
|
|
|
offset++
|
2009-10-13 15:37:48 -06:00
|
|
|
if b&0x80 == 0 {
|
|
|
|
// The length is encoded in the bottom 7 bits.
|
2009-11-09 22:09:34 -07:00
|
|
|
ret.length = int(b & 0x7f)
|
2009-10-13 15:37:48 -06:00
|
|
|
} else {
|
|
|
|
// Bottom 7 bits give the number of length bytes to follow.
|
2009-12-15 16:33:31 -07:00
|
|
|
numBytes := int(b & 0x7f)
|
2009-10-13 15:37:48 -06:00
|
|
|
// We risk overflowing a signed 32-bit number if we accept more than 3 bytes.
|
|
|
|
if numBytes > 3 {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = StructuralError{"length too large"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
if numBytes == 0 {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"indefinite length found (not DER)"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
ret.length = 0
|
2009-10-13 15:37:48 -06:00
|
|
|
for i := 0; i < numBytes; i++ {
|
|
|
|
if offset >= len(bytes) {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"truncated tag or length"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
b = bytes[offset]
|
|
|
|
offset++
|
|
|
|
ret.length <<= 8
|
|
|
|
ret.length |= int(b)
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseSequenceOf is used for SEQUENCE OF and SET OF values. It tries to parse
|
|
|
|
// a number of ASN.1 values from the given byte array and returns them as a
|
|
|
|
// slice of Go values of the given type.
|
|
|
|
func parseSequenceOf(bytes []byte, sliceType *reflect.SliceType, elemType reflect.Type) (ret *reflect.SliceValue, err os.Error) {
|
2009-12-15 16:33:31 -07:00
|
|
|
expectedTag, compoundType, ok := getUniversalType(elemType)
|
2009-10-13 15:37:48 -06:00
|
|
|
if !ok {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = StructuralError{"unknown Go type for slice"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// First we iterate over the input and count the number of elements,
|
|
|
|
// checking that the types are correct in each case.
|
2009-12-15 16:33:31 -07:00
|
|
|
numElements := 0
|
2009-10-13 15:37:48 -06:00
|
|
|
for offset := 0; offset < len(bytes); {
|
2009-12-15 16:33:31 -07:00
|
|
|
var t tagAndLength
|
|
|
|
t, offset, err = parseTagAndLength(bytes, offset)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
if t.class != classUniversal || t.isCompound != compoundType || t.tag != expectedTag {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = StructuralError{"sequence tag mismatch"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
if invalidLength(offset, t.length, len(bytes)) {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"truncated sequence"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
offset += t.length
|
|
|
|
numElements++
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
ret = reflect.MakeSlice(sliceType, numElements, numElements)
|
|
|
|
params := fieldParameters{}
|
|
|
|
offset := 0
|
2009-10-13 15:37:48 -06:00
|
|
|
for i := 0; i < numElements; i++ {
|
2009-12-15 16:33:31 -07:00
|
|
|
offset, err = parseField(ret.Elem(i), bytes, offset, params)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2009-12-15 16:33:31 -07:00
|
|
|
bitStringType = reflect.Typeof(BitString{})
|
|
|
|
objectIdentifierType = reflect.Typeof(ObjectIdentifier{})
|
|
|
|
timeType = reflect.Typeof(&time.Time{})
|
|
|
|
rawValueType = reflect.Typeof(RawValue{})
|
|
|
|
rawContentsType = reflect.Typeof(RawContent(nil))
|
2009-10-13 15:37:48 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
// invalidLength returns true iff offset + length > sliceLength, or if the
|
|
|
|
// addition would overflow.
|
|
|
|
func invalidLength(offset, length, sliceLength int) bool {
|
2009-11-09 13:07:39 -07:00
|
|
|
return offset+length < offset || offset+length > sliceLength
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseField is the main parsing function. Given a byte array and an offset
|
|
|
|
// into the array, it will try to parse a suitable ASN.1 value out and store it
|
|
|
|
// in the given Value.
|
|
|
|
func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParameters) (offset int, err os.Error) {
|
2009-12-15 16:33:31 -07:00
|
|
|
offset = initOffset
|
|
|
|
fieldType := v.Type()
|
2009-10-13 15:37:48 -06:00
|
|
|
|
|
|
|
// If we have run out of data, it may be that there are optional elements at the end.
|
|
|
|
if offset == len(bytes) {
|
|
|
|
if !setDefaultValue(v, params) {
|
2009-11-09 13:07:39 -07:00
|
|
|
err = SyntaxError{"sequence truncated"}
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deal with raw values.
|
|
|
|
if fieldType == rawValueType {
|
2009-12-15 16:33:31 -07:00
|
|
|
var t tagAndLength
|
|
|
|
t, offset, err = parseTagAndLength(bytes, offset)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
if invalidLength(offset, t.length, len(bytes)) {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"data truncated"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
result := RawValue{t.class, t.tag, t.isCompound, bytes[offset : offset+t.length]}
|
|
|
|
offset += t.length
|
|
|
|
v.(*reflect.StructValue).Set(reflect.NewValue(result).(*reflect.StructValue))
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Deal with the ANY type.
|
|
|
|
if ifaceType, ok := fieldType.(*reflect.InterfaceType); ok && ifaceType.NumMethod() == 0 {
|
2009-12-15 16:33:31 -07:00
|
|
|
ifaceValue := v.(*reflect.InterfaceValue)
|
|
|
|
var t tagAndLength
|
|
|
|
t, offset, err = parseTagAndLength(bytes, offset)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
if invalidLength(offset, t.length, len(bytes)) {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"data truncated"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
var result interface{}
|
|
|
|
if !t.isCompound && t.class == classUniversal {
|
2009-12-15 16:33:31 -07:00
|
|
|
innerBytes := bytes[offset : offset+t.length]
|
2009-10-13 15:37:48 -06:00
|
|
|
switch t.tag {
|
|
|
|
case tagPrintableString:
|
2009-11-09 13:07:39 -07:00
|
|
|
result, err = parsePrintableString(innerBytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
case tagIA5String:
|
2009-11-09 13:07:39 -07:00
|
|
|
result, err = parseIA5String(innerBytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
case tagInteger:
|
2009-11-09 13:07:39 -07:00
|
|
|
result, err = parseInt64(innerBytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
case tagBitString:
|
2009-11-09 13:07:39 -07:00
|
|
|
result, err = parseBitString(innerBytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
case tagOID:
|
2009-11-09 13:07:39 -07:00
|
|
|
result, err = parseObjectIdentifier(innerBytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
case tagUTCTime:
|
2009-11-09 13:07:39 -07:00
|
|
|
result, err = parseUTCTime(innerBytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
case tagOctetString:
|
2009-11-09 13:07:39 -07:00
|
|
|
result = innerBytes
|
2009-10-13 15:37:48 -06:00
|
|
|
default:
|
2009-11-04 18:03:08 -07:00
|
|
|
// If we don't know how to handle the type, we just leave Value as nil.
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
offset += t.length
|
2009-10-13 15:37:48 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
if result != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
ifaceValue.Set(reflect.NewValue(result))
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
universalTag, compoundType, ok1 := getUniversalType(fieldType)
|
2009-10-13 15:37:48 -06:00
|
|
|
if !ok1 {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = StructuralError{fmt.Sprintf("unknown Go type: %v", fieldType)}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
t, offset, err := parseTagAndLength(bytes, offset)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
if params.explicit {
|
|
|
|
if t.class == classContextSpecific && t.tag == *params.tag && t.isCompound {
|
2009-12-15 16:33:31 -07:00
|
|
|
t, offset, err = parseTagAndLength(bytes, offset)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// The tags didn't match, it might be an optional element.
|
2009-12-15 16:33:31 -07:00
|
|
|
ok := setDefaultValue(v, params)
|
2009-10-13 15:37:48 -06:00
|
|
|
if ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
offset = initOffset
|
2009-10-13 15:37:48 -06:00
|
|
|
} else {
|
2009-11-09 13:07:39 -07:00
|
|
|
err = StructuralError{"explicitly tagged member didn't match"}
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Special case for strings: PrintableString and IA5String both map to
|
|
|
|
// the Go type string. getUniversalType returns the tag for
|
|
|
|
// PrintableString when it sees a string so, if we see an IA5String on
|
|
|
|
// the wire, we change the universal type to match.
|
|
|
|
if universalTag == tagPrintableString && t.tag == tagIA5String {
|
2009-11-09 13:07:39 -07:00
|
|
|
universalTag = tagIA5String
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
expectedClass := classUniversal
|
|
|
|
expectedTag := universalTag
|
2009-10-13 15:37:48 -06:00
|
|
|
|
|
|
|
if !params.explicit && params.tag != nil {
|
2009-12-15 16:33:31 -07:00
|
|
|
expectedClass = classContextSpecific
|
|
|
|
expectedTag = *params.tag
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// We have unwrapped any explicit tagging at this point.
|
|
|
|
if t.class != expectedClass || t.tag != expectedTag || t.isCompound != compoundType {
|
|
|
|
// Tags don't match. Again, it could be an optional element.
|
2009-12-15 16:33:31 -07:00
|
|
|
ok := setDefaultValue(v, params)
|
2009-10-13 15:37:48 -06:00
|
|
|
if ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
offset = initOffset
|
2009-10-13 15:37:48 -06:00
|
|
|
} else {
|
2009-11-18 17:32:44 -07:00
|
|
|
err = StructuralError{fmt.Sprintf("tags don't match (%d vs %+v) %+v %s @%d", expectedTag, t, params, fieldType.Name(), offset)}
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
if invalidLength(offset, t.length, len(bytes)) {
|
2009-12-15 16:33:31 -07:00
|
|
|
err = SyntaxError{"data truncated"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
innerBytes := bytes[offset : offset+t.length]
|
2010-01-11 19:53:58 -07:00
|
|
|
offset += t.length
|
2009-10-13 15:37:48 -06:00
|
|
|
|
|
|
|
// We deal with the structures defined in this package first.
|
|
|
|
switch fieldType {
|
|
|
|
case objectIdentifierType:
|
2009-12-15 16:33:31 -07:00
|
|
|
newSlice, err1 := parseObjectIdentifier(innerBytes)
|
|
|
|
sliceValue := v.(*reflect.SliceValue)
|
|
|
|
sliceValue.Set(reflect.MakeSlice(sliceValue.Type().(*reflect.SliceType), len(newSlice), len(newSlice)))
|
2009-10-13 15:37:48 -06:00
|
|
|
if err1 == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
reflect.ArrayCopy(sliceValue, reflect.NewValue(newSlice).(reflect.ArrayOrSliceValue))
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
err = err1
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
case bitStringType:
|
2009-12-15 16:33:31 -07:00
|
|
|
structValue := v.(*reflect.StructValue)
|
|
|
|
bs, err1 := parseBitString(innerBytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err1 == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
structValue.Set(reflect.NewValue(bs).(*reflect.StructValue))
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
err = err1
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
case timeType:
|
2009-12-15 16:33:31 -07:00
|
|
|
ptrValue := v.(*reflect.PtrValue)
|
|
|
|
time, err1 := parseUTCTime(innerBytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err1 == nil {
|
2009-11-17 19:09:41 -07:00
|
|
|
ptrValue.Set(reflect.NewValue(time).(*reflect.PtrValue))
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
err = err1
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
switch val := v.(type) {
|
|
|
|
case *reflect.BoolValue:
|
2009-12-15 16:33:31 -07:00
|
|
|
parsedBool, err1 := parseBool(innerBytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err1 == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
val.Set(parsedBool)
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
err = err1
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
case *reflect.IntValue:
|
2009-12-15 16:33:31 -07:00
|
|
|
parsedInt, err1 := parseInt(innerBytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err1 == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
val.Set(parsedInt)
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
err = err1
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
case *reflect.Int64Value:
|
2009-12-15 16:33:31 -07:00
|
|
|
parsedInt, err1 := parseInt64(innerBytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
if err1 == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
val.Set(parsedInt)
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
err = err1
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
case *reflect.StructValue:
|
2009-12-15 16:33:31 -07:00
|
|
|
structType := fieldType.(*reflect.StructType)
|
2009-11-18 17:32:44 -07:00
|
|
|
|
|
|
|
if structType.NumField() > 0 &&
|
|
|
|
structType.Field(0).Type == rawContentsType {
|
2010-01-11 19:53:58 -07:00
|
|
|
bytes := bytes[initOffset:offset]
|
2009-12-15 16:33:31 -07:00
|
|
|
val.Field(0).SetValue(reflect.NewValue(RawContent(bytes)))
|
2009-11-18 17:32:44 -07:00
|
|
|
}
|
|
|
|
|
2009-12-15 16:33:31 -07:00
|
|
|
innerOffset := 0
|
2009-10-13 15:37:48 -06:00
|
|
|
for i := 0; i < structType.NumField(); i++ {
|
2009-12-15 16:33:31 -07:00
|
|
|
field := structType.Field(i)
|
2009-11-18 17:32:44 -07:00
|
|
|
if i == 0 && field.Type == rawContentsType {
|
|
|
|
continue
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
innerOffset, err = parseField(val.Field(i), innerBytes, innerOffset, parseFieldParameters(field.Tag))
|
2009-10-13 15:37:48 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// We allow extra bytes at the end of the SEQUENCE because
|
|
|
|
// adding elements to the end has been used in X.509 as the
|
|
|
|
// version numbers have increased.
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
case *reflect.SliceValue:
|
2009-12-15 16:33:31 -07:00
|
|
|
sliceType := fieldType.(*reflect.SliceType)
|
2009-10-13 15:37:48 -06:00
|
|
|
if _, ok := sliceType.Elem().(*reflect.Uint8Type); ok {
|
2009-12-15 16:33:31 -07:00
|
|
|
val.Set(reflect.MakeSlice(sliceType, len(innerBytes), len(innerBytes)))
|
|
|
|
reflect.ArrayCopy(val, reflect.NewValue(innerBytes).(reflect.ArrayOrSliceValue))
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
newSlice, err1 := parseSequenceOf(innerBytes, sliceType, sliceType.Elem())
|
2009-10-13 15:37:48 -06:00
|
|
|
if err1 == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
val.Set(newSlice)
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
err = err1
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
case *reflect.StringValue:
|
2009-12-15 16:33:31 -07:00
|
|
|
var v string
|
2009-10-13 15:37:48 -06:00
|
|
|
switch universalTag {
|
|
|
|
case tagPrintableString:
|
2009-11-09 13:07:39 -07:00
|
|
|
v, err = parsePrintableString(innerBytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
case tagIA5String:
|
2009-11-09 13:07:39 -07:00
|
|
|
v, err = parseIA5String(innerBytes)
|
2009-10-13 15:37:48 -06:00
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
err = SyntaxError{fmt.Sprintf("internal error: unknown string type %d", universalTag)}
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
if err == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
val.Set(v)
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
err = StructuralError{"unknown Go type"}
|
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// setDefaultValue is used to install a default value, from a tag string, into
|
|
|
|
// a Value. It is successful is the field was optional, even if a default value
|
|
|
|
// wasn't provided or it failed to install it into the Value.
|
|
|
|
func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) {
|
|
|
|
if !params.optional {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
ok = true
|
2009-10-13 15:37:48 -06:00
|
|
|
if params.defaultValue == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
switch val := v.(type) {
|
|
|
|
case *reflect.IntValue:
|
2009-11-09 13:07:39 -07:00
|
|
|
val.Set(int(*params.defaultValue))
|
2009-10-13 15:37:48 -06:00
|
|
|
case *reflect.Int64Value:
|
2009-11-09 13:07:39 -07:00
|
|
|
val.Set(int64(*params.defaultValue))
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal parses the DER-encoded ASN.1 data structure b
|
|
|
|
// and uses the reflect package to fill in an arbitrary value pointed at by val.
|
|
|
|
// Because Unmarshal uses the reflect package, the structs
|
|
|
|
// being written to must use upper case field names.
|
|
|
|
//
|
|
|
|
// An ASN.1 INTEGER can be written to an int or int64.
|
|
|
|
// If the encoded value does not fit in the Go type,
|
|
|
|
// Unmarshal returns a parse error.
|
|
|
|
//
|
|
|
|
// An ASN.1 BIT STRING can be written to a BitString.
|
|
|
|
//
|
|
|
|
// An ASN.1 OCTET STRING can be written to a []byte.
|
|
|
|
//
|
|
|
|
// An ASN.1 OBJECT IDENTIFIER can be written to an
|
|
|
|
// ObjectIdentifier.
|
|
|
|
//
|
|
|
|
// An ASN.1 PrintableString or IA5String can be written to a string.
|
|
|
|
//
|
|
|
|
// Any of the above ASN.1 values can be written to an interface{}.
|
|
|
|
// The value stored in the interface has the corresponding Go type.
|
|
|
|
// For integers, that type is int64.
|
|
|
|
//
|
|
|
|
// An ASN.1 SEQUENCE OF x or SET OF x can be written
|
|
|
|
// to a slice if an x can be written to the slice's element type.
|
|
|
|
//
|
|
|
|
// An ASN.1 SEQUENCE or SET can be written to a struct
|
|
|
|
// if each of the elements in the sequence can be
|
|
|
|
// written to the corresponding element in the struct.
|
|
|
|
//
|
|
|
|
// The following tags on struct fields have special meaning to Unmarshal:
|
|
|
|
//
|
|
|
|
// optional marks the field as ASN.1 OPTIONAL
|
|
|
|
// [explicit] tag:x specifies the ASN.1 tag number; implies ASN.1 CONTEXT SPECIFIC
|
|
|
|
// default:x sets the default value for optional integer fields
|
|
|
|
//
|
2009-11-18 17:32:44 -07:00
|
|
|
// If the type of the first field of a structure is RawContent then the raw
|
|
|
|
// ASN1 contents of the struct will be stored in it.
|
|
|
|
//
|
2009-10-13 15:37:48 -06:00
|
|
|
// Other ASN.1 types are not supported; if it encounters them,
|
|
|
|
// Unmarshal returns a parse error.
|
2009-11-17 19:09:41 -07:00
|
|
|
func Unmarshal(val interface{}, b []byte) (rest []byte, err os.Error) {
|
2009-12-15 16:33:31 -07:00
|
|
|
v := reflect.NewValue(val).(*reflect.PtrValue).Elem()
|
|
|
|
offset, err := parseField(v, b, 0, fieldParameters{})
|
2009-11-17 19:09:41 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2009-12-15 16:33:31 -07:00
|
|
|
return b[offset:], nil
|
2009-10-13 15:37:48 -06:00
|
|
|
}
|