asn1.go 33 KB

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