asn1.go 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195
  1. // 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. // Package asn1 implements parsing of DER-encoded ASN.1 data structures,
  5. // as defined in ITU-T Rec X.690.
  6. //
  7. // See also “A Layman's Guide to a Subset of ASN.1, BER, and DER,”
  8. // http://luca.ntop.org/Teaching/Appunti/asn1.html.
  9. //
  10. // This is a fork of the Go standard library ASN.1 implementation
  11. // (encoding/asn1), with the aim of relaxing checks for various things
  12. // that are common errors present in many X.509 certificates in the
  13. // wild.
  14. //
  15. // Main differences:
  16. // - Extra "lax" tag that recursively applies and relaxes some strict
  17. // checks:
  18. // - parsePrintableString() copes with invalid PrintableString contents,
  19. // e.g. use of tagPrintableString when the string data is really
  20. // ISO8859-1.
  21. // - checkInteger() allows integers that are not minimally encoded (and
  22. // so are not correct DER).
  23. // - parseObjectIdentifier() allows zero-length OIDs.
  24. // - Better diagnostics on which particular field causes errors.
  25. package asn1
  26. // ASN.1 is a syntax for specifying abstract objects and BER, DER, PER, XER etc
  27. // are different encoding formats for those objects. Here, we'll be dealing
  28. // with DER, the Distinguished Encoding Rules. DER is used in X.509 because
  29. // it's fast to parse and, unlike BER, has a unique encoding for every object.
  30. // When calculating hashes over objects, it's important that the resulting
  31. // bytes be the same at both ends and DER removes this margin of error.
  32. //
  33. // ASN.1 is very complex and this package doesn't attempt to implement
  34. // everything by any means.
  35. import (
  36. "errors"
  37. "fmt"
  38. "math"
  39. "math/big"
  40. "reflect"
  41. "strconv"
  42. "time"
  43. "unicode/utf16"
  44. "unicode/utf8"
  45. )
  46. // A StructuralError suggests that the ASN.1 data is valid, but the Go type
  47. // which is receiving it doesn't match.
  48. type StructuralError struct {
  49. Msg string
  50. Field string
  51. }
  52. func (e StructuralError) Error() string {
  53. var prefix string
  54. if e.Field != "" {
  55. prefix = e.Field + ": "
  56. }
  57. return "asn1: structure error: " + prefix + e.Msg
  58. }
  59. // A SyntaxError suggests that the ASN.1 data is invalid.
  60. type SyntaxError struct {
  61. Msg string
  62. Field string
  63. }
  64. func (e SyntaxError) Error() string {
  65. var prefix string
  66. if e.Field != "" {
  67. prefix = e.Field + ": "
  68. }
  69. return "asn1: syntax error: " + prefix + e.Msg
  70. }
  71. // We start by dealing with each of the primitive types in turn.
  72. // BOOLEAN
  73. func parseBool(bytes []byte, fieldName string) (ret bool, err error) {
  74. if len(bytes) != 1 {
  75. err = SyntaxError{"invalid boolean", fieldName}
  76. return
  77. }
  78. // DER demands that "If the encoding represents the boolean value TRUE,
  79. // its single contents octet shall have all eight bits set to one."
  80. // Thus only 0 and 255 are valid encoded values.
  81. switch bytes[0] {
  82. case 0:
  83. ret = false
  84. case 0xff:
  85. ret = true
  86. default:
  87. err = SyntaxError{"invalid boolean", fieldName}
  88. }
  89. return
  90. }
  91. // INTEGER
  92. // checkInteger returns nil if the given bytes are a valid DER-encoded
  93. // INTEGER and an error otherwise.
  94. func checkInteger(bytes []byte, lax bool, fieldName string) error {
  95. if len(bytes) == 0 {
  96. return StructuralError{"empty integer", fieldName}
  97. }
  98. if len(bytes) == 1 {
  99. return nil
  100. }
  101. if lax {
  102. return nil
  103. }
  104. if (bytes[0] == 0 && bytes[1]&0x80 == 0) || (bytes[0] == 0xff && bytes[1]&0x80 == 0x80) {
  105. return StructuralError{"integer not minimally-encoded", fieldName}
  106. }
  107. return nil
  108. }
  109. // parseInt64 treats the given bytes as a big-endian, signed integer and
  110. // returns the result.
  111. func parseInt64(bytes []byte, lax bool, fieldName string) (ret int64, err error) {
  112. err = checkInteger(bytes, lax, fieldName)
  113. if err != nil {
  114. return
  115. }
  116. if len(bytes) > 8 {
  117. // We'll overflow an int64 in this case.
  118. err = StructuralError{"integer too large", fieldName}
  119. return
  120. }
  121. for bytesRead := 0; bytesRead < len(bytes); bytesRead++ {
  122. ret <<= 8
  123. ret |= int64(bytes[bytesRead])
  124. }
  125. // Shift up and down in order to sign extend the result.
  126. ret <<= 64 - uint8(len(bytes))*8
  127. ret >>= 64 - uint8(len(bytes))*8
  128. return
  129. }
  130. // parseInt treats the given bytes as a big-endian, signed integer and returns
  131. // the result.
  132. func parseInt32(bytes []byte, lax bool, fieldName string) (int32, error) {
  133. if err := checkInteger(bytes, lax, fieldName); err != nil {
  134. return 0, err
  135. }
  136. ret64, err := parseInt64(bytes, lax, fieldName)
  137. if err != nil {
  138. return 0, err
  139. }
  140. if ret64 != int64(int32(ret64)) {
  141. return 0, StructuralError{"integer too large", fieldName}
  142. }
  143. return int32(ret64), nil
  144. }
  145. var bigOne = big.NewInt(1)
  146. // parseBigInt treats the given bytes as a big-endian, signed integer and returns
  147. // the result.
  148. func parseBigInt(bytes []byte, lax bool, fieldName string) (*big.Int, error) {
  149. if err := checkInteger(bytes, lax, fieldName); err != nil {
  150. return nil, err
  151. }
  152. ret := new(big.Int)
  153. if len(bytes) > 0 && bytes[0]&0x80 == 0x80 {
  154. // This is a negative number.
  155. notBytes := make([]byte, len(bytes))
  156. for i := range notBytes {
  157. notBytes[i] = ^bytes[i]
  158. }
  159. ret.SetBytes(notBytes)
  160. ret.Add(ret, bigOne)
  161. ret.Neg(ret)
  162. return ret, nil
  163. }
  164. ret.SetBytes(bytes)
  165. return ret, nil
  166. }
  167. // BIT STRING
  168. // BitString is the structure to use when you want an ASN.1 BIT STRING type. A
  169. // bit string is padded up to the nearest byte in memory and the number of
  170. // valid bits is recorded. Padding bits will be zero.
  171. type BitString struct {
  172. Bytes []byte // bits packed into bytes.
  173. BitLength int // length in bits.
  174. }
  175. // At returns the bit at the given index. If the index is out of range it
  176. // returns false.
  177. func (b BitString) At(i int) int {
  178. if i < 0 || i >= b.BitLength {
  179. return 0
  180. }
  181. x := i / 8
  182. y := 7 - uint(i%8)
  183. return int(b.Bytes[x]>>y) & 1
  184. }
  185. // RightAlign returns a slice where the padding bits are at the beginning. The
  186. // slice may share memory with the BitString.
  187. func (b BitString) RightAlign() []byte {
  188. shift := uint(8 - (b.BitLength % 8))
  189. if shift == 8 || len(b.Bytes) == 0 {
  190. return b.Bytes
  191. }
  192. a := make([]byte, len(b.Bytes))
  193. a[0] = b.Bytes[0] >> shift
  194. for i := 1; i < len(b.Bytes); i++ {
  195. a[i] = b.Bytes[i-1] << (8 - shift)
  196. a[i] |= b.Bytes[i] >> shift
  197. }
  198. return a
  199. }
  200. // parseBitString parses an ASN.1 bit string from the given byte slice and returns it.
  201. func parseBitString(bytes []byte, fieldName string) (ret BitString, err error) {
  202. if len(bytes) == 0 {
  203. err = SyntaxError{"zero length BIT STRING", fieldName}
  204. return
  205. }
  206. paddingBits := int(bytes[0])
  207. if paddingBits > 7 ||
  208. len(bytes) == 1 && paddingBits > 0 ||
  209. bytes[len(bytes)-1]&((1<<bytes[0])-1) != 0 {
  210. err = SyntaxError{"invalid padding bits in BIT STRING", fieldName}
  211. return
  212. }
  213. ret.BitLength = (len(bytes)-1)*8 - paddingBits
  214. ret.Bytes = bytes[1:]
  215. return
  216. }
  217. // NULL
  218. // NullRawValue is a RawValue with its Tag set to the ASN.1 NULL type tag (5).
  219. var NullRawValue = RawValue{Tag: TagNull}
  220. // NullBytes contains bytes representing the DER-encoded ASN.1 NULL type.
  221. var NullBytes = []byte{TagNull, 0}
  222. // OBJECT IDENTIFIER
  223. // An ObjectIdentifier represents an ASN.1 OBJECT IDENTIFIER.
  224. type ObjectIdentifier []int
  225. // Equal reports whether oi and other represent the same identifier.
  226. func (oi ObjectIdentifier) Equal(other ObjectIdentifier) bool {
  227. if len(oi) != len(other) {
  228. return false
  229. }
  230. for i := 0; i < len(oi); i++ {
  231. if oi[i] != other[i] {
  232. return false
  233. }
  234. }
  235. return true
  236. }
  237. func (oi ObjectIdentifier) String() string {
  238. var s string
  239. for i, v := range oi {
  240. if i > 0 {
  241. s += "."
  242. }
  243. s += strconv.Itoa(v)
  244. }
  245. return s
  246. }
  247. // parseObjectIdentifier parses an OBJECT IDENTIFIER from the given bytes and
  248. // returns it. An object identifier is a sequence of variable length integers
  249. // that are assigned in a hierarchy.
  250. func parseObjectIdentifier(bytes []byte, lax bool, fieldName string) (s ObjectIdentifier, err error) {
  251. if len(bytes) == 0 {
  252. if lax {
  253. return ObjectIdentifier{}, nil
  254. }
  255. err = SyntaxError{"zero length OBJECT IDENTIFIER", fieldName}
  256. return
  257. }
  258. // In the worst case, we get two elements from the first byte (which is
  259. // encoded differently) and then every varint is a single byte long.
  260. s = make([]int, len(bytes)+1)
  261. // The first varint is 40*value1 + value2:
  262. // According to this packing, value1 can take the values 0, 1 and 2 only.
  263. // When value1 = 0 or value1 = 1, then value2 is <= 39. When value1 = 2,
  264. // then there are no restrictions on value2.
  265. v, offset, err := parseBase128Int(bytes, 0, fieldName)
  266. if err != nil {
  267. return
  268. }
  269. if v < 80 {
  270. s[0] = v / 40
  271. s[1] = v % 40
  272. } else {
  273. s[0] = 2
  274. s[1] = v - 80
  275. }
  276. i := 2
  277. for ; offset < len(bytes); i++ {
  278. v, offset, err = parseBase128Int(bytes, offset, fieldName)
  279. if err != nil {
  280. return
  281. }
  282. s[i] = v
  283. }
  284. s = s[0:i]
  285. return
  286. }
  287. // ENUMERATED
  288. // An Enumerated is represented as a plain int.
  289. type Enumerated int
  290. // FLAG
  291. // A Flag accepts any data and is set to true if present.
  292. type Flag bool
  293. // parseBase128Int parses a base-128 encoded int from the given offset in the
  294. // given byte slice. It returns the value and the new offset.
  295. func parseBase128Int(bytes []byte, initOffset int, fieldName string) (ret, offset int, err error) {
  296. offset = initOffset
  297. var ret64 int64
  298. for shifted := 0; offset < len(bytes); shifted++ {
  299. // 5 * 7 bits per byte == 35 bits of data
  300. // Thus the representation is either non-minimal or too large for an int32
  301. if shifted == 5 {
  302. err = StructuralError{"base 128 integer too large", fieldName}
  303. return
  304. }
  305. ret64 <<= 7
  306. b := bytes[offset]
  307. ret64 |= int64(b & 0x7f)
  308. offset++
  309. if b&0x80 == 0 {
  310. ret = int(ret64)
  311. // Ensure that the returned value fits in an int on all platforms
  312. if ret64 > math.MaxInt32 {
  313. err = StructuralError{"base 128 integer too large", fieldName}
  314. }
  315. return
  316. }
  317. }
  318. err = SyntaxError{"truncated base 128 integer", fieldName}
  319. return
  320. }
  321. // UTCTime
  322. func parseUTCTime(bytes []byte) (ret time.Time, err error) {
  323. s := string(bytes)
  324. formatStr := "0601021504Z0700"
  325. ret, err = time.Parse(formatStr, s)
  326. if err != nil {
  327. formatStr = "060102150405Z0700"
  328. ret, err = time.Parse(formatStr, s)
  329. }
  330. if err != nil {
  331. return
  332. }
  333. if serialized := ret.Format(formatStr); serialized != s {
  334. err = fmt.Errorf("asn1: time did not serialize back to the original value and may be invalid: given %q, but serialized as %q", s, serialized)
  335. return
  336. }
  337. if ret.Year() >= 2050 {
  338. // UTCTime only encodes times prior to 2050. See https://tools.ietf.org/html/rfc5280#section-4.1.2.5.1
  339. ret = ret.AddDate(-100, 0, 0)
  340. }
  341. return
  342. }
  343. // parseGeneralizedTime parses the GeneralizedTime from the given byte slice
  344. // and returns the resulting time.
  345. func parseGeneralizedTime(bytes []byte) (ret time.Time, err error) {
  346. const formatStr = "20060102150405Z0700"
  347. s := string(bytes)
  348. if ret, err = time.Parse(formatStr, s); err != nil {
  349. return
  350. }
  351. if serialized := ret.Format(formatStr); serialized != s {
  352. err = fmt.Errorf("asn1: time did not serialize back to the original value and may be invalid: given %q, but serialized as %q", s, serialized)
  353. }
  354. return
  355. }
  356. // NumericString
  357. // parseNumericString parses an ASN.1 NumericString from the given byte array
  358. // and returns it.
  359. func parseNumericString(bytes []byte, fieldName string) (ret string, err error) {
  360. for _, b := range bytes {
  361. if !isNumeric(b) {
  362. return "", SyntaxError{"NumericString contains invalid character", fieldName}
  363. }
  364. }
  365. return string(bytes), nil
  366. }
  367. // isNumeric reports whether the given b is in the ASN.1 NumericString set.
  368. func isNumeric(b byte) bool {
  369. return '0' <= b && b <= '9' ||
  370. b == ' '
  371. }
  372. // PrintableString
  373. // parsePrintableString parses an ASN.1 PrintableString from the given byte
  374. // array and returns it.
  375. func parsePrintableString(bytes []byte, lax bool, fieldName string) (ret string, err error) {
  376. for _, b := range bytes {
  377. if !isPrintable(b, allowAsterisk, allowAmpersand) {
  378. if !lax {
  379. err = SyntaxError{"PrintableString contains invalid character", fieldName}
  380. } else {
  381. // Might be an ISO8859-1 string stuffed in, check if it
  382. // would be valid and assume that's what's happened if so,
  383. // otherwise try T.61, failing that give up and just assign
  384. // the bytes
  385. switch {
  386. case couldBeISO8859_1(bytes):
  387. ret, err = iso8859_1ToUTF8(bytes), nil
  388. case couldBeT61(bytes):
  389. ret, err = parseT61String(bytes)
  390. default:
  391. err = SyntaxError{"PrintableString contains invalid character, couldn't determine correct String type", fieldName}
  392. }
  393. }
  394. return
  395. }
  396. }
  397. ret = string(bytes)
  398. return
  399. }
  400. type asteriskFlag bool
  401. type ampersandFlag bool
  402. const (
  403. allowAsterisk asteriskFlag = true
  404. rejectAsterisk asteriskFlag = false
  405. allowAmpersand ampersandFlag = true
  406. rejectAmpersand ampersandFlag = false
  407. )
  408. // isPrintable reports whether the given b is in the ASN.1 PrintableString set.
  409. // If asterisk is allowAsterisk then '*' is also allowed, reflecting existing
  410. // practice. If ampersand is allowAmpersand then '&' is allowed as well.
  411. func isPrintable(b byte, asterisk asteriskFlag, ampersand ampersandFlag) bool {
  412. return 'a' <= b && b <= 'z' ||
  413. 'A' <= b && b <= 'Z' ||
  414. '0' <= b && b <= '9' ||
  415. '\'' <= b && b <= ')' ||
  416. '+' <= b && b <= '/' ||
  417. b == ' ' ||
  418. b == ':' ||
  419. b == '=' ||
  420. b == '?' ||
  421. // This is technically not allowed in a PrintableString.
  422. // However, x509 certificates with wildcard strings don't
  423. // always use the correct string type so we permit it.
  424. (bool(asterisk) && b == '*') ||
  425. // This is not technically allowed either. However, not
  426. // only is it relatively common, but there are also a
  427. // handful of CA certificates that contain it. At least
  428. // one of which will not expire until 2027.
  429. (bool(ampersand) && b == '&')
  430. }
  431. // IA5String
  432. // parseIA5String parses an ASN.1 IA5String (ASCII string) from the given
  433. // byte slice and returns it.
  434. func parseIA5String(bytes []byte, fieldName string) (ret string, err error) {
  435. for _, b := range bytes {
  436. if b >= utf8.RuneSelf {
  437. err = SyntaxError{"IA5String contains invalid character", fieldName}
  438. return
  439. }
  440. }
  441. ret = string(bytes)
  442. return
  443. }
  444. // T61String
  445. // parseT61String parses an ASN.1 T61String (8-bit clean string) from the given
  446. // byte slice and returns it.
  447. func parseT61String(bytes []byte) (ret string, err error) {
  448. return string(bytes), nil
  449. }
  450. // UTF8String
  451. // parseUTF8String parses an ASN.1 UTF8String (raw UTF-8) from the given byte
  452. // array and returns it.
  453. func parseUTF8String(bytes []byte) (ret string, err error) {
  454. if !utf8.Valid(bytes) {
  455. return "", errors.New("asn1: invalid UTF-8 string")
  456. }
  457. return string(bytes), nil
  458. }
  459. // BMPString
  460. // parseBMPString parses an ASN.1 BMPString (Basic Multilingual Plane of
  461. // ISO/IEC/ITU 10646-1) from the given byte slice and returns it.
  462. func parseBMPString(bmpString []byte) (string, error) {
  463. if len(bmpString)%2 != 0 {
  464. return "", errors.New("pkcs12: odd-length BMP string")
  465. }
  466. // Strip terminator if present.
  467. if l := len(bmpString); l >= 2 && bmpString[l-1] == 0 && bmpString[l-2] == 0 {
  468. bmpString = bmpString[:l-2]
  469. }
  470. s := make([]uint16, 0, len(bmpString)/2)
  471. for len(bmpString) > 0 {
  472. s = append(s, uint16(bmpString[0])<<8+uint16(bmpString[1]))
  473. bmpString = bmpString[2:]
  474. }
  475. return string(utf16.Decode(s)), nil
  476. }
  477. // A RawValue represents an undecoded ASN.1 object.
  478. type RawValue struct {
  479. Class, Tag int
  480. IsCompound bool
  481. Bytes []byte
  482. FullBytes []byte // includes the tag and length
  483. }
  484. // RawContent is used to signal that the undecoded, DER data needs to be
  485. // preserved for a struct. To use it, the first field of the struct must have
  486. // this type. It's an error for any of the other fields to have this type.
  487. type RawContent []byte
  488. // Tagging
  489. // parseTagAndLength parses an ASN.1 tag and length pair from the given offset
  490. // into a byte slice. It returns the parsed data and the new offset. SET and
  491. // SET OF (tag 17) are mapped to SEQUENCE and SEQUENCE OF (tag 16) since we
  492. // don't distinguish between ordered and unordered objects in this code.
  493. func parseTagAndLength(bytes []byte, initOffset int, fieldName string) (ret tagAndLength, offset int, err error) {
  494. offset = initOffset
  495. // parseTagAndLength should not be called without at least a single
  496. // byte to read. Thus this check is for robustness:
  497. if offset >= len(bytes) {
  498. err = errors.New("asn1: internal error in parseTagAndLength")
  499. return
  500. }
  501. b := bytes[offset]
  502. offset++
  503. ret.class = int(b >> 6)
  504. ret.isCompound = b&0x20 == 0x20
  505. ret.tag = int(b & 0x1f)
  506. // If the bottom five bits are set, then the tag number is actually base 128
  507. // encoded afterwards
  508. if ret.tag == 0x1f {
  509. ret.tag, offset, err = parseBase128Int(bytes, offset, fieldName)
  510. if err != nil {
  511. return
  512. }
  513. // Tags should be encoded in minimal form.
  514. if ret.tag < 0x1f {
  515. err = SyntaxError{"non-minimal tag", fieldName}
  516. return
  517. }
  518. }
  519. if offset >= len(bytes) {
  520. err = SyntaxError{"truncated tag or length", fieldName}
  521. return
  522. }
  523. b = bytes[offset]
  524. offset++
  525. if b&0x80 == 0 {
  526. // The length is encoded in the bottom 7 bits.
  527. ret.length = int(b & 0x7f)
  528. } else {
  529. // Bottom 7 bits give the number of length bytes to follow.
  530. numBytes := int(b & 0x7f)
  531. if numBytes == 0 {
  532. err = SyntaxError{"indefinite length found (not DER)", fieldName}
  533. return
  534. }
  535. ret.length = 0
  536. for i := 0; i < numBytes; i++ {
  537. if offset >= len(bytes) {
  538. err = SyntaxError{"truncated tag or length", fieldName}
  539. return
  540. }
  541. b = bytes[offset]
  542. offset++
  543. if ret.length >= 1<<23 {
  544. // We can't shift ret.length up without
  545. // overflowing.
  546. err = StructuralError{"length too large", fieldName}
  547. return
  548. }
  549. ret.length <<= 8
  550. ret.length |= int(b)
  551. if ret.length == 0 {
  552. // DER requires that lengths be minimal.
  553. err = StructuralError{"superfluous leading zeros in length", fieldName}
  554. return
  555. }
  556. }
  557. // Short lengths must be encoded in short form.
  558. if ret.length < 0x80 {
  559. err = StructuralError{"non-minimal length", fieldName}
  560. return
  561. }
  562. }
  563. return
  564. }
  565. // parseSequenceOf is used for SEQUENCE OF and SET OF values. It tries to parse
  566. // a number of ASN.1 values from the given byte slice and returns them as a
  567. // slice of Go values of the given type.
  568. func parseSequenceOf(bytes []byte, sliceType reflect.Type, elemType reflect.Type, lax bool, fieldName string) (ret reflect.Value, err error) {
  569. matchAny, expectedTag, compoundType, ok := getUniversalType(elemType)
  570. if !ok {
  571. err = StructuralError{"unknown Go type for slice", fieldName}
  572. return
  573. }
  574. // First we iterate over the input and count the number of elements,
  575. // checking that the types are correct in each case.
  576. numElements := 0
  577. for offset := 0; offset < len(bytes); {
  578. var t tagAndLength
  579. t, offset, err = parseTagAndLength(bytes, offset, fieldName)
  580. if err != nil {
  581. return
  582. }
  583. switch t.tag {
  584. case TagIA5String, TagGeneralString, TagT61String, TagUTF8String, TagNumericString, TagBMPString:
  585. // We pretend that various other string types are
  586. // PRINTABLE STRINGs so that a sequence of them can be
  587. // parsed into a []string.
  588. t.tag = TagPrintableString
  589. case TagGeneralizedTime, TagUTCTime:
  590. // Likewise, both time types are treated the same.
  591. t.tag = TagUTCTime
  592. }
  593. if !matchAny && (t.class != ClassUniversal || t.isCompound != compoundType || t.tag != expectedTag) {
  594. err = StructuralError{fmt.Sprintf("sequence tag mismatch (got:%+v, want:0/%d/%t)", t, expectedTag, compoundType), fieldName}
  595. return
  596. }
  597. if invalidLength(offset, t.length, len(bytes)) {
  598. err = SyntaxError{"truncated sequence", fieldName}
  599. return
  600. }
  601. offset += t.length
  602. numElements++
  603. }
  604. ret = reflect.MakeSlice(sliceType, numElements, numElements)
  605. params := fieldParameters{lax: lax}
  606. offset := 0
  607. for i := 0; i < numElements; i++ {
  608. offset, err = parseField(ret.Index(i), bytes, offset, params)
  609. if err != nil {
  610. return
  611. }
  612. }
  613. return
  614. }
  615. var (
  616. bitStringType = reflect.TypeOf(BitString{})
  617. objectIdentifierType = reflect.TypeOf(ObjectIdentifier{})
  618. enumeratedType = reflect.TypeOf(Enumerated(0))
  619. flagType = reflect.TypeOf(Flag(false))
  620. timeType = reflect.TypeOf(time.Time{})
  621. rawValueType = reflect.TypeOf(RawValue{})
  622. rawContentsType = reflect.TypeOf(RawContent(nil))
  623. bigIntType = reflect.TypeOf(new(big.Int))
  624. )
  625. // invalidLength reports whether offset + length > sliceLength, or if the
  626. // addition would overflow.
  627. func invalidLength(offset, length, sliceLength int) bool {
  628. return offset+length < offset || offset+length > sliceLength
  629. }
  630. // Tests whether the data in |bytes| would be a valid ISO8859-1 string.
  631. // Clearly, a sequence of bytes comprised solely of valid ISO8859-1
  632. // codepoints does not imply that the encoding MUST be ISO8859-1, rather that
  633. // you would not encounter an error trying to interpret the data as such.
  634. func couldBeISO8859_1(bytes []byte) bool {
  635. for _, b := range bytes {
  636. if b < 0x20 || (b >= 0x7F && b < 0xA0) {
  637. return false
  638. }
  639. }
  640. return true
  641. }
  642. // Checks whether the data in |bytes| would be a valid T.61 string.
  643. // Clearly, a sequence of bytes comprised solely of valid T.61
  644. // codepoints does not imply that the encoding MUST be T.61, rather that
  645. // you would not encounter an error trying to interpret the data as such.
  646. func couldBeT61(bytes []byte) bool {
  647. for _, b := range bytes {
  648. switch b {
  649. case 0x00:
  650. // Since we're guessing at (incorrect) encodings for a
  651. // PrintableString, we'll err on the side of caution and disallow
  652. // strings with a NUL in them, don't want to re-create a PayPal NUL
  653. // situation in monitors.
  654. fallthrough
  655. case 0x23, 0x24, 0x5C, 0x5E, 0x60, 0x7B, 0x7D, 0x7E, 0xA5, 0xA6, 0xAC, 0xAD, 0xAE, 0xAF,
  656. 0xB9, 0xBA, 0xC0, 0xC9, 0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9,
  657. 0xDA, 0xDB, 0xDC, 0xDE, 0xDF, 0xE5, 0xFF:
  658. // These are all invalid code points in T.61, so it can't be a T.61 string.
  659. return false
  660. }
  661. }
  662. return true
  663. }
  664. // Converts the data in |bytes| to the equivalent UTF-8 string.
  665. func iso8859_1ToUTF8(bytes []byte) string {
  666. buf := make([]rune, len(bytes))
  667. for i, b := range bytes {
  668. buf[i] = rune(b)
  669. }
  670. return string(buf)
  671. }
  672. // parseField is the main parsing function. Given a byte slice and an offset
  673. // into the array, it will try to parse a suitable ASN.1 value out and store it
  674. // in the given Value.
  675. func parseField(v reflect.Value, bytes []byte, initOffset int, params fieldParameters) (offset int, err error) {
  676. offset = initOffset
  677. fieldType := v.Type()
  678. // If we have run out of data, it may be that there are optional elements at the end.
  679. if offset == len(bytes) {
  680. if !setDefaultValue(v, params) {
  681. err = SyntaxError{"sequence truncated", params.name}
  682. }
  683. return
  684. }
  685. // Deal with the ANY type.
  686. if ifaceType := fieldType; ifaceType.Kind() == reflect.Interface && ifaceType.NumMethod() == 0 {
  687. var t tagAndLength
  688. t, offset, err = parseTagAndLength(bytes, offset, params.name)
  689. if err != nil {
  690. return
  691. }
  692. if invalidLength(offset, t.length, len(bytes)) {
  693. err = SyntaxError{"data truncated", params.name}
  694. return
  695. }
  696. var result interface{}
  697. if !t.isCompound && t.class == ClassUniversal {
  698. innerBytes := bytes[offset : offset+t.length]
  699. switch t.tag {
  700. case TagPrintableString:
  701. result, err = parsePrintableString(innerBytes, params.lax, params.name)
  702. case TagNumericString:
  703. result, err = parseNumericString(innerBytes, params.name)
  704. case TagIA5String:
  705. result, err = parseIA5String(innerBytes, params.name)
  706. case TagT61String:
  707. result, err = parseT61String(innerBytes)
  708. case TagUTF8String:
  709. result, err = parseUTF8String(innerBytes)
  710. case TagInteger:
  711. result, err = parseInt64(innerBytes, params.lax, params.name)
  712. case TagBitString:
  713. result, err = parseBitString(innerBytes, params.name)
  714. case TagOID:
  715. result, err = parseObjectIdentifier(innerBytes, params.lax, params.name)
  716. case TagUTCTime:
  717. result, err = parseUTCTime(innerBytes)
  718. case TagGeneralizedTime:
  719. result, err = parseGeneralizedTime(innerBytes)
  720. case TagOctetString:
  721. result = innerBytes
  722. case TagBMPString:
  723. result, err = parseBMPString(innerBytes)
  724. default:
  725. // If we don't know how to handle the type, we just leave Value as nil.
  726. }
  727. }
  728. offset += t.length
  729. if err != nil {
  730. return
  731. }
  732. if result != nil {
  733. v.Set(reflect.ValueOf(result))
  734. }
  735. return
  736. }
  737. t, offset, err := parseTagAndLength(bytes, offset, params.name)
  738. if err != nil {
  739. return
  740. }
  741. if params.explicit {
  742. expectedClass := ClassContextSpecific
  743. if params.application {
  744. expectedClass = ClassApplication
  745. }
  746. if offset == len(bytes) {
  747. err = StructuralError{"explicit tag has no child", params.name}
  748. return
  749. }
  750. if t.class == expectedClass && t.tag == *params.tag && (t.length == 0 || t.isCompound) {
  751. if fieldType == rawValueType {
  752. // The inner element should not be parsed for RawValues.
  753. } else if t.length > 0 {
  754. t, offset, err = parseTagAndLength(bytes, offset, params.name)
  755. if err != nil {
  756. return
  757. }
  758. } else {
  759. if fieldType != flagType {
  760. err = StructuralError{"zero length explicit tag was not an asn1.Flag", params.name}
  761. return
  762. }
  763. v.SetBool(true)
  764. return
  765. }
  766. } else {
  767. // The tags didn't match, it might be an optional element.
  768. ok := setDefaultValue(v, params)
  769. if ok {
  770. offset = initOffset
  771. } else {
  772. err = StructuralError{"explicitly tagged member didn't match", params.name}
  773. }
  774. return
  775. }
  776. }
  777. matchAny, universalTag, compoundType, ok1 := getUniversalType(fieldType)
  778. if !ok1 {
  779. err = StructuralError{fmt.Sprintf("unknown Go type: %v", fieldType), params.name}
  780. return
  781. }
  782. // Special case for strings: all the ASN.1 string types map to the Go
  783. // type string. getUniversalType returns the tag for PrintableString
  784. // when it sees a string, so if we see a different string type on the
  785. // wire, we change the universal type to match.
  786. if universalTag == TagPrintableString {
  787. if t.class == ClassUniversal {
  788. switch t.tag {
  789. case TagIA5String, TagGeneralString, TagT61String, TagUTF8String, TagNumericString, TagBMPString:
  790. universalTag = t.tag
  791. }
  792. } else if params.stringType != 0 {
  793. universalTag = params.stringType
  794. }
  795. }
  796. // Special case for time: UTCTime and GeneralizedTime both map to the
  797. // Go type time.Time.
  798. if universalTag == TagUTCTime && t.tag == TagGeneralizedTime && t.class == ClassUniversal {
  799. universalTag = TagGeneralizedTime
  800. }
  801. if params.set {
  802. universalTag = TagSet
  803. }
  804. matchAnyClassAndTag := matchAny
  805. expectedClass := ClassUniversal
  806. expectedTag := universalTag
  807. if !params.explicit && params.tag != nil {
  808. expectedClass = ClassContextSpecific
  809. expectedTag = *params.tag
  810. matchAnyClassAndTag = false
  811. }
  812. if !params.explicit && params.application && params.tag != nil {
  813. expectedClass = ClassApplication
  814. expectedTag = *params.tag
  815. matchAnyClassAndTag = false
  816. }
  817. if !params.explicit && params.private && params.tag != nil {
  818. expectedClass = ClassPrivate
  819. expectedTag = *params.tag
  820. matchAnyClassAndTag = false
  821. }
  822. // We have unwrapped any explicit tagging at this point.
  823. if !matchAnyClassAndTag && (t.class != expectedClass || t.tag != expectedTag) ||
  824. (!matchAny && t.isCompound != compoundType) {
  825. // Tags don't match. Again, it could be an optional element.
  826. ok := setDefaultValue(v, params)
  827. if ok {
  828. offset = initOffset
  829. } else {
  830. err = StructuralError{fmt.Sprintf("tags don't match (%d vs %+v) %+v %s @%d", expectedTag, t, params, fieldType.Name(), offset), params.name}
  831. }
  832. return
  833. }
  834. if invalidLength(offset, t.length, len(bytes)) {
  835. err = SyntaxError{"data truncated", params.name}
  836. return
  837. }
  838. innerBytes := bytes[offset : offset+t.length]
  839. offset += t.length
  840. // We deal with the structures defined in this package first.
  841. switch fieldType {
  842. case rawValueType:
  843. result := RawValue{t.class, t.tag, t.isCompound, innerBytes, bytes[initOffset:offset]}
  844. v.Set(reflect.ValueOf(result))
  845. return
  846. case objectIdentifierType:
  847. newSlice, err1 := parseObjectIdentifier(innerBytes, params.lax, params.name)
  848. v.Set(reflect.MakeSlice(v.Type(), len(newSlice), len(newSlice)))
  849. if err1 == nil {
  850. reflect.Copy(v, reflect.ValueOf(newSlice))
  851. }
  852. err = err1
  853. return
  854. case bitStringType:
  855. bs, err1 := parseBitString(innerBytes, params.name)
  856. if err1 == nil {
  857. v.Set(reflect.ValueOf(bs))
  858. }
  859. err = err1
  860. return
  861. case timeType:
  862. var time time.Time
  863. var err1 error
  864. if universalTag == TagUTCTime {
  865. time, err1 = parseUTCTime(innerBytes)
  866. } else {
  867. time, err1 = parseGeneralizedTime(innerBytes)
  868. }
  869. if err1 == nil {
  870. v.Set(reflect.ValueOf(time))
  871. }
  872. err = err1
  873. return
  874. case enumeratedType:
  875. parsedInt, err1 := parseInt32(innerBytes, params.lax, params.name)
  876. if err1 == nil {
  877. v.SetInt(int64(parsedInt))
  878. }
  879. err = err1
  880. return
  881. case flagType:
  882. v.SetBool(true)
  883. return
  884. case bigIntType:
  885. parsedInt, err1 := parseBigInt(innerBytes, params.lax, params.name)
  886. if err1 == nil {
  887. v.Set(reflect.ValueOf(parsedInt))
  888. }
  889. err = err1
  890. return
  891. }
  892. switch val := v; val.Kind() {
  893. case reflect.Bool:
  894. parsedBool, err1 := parseBool(innerBytes, params.name)
  895. if err1 == nil {
  896. val.SetBool(parsedBool)
  897. }
  898. err = err1
  899. return
  900. case reflect.Int, reflect.Int32, reflect.Int64:
  901. if val.Type().Size() == 4 {
  902. parsedInt, err1 := parseInt32(innerBytes, params.lax, params.name)
  903. if err1 == nil {
  904. val.SetInt(int64(parsedInt))
  905. }
  906. err = err1
  907. } else {
  908. parsedInt, err1 := parseInt64(innerBytes, params.lax, params.name)
  909. if err1 == nil {
  910. val.SetInt(parsedInt)
  911. }
  912. err = err1
  913. }
  914. return
  915. // TODO(dfc) Add support for the remaining integer types
  916. case reflect.Struct:
  917. structType := fieldType
  918. for i := 0; i < structType.NumField(); i++ {
  919. if structType.Field(i).PkgPath != "" {
  920. err = StructuralError{"struct contains unexported fields", structType.Field(i).Name}
  921. return
  922. }
  923. }
  924. if structType.NumField() > 0 &&
  925. structType.Field(0).Type == rawContentsType {
  926. bytes := bytes[initOffset:offset]
  927. val.Field(0).Set(reflect.ValueOf(RawContent(bytes)))
  928. }
  929. innerOffset := 0
  930. for i := 0; i < structType.NumField(); i++ {
  931. field := structType.Field(i)
  932. if i == 0 && field.Type == rawContentsType {
  933. continue
  934. }
  935. innerParams := parseFieldParameters(field.Tag.Get("asn1"))
  936. innerParams.name = field.Name
  937. innerParams.lax = params.lax
  938. innerOffset, err = parseField(val.Field(i), innerBytes, innerOffset, innerParams)
  939. if err != nil {
  940. return
  941. }
  942. }
  943. // We allow extra bytes at the end of the SEQUENCE because
  944. // adding elements to the end has been used in X.509 as the
  945. // version numbers have increased.
  946. return
  947. case reflect.Slice:
  948. sliceType := fieldType
  949. if sliceType.Elem().Kind() == reflect.Uint8 {
  950. val.Set(reflect.MakeSlice(sliceType, len(innerBytes), len(innerBytes)))
  951. reflect.Copy(val, reflect.ValueOf(innerBytes))
  952. return
  953. }
  954. newSlice, err1 := parseSequenceOf(innerBytes, sliceType, sliceType.Elem(), params.lax, params.name)
  955. if err1 == nil {
  956. val.Set(newSlice)
  957. }
  958. err = err1
  959. return
  960. case reflect.String:
  961. var v string
  962. switch universalTag {
  963. case TagPrintableString:
  964. v, err = parsePrintableString(innerBytes, params.lax, params.name)
  965. case TagNumericString:
  966. v, err = parseNumericString(innerBytes, params.name)
  967. case TagIA5String:
  968. v, err = parseIA5String(innerBytes, params.name)
  969. case TagT61String:
  970. v, err = parseT61String(innerBytes)
  971. case TagUTF8String:
  972. v, err = parseUTF8String(innerBytes)
  973. case TagGeneralString:
  974. // GeneralString is specified in ISO-2022/ECMA-35,
  975. // A brief review suggests that it includes structures
  976. // that allow the encoding to change midstring and
  977. // such. We give up and pass it as an 8-bit string.
  978. v, err = parseT61String(innerBytes)
  979. case TagBMPString:
  980. v, err = parseBMPString(innerBytes)
  981. default:
  982. err = SyntaxError{fmt.Sprintf("internal error: unknown string type %d", universalTag), params.name}
  983. }
  984. if err == nil {
  985. val.SetString(v)
  986. }
  987. return
  988. }
  989. err = StructuralError{"unsupported: " + v.Type().String(), params.name}
  990. return
  991. }
  992. // canHaveDefaultValue reports whether k is a Kind that we will set a default
  993. // value for. (A signed integer, essentially.)
  994. func canHaveDefaultValue(k reflect.Kind) bool {
  995. switch k {
  996. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  997. return true
  998. }
  999. return false
  1000. }
  1001. // setDefaultValue is used to install a default value, from a tag string, into
  1002. // a Value. It is successful if the field was optional, even if a default value
  1003. // wasn't provided or it failed to install it into the Value.
  1004. func setDefaultValue(v reflect.Value, params fieldParameters) (ok bool) {
  1005. if !params.optional {
  1006. return
  1007. }
  1008. ok = true
  1009. if params.defaultValue == nil {
  1010. return
  1011. }
  1012. if canHaveDefaultValue(v.Kind()) {
  1013. v.SetInt(*params.defaultValue)
  1014. }
  1015. return
  1016. }
  1017. // Unmarshal parses the DER-encoded ASN.1 data structure b
  1018. // and uses the reflect package to fill in an arbitrary value pointed at by val.
  1019. // Because Unmarshal uses the reflect package, the structs
  1020. // being written to must use upper case field names.
  1021. //
  1022. // An ASN.1 INTEGER can be written to an int, int32, int64,
  1023. // or *big.Int (from the math/big package).
  1024. // If the encoded value does not fit in the Go type,
  1025. // Unmarshal returns a parse error.
  1026. //
  1027. // An ASN.1 BIT STRING can be written to a BitString.
  1028. //
  1029. // An ASN.1 OCTET STRING can be written to a []byte.
  1030. //
  1031. // An ASN.1 OBJECT IDENTIFIER can be written to an
  1032. // ObjectIdentifier.
  1033. //
  1034. // An ASN.1 ENUMERATED can be written to an Enumerated.
  1035. //
  1036. // An ASN.1 UTCTIME or GENERALIZEDTIME can be written to a time.Time.
  1037. //
  1038. // An ASN.1 PrintableString, IA5String, or NumericString can be written to a string.
  1039. //
  1040. // Any of the above ASN.1 values can be written to an interface{}.
  1041. // The value stored in the interface has the corresponding Go type.
  1042. // For integers, that type is int64.
  1043. //
  1044. // An ASN.1 SEQUENCE OF x or SET OF x can be written
  1045. // to a slice if an x can be written to the slice's element type.
  1046. //
  1047. // An ASN.1 SEQUENCE or SET can be written to a struct
  1048. // if each of the elements in the sequence can be
  1049. // written to the corresponding element in the struct.
  1050. //
  1051. // The following tags on struct fields have special meaning to Unmarshal:
  1052. //
  1053. // application specifies that an APPLICATION tag is used
  1054. // private specifies that a PRIVATE tag is used
  1055. // default:x sets the default value for optional integer fields (only used if optional is also present)
  1056. // explicit specifies that an additional, explicit tag wraps the implicit one
  1057. // optional marks the field as ASN.1 OPTIONAL
  1058. // set causes a SET, rather than a SEQUENCE type to be expected
  1059. // tag:x specifies the ASN.1 tag number; implies ASN.1 CONTEXT SPECIFIC
  1060. // lax relax strict encoding checks for this field, and for any fields within it
  1061. //
  1062. // If the type of the first field of a structure is RawContent then the raw
  1063. // ASN1 contents of the struct will be stored in it.
  1064. //
  1065. // If the type name of a slice element ends with "SET" then it's treated as if
  1066. // the "set" tag was set on it. This can be used with nested slices where a
  1067. // struct tag cannot be given.
  1068. //
  1069. // Other ASN.1 types are not supported; if it encounters them,
  1070. // Unmarshal returns a parse error.
  1071. func Unmarshal(b []byte, val interface{}) (rest []byte, err error) {
  1072. return UnmarshalWithParams(b, val, "")
  1073. }
  1074. // UnmarshalWithParams allows field parameters to be specified for the
  1075. // top-level element. The form of the params is the same as the field tags.
  1076. func UnmarshalWithParams(b []byte, val interface{}, params string) (rest []byte, err error) {
  1077. v := reflect.ValueOf(val).Elem()
  1078. offset, err := parseField(v, b, 0, parseFieldParameters(params))
  1079. if err != nil {
  1080. return nil, err
  1081. }
  1082. return b[offset:], nil
  1083. }