| 1 |
// Copyright 2017 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 cryptobyte |
| 6 |
|
| 7 |
import ( |
| 8 |
encoding_asn1 "encoding/asn1" |
| 9 |
"fmt" |
| 10 |
"math/big" |
| 11 |
"reflect" |
| 12 |
"time" |
| 13 |
|
| 14 |
"golang.org/x/crypto/cryptobyte/asn1" |
| 15 |
) |
| 16 |
|
| 17 |
// This file contains ASN.1-related methods for String and Builder. |
| 18 |
|
| 19 |
// Builder |
| 20 |
|
| 21 |
// AddASN1Int64 appends a DER-encoded ASN.1 INTEGER. |
| 22 |
func (b *Builder) AddASN1Int64(v int64) { |
| 23 |
b.addASN1Signed(asn1.INTEGER, v) |
| 24 |
} |
| 25 |
|
| 26 |
// AddASN1Int64WithTag appends a DER-encoded ASN.1 INTEGER with the |
| 27 |
// given tag. |
| 28 |
func (b *Builder) AddASN1Int64WithTag(v int64, tag asn1.Tag) { |
| 29 |
b.addASN1Signed(tag, v) |
| 30 |
} |
| 31 |
|
| 32 |
// AddASN1Enum appends a DER-encoded ASN.1 ENUMERATION. |
| 33 |
func (b *Builder) AddASN1Enum(v int64) { |
| 34 |
b.addASN1Signed(asn1.ENUM, v) |
| 35 |
} |
| 36 |
|
| 37 |
func (b *Builder) addASN1Signed(tag asn1.Tag, v int64) { |
| 38 |
b.AddASN1(tag, func(c *Builder) { |
| 39 |
length := 1 |
| 40 |
for i := v; i >= 0x80 || i < -0x80; i >>= 8 { |
| 41 |
length++ |
| 42 |
} |
| 43 |
|
| 44 |
for ; length > 0; length-- { |
| 45 |
i := v >> uint((length-1)*8) & 0xff |
| 46 |
c.AddUint8(uint8(i)) |
| 47 |
} |
| 48 |
}) |
| 49 |
} |
| 50 |
|
| 51 |
// AddASN1Uint64 appends a DER-encoded ASN.1 INTEGER. |
| 52 |
func (b *Builder) AddASN1Uint64(v uint64) { |
| 53 |
b.AddASN1(asn1.INTEGER, func(c *Builder) { |
| 54 |
length := 1 |
| 55 |
for i := v; i >= 0x80; i >>= 8 { |
| 56 |
length++ |
| 57 |
} |
| 58 |
|
| 59 |
for ; length > 0; length-- { |
| 60 |
i := v >> uint((length-1)*8) & 0xff |
| 61 |
c.AddUint8(uint8(i)) |
| 62 |
} |
| 63 |
}) |
| 64 |
} |
| 65 |
|
| 66 |
// AddASN1BigInt appends a DER-encoded ASN.1 INTEGER. |
| 67 |
func (b *Builder) AddASN1BigInt(n *big.Int) { |
| 68 |
if b.err != nil { |
| 69 |
return |
| 70 |
} |
| 71 |
|
| 72 |
b.AddASN1(asn1.INTEGER, func(c *Builder) { |
| 73 |
if n.Sign() < 0 { |
| 74 |
// A negative number has to be converted to two's-complement form. So we |
| 75 |
// invert and subtract 1. If the most-significant-bit isn't set then |
| 76 |
// we'll need to pad the beginning with 0xff in order to keep the number |
| 77 |
// negative. |
| 78 |
nMinus1 := new(big.Int).Neg(n) |
| 79 |
nMinus1.Sub(nMinus1, bigOne) |
| 80 |
bytes := nMinus1.Bytes() |
| 81 |
for i := range bytes { |
| 82 |
bytes[i] ^= 0xff |
| 83 |
} |
| 84 |
if len(bytes) == 0 || bytes[0]&0x80 == 0 { |
| 85 |
c.add(0xff) |
| 86 |
} |
| 87 |
c.add(bytes...) |
| 88 |
} else if n.Sign() == 0 { |
| 89 |
c.add(0) |
| 90 |
} else { |
| 91 |
bytes := n.Bytes() |
| 92 |
if bytes[0]&0x80 != 0 { |
| 93 |
c.add(0) |
| 94 |
} |
| 95 |
c.add(bytes...) |
| 96 |
} |
| 97 |
}) |
| 98 |
} |
| 99 |
|
| 100 |
// AddASN1OctetString appends a DER-encoded ASN.1 OCTET STRING. |
| 101 |
func (b *Builder) AddASN1OctetString(bytes []byte) { |
| 102 |
b.AddASN1(asn1.OCTET_STRING, func(c *Builder) { |
| 103 |
c.AddBytes(bytes) |
| 104 |
}) |
| 105 |
} |
| 106 |
|
| 107 |
const generalizedTimeFormatStr = "20060102150405Z0700" |
| 108 |
|
| 109 |
// AddASN1GeneralizedTime appends a DER-encoded ASN.1 GENERALIZEDTIME. |
| 110 |
func (b *Builder) AddASN1GeneralizedTime(t time.Time) { |
| 111 |
if t.Year() < 0 || t.Year() > 9999 { |
| 112 |
b.err = fmt.Errorf("cryptobyte: cannot represent %v as a GeneralizedTime", t) |
| 113 |
return |
| 114 |
} |
| 115 |
b.AddASN1(asn1.GeneralizedTime, func(c *Builder) { |
| 116 |
c.AddBytes([]byte(t.Format(generalizedTimeFormatStr))) |
| 117 |
}) |
| 118 |
} |
| 119 |
|
| 120 |
// AddASN1UTCTime appends a DER-encoded ASN.1 UTCTime. |
| 121 |
func (b *Builder) AddASN1UTCTime(t time.Time) { |
| 122 |
b.AddASN1(asn1.UTCTime, func(c *Builder) { |
| 123 |
// As utilized by the X.509 profile, UTCTime can only |
| 124 |
// represent the years 1950 through 2049. |
| 125 |
if t.Year() < 1950 || t.Year() >= 2050 { |
| 126 |
b.err = fmt.Errorf("cryptobyte: cannot represent %v as a UTCTime", t) |
| 127 |
return |
| 128 |
} |
| 129 |
c.AddBytes([]byte(t.Format(defaultUTCTimeFormatStr))) |
| 130 |
}) |
| 131 |
} |
| 132 |
|
| 133 |
// AddASN1BitString appends a DER-encoded ASN.1 BIT STRING. This does not |
| 134 |
// support BIT STRINGs that are not a whole number of bytes. |
| 135 |
func (b *Builder) AddASN1BitString(data []byte) { |
| 136 |
b.AddASN1(asn1.BIT_STRING, func(b *Builder) { |
| 137 |
b.AddUint8(0) |
| 138 |
b.AddBytes(data) |
| 139 |
}) |
| 140 |
} |
| 141 |
|
| 142 |
func (b *Builder) addBase128Int(n int64) { |
| 143 |
var length int |
| 144 |
if n == 0 { |
| 145 |
length = 1 |
| 146 |
} else { |
| 147 |
for i := n; i > 0; i >>= 7 { |
| 148 |
length++ |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
for i := length - 1; i >= 0; i-- { |
| 153 |
o := byte(n >> uint(i*7)) |
| 154 |
o &= 0x7f |
| 155 |
if i != 0 { |
| 156 |
o |= 0x80 |
| 157 |
} |
| 158 |
|
| 159 |
b.add(o) |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
func isValidOID(oid encoding_asn1.ObjectIdentifier) bool { |
| 164 |
if len(oid) < 2 { |
| 165 |
return false |
| 166 |
} |
| 167 |
|
| 168 |
if oid[0] > 2 || (oid[0] <= 1 && oid[1] >= 40) { |
| 169 |
return false |
| 170 |
} |
| 171 |
|
| 172 |
for _, v := range oid { |
| 173 |
if v < 0 { |
| 174 |
return false |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
return true |
| 179 |
} |
| 180 |
|
| 181 |
func (b *Builder) AddASN1ObjectIdentifier(oid encoding_asn1.ObjectIdentifier) { |
| 182 |
b.AddASN1(asn1.OBJECT_IDENTIFIER, func(b *Builder) { |
| 183 |
if !isValidOID(oid) { |
| 184 |
b.err = fmt.Errorf("cryptobyte: invalid OID: %v", oid) |
| 185 |
return |
| 186 |
} |
| 187 |
|
| 188 |
b.addBase128Int(int64(oid[0])*40 + int64(oid[1])) |
| 189 |
for _, v := range oid[2:] { |
| 190 |
b.addBase128Int(int64(v)) |
| 191 |
} |
| 192 |
}) |
| 193 |
} |
| 194 |
|
| 195 |
func (b *Builder) AddASN1Boolean(v bool) { |
| 196 |
b.AddASN1(asn1.BOOLEAN, func(b *Builder) { |
| 197 |
if v { |
| 198 |
b.AddUint8(0xff) |
| 199 |
} else { |
| 200 |
b.AddUint8(0) |
| 201 |
} |
| 202 |
}) |
| 203 |
} |
| 204 |
|
| 205 |
func (b *Builder) AddASN1NULL() { |
| 206 |
b.add(uint8(asn1.NULL), 0) |
| 207 |
} |
| 208 |
|
| 209 |
// MarshalASN1 calls encoding_asn1.Marshal on its input and appends the result if |
| 210 |
// successful or records an error if one occurred. |
| 211 |
func (b *Builder) MarshalASN1(v interface{}) { |
| 212 |
// NOTE(martinkr): This is somewhat of a hack to allow propagation of |
| 213 |
// encoding_asn1.Marshal errors into Builder.err. N.B. if you call MarshalASN1 with a |
| 214 |
// value embedded into a struct, its tag information is lost. |
| 215 |
if b.err != nil { |
| 216 |
return |
| 217 |
} |
| 218 |
bytes, err := encoding_asn1.Marshal(v) |
| 219 |
if err != nil { |
| 220 |
b.err = err |
| 221 |
return |
| 222 |
} |
| 223 |
b.AddBytes(bytes) |
| 224 |
} |
| 225 |
|
| 226 |
// AddASN1 appends an ASN.1 object. The object is prefixed with the given tag. |
| 227 |
// Tags greater than 30 are not supported and result in an error (i.e. |
| 228 |
// low-tag-number form only). The child builder passed to the |
| 229 |
// BuilderContinuation can be used to build the content of the ASN.1 object. |
| 230 |
func (b *Builder) AddASN1(tag asn1.Tag, f BuilderContinuation) { |
| 231 |
if b.err != nil { |
| 232 |
return |
| 233 |
} |
| 234 |
// Identifiers with the low five bits set indicate high-tag-number format |
| 235 |
// (two or more octets), which we don't support. |
| 236 |
if tag&0x1f == 0x1f { |
| 237 |
b.err = fmt.Errorf("cryptobyte: high-tag number identifier octects not supported: 0x%x", tag) |
| 238 |
return |
| 239 |
} |
| 240 |
b.AddUint8(uint8(tag)) |
| 241 |
b.addLengthPrefixed(1, true, f) |
| 242 |
} |
| 243 |
|
| 244 |
// String |
| 245 |
|
| 246 |
// ReadASN1Boolean decodes an ASN.1 BOOLEAN and converts it to a boolean |
| 247 |
// representation into out and advances. It reports whether the read |
| 248 |
// was successful. |
| 249 |
func (s *String) ReadASN1Boolean(out *bool) bool { |
| 250 |
var bytes String |
| 251 |
if !s.ReadASN1(&bytes, asn1.BOOLEAN) || len(bytes) != 1 { |
| 252 |
return false |
| 253 |
} |
| 254 |
|
| 255 |
switch bytes[0] { |
| 256 |
case 0: |
| 257 |
*out = false |
| 258 |
case 0xff: |
| 259 |
*out = true |
| 260 |
default: |
| 261 |
return false |
| 262 |
} |
| 263 |
|
| 264 |
return true |
| 265 |
} |
| 266 |
|
| 267 |
var bigIntType = reflect.TypeOf((*big.Int)(nil)).Elem() |
| 268 |
|
| 269 |
// ReadASN1Integer decodes an ASN.1 INTEGER into out and advances. If out does |
| 270 |
// not point to an integer or to a big.Int, it panics. It reports whether the |
| 271 |
// read was successful. |
| 272 |
func (s *String) ReadASN1Integer(out interface{}) bool { |
| 273 |
if reflect.TypeOf(out).Kind() != reflect.Ptr { |
| 274 |
panic("out is not a pointer") |
| 275 |
} |
| 276 |
switch reflect.ValueOf(out).Elem().Kind() { |
| 277 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 278 |
var i int64 |
| 279 |
if !s.readASN1Int64(&i) || reflect.ValueOf(out).Elem().OverflowInt(i) { |
| 280 |
return false |
| 281 |
} |
| 282 |
reflect.ValueOf(out).Elem().SetInt(i) |
| 283 |
return true |
| 284 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 285 |
var u uint64 |
| 286 |
if !s.readASN1Uint64(&u) || reflect.ValueOf(out).Elem().OverflowUint(u) { |
| 287 |
return false |
| 288 |
} |
| 289 |
reflect.ValueOf(out).Elem().SetUint(u) |
| 290 |
return true |
| 291 |
case reflect.Struct: |
| 292 |
if reflect.TypeOf(out).Elem() == bigIntType { |
| 293 |
return s.readASN1BigInt(out.(*big.Int)) |
| 294 |
} |
| 295 |
} |
| 296 |
panic("out does not point to an integer type") |
| 297 |
} |
| 298 |
|
| 299 |
func checkASN1Integer(bytes []byte) bool { |
| 300 |
if len(bytes) == 0 { |
| 301 |
// An INTEGER is encoded with at least one octet. |
| 302 |
return false |
| 303 |
} |
| 304 |
if len(bytes) == 1 { |
| 305 |
return true |
| 306 |
} |
| 307 |
if bytes[0] == 0 && bytes[1]&0x80 == 0 || bytes[0] == 0xff && bytes[1]&0x80 == 0x80 { |
| 308 |
// Value is not minimally encoded. |
| 309 |
return false |
| 310 |
} |
| 311 |
return true |
| 312 |
} |
| 313 |
|
| 314 |
var bigOne = big.NewInt(1) |
| 315 |
|
| 316 |
func (s *String) readASN1BigInt(out *big.Int) bool { |
| 317 |
var bytes String |
| 318 |
if !s.ReadASN1(&bytes, asn1.INTEGER) || !checkASN1Integer(bytes) { |
| 319 |
return false |
| 320 |
} |
| 321 |
if bytes[0]&0x80 == 0x80 { |
| 322 |
// Negative number. |
| 323 |
neg := make([]byte, len(bytes)) |
| 324 |
for i, b := range bytes { |
| 325 |
neg[i] = ^b |
| 326 |
} |
| 327 |
out.SetBytes(neg) |
| 328 |
out.Add(out, bigOne) |
| 329 |
out.Neg(out) |
| 330 |
} else { |
| 331 |
out.SetBytes(bytes) |
| 332 |
} |
| 333 |
return true |
| 334 |
} |
| 335 |
|
| 336 |
func (s *String) readASN1Int64(out *int64) bool { |
| 337 |
var bytes String |
| 338 |
if !s.ReadASN1(&bytes, asn1.INTEGER) || !checkASN1Integer(bytes) || !asn1Signed(out, bytes) { |
| 339 |
return false |
| 340 |
} |
| 341 |
return true |
| 342 |
} |
| 343 |
|
| 344 |
func asn1Signed(out *int64, n []byte) bool { |
| 345 |
length := len(n) |
| 346 |
if length > 8 { |
| 347 |
return false |
| 348 |
} |
| 349 |
for i := 0; i < length; i++ { |
| 350 |
*out <<= 8 |
| 351 |
*out |= int64(n[i]) |
| 352 |
} |
| 353 |
// Shift up and down in order to sign extend the result. |
| 354 |
*out <<= 64 - uint8(length)*8 |
| 355 |
*out >>= 64 - uint8(length)*8 |
| 356 |
return true |
| 357 |
} |
| 358 |
|
| 359 |
func (s *String) readASN1Uint64(out *uint64) bool { |
| 360 |
var bytes String |
| 361 |
if !s.ReadASN1(&bytes, asn1.INTEGER) || !checkASN1Integer(bytes) || !asn1Unsigned(out, bytes) { |
| 362 |
return false |
| 363 |
} |
| 364 |
return true |
| 365 |
} |
| 366 |
|
| 367 |
func asn1Unsigned(out *uint64, n []byte) bool { |
| 368 |
length := len(n) |
| 369 |
if length > 9 || length == 9 && n[0] != 0 { |
| 370 |
// Too large for uint64. |
| 371 |
return false |
| 372 |
} |
| 373 |
if n[0]&0x80 != 0 { |
| 374 |
// Negative number. |
| 375 |
return false |
| 376 |
} |
| 377 |
for i := 0; i < length; i++ { |
| 378 |
*out <<= 8 |
| 379 |
*out |= uint64(n[i]) |
| 380 |
} |
| 381 |
return true |
| 382 |
} |
| 383 |
|
| 384 |
// ReadASN1Int64WithTag decodes an ASN.1 INTEGER with the given tag into out |
| 385 |
// and advances. It reports whether the read was successful and resulted in a |
| 386 |
// value that can be represented in an int64. |
| 387 |
func (s *String) ReadASN1Int64WithTag(out *int64, tag asn1.Tag) bool { |
| 388 |
var bytes String |
| 389 |
return s.ReadASN1(&bytes, tag) && checkASN1Integer(bytes) && asn1Signed(out, bytes) |
| 390 |
} |
| 391 |
|
| 392 |
// ReadASN1Enum decodes an ASN.1 ENUMERATION into out and advances. It reports |
| 393 |
// whether the read was successful. |
| 394 |
func (s *String) ReadASN1Enum(out *int) bool { |
| 395 |
var bytes String |
| 396 |
var i int64 |
| 397 |
if !s.ReadASN1(&bytes, asn1.ENUM) || !checkASN1Integer(bytes) || !asn1Signed(&i, bytes) { |
| 398 |
return false |
| 399 |
} |
| 400 |
if int64(int(i)) != i { |
| 401 |
return false |
| 402 |
} |
| 403 |
*out = int(i) |
| 404 |
return true |
| 405 |
} |
| 406 |
|
| 407 |
func (s *String) readBase128Int(out *int) bool { |
| 408 |
ret := 0 |
| 409 |
for i := 0; len(*s) > 0; i++ { |
| 410 |
if i == 5 { |
| 411 |
return false |
| 412 |
} |
| 413 |
// Avoid overflowing int on a 32-bit platform. |
| 414 |
// We don't want different behavior based on the architecture. |
| 415 |
if ret >= 1<<(31-7) { |
| 416 |
return false |
| 417 |
} |
| 418 |
ret <<= 7 |
| 419 |
b := s.read(1)[0] |
| 420 |
ret |= int(b & 0x7f) |
| 421 |
if b&0x80 == 0 { |
| 422 |
*out = ret |
| 423 |
return true |
| 424 |
} |
| 425 |
} |
| 426 |
return false // truncated |
| 427 |
} |
| 428 |
|
| 429 |
// ReadASN1ObjectIdentifier decodes an ASN.1 OBJECT IDENTIFIER into out and |
| 430 |
// advances. It reports whether the read was successful. |
| 431 |
func (s *String) ReadASN1ObjectIdentifier(out *encoding_asn1.ObjectIdentifier) bool { |
| 432 |
var bytes String |
| 433 |
if !s.ReadASN1(&bytes, asn1.OBJECT_IDENTIFIER) || len(bytes) == 0 { |
| 434 |
return false |
| 435 |
} |
| 436 |
|
| 437 |
// In the worst case, we get two elements from the first byte (which is |
| 438 |
// encoded differently) and then every varint is a single byte long. |
| 439 |
components := make([]int, len(bytes)+1) |
| 440 |
|
| 441 |
// The first varint is 40*value1 + value2: |
| 442 |
// According to this packing, value1 can take the values 0, 1 and 2 only. |
| 443 |
// When value1 = 0 or value1 = 1, then value2 is <= 39. When value1 = 2, |
| 444 |
// then there are no restrictions on value2. |
| 445 |
var v int |
| 446 |
if !bytes.readBase128Int(&v) { |
| 447 |
return false |
| 448 |
} |
| 449 |
if v < 80 { |
| 450 |
components[0] = v / 40 |
| 451 |
components[1] = v % 40 |
| 452 |
} else { |
| 453 |
components[0] = 2 |
| 454 |
components[1] = v - 80 |
| 455 |
} |
| 456 |
|
| 457 |
i := 2 |
| 458 |
for ; len(bytes) > 0; i++ { |
| 459 |
if !bytes.readBase128Int(&v) { |
| 460 |
return false |
| 461 |
} |
| 462 |
components[i] = v |
| 463 |
} |
| 464 |
*out = components[:i] |
| 465 |
return true |
| 466 |
} |
| 467 |
|
| 468 |
// ReadASN1GeneralizedTime decodes an ASN.1 GENERALIZEDTIME into out and |
| 469 |
// advances. It reports whether the read was successful. |
| 470 |
func (s *String) ReadASN1GeneralizedTime(out *time.Time) bool { |
| 471 |
var bytes String |
| 472 |
if !s.ReadASN1(&bytes, asn1.GeneralizedTime) { |
| 473 |
return false |
| 474 |
} |
| 475 |
t := string(bytes) |
| 476 |
res, err := time.Parse(generalizedTimeFormatStr, t) |
| 477 |
if err != nil { |
| 478 |
return false |
| 479 |
} |
| 480 |
if serialized := res.Format(generalizedTimeFormatStr); serialized != t { |
| 481 |
return false |
| 482 |
} |
| 483 |
*out = res |
| 484 |
return true |
| 485 |
} |
| 486 |
|
| 487 |
const defaultUTCTimeFormatStr = "060102150405Z0700" |
| 488 |
|
| 489 |
// ReadASN1UTCTime decodes an ASN.1 UTCTime into out and advances. |
| 490 |
// It reports whether the read was successful. |
| 491 |
func (s *String) ReadASN1UTCTime(out *time.Time) bool { |
| 492 |
var bytes String |
| 493 |
if !s.ReadASN1(&bytes, asn1.UTCTime) { |
| 494 |
return false |
| 495 |
} |
| 496 |
t := string(bytes) |
| 497 |
|
| 498 |
formatStr := defaultUTCTimeFormatStr |
| 499 |
var err error |
| 500 |
res, err := time.Parse(formatStr, t) |
| 501 |
if err != nil { |
| 502 |
// Fallback to minute precision if we can't parse second |
| 503 |
// precision. If we are following X.509 or X.690 we shouldn't |
| 504 |
// support this, but we do. |
| 505 |
formatStr = "0601021504Z0700" |
| 506 |
res, err = time.Parse(formatStr, t) |
| 507 |
} |
| 508 |
if err != nil { |
| 509 |
return false |
| 510 |
} |
| 511 |
|
| 512 |
if serialized := res.Format(formatStr); serialized != t { |
| 513 |
return false |
| 514 |
} |
| 515 |
|
| 516 |
if res.Year() >= 2050 { |
| 517 |
// UTCTime interprets the low order digits 50-99 as 1950-99. |
| 518 |
// This only applies to its use in the X.509 profile. |
| 519 |
// See https://tools.ietf.org/html/rfc5280#section-4.1.2.5.1 |
| 520 |
res = res.AddDate(-100, 0, 0) |
| 521 |
} |
| 522 |
*out = res |
| 523 |
return true |
| 524 |
} |
| 525 |
|
| 526 |
// ReadASN1BitString decodes an ASN.1 BIT STRING into out and advances. |
| 527 |
// It reports whether the read was successful. |
| 528 |
func (s *String) ReadASN1BitString(out *encoding_asn1.BitString) bool { |
| 529 |
var bytes String |
| 530 |
if !s.ReadASN1(&bytes, asn1.BIT_STRING) || len(bytes) == 0 || |
| 531 |
len(bytes)*8/8 != len(bytes) { |
| 532 |
return false |
| 533 |
} |
| 534 |
|
| 535 |
paddingBits := uint8(bytes[0]) |
| 536 |
bytes = bytes[1:] |
| 537 |
if paddingBits > 7 || |
| 538 |
len(bytes) == 0 && paddingBits != 0 || |
| 539 |
len(bytes) > 0 && bytes[len(bytes)-1]&(1<<paddingBits-1) != 0 { |
| 540 |
return false |
| 541 |
} |
| 542 |
|
| 543 |
out.BitLength = len(bytes)*8 - int(paddingBits) |
| 544 |
out.Bytes = bytes |
| 545 |
return true |
| 546 |
} |
| 547 |
|
| 548 |
// ReadASN1BitString decodes an ASN.1 BIT STRING into out and advances. It is |
| 549 |
// an error if the BIT STRING is not a whole number of bytes. It reports |
| 550 |
// whether the read was successful. |
| 551 |
func (s *String) ReadASN1BitStringAsBytes(out *[]byte) bool { |
| 552 |
var bytes String |
| 553 |
if !s.ReadASN1(&bytes, asn1.BIT_STRING) || len(bytes) == 0 { |
| 554 |
return false |
| 555 |
} |
| 556 |
|
| 557 |
paddingBits := uint8(bytes[0]) |
| 558 |
if paddingBits != 0 { |
| 559 |
return false |
| 560 |
} |
| 561 |
*out = bytes[1:] |
| 562 |
return true |
| 563 |
} |
| 564 |
|
| 565 |
// ReadASN1Bytes reads the contents of a DER-encoded ASN.1 element (not including |
| 566 |
// tag and length bytes) into out, and advances. The element must match the |
| 567 |
// given tag. It reports whether the read was successful. |
| 568 |
func (s *String) ReadASN1Bytes(out *[]byte, tag asn1.Tag) bool { |
| 569 |
return s.ReadASN1((*String)(out), tag) |
| 570 |
} |
| 571 |
|
| 572 |
// ReadASN1 reads the contents of a DER-encoded ASN.1 element (not including |
| 573 |
// tag and length bytes) into out, and advances. The element must match the |
| 574 |
// given tag. It reports whether the read was successful. |
| 575 |
// |
| 576 |
// Tags greater than 30 are not supported (i.e. low-tag-number format only). |
| 577 |
func (s *String) ReadASN1(out *String, tag asn1.Tag) bool { |
| 578 |
var t asn1.Tag |
| 579 |
if !s.ReadAnyASN1(out, &t) || t != tag { |
| 580 |
return false |
| 581 |
} |
| 582 |
return true |
| 583 |
} |
| 584 |
|
| 585 |
// ReadASN1Element reads the contents of a DER-encoded ASN.1 element (including |
| 586 |
// tag and length bytes) into out, and advances. The element must match the |
| 587 |
// given tag. It reports whether the read was successful. |
| 588 |
// |
| 589 |
// Tags greater than 30 are not supported (i.e. low-tag-number format only). |
| 590 |
func (s *String) ReadASN1Element(out *String, tag asn1.Tag) bool { |
| 591 |
var t asn1.Tag |
| 592 |
if !s.ReadAnyASN1Element(out, &t) || t != tag { |
| 593 |
return false |
| 594 |
} |
| 595 |
return true |
| 596 |
} |
| 597 |
|
| 598 |
// ReadAnyASN1 reads the contents of a DER-encoded ASN.1 element (not including |
| 599 |
// tag and length bytes) into out, sets outTag to its tag, and advances. |
| 600 |
// It reports whether the read was successful. |
| 601 |
// |
| 602 |
// Tags greater than 30 are not supported (i.e. low-tag-number format only). |
| 603 |
func (s *String) ReadAnyASN1(out *String, outTag *asn1.Tag) bool { |
| 604 |
return s.readASN1(out, outTag, true /* skip header */) |
| 605 |
} |
| 606 |
|
| 607 |
// ReadAnyASN1Element reads the contents of a DER-encoded ASN.1 element |
| 608 |
// (including tag and length bytes) into out, sets outTag to is tag, and |
| 609 |
// advances. It reports whether the read was successful. |
| 610 |
// |
| 611 |
// Tags greater than 30 are not supported (i.e. low-tag-number format only). |
| 612 |
func (s *String) ReadAnyASN1Element(out *String, outTag *asn1.Tag) bool { |
| 613 |
return s.readASN1(out, outTag, false /* include header */) |
| 614 |
} |
| 615 |
|
| 616 |
// PeekASN1Tag reports whether the next ASN.1 value on the string starts with |
| 617 |
// the given tag. |
| 618 |
func (s String) PeekASN1Tag(tag asn1.Tag) bool { |
| 619 |
if len(s) == 0 { |
| 620 |
return false |
| 621 |
} |
| 622 |
return asn1.Tag(s[0]) == tag |
| 623 |
} |
| 624 |
|
| 625 |
// SkipASN1 reads and discards an ASN.1 element with the given tag. It |
| 626 |
// reports whether the operation was successful. |
| 627 |
func (s *String) SkipASN1(tag asn1.Tag) bool { |
| 628 |
var unused String |
| 629 |
return s.ReadASN1(&unused, tag) |
| 630 |
} |
| 631 |
|
| 632 |
// ReadOptionalASN1 attempts to read the contents of a DER-encoded ASN.1 |
| 633 |
// element (not including tag and length bytes) tagged with the given tag into |
| 634 |
// out. It stores whether an element with the tag was found in outPresent, |
| 635 |
// unless outPresent is nil. It reports whether the read was successful. |
| 636 |
func (s *String) ReadOptionalASN1(out *String, outPresent *bool, tag asn1.Tag) bool { |
| 637 |
present := s.PeekASN1Tag(tag) |
| 638 |
if outPresent != nil { |
| 639 |
*outPresent = present |
| 640 |
} |
| 641 |
if present && !s.ReadASN1(out, tag) { |
| 642 |
return false |
| 643 |
} |
| 644 |
return true |
| 645 |
} |
| 646 |
|
| 647 |
// SkipOptionalASN1 advances s over an ASN.1 element with the given tag, or |
| 648 |
// else leaves s unchanged. It reports whether the operation was successful. |
| 649 |
func (s *String) SkipOptionalASN1(tag asn1.Tag) bool { |
| 650 |
if !s.PeekASN1Tag(tag) { |
| 651 |
return true |
| 652 |
} |
| 653 |
var unused String |
| 654 |
return s.ReadASN1(&unused, tag) |
| 655 |
} |
| 656 |
|
| 657 |
// ReadOptionalASN1Integer attempts to read an optional ASN.1 INTEGER |
| 658 |
// explicitly tagged with tag into out and advances. If no element with a |
| 659 |
// matching tag is present, it writes defaultValue into out instead. If out |
| 660 |
// does not point to an integer or to a big.Int, it panics. It reports |
| 661 |
// whether the read was successful. |
| 662 |
func (s *String) ReadOptionalASN1Integer(out interface{}, tag asn1.Tag, defaultValue interface{}) bool { |
| 663 |
if reflect.TypeOf(out).Kind() != reflect.Ptr { |
| 664 |
panic("out is not a pointer") |
| 665 |
} |
| 666 |
var present bool |
| 667 |
var i String |
| 668 |
if !s.ReadOptionalASN1(&i, &present, tag) { |
| 669 |
return false |
| 670 |
} |
| 671 |
if !present { |
| 672 |
switch reflect.ValueOf(out).Elem().Kind() { |
| 673 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, |
| 674 |
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 675 |
reflect.ValueOf(out).Elem().Set(reflect.ValueOf(defaultValue)) |
| 676 |
case reflect.Struct: |
| 677 |
if reflect.TypeOf(out).Elem() != bigIntType { |
| 678 |
panic("invalid integer type") |
| 679 |
} |
| 680 |
if reflect.TypeOf(defaultValue).Kind() != reflect.Ptr || |
| 681 |
reflect.TypeOf(defaultValue).Elem() != bigIntType { |
| 682 |
panic("out points to big.Int, but defaultValue does not") |
| 683 |
} |
| 684 |
out.(*big.Int).Set(defaultValue.(*big.Int)) |
| 685 |
default: |
| 686 |
panic("invalid integer type") |
| 687 |
} |
| 688 |
return true |
| 689 |
} |
| 690 |
if !i.ReadASN1Integer(out) || !i.Empty() { |
| 691 |
return false |
| 692 |
} |
| 693 |
return true |
| 694 |
} |
| 695 |
|
| 696 |
// ReadOptionalASN1OctetString attempts to read an optional ASN.1 OCTET STRING |
| 697 |
// explicitly tagged with tag into out and advances. If no element with a |
| 698 |
// matching tag is present, it sets "out" to nil instead. It reports |
| 699 |
// whether the read was successful. |
| 700 |
func (s *String) ReadOptionalASN1OctetString(out *[]byte, outPresent *bool, tag asn1.Tag) bool { |
| 701 |
var present bool |
| 702 |
var child String |
| 703 |
if !s.ReadOptionalASN1(&child, &present, tag) { |
| 704 |
return false |
| 705 |
} |
| 706 |
if outPresent != nil { |
| 707 |
*outPresent = present |
| 708 |
} |
| 709 |
if present { |
| 710 |
var oct String |
| 711 |
if !child.ReadASN1(&oct, asn1.OCTET_STRING) || !child.Empty() { |
| 712 |
return false |
| 713 |
} |
| 714 |
*out = oct |
| 715 |
} else { |
| 716 |
*out = nil |
| 717 |
} |
| 718 |
return true |
| 719 |
} |
| 720 |
|
| 721 |
// ReadOptionalASN1Boolean sets *out to the value of the next ASN.1 BOOLEAN or, |
| 722 |
// if the next bytes are not an ASN.1 BOOLEAN, to the value of defaultValue. |
| 723 |
// It reports whether the operation was successful. |
| 724 |
func (s *String) ReadOptionalASN1Boolean(out *bool, defaultValue bool) bool { |
| 725 |
var present bool |
| 726 |
var child String |
| 727 |
if !s.ReadOptionalASN1(&child, &present, asn1.BOOLEAN) { |
| 728 |
return false |
| 729 |
} |
| 730 |
|
| 731 |
if !present { |
| 732 |
*out = defaultValue |
| 733 |
return true |
| 734 |
} |
| 735 |
|
| 736 |
return s.ReadASN1Boolean(out) |
| 737 |
} |
| 738 |
|
| 739 |
func (s *String) readASN1(out *String, outTag *asn1.Tag, skipHeader bool) bool { |
| 740 |
if len(*s) < 2 { |
| 741 |
return false |
| 742 |
} |
| 743 |
tag, lenByte := (*s)[0], (*s)[1] |
| 744 |
|
| 745 |
if tag&0x1f == 0x1f { |
| 746 |
// ITU-T X.690 section 8.1.2 |
| 747 |
// |
| 748 |
// An identifier octet with a tag part of 0x1f indicates a high-tag-number |
| 749 |
// form identifier with two or more octets. We only support tags less than |
| 750 |
// 31 (i.e. low-tag-number form, single octet identifier). |
| 751 |
return false |
| 752 |
} |
| 753 |
|
| 754 |
if outTag != nil { |
| 755 |
*outTag = asn1.Tag(tag) |
| 756 |
} |
| 757 |
|
| 758 |
// ITU-T X.690 section 8.1.3 |
| 759 |
// |
| 760 |
// Bit 8 of the first length byte indicates whether the length is short- or |
| 761 |
// long-form. |
| 762 |
var length, headerLen uint32 // length includes headerLen |
| 763 |
if lenByte&0x80 == 0 { |
| 764 |
// Short-form length (section 8.1.3.4), encoded in bits 1-7. |
| 765 |
length = uint32(lenByte) + 2 |
| 766 |
headerLen = 2 |
| 767 |
} else { |
| 768 |
// Long-form length (section 8.1.3.5). Bits 1-7 encode the number of octets |
| 769 |
// used to encode the length. |
| 770 |
lenLen := lenByte & 0x7f |
| 771 |
var len32 uint32 |
| 772 |
|
| 773 |
if lenLen == 0 || lenLen > 4 || len(*s) < int(2+lenLen) { |
| 774 |
return false |
| 775 |
} |
| 776 |
|
| 777 |
lenBytes := String((*s)[2 : 2+lenLen]) |
| 778 |
if !lenBytes.readUnsigned(&len32, int(lenLen)) { |
| 779 |
return false |
| 780 |
} |
| 781 |
|
| 782 |
// ITU-T X.690 section 10.1 (DER length forms) requires encoding the length |
| 783 |
// with the minimum number of octets. |
| 784 |
if len32 < 128 { |
| 785 |
// Length should have used short-form encoding. |
| 786 |
return false |
| 787 |
} |
| 788 |
if len32>>((lenLen-1)*8) == 0 { |
| 789 |
// Leading octet is 0. Length should have been at least one byte shorter. |
| 790 |
return false |
| 791 |
} |
| 792 |
|
| 793 |
headerLen = 2 + uint32(lenLen) |
| 794 |
if headerLen+len32 < len32 { |
| 795 |
// Overflow. |
| 796 |
return false |
| 797 |
} |
| 798 |
length = headerLen + len32 |
| 799 |
} |
| 800 |
|
| 801 |
if int(length) < 0 || !s.ReadBytes((*[]byte)(out), int(length)) { |
| 802 |
return false |
| 803 |
} |
| 804 |
if skipHeader && !out.Skip(int(headerLen)) { |
| 805 |
panic("cryptobyte: internal error") |
| 806 |
} |
| 807 |
|
| 808 |
return true |
| 809 |
} |