| 1 |
747 |
jeremybenn |
// 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 |
|
|
"fmt"
|
| 24 |
|
|
"math/big"
|
| 25 |
|
|
"reflect"
|
| 26 |
|
|
"time"
|
| 27 |
|
|
)
|
| 28 |
|
|
|
| 29 |
|
|
// A StructuralError suggests that the ASN.1 data is valid, but the Go type
|
| 30 |
|
|
// which is receiving it doesn't match.
|
| 31 |
|
|
type StructuralError struct {
|
| 32 |
|
|
Msg string
|
| 33 |
|
|
}
|
| 34 |
|
|
|
| 35 |
|
|
func (e StructuralError) Error() string { return "ASN.1 structure error: " + e.Msg }
|
| 36 |
|
|
|
| 37 |
|
|
// A SyntaxError suggests that the ASN.1 data is invalid.
|
| 38 |
|
|
type SyntaxError struct {
|
| 39 |
|
|
Msg string
|
| 40 |
|
|
}
|
| 41 |
|
|
|
| 42 |
|
|
func (e SyntaxError) Error() string { return "ASN.1 syntax error: " + e.Msg }
|
| 43 |
|
|
|
| 44 |
|
|
// We start by dealing with each of the primitive types in turn.
|
| 45 |
|
|
|
| 46 |
|
|
// BOOLEAN
|
| 47 |
|
|
|
| 48 |
|
|
func parseBool(bytes []byte) (ret bool, err error) {
|
| 49 |
|
|
if len(bytes) != 1 {
|
| 50 |
|
|
err = SyntaxError{"invalid boolean"}
|
| 51 |
|
|
return
|
| 52 |
|
|
}
|
| 53 |
|
|
|
| 54 |
|
|
return bytes[0] != 0, nil
|
| 55 |
|
|
}
|
| 56 |
|
|
|
| 57 |
|
|
// INTEGER
|
| 58 |
|
|
|
| 59 |
|
|
// parseInt64 treats the given bytes as a big-endian, signed integer and
|
| 60 |
|
|
// returns the result.
|
| 61 |
|
|
func parseInt64(bytes []byte) (ret int64, err error) {
|
| 62 |
|
|
if len(bytes) > 8 {
|
| 63 |
|
|
// We'll overflow an int64 in this case.
|
| 64 |
|
|
err = StructuralError{"integer too large"}
|
| 65 |
|
|
return
|
| 66 |
|
|
}
|
| 67 |
|
|
for bytesRead := 0; bytesRead < len(bytes); bytesRead++ {
|
| 68 |
|
|
ret <<= 8
|
| 69 |
|
|
ret |= int64(bytes[bytesRead])
|
| 70 |
|
|
}
|
| 71 |
|
|
|
| 72 |
|
|
// Shift up and down in order to sign extend the result.
|
| 73 |
|
|
ret <<= 64 - uint8(len(bytes))*8
|
| 74 |
|
|
ret >>= 64 - uint8(len(bytes))*8
|
| 75 |
|
|
return
|
| 76 |
|
|
}
|
| 77 |
|
|
|
| 78 |
|
|
// parseInt treats the given bytes as a big-endian, signed integer and returns
|
| 79 |
|
|
// the result.
|
| 80 |
|
|
func parseInt(bytes []byte) (int, error) {
|
| 81 |
|
|
ret64, err := parseInt64(bytes)
|
| 82 |
|
|
if err != nil {
|
| 83 |
|
|
return 0, err
|
| 84 |
|
|
}
|
| 85 |
|
|
if ret64 != int64(int(ret64)) {
|
| 86 |
|
|
return 0, StructuralError{"integer too large"}
|
| 87 |
|
|
}
|
| 88 |
|
|
return int(ret64), nil
|
| 89 |
|
|
}
|
| 90 |
|
|
|
| 91 |
|
|
var bigOne = big.NewInt(1)
|
| 92 |
|
|
|
| 93 |
|
|
// parseBigInt treats the given bytes as a big-endian, signed integer and returns
|
| 94 |
|
|
// the result.
|
| 95 |
|
|
func parseBigInt(bytes []byte) *big.Int {
|
| 96 |
|
|
ret := new(big.Int)
|
| 97 |
|
|
if len(bytes) > 0 && bytes[0]&0x80 == 0x80 {
|
| 98 |
|
|
// This is a negative number.
|
| 99 |
|
|
notBytes := make([]byte, len(bytes))
|
| 100 |
|
|
for i := range notBytes {
|
| 101 |
|
|
notBytes[i] = ^bytes[i]
|
| 102 |
|
|
}
|
| 103 |
|
|
ret.SetBytes(notBytes)
|
| 104 |
|
|
ret.Add(ret, bigOne)
|
| 105 |
|
|
ret.Neg(ret)
|
| 106 |
|
|
return ret
|
| 107 |
|
|
}
|
| 108 |
|
|
ret.SetBytes(bytes)
|
| 109 |
|
|
return ret
|
| 110 |
|
|
}
|
| 111 |
|
|
|
| 112 |
|
|
// BIT STRING
|
| 113 |
|
|
|
| 114 |
|
|
// BitString is the structure to use when you want an ASN.1 BIT STRING type. A
|
| 115 |
|
|
// bit string is padded up to the nearest byte in memory and the number of
|
| 116 |
|
|
// valid bits is recorded. Padding bits will be zero.
|
| 117 |
|
|
type BitString struct {
|
| 118 |
|
|
Bytes []byte // bits packed into bytes.
|
| 119 |
|
|
BitLength int // length in bits.
|
| 120 |
|
|
}
|
| 121 |
|
|
|
| 122 |
|
|
// At returns the bit at the given index. If the index is out of range it
|
| 123 |
|
|
// returns false.
|
| 124 |
|
|
func (b BitString) At(i int) int {
|
| 125 |
|
|
if i < 0 || i >= b.BitLength {
|
| 126 |
|
|
return 0
|
| 127 |
|
|
}
|
| 128 |
|
|
x := i / 8
|
| 129 |
|
|
y := 7 - uint(i%8)
|
| 130 |
|
|
return int(b.Bytes[x]>>y) & 1
|
| 131 |
|
|
}
|
| 132 |
|
|
|
| 133 |
|
|
// RightAlign returns a slice where the padding bits are at the beginning. The
|
| 134 |
|
|
// slice may share memory with the BitString.
|
| 135 |
|
|
func (b BitString) RightAlign() []byte {
|
| 136 |
|
|
shift := uint(8 - (b.BitLength % 8))
|
| 137 |
|
|
if shift == 8 || len(b.Bytes) == 0 {
|
| 138 |
|
|
return b.Bytes
|
| 139 |
|
|
}
|
| 140 |
|
|
|
| 141 |
|
|
a := make([]byte, len(b.Bytes))
|
| 142 |
|
|
a[0] = b.Bytes[0] >> shift
|
| 143 |
|
|
for i := 1; i < len(b.Bytes); i++ {
|
| 144 |
|
|
a[i] = b.Bytes[i-1] << (8 - shift)
|
| 145 |
|
|
a[i] |= b.Bytes[i] >> shift
|
| 146 |
|
|
}
|
| 147 |
|
|
|
| 148 |
|
|
return a
|
| 149 |
|
|
}
|
| 150 |
|
|
|
| 151 |
|
|
// parseBitString parses an ASN.1 bit string from the given byte slice and returns it.
|
| 152 |
|
|
func parseBitString(bytes []byte) (ret BitString, err error) {
|
| 153 |
|
|
if len(bytes) == 0 {
|
| 154 |
|
|
err = SyntaxError{"zero length BIT STRING"}
|
| 155 |
|
|
return
|
| 156 |
|
|
}
|
| 157 |
|
|
paddingBits := int(bytes[0])
|
| 158 |
|
|
if paddingBits > 7 ||
|
| 159 |
|
|
len(bytes) == 1 && paddingBits > 0 ||
|
| 160 |
|
|
bytes[len(bytes)-1]&((1<
|
| 161 |
|
|
err = SyntaxError{"invalid padding bits in BIT STRING"}
|
| 162 |
|
|
return
|
| 163 |
|
|
}
|
| 164 |
|
|
ret.BitLength = (len(bytes)-1)*8 - paddingBits
|
| 165 |
|
|
ret.Bytes = bytes[1:]
|
| 166 |
|
|
return
|
| 167 |
|
|
}
|
| 168 |
|
|
|
| 169 |
|
|
// OBJECT IDENTIFIER
|
| 170 |
|
|
|
| 171 |
|
|
// An ObjectIdentifier represents an ASN.1 OBJECT IDENTIFIER.
|
| 172 |
|
|
type ObjectIdentifier []int
|
| 173 |
|
|
|
| 174 |
|
|
// Equal returns true iff oi and other represent the same identifier.
|
| 175 |
|
|
func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool {
|
| 176 |
|
|
if len(oi) != len(other) {
|
| 177 |
|
|
return false
|
| 178 |
|
|
}
|
| 179 |
|
|
for i := 0; i < len(oi); i++ {
|
| 180 |
|
|
if oi[i] != other[i] {
|
| 181 |
|
|
return false
|
| 182 |
|
|
}
|
| 183 |
|
|
}
|
| 184 |
|
|
|
| 185 |
|
|
return true
|
| 186 |
|
|
}
|
| 187 |
|
|
|
| 188 |
|
|
// parseObjectIdentifier parses an OBJECT IDENTIFIER from the given bytes and
|
| 189 |
|
|
// returns it. An object identifier is a sequence of variable length integers
|
| 190 |
|
|
// that are assigned in a hierarchy.
|
| 191 |
|
|
func parseObjectIdentifier(bytes []byte) (s []int, err error) {
|
| 192 |
|
|
if len(bytes) == 0 {
|
| 193 |
|
|
err = SyntaxError{"zero length OBJECT IDENTIFIER"}
|
| 194 |
|
|
return
|
| 195 |
|
|
}
|
| 196 |
|
|
|
| 197 |
|
|
// In the worst case, we get two elements from the first byte (which is
|
| 198 |
|
|
// encoded differently) and then every varint is a single byte long.
|
| 199 |
|
|
s = make([]int, len(bytes)+1)
|
| 200 |
|
|
|
| 201 |
|
|
// The first byte is 40*value1 + value2:
|
| 202 |
|
|
s[0] = int(bytes[0]) / 40
|
| 203 |
|
|
s[1] = int(bytes[0]) % 40
|
| 204 |
|
|
i := 2
|
| 205 |
|
|
for offset := 1; offset < len(bytes); i++ {
|
| 206 |
|
|
var v int
|
| 207 |
|
|
v, offset, err = parseBase128Int(bytes, offset)
|
| 208 |
|
|
if err != nil {
|
| 209 |
|
|
return
|
| 210 |
|
|
}
|
| 211 |
|
|
s[i] = v
|
| 212 |
|
|
}
|
| 213 |
|
|
s = s[0:i]
|
| 214 |
|
|
return
|
| 215 |
|
|
}
|
| 216 |
|
|
|
| 217 |
|
|
// ENUMERATED
|
| 218 |
|
|
|
| 219 |
|
|
// An Enumerated is represented as a plain int.
|
| 220 |
|
|
type Enumerated int
|
| 221 |
|
|
|
| 222 |
|
|
// FLAG
|
| 223 |
|
|
|
| 224 |
|
|
// A Flag accepts any data and is set to true if present.
|
| 225 |
|
|
type Flag bool
|
| 226 |
|
|
|
| 227 |
|
|
// parseBase128Int parses a base-128 encoded int from the given offset in the
|
| 228 |
|
|
// given byte slice. It returns the value and the new offset.
|
| 229 |
|
|
func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err error) {
|
| 230 |
|
|
offset = initOffset
|
| 231 |
|
|
for shifted := 0; offset < len(bytes); shifted++ {
|
| 232 |
|
|
if shifted > 4 {
|
| 233 |
|
|
err = StructuralError{"base 128 integer too large"}
|
| 234 |
|
|
return
|
| 235 |
|
|
}
|
| 236 |
|
|
ret <<= 7
|
| 237 |
|
|
b := bytes[offset]
|
| 238 |
|
|
ret |= int(b & 0x7f)
|
| 239 |
|
|
offset++
|
| 240 |
|
|
if b&0x80 == 0 {
|
| 241 |
|
|
return
|
| 242 |
|
|
}
|
| 243 |
|
|
}
|
| 244 |
|
|
err = SyntaxError{"truncated base 128 integer"}
|
| 245 |
|
|
return
|
| 246 |
|
|
}
|
| 247 |
|
|
|
| 248 |
|
|
// UTCTime
|
| 249 |
|
|
|
| 250 |
|
|
func parseUTCTime(bytes []byte) (ret time.Time, err error) {
|
| 251 |
|
|
s := string(bytes)
|
| 252 |
|
|
ret, err = time.Parse("0601021504Z0700", s)
|
| 253 |
|
|
if err == nil {
|
| 254 |
|
|
return
|
| 255 |
|
|
}
|
| 256 |
|
|
ret, err = time.Parse("060102150405Z0700", s)
|
| 257 |
|
|
return
|
| 258 |
|
|
}
|
| 259 |
|
|
|
| 260 |
|
|
// parseGeneralizedTime parses the GeneralizedTime from the given byte slice
|
| 261 |
|
|
// and returns the resulting time.
|
| 262 |
|
|
func parseGeneralizedTime(bytes []byte) (ret time.Time, err error) {
|
| 263 |
|
|
return time.Parse("20060102150405Z0700", string(bytes))
|
| 264 |
|
|
}
|
| 265 |
|
|
|
| 266 |
|
|
// PrintableString
|
| 267 |
|
|
|
| 268 |
|
|
// parsePrintableString parses a ASN.1 PrintableString from the given byte
|
| 269 |
|
|
// array and returns it.
|
| 270 |
|
|
func parsePrintableString(bytes []byte) (ret string, err error) {
|
| 271 |
|
|
for _, b := range bytes {
|
| 272 |
|
|
if !isPrintable(b) {
|
| 273 |
|
|
err = SyntaxError{"PrintableString contains invalid character"}
|
| 274 |
|
|
return
|
| 275 |
|
|
}
|
| 276 |
|
|
}
|
| 277 |
|
|
ret = string(bytes)
|
| 278 |
|
|
return
|
| 279 |
|
|
}
|
| 280 |
|
|
|
| 281 |
|
|
// isPrintable returns true iff the given b is in the ASN.1 PrintableString set.
|
| 282 |
|
|
func isPrintable(b byte) bool {
|
| 283 |
|
|
return 'a' <= b && b <= 'z' ||
|
| 284 |
|
|
'A' <= b && b <= 'Z' ||
|
| 285 |
|
|
'0' <= b && b <= '9' ||
|
| 286 |
|
|
'\'' <= b && b <= ')' ||
|
| 287 |
|
|
'+' <= b && b <= '/' ||
|
| 288 |
|
|
b == ' ' ||
|
| 289 |
|
|
b == ':' ||
|
| 290 |
|
|
b == '=' ||
|
| 291 |
|
|
b == '?' ||
|
| 292 |
|
|
// This is technically not allowed in a PrintableString.
|
| 293 |
|
|
// However, x509 certificates with wildcard strings don't
|
| 294 |
|
|
// always use the correct string type so we permit it.
|
| 295 |
|
|
b == '*'
|
| 296 |
|
|
}
|
| 297 |
|
|
|
| 298 |
|
|
// IA5String
|
| 299 |
|
|
|
| 300 |
|
|
// parseIA5String parses a ASN.1 IA5String (ASCII string) from the given
|
| 301 |
|
|
// byte slice and returns it.
|
| 302 |
|
|
func parseIA5String(bytes []byte) (ret string, err error) {
|
| 303 |
|
|
for _, b := range bytes {
|
| 304 |
|
|
if b >= 0x80 {
|
| 305 |
|
|
err = SyntaxError{"IA5String contains invalid character"}
|
| 306 |
|
|
return
|
| 307 |
|
|
}
|
| 308 |
|
|
}
|
| 309 |
|
|
ret = string(bytes)
|
| 310 |
|
|
return
|
| 311 |
|
|
}
|
| 312 |
|
|
|
| 313 |
|
|
// T61String
|
| 314 |
|
|
|
| 315 |
|
|
// parseT61String parses a ASN.1 T61String (8-bit clean string) from the given
|
| 316 |
|
|
// byte slice and returns it.
|
| 317 |
|
|
func parseT61String(bytes []byte) (ret string, err error) {
|
| 318 |
|
|
return string(bytes), nil
|
| 319 |
|
|
}
|
| 320 |
|
|
|
| 321 |
|
|
// UTF8String
|
| 322 |
|
|
|
| 323 |
|
|
// parseUTF8String parses a ASN.1 UTF8String (raw UTF-8) from the given byte
|
| 324 |
|
|
// array and returns it.
|
| 325 |
|
|
func parseUTF8String(bytes []byte) (ret string, err error) {
|
| 326 |
|
|
return string(bytes), nil
|
| 327 |
|
|
}
|
| 328 |
|
|
|
| 329 |
|
|
// A RawValue represents an undecoded ASN.1 object.
|
| 330 |
|
|
type RawValue struct {
|
| 331 |
|
|
Class, Tag int
|
| 332 |
|
|
IsCompound bool
|
| 333 |
|
|
Bytes []byte
|
| 334 |
|
|
FullBytes []byte // includes the tag and length
|
| 335 |
|
|
}
|
| 336 |
|
|
|
| 337 |
|
|
// RawContent is used to signal that the undecoded, DER data needs to be
|
| 338 |
|
|
// preserved for a struct. To use it, the first field of the struct must have
|
| 339 |
|
|
// this type. It's an error for any of the other fields to have this type.
|
| 340 |
|
|
type RawContent []byte
|
| 341 |
|
|
|
| 342 |
|
|
// Tagging
|
| 343 |
|
|
|
| 344 |
|
|
// parseTagAndLength parses an ASN.1 tag and length pair from the given offset
|
| 345 |
|
|
// into a byte slice. It returns the parsed data and the new offset. SET and
|
| 346 |
|
|
// SET OF (tag 17) are mapped to SEQUENCE and SEQUENCE OF (tag 16) since we
|
| 347 |
|
|
// don't distinguish between ordered and unordered objects in this code.
|
| 348 |
|
|
func parseTagAndLength(bytes []byte, initOffset int) (ret tagAndLength, offset int, err error) {
|
| 349 |
|
|
offset = initOffset
|
| 350 |
|
|
b := bytes[offset]
|
| 351 |
|
|
offset++
|
| 352 |
|
|
ret.class = int(b >> 6)
|
| 353 |
|
|
ret.isCompound = b&0x20 == 0x20
|
| 354 |
|
|
ret.tag = int(b & 0x1f)
|
| 355 |
|
|
|
| 356 |
|
|
// If the bottom five bits are set, then the tag number is actually base 128
|
| 357 |
|
|
// encoded afterwards
|
| 358 |
|
|
if ret.tag == 0x1f {
|
| 359 |
|
|
ret.tag, offset, err = parseBase128Int(bytes, offset)
|
| 360 |
|
|
if err != nil {
|
| 361 |
|
|
return
|
| 362 |
|
|
}
|
| 363 |
|
|
}
|
| 364 |
|
|
if offset >= len(bytes) {
|
| 365 |
|
|
err = SyntaxError{"truncated tag or length"}
|
| 366 |
|
|
return
|
| 367 |
|
|
}
|
| 368 |
|
|
b = bytes[offset]
|
| 369 |
|
|
offset++
|
| 370 |
|
|
if b&0x80 == 0 {
|
| 371 |
|
|
// The length is encoded in the bottom 7 bits.
|
| 372 |
|
|
ret.length = int(b & 0x7f)
|
| 373 |
|
|
} else {
|
| 374 |
|
|
// Bottom 7 bits give the number of length bytes to follow.
|
| 375 |
|
|
numBytes := int(b & 0x7f)
|
| 376 |
|
|
// We risk overflowing a signed 32-bit number if we accept more than 3 bytes.
|
| 377 |
|
|
if numBytes > 3 {
|
| 378 |
|
|
err = StructuralError{"length too large"}
|
| 379 |
|
|
return
|
| 380 |
|
|
}
|
| 381 |
|
|
if numBytes == 0 {
|
| 382 |
|
|
err = SyntaxError{"indefinite length found (not DER)"}
|
| 383 |
|
|
return
|
| 384 |
|
|
}
|
| 385 |
|
|
ret.length = 0
|
| 386 |
|
|
for i := 0; i < numBytes; i++ {
|
| 387 |
|
|
if offset >= len(bytes) {
|
| 388 |
|
|
err = SyntaxError{"truncated tag or length"}
|
| 389 |
|
|
return
|
| 390 |
|
|
}
|
| 391 |
|
|
b = bytes[offset]
|
| 392 |
|
|
offset++
|
| 393 |
|
|
ret.length <<= 8
|
| 394 |
|
|
ret.length |= int(b)
|
| 395 |
|
|
}
|
| 396 |
|
|
}
|
| 397 |
|
|
|
| 398 |
|
|
return
|
| 399 |
|
|
}
|
| 400 |
|
|
|
| 401 |
|
|
// parseSequenceOf is used for SEQUENCE OF and SET OF values. It tries to parse
|
| 402 |
|
|
// a number of ASN.1 values from the given byte slice and returns them as a
|
| 403 |
|
|
// slice of Go values of the given type.
|
| 404 |
|
|
func parseSequenceOf(bytes []byte, sliceType reflect.Type, elemType reflect.Type) (ret reflect.Value, err error) {
|
| 405 |
|
|
expectedTag, compoundType, ok := getUniversalType(elemType)
|
| 406 |
|
|
if !ok {
|
| 407 |
|
|
err = StructuralError{"unknown Go type for slice"}
|
| 408 |
|
|
return
|
| 409 |
|
|
}
|
| 410 |
|
|
|
| 411 |
|
|
// First we iterate over the input and count the number of elements,
|
| 412 |
|
|
// checking that the types are correct in each case.
|
| 413 |
|
|
numElements := 0
|
| 414 |
|
|
for offset := 0; offset < len(bytes); {
|
| 415 |
|
|
var t tagAndLength
|
| 416 |
|
|
t, offset, err = parseTagAndLength(bytes, offset)
|
| 417 |
|
|
if err != nil {
|
| 418 |
|
|
return
|
| 419 |
|
|
}
|
| 420 |
|
|
// We pretend that GENERAL STRINGs are PRINTABLE STRINGs so
|
| 421 |
|
|
// that a sequence of them can be parsed into a []string.
|
| 422 |
|
|
if t.tag == tagGeneralString {
|
| 423 |
|
|
t.tag = tagPrintableString
|
| 424 |
|
|
}
|
| 425 |
|
|
if t.class != classUniversal || t.isCompound != compoundType || t.tag != expectedTag {
|
| 426 |
|
|
err = StructuralError{"sequence tag mismatch"}
|
| 427 |
|
|
return
|
| 428 |
|
|
}
|
| 429 |
|
|
if invalidLength(offset, t.length, len(bytes)) {
|
| 430 |
|
|
err = SyntaxError{"truncated sequence"}
|
| 431 |
|
|
return
|
| 432 |
|
|
}
|
| 433 |
|
|
offset += t.length
|
| 434 |
|
|
numElements++
|
| 435 |
|
|
}
|
| 436 |
|
|
ret = reflect.MakeSlice(sliceType, numElements, numElements)
|
| 437 |
|
|
params := fieldParameters{}
|
| 438 |
|
|
offset := 0
|
| 439 |
|
|
for i := 0; i < numElements; i++ {
|
| 440 |
|
|
offset, err = parseField(ret.Index(i), bytes, offset, params)
|
| 441 |
|
|
if err != nil {
|
| 442 |
|
|
return
|
| 443 |
|
|
}
|
| 444 |
|
|
}
|
| 445 |
|
|
return
|
| 446 |
|
|
}
|
| 447 |
|
|
|
| 448 |
|
|
var (
|
| 449 |
|
|
bitStringType = reflect.TypeOf(BitString{})
|
| 450 |
|
|
objectIdentifierType = reflect.TypeOf(ObjectIdentifier{})
|
| 451 |
|
|
enumeratedType = reflect.TypeOf(Enumerated(0))
|
| 452 |
|
|
flagType = reflect.TypeOf(Flag(false))
|
| 453 |
|
|
timeType = reflect.TypeOf(time.Time{})
|
| 454 |
|
|
rawValueType = reflect.TypeOf(RawValue{})
|
| 455 |
|
|
rawContentsType = reflect.TypeOf(RawContent(nil))
|
| 456 |
|
|
bigIntType = reflect.TypeOf(new(big.Int))
|
| 457 |
|
|
)
|
| 458 |
|
|
|
| 459 |
|
|
// invalidLength returns true iff offset + length > sliceLength, or if the
|
| 460 |
|
|
// addition would overflow.
|
| 461 |
|
|
func invalidLength(offset, length, sliceLength int) bool {
|
| 462 |
|
|
return offset+length < offset || offset+length > sliceLength
|
| 463 |
|
|
}
|
| 464 |
|
|
|
| 465 |
|
|
// parseField is the main parsing function. Given a byte slice and an offset
|
| 466 |
|
|
// into the array, it will try to parse a suitable ASN.1 value out and store it
|
| 467 |
|
|
// in the given Value.
|
| 468 |
|
|
func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParameters) (offset int, err error) {
|
| 469 |
|
|
offset = initOffset
|
| 470 |
|
|
fieldType := v.Type()
|
| 471 |
|
|
|
| 472 |
|
|
// If we have run out of data, it may be that there are optional elements at the end.
|
| 473 |
|
|
if offset == len(bytes) {
|
| 474 |
|
|
if !setDefaultValue(v, params) {
|
| 475 |
|
|
err = SyntaxError{"sequence truncated"}
|
| 476 |
|
|
}
|
| 477 |
|
|
return
|
| 478 |
|
|
}
|
| 479 |
|
|
|
| 480 |
|
|
// Deal with raw values.
|
| 481 |
|
|
if fieldType == rawValueType {
|
| 482 |
|
|
var t tagAndLength
|
| 483 |
|
|
t, offset, err = parseTagAndLength(bytes, offset)
|
| 484 |
|
|
if err != nil {
|
| 485 |
|
|
return
|
| 486 |
|
|
}
|
| 487 |
|
|
if invalidLength(offset, t.length, len(bytes)) {
|
| 488 |
|
|
err = SyntaxError{"data truncated"}
|
| 489 |
|
|
return
|
| 490 |
|
|
}
|
| 491 |
|
|
result := RawValue{t.class, t.tag, t.isCompound, bytes[offset : offset+t.length], bytes[initOffset : offset+t.length]}
|
| 492 |
|
|
offset += t.length
|
| 493 |
|
|
v.Set(reflect.ValueOf(result))
|
| 494 |
|
|
return
|
| 495 |
|
|
}
|
| 496 |
|
|
|
| 497 |
|
|
// Deal with the ANY type.
|
| 498 |
|
|
if ifaceType := fieldType; ifaceType.Kind() == reflect.Interface && ifaceType.NumMethod() == 0 {
|
| 499 |
|
|
var t tagAndLength
|
| 500 |
|
|
t, offset, err = parseTagAndLength(bytes, offset)
|
| 501 |
|
|
if err != nil {
|
| 502 |
|
|
return
|
| 503 |
|
|
}
|
| 504 |
|
|
if invalidLength(offset, t.length, len(bytes)) {
|
| 505 |
|
|
err = SyntaxError{"data truncated"}
|
| 506 |
|
|
return
|
| 507 |
|
|
}
|
| 508 |
|
|
var result interface{}
|
| 509 |
|
|
if !t.isCompound && t.class == classUniversal {
|
| 510 |
|
|
innerBytes := bytes[offset : offset+t.length]
|
| 511 |
|
|
switch t.tag {
|
| 512 |
|
|
case tagPrintableString:
|
| 513 |
|
|
result, err = parsePrintableString(innerBytes)
|
| 514 |
|
|
case tagIA5String:
|
| 515 |
|
|
result, err = parseIA5String(innerBytes)
|
| 516 |
|
|
case tagT61String:
|
| 517 |
|
|
result, err = parseT61String(innerBytes)
|
| 518 |
|
|
case tagUTF8String:
|
| 519 |
|
|
result, err = parseUTF8String(innerBytes)
|
| 520 |
|
|
case tagInteger:
|
| 521 |
|
|
result, err = parseInt64(innerBytes)
|
| 522 |
|
|
case tagBitString:
|
| 523 |
|
|
result, err = parseBitString(innerBytes)
|
| 524 |
|
|
case tagOID:
|
| 525 |
|
|
result, err = parseObjectIdentifier(innerBytes)
|
| 526 |
|
|
case tagUTCTime:
|
| 527 |
|
|
result, err = parseUTCTime(innerBytes)
|
| 528 |
|
|
case tagOctetString:
|
| 529 |
|
|
result = innerBytes
|
| 530 |
|
|
default:
|
| 531 |
|
|
// If we don't know how to handle the type, we just leave Value as nil.
|
| 532 |
|
|
}
|
| 533 |
|
|
}
|
| 534 |
|
|
offset += t.length
|
| 535 |
|
|
if err != nil {
|
| 536 |
|
|
return
|
| 537 |
|
|
}
|
| 538 |
|
|
if result != nil {
|
| 539 |
|
|
v.Set(reflect.ValueOf(result))
|
| 540 |
|
|
}
|
| 541 |
|
|
return
|
| 542 |
|
|
}
|
| 543 |
|
|
universalTag, compoundType, ok1 := getUniversalType(fieldType)
|
| 544 |
|
|
if !ok1 {
|
| 545 |
|
|
err = StructuralError{fmt.Sprintf("unknown Go type: %v", fieldType)}
|
| 546 |
|
|
return
|
| 547 |
|
|
}
|
| 548 |
|
|
|
| 549 |
|
|
t, offset, err := parseTagAndLength(bytes, offset)
|
| 550 |
|
|
if err != nil {
|
| 551 |
|
|
return
|
| 552 |
|
|
}
|
| 553 |
|
|
if params.explicit {
|
| 554 |
|
|
expectedClass := classContextSpecific
|
| 555 |
|
|
if params.application {
|
| 556 |
|
|
expectedClass = classApplication
|
| 557 |
|
|
}
|
| 558 |
|
|
if t.class == expectedClass && t.tag == *params.tag && (t.length == 0 || t.isCompound) {
|
| 559 |
|
|
if t.length > 0 {
|
| 560 |
|
|
t, offset, err = parseTagAndLength(bytes, offset)
|
| 561 |
|
|
if err != nil {
|
| 562 |
|
|
return
|
| 563 |
|
|
}
|
| 564 |
|
|
} else {
|
| 565 |
|
|
if fieldType != flagType {
|
| 566 |
|
|
err = StructuralError{"Zero length explicit tag was not an asn1.Flag"}
|
| 567 |
|
|
return
|
| 568 |
|
|
}
|
| 569 |
|
|
v.SetBool(true)
|
| 570 |
|
|
return
|
| 571 |
|
|
}
|
| 572 |
|
|
} else {
|
| 573 |
|
|
// The tags didn't match, it might be an optional element.
|
| 574 |
|
|
ok := setDefaultValue(v, params)
|
| 575 |
|
|
if ok {
|
| 576 |
|
|
offset = initOffset
|
| 577 |
|
|
} else {
|
| 578 |
|
|
err = StructuralError{"explicitly tagged member didn't match"}
|
| 579 |
|
|
}
|
| 580 |
|
|
return
|
| 581 |
|
|
}
|
| 582 |
|
|
}
|
| 583 |
|
|
|
| 584 |
|
|
// Special case for strings: all the ASN.1 string types map to the Go
|
| 585 |
|
|
// type string. getUniversalType returns the tag for PrintableString
|
| 586 |
|
|
// when it sees a string, so if we see a different string type on the
|
| 587 |
|
|
// wire, we change the universal type to match.
|
| 588 |
|
|
if universalTag == tagPrintableString {
|
| 589 |
|
|
switch t.tag {
|
| 590 |
|
|
case tagIA5String, tagGeneralString, tagT61String, tagUTF8String:
|
| 591 |
|
|
universalTag = t.tag
|
| 592 |
|
|
}
|
| 593 |
|
|
}
|
| 594 |
|
|
|
| 595 |
|
|
// Special case for time: UTCTime and GeneralizedTime both map to the
|
| 596 |
|
|
// Go type time.Time.
|
| 597 |
|
|
if universalTag == tagUTCTime && t.tag == tagGeneralizedTime {
|
| 598 |
|
|
universalTag = tagGeneralizedTime
|
| 599 |
|
|
}
|
| 600 |
|
|
|
| 601 |
|
|
expectedClass := classUniversal
|
| 602 |
|
|
expectedTag := universalTag
|
| 603 |
|
|
|
| 604 |
|
|
if !params.explicit && params.tag != nil {
|
| 605 |
|
|
expectedClass = classContextSpecific
|
| 606 |
|
|
expectedTag = *params.tag
|
| 607 |
|
|
}
|
| 608 |
|
|
|
| 609 |
|
|
if !params.explicit && params.application && params.tag != nil {
|
| 610 |
|
|
expectedClass = classApplication
|
| 611 |
|
|
expectedTag = *params.tag
|
| 612 |
|
|
}
|
| 613 |
|
|
|
| 614 |
|
|
// We have unwrapped any explicit tagging at this point.
|
| 615 |
|
|
if t.class != expectedClass || t.tag != expectedTag || t.isCompound != compoundType {
|
| 616 |
|
|
// Tags don't match. Again, it could be an optional element.
|
| 617 |
|
|
ok := setDefaultValue(v, params)
|
| 618 |
|
|
if ok {
|
| 619 |
|
|
offset = initOffset
|
| 620 |
|
|
} else {
|
| 621 |
|
|
err = StructuralError{fmt.Sprintf("tags don't match (%d vs %+v) %+v %s @%d", expectedTag, t, params, fieldType.Name(), offset)}
|
| 622 |
|
|
}
|
| 623 |
|
|
return
|
| 624 |
|
|
}
|
| 625 |
|
|
if invalidLength(offset, t.length, len(bytes)) {
|
| 626 |
|
|
err = SyntaxError{"data truncated"}
|
| 627 |
|
|
return
|
| 628 |
|
|
}
|
| 629 |
|
|
innerBytes := bytes[offset : offset+t.length]
|
| 630 |
|
|
offset += t.length
|
| 631 |
|
|
|
| 632 |
|
|
// We deal with the structures defined in this package first.
|
| 633 |
|
|
switch fieldType {
|
| 634 |
|
|
case objectIdentifierType:
|
| 635 |
|
|
newSlice, err1 := parseObjectIdentifier(innerBytes)
|
| 636 |
|
|
v.Set(reflect.MakeSlice(v.Type(), len(newSlice), len(newSlice)))
|
| 637 |
|
|
if err1 == nil {
|
| 638 |
|
|
reflect.Copy(v, reflect.ValueOf(newSlice))
|
| 639 |
|
|
}
|
| 640 |
|
|
err = err1
|
| 641 |
|
|
return
|
| 642 |
|
|
case bitStringType:
|
| 643 |
|
|
bs, err1 := parseBitString(innerBytes)
|
| 644 |
|
|
if err1 == nil {
|
| 645 |
|
|
v.Set(reflect.ValueOf(bs))
|
| 646 |
|
|
}
|
| 647 |
|
|
err = err1
|
| 648 |
|
|
return
|
| 649 |
|
|
case timeType:
|
| 650 |
|
|
var time time.Time
|
| 651 |
|
|
var err1 error
|
| 652 |
|
|
if universalTag == tagUTCTime {
|
| 653 |
|
|
time, err1 = parseUTCTime(innerBytes)
|
| 654 |
|
|
} else {
|
| 655 |
|
|
time, err1 = parseGeneralizedTime(innerBytes)
|
| 656 |
|
|
}
|
| 657 |
|
|
if err1 == nil {
|
| 658 |
|
|
v.Set(reflect.ValueOf(time))
|
| 659 |
|
|
}
|
| 660 |
|
|
err = err1
|
| 661 |
|
|
return
|
| 662 |
|
|
case enumeratedType:
|
| 663 |
|
|
parsedInt, err1 := parseInt(innerBytes)
|
| 664 |
|
|
if err1 == nil {
|
| 665 |
|
|
v.SetInt(int64(parsedInt))
|
| 666 |
|
|
}
|
| 667 |
|
|
err = err1
|
| 668 |
|
|
return
|
| 669 |
|
|
case flagType:
|
| 670 |
|
|
v.SetBool(true)
|
| 671 |
|
|
return
|
| 672 |
|
|
case bigIntType:
|
| 673 |
|
|
parsedInt := parseBigInt(innerBytes)
|
| 674 |
|
|
v.Set(reflect.ValueOf(parsedInt))
|
| 675 |
|
|
return
|
| 676 |
|
|
}
|
| 677 |
|
|
switch val := v; val.Kind() {
|
| 678 |
|
|
case reflect.Bool:
|
| 679 |
|
|
parsedBool, err1 := parseBool(innerBytes)
|
| 680 |
|
|
if err1 == nil {
|
| 681 |
|
|
val.SetBool(parsedBool)
|
| 682 |
|
|
}
|
| 683 |
|
|
err = err1
|
| 684 |
|
|
return
|
| 685 |
|
|
case reflect.Int, reflect.Int32:
|
| 686 |
|
|
parsedInt, err1 := parseInt(innerBytes)
|
| 687 |
|
|
if err1 == nil {
|
| 688 |
|
|
val.SetInt(int64(parsedInt))
|
| 689 |
|
|
}
|
| 690 |
|
|
err = err1
|
| 691 |
|
|
return
|
| 692 |
|
|
case reflect.Int64:
|
| 693 |
|
|
parsedInt, err1 := parseInt64(innerBytes)
|
| 694 |
|
|
if err1 == nil {
|
| 695 |
|
|
val.SetInt(parsedInt)
|
| 696 |
|
|
}
|
| 697 |
|
|
err = err1
|
| 698 |
|
|
return
|
| 699 |
|
|
// TODO(dfc) Add support for the remaining integer types
|
| 700 |
|
|
case reflect.Struct:
|
| 701 |
|
|
structType := fieldType
|
| 702 |
|
|
|
| 703 |
|
|
if structType.NumField() > 0 &&
|
| 704 |
|
|
structType.Field(0).Type == rawContentsType {
|
| 705 |
|
|
bytes := bytes[initOffset:offset]
|
| 706 |
|
|
val.Field(0).Set(reflect.ValueOf(RawContent(bytes)))
|
| 707 |
|
|
}
|
| 708 |
|
|
|
| 709 |
|
|
innerOffset := 0
|
| 710 |
|
|
for i := 0; i < structType.NumField(); i++ {
|
| 711 |
|
|
field := structType.Field(i)
|
| 712 |
|
|
if i == 0 && field.Type == rawContentsType {
|
| 713 |
|
|
continue
|
| 714 |
|
|
}
|
| 715 |
|
|
innerOffset, err = parseField(val.Field(i), innerBytes, innerOffset, parseFieldParameters(field.Tag.Get("asn1")))
|
| 716 |
|
|
if err != nil {
|
| 717 |
|
|
return
|
| 718 |
|
|
}
|
| 719 |
|
|
}
|
| 720 |
|
|
// We allow extra bytes at the end of the SEQUENCE because
|
| 721 |
|
|
// adding elements to the end has been used in X.509 as the
|
| 722 |
|
|
// version numbers have increased.
|
| 723 |
|
|
return
|
| 724 |
|
|
case reflect.Slice:
|
| 725 |
|
|
sliceType := fieldType
|
| 726 |
|
|
if sliceType.Elem().Kind() == reflect.Uint8 {
|
| 727 |
|
|
val.Set(reflect.MakeSlice(sliceType, len(innerBytes), len(innerBytes)))
|
| 728 |
|
|
reflect.Copy(val, reflect.ValueOf(innerBytes))
|
| 729 |
|
|
return
|
| 730 |
|
|
}
|
| 731 |
|
|
newSlice, err1 := parseSequenceOf(innerBytes, sliceType, sliceType.Elem())
|
| 732 |
|
|
if err1 == nil {
|
| 733 |
|
|
val.Set(newSlice)
|
| 734 |
|
|
}
|
| 735 |
|
|
err = err1
|
| 736 |
|
|
return
|
| 737 |
|
|
case reflect.String:
|
| 738 |
|
|
var v string
|
| 739 |
|
|
switch universalTag {
|
| 740 |
|
|
case tagPrintableString:
|
| 741 |
|
|
v, err = parsePrintableString(innerBytes)
|
| 742 |
|
|
case tagIA5String:
|
| 743 |
|
|
v, err = parseIA5String(innerBytes)
|
| 744 |
|
|
case tagT61String:
|
| 745 |
|
|
v, err = parseT61String(innerBytes)
|
| 746 |
|
|
case tagUTF8String:
|
| 747 |
|
|
v, err = parseUTF8String(innerBytes)
|
| 748 |
|
|
case tagGeneralString:
|
| 749 |
|
|
// GeneralString is specified in ISO-2022/ECMA-35,
|
| 750 |
|
|
// A brief review suggests that it includes structures
|
| 751 |
|
|
// that allow the encoding to change midstring and
|
| 752 |
|
|
// such. We give up and pass it as an 8-bit string.
|
| 753 |
|
|
v, err = parseT61String(innerBytes)
|
| 754 |
|
|
default:
|
| 755 |
|
|
err = SyntaxError{fmt.Sprintf("internal error: unknown string type %d", universalTag)}
|
| 756 |
|
|
}
|
| 757 |
|
|
if err == nil {
|
| 758 |
|
|
val.SetString(v)
|
| 759 |
|
|
}
|
| 760 |
|
|
return
|
| 761 |
|
|
}
|
| 762 |
|
|
err = StructuralError{"unsupported: " + v.Type().String()}
|
| 763 |
|
|
return
|
| 764 |
|
|
}
|
| 765 |
|
|
|
| 766 |
|
|
// setDefaultValue is used to install a default value, from a tag string, into
|
| 767 |
|
|
// a Value. It is successful is the field was optional, even if a default value
|
| 768 |
|
|
// wasn't provided or it failed to install it into the Value.
|
| 769 |
|
|
func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) {
|
| 770 |
|
|
if !params.optional {
|
| 771 |
|
|
return
|
| 772 |
|
|
}
|
| 773 |
|
|
ok = true
|
| 774 |
|
|
if params.defaultValue == nil {
|
| 775 |
|
|
return
|
| 776 |
|
|
}
|
| 777 |
|
|
switch val := v; val.Kind() {
|
| 778 |
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
| 779 |
|
|
val.SetInt(*params.defaultValue)
|
| 780 |
|
|
}
|
| 781 |
|
|
return
|
| 782 |
|
|
}
|
| 783 |
|
|
|
| 784 |
|
|
// Unmarshal parses the DER-encoded ASN.1 data structure b
|
| 785 |
|
|
// and uses the reflect package to fill in an arbitrary value pointed at by val.
|
| 786 |
|
|
// Because Unmarshal uses the reflect package, the structs
|
| 787 |
|
|
// being written to must use upper case field names.
|
| 788 |
|
|
//
|
| 789 |
|
|
// An ASN.1 INTEGER can be written to an int, int32, int64,
|
| 790 |
|
|
// or *big.Int (from the math/big package).
|
| 791 |
|
|
// If the encoded value does not fit in the Go type,
|
| 792 |
|
|
// Unmarshal returns a parse error.
|
| 793 |
|
|
//
|
| 794 |
|
|
// An ASN.1 BIT STRING can be written to a BitString.
|
| 795 |
|
|
//
|
| 796 |
|
|
// An ASN.1 OCTET STRING can be written to a []byte.
|
| 797 |
|
|
//
|
| 798 |
|
|
// An ASN.1 OBJECT IDENTIFIER can be written to an
|
| 799 |
|
|
// ObjectIdentifier.
|
| 800 |
|
|
//
|
| 801 |
|
|
// An ASN.1 ENUMERATED can be written to an Enumerated.
|
| 802 |
|
|
//
|
| 803 |
|
|
// An ASN.1 UTCTIME or GENERALIZEDTIME can be written to a time.Time.
|
| 804 |
|
|
//
|
| 805 |
|
|
// An ASN.1 PrintableString or IA5String can be written to a string.
|
| 806 |
|
|
//
|
| 807 |
|
|
// Any of the above ASN.1 values can be written to an interface{}.
|
| 808 |
|
|
// The value stored in the interface has the corresponding Go type.
|
| 809 |
|
|
// For integers, that type is int64.
|
| 810 |
|
|
//
|
| 811 |
|
|
// An ASN.1 SEQUENCE OF x or SET OF x can be written
|
| 812 |
|
|
// to a slice if an x can be written to the slice's element type.
|
| 813 |
|
|
//
|
| 814 |
|
|
// An ASN.1 SEQUENCE or SET can be written to a struct
|
| 815 |
|
|
// if each of the elements in the sequence can be
|
| 816 |
|
|
// written to the corresponding element in the struct.
|
| 817 |
|
|
//
|
| 818 |
|
|
// The following tags on struct fields have special meaning to Unmarshal:
|
| 819 |
|
|
//
|
| 820 |
|
|
// optional marks the field as ASN.1 OPTIONAL
|
| 821 |
|
|
// [explicit] tag:x specifies the ASN.1 tag number; implies ASN.1 CONTEXT SPECIFIC
|
| 822 |
|
|
// default:x sets the default value for optional integer fields
|
| 823 |
|
|
//
|
| 824 |
|
|
// If the type of the first field of a structure is RawContent then the raw
|
| 825 |
|
|
// ASN1 contents of the struct will be stored in it.
|
| 826 |
|
|
//
|
| 827 |
|
|
// Other ASN.1 types are not supported; if it encounters them,
|
| 828 |
|
|
// Unmarshal returns a parse error.
|
| 829 |
|
|
func Unmarshal(b []byte, val interface{}) (rest []byte, err error) {
|
| 830 |
|
|
return UnmarshalWithParams(b, val, "")
|
| 831 |
|
|
}
|
| 832 |
|
|
|
| 833 |
|
|
// UnmarshalWithParams allows field parameters to be specified for the
|
| 834 |
|
|
// top-level element. The form of the params is the same as the field tags.
|
| 835 |
|
|
func UnmarshalWithParams(b []byte, val interface{}, params string) (rest []byte, err error) {
|
| 836 |
|
|
v := reflect.ValueOf(val).Elem()
|
| 837 |
|
|
offset, err := parseField(v, b, 0, parseFieldParameters(params))
|
| 838 |
|
|
if err != nil {
|
| 839 |
|
|
return nil, err
|
| 840 |
|
|
}
|
| 841 |
|
|
return b[offset:], nil
|
| 842 |
|
|
}
|