You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

752 lines
20 KiB

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