marshal.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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
  5. import (
  6. "errors"
  7. "fmt"
  8. "math/big"
  9. "reflect"
  10. "time"
  11. "unicode/utf8"
  12. )
  13. var (
  14. byte00Encoder encoder = byteEncoder(0x00)
  15. byteFFEncoder encoder = byteEncoder(0xff)
  16. )
  17. // encoder represents an ASN.1 element that is waiting to be marshaled.
  18. type encoder interface {
  19. // Len returns the number of bytes needed to marshal this element.
  20. Len() int
  21. // Encode encodes this element by writing Len() bytes to dst.
  22. Encode(dst []byte)
  23. }
  24. type byteEncoder byte
  25. func (c byteEncoder) Len() int {
  26. return 1
  27. }
  28. func (c byteEncoder) Encode(dst []byte) {
  29. dst[0] = byte(c)
  30. }
  31. type bytesEncoder []byte
  32. func (b bytesEncoder) Len() int {
  33. return len(b)
  34. }
  35. func (b bytesEncoder) Encode(dst []byte) {
  36. if copy(dst, b) != len(b) {
  37. panic("internal error")
  38. }
  39. }
  40. type stringEncoder string
  41. func (s stringEncoder) Len() int {
  42. return len(s)
  43. }
  44. func (s stringEncoder) Encode(dst []byte) {
  45. if copy(dst, s) != len(s) {
  46. panic("internal error")
  47. }
  48. }
  49. type multiEncoder []encoder
  50. func (m multiEncoder) Len() int {
  51. var size int
  52. for _, e := range m {
  53. size += e.Len()
  54. }
  55. return size
  56. }
  57. func (m multiEncoder) Encode(dst []byte) {
  58. var off int
  59. for _, e := range m {
  60. e.Encode(dst[off:])
  61. off += e.Len()
  62. }
  63. }
  64. type taggedEncoder struct {
  65. // scratch contains temporary space for encoding the tag and length of
  66. // an element in order to avoid extra allocations.
  67. scratch [8]byte
  68. tag encoder
  69. body encoder
  70. }
  71. func (t *taggedEncoder) Len() int {
  72. return t.tag.Len() + t.body.Len()
  73. }
  74. func (t *taggedEncoder) Encode(dst []byte) {
  75. t.tag.Encode(dst)
  76. t.body.Encode(dst[t.tag.Len():])
  77. }
  78. type int64Encoder int64
  79. func (i int64Encoder) Len() int {
  80. n := 1
  81. for i > 127 {
  82. n++
  83. i >>= 8
  84. }
  85. for i < -128 {
  86. n++
  87. i >>= 8
  88. }
  89. return n
  90. }
  91. func (i int64Encoder) Encode(dst []byte) {
  92. n := i.Len()
  93. for j := 0; j < n; j++ {
  94. dst[j] = byte(i >> uint((n-1-j)*8))
  95. }
  96. }
  97. func base128IntLength(n int64) int {
  98. if n == 0 {
  99. return 1
  100. }
  101. l := 0
  102. for i := n; i > 0; i >>= 7 {
  103. l++
  104. }
  105. return l
  106. }
  107. func appendBase128Int(dst []byte, n int64) []byte {
  108. l := base128IntLength(n)
  109. for i := l - 1; i >= 0; i-- {
  110. o := byte(n >> uint(i*7))
  111. o &= 0x7f
  112. if i != 0 {
  113. o |= 0x80
  114. }
  115. dst = append(dst, o)
  116. }
  117. return dst
  118. }
  119. func makeBigInt(n *big.Int, fieldName string) (encoder, error) {
  120. if n == nil {
  121. return nil, StructuralError{"empty integer", fieldName}
  122. }
  123. if n.Sign() < 0 {
  124. // A negative number has to be converted to two's-complement
  125. // form. So we'll invert and subtract 1. If the
  126. // most-significant-bit isn't set then we'll need to pad the
  127. // beginning with 0xff in order to keep the number negative.
  128. nMinus1 := new(big.Int).Neg(n)
  129. nMinus1.Sub(nMinus1, bigOne)
  130. bytes := nMinus1.Bytes()
  131. for i := range bytes {
  132. bytes[i] ^= 0xff
  133. }
  134. if len(bytes) == 0 || bytes[0]&0x80 == 0 {
  135. return multiEncoder([]encoder{byteFFEncoder, bytesEncoder(bytes)}), nil
  136. }
  137. return bytesEncoder(bytes), nil
  138. } else if n.Sign() == 0 {
  139. // Zero is written as a single 0 zero rather than no bytes.
  140. return byte00Encoder, nil
  141. } else {
  142. bytes := n.Bytes()
  143. if len(bytes) > 0 && bytes[0]&0x80 != 0 {
  144. // We'll have to pad this with 0x00 in order to stop it
  145. // looking like a negative number.
  146. return multiEncoder([]encoder{byte00Encoder, bytesEncoder(bytes)}), nil
  147. }
  148. return bytesEncoder(bytes), nil
  149. }
  150. }
  151. func appendLength(dst []byte, i int) []byte {
  152. n := lengthLength(i)
  153. for ; n > 0; n-- {
  154. dst = append(dst, byte(i>>uint((n-1)*8)))
  155. }
  156. return dst
  157. }
  158. func lengthLength(i int) (numBytes int) {
  159. numBytes = 1
  160. for i > 255 {
  161. numBytes++
  162. i >>= 8
  163. }
  164. return
  165. }
  166. func appendTagAndLength(dst []byte, t tagAndLength) []byte {
  167. b := uint8(t.class) << 6
  168. if t.isCompound {
  169. b |= 0x20
  170. }
  171. if t.tag >= 31 {
  172. b |= 0x1f
  173. dst = append(dst, b)
  174. dst = appendBase128Int(dst, int64(t.tag))
  175. } else {
  176. b |= uint8(t.tag)
  177. dst = append(dst, b)
  178. }
  179. if t.length >= 128 {
  180. l := lengthLength(t.length)
  181. dst = append(dst, 0x80|byte(l))
  182. dst = appendLength(dst, t.length)
  183. } else {
  184. dst = append(dst, byte(t.length))
  185. }
  186. return dst
  187. }
  188. type bitStringEncoder BitString
  189. func (b bitStringEncoder) Len() int {
  190. return len(b.Bytes) + 1
  191. }
  192. func (b bitStringEncoder) Encode(dst []byte) {
  193. dst[0] = byte((8 - b.BitLength%8) % 8)
  194. if copy(dst[1:], b.Bytes) != len(b.Bytes) {
  195. panic("internal error")
  196. }
  197. }
  198. type oidEncoder []int
  199. func (oid oidEncoder) Len() int {
  200. l := base128IntLength(int64(oid[0]*40 + oid[1]))
  201. for i := 2; i < len(oid); i++ {
  202. l += base128IntLength(int64(oid[i]))
  203. }
  204. return l
  205. }
  206. func (oid oidEncoder) Encode(dst []byte) {
  207. dst = appendBase128Int(dst[:0], int64(oid[0]*40+oid[1]))
  208. for i := 2; i < len(oid); i++ {
  209. dst = appendBase128Int(dst, int64(oid[i]))
  210. }
  211. }
  212. func makeObjectIdentifier(oid []int, fieldName string) (e encoder, err error) {
  213. if len(oid) < 2 || oid[0] > 2 || (oid[0] < 2 && oid[1] >= 40) {
  214. return nil, StructuralError{"invalid object identifier", fieldName}
  215. }
  216. return oidEncoder(oid), nil
  217. }
  218. func makePrintableString(s, fieldName string) (e encoder, err error) {
  219. for i := 0; i < len(s); i++ {
  220. // The asterisk is often used in PrintableString, even though
  221. // it is invalid. If a PrintableString was specifically
  222. // requested then the asterisk is permitted by this code.
  223. // Ampersand is allowed in parsing due a handful of CA
  224. // certificates, however when making new certificates
  225. // it is rejected.
  226. if !isPrintable(s[i], allowAsterisk, rejectAmpersand) {
  227. return nil, StructuralError{"PrintableString contains invalid character", fieldName}
  228. }
  229. }
  230. return stringEncoder(s), nil
  231. }
  232. func makeIA5String(s, fieldName string) (e encoder, err error) {
  233. for i := 0; i < len(s); i++ {
  234. if s[i] > 127 {
  235. return nil, StructuralError{"IA5String contains invalid character", fieldName}
  236. }
  237. }
  238. return stringEncoder(s), nil
  239. }
  240. func makeNumericString(s string, fieldName string) (e encoder, err error) {
  241. for i := 0; i < len(s); i++ {
  242. if !isNumeric(s[i]) {
  243. return nil, StructuralError{"NumericString contains invalid character", fieldName}
  244. }
  245. }
  246. return stringEncoder(s), nil
  247. }
  248. func makeUTF8String(s string) encoder {
  249. return stringEncoder(s)
  250. }
  251. func appendTwoDigits(dst []byte, v int) []byte {
  252. return append(dst, byte('0'+(v/10)%10), byte('0'+v%10))
  253. }
  254. func appendFourDigits(dst []byte, v int) []byte {
  255. var bytes [4]byte
  256. for i := range bytes {
  257. bytes[3-i] = '0' + byte(v%10)
  258. v /= 10
  259. }
  260. return append(dst, bytes[:]...)
  261. }
  262. func outsideUTCRange(t time.Time) bool {
  263. year := t.Year()
  264. return year < 1950 || year >= 2050
  265. }
  266. func makeUTCTime(t time.Time, fieldName string) (e encoder, err error) {
  267. dst := make([]byte, 0, 18)
  268. dst, err = appendUTCTime(dst, t, fieldName)
  269. if err != nil {
  270. return nil, err
  271. }
  272. return bytesEncoder(dst), nil
  273. }
  274. func makeGeneralizedTime(t time.Time, fieldName string) (e encoder, err error) {
  275. dst := make([]byte, 0, 20)
  276. dst, err = appendGeneralizedTime(dst, t, fieldName)
  277. if err != nil {
  278. return nil, err
  279. }
  280. return bytesEncoder(dst), nil
  281. }
  282. func appendUTCTime(dst []byte, t time.Time, fieldName string) (ret []byte, err error) {
  283. year := t.Year()
  284. switch {
  285. case 1950 <= year && year < 2000:
  286. dst = appendTwoDigits(dst, year-1900)
  287. case 2000 <= year && year < 2050:
  288. dst = appendTwoDigits(dst, year-2000)
  289. default:
  290. return nil, StructuralError{"cannot represent time as UTCTime", fieldName}
  291. }
  292. return appendTimeCommon(dst, t), nil
  293. }
  294. func appendGeneralizedTime(dst []byte, t time.Time, fieldName string) (ret []byte, err error) {
  295. year := t.Year()
  296. if year < 0 || year > 9999 {
  297. return nil, StructuralError{"cannot represent time as GeneralizedTime", fieldName}
  298. }
  299. dst = appendFourDigits(dst, year)
  300. return appendTimeCommon(dst, t), nil
  301. }
  302. func appendTimeCommon(dst []byte, t time.Time) []byte {
  303. _, month, day := t.Date()
  304. dst = appendTwoDigits(dst, int(month))
  305. dst = appendTwoDigits(dst, day)
  306. hour, min, sec := t.Clock()
  307. dst = appendTwoDigits(dst, hour)
  308. dst = appendTwoDigits(dst, min)
  309. dst = appendTwoDigits(dst, sec)
  310. _, offset := t.Zone()
  311. switch {
  312. case offset/60 == 0:
  313. return append(dst, 'Z')
  314. case offset > 0:
  315. dst = append(dst, '+')
  316. case offset < 0:
  317. dst = append(dst, '-')
  318. }
  319. offsetMinutes := offset / 60
  320. if offsetMinutes < 0 {
  321. offsetMinutes = -offsetMinutes
  322. }
  323. dst = appendTwoDigits(dst, offsetMinutes/60)
  324. dst = appendTwoDigits(dst, offsetMinutes%60)
  325. return dst
  326. }
  327. func stripTagAndLength(in []byte) []byte {
  328. _, offset, err := parseTagAndLength(in, 0, "")
  329. if err != nil {
  330. return in
  331. }
  332. return in[offset:]
  333. }
  334. func makeBody(value reflect.Value, params fieldParameters) (e encoder, err error) {
  335. switch value.Type() {
  336. case flagType:
  337. return bytesEncoder(nil), nil
  338. case timeType:
  339. t := value.Interface().(time.Time)
  340. if params.timeType == TagGeneralizedTime || outsideUTCRange(t) {
  341. return makeGeneralizedTime(t, params.name)
  342. }
  343. return makeUTCTime(t, params.name)
  344. case bitStringType:
  345. return bitStringEncoder(value.Interface().(BitString)), nil
  346. case objectIdentifierType:
  347. return makeObjectIdentifier(value.Interface().(ObjectIdentifier), params.name)
  348. case bigIntType:
  349. return makeBigInt(value.Interface().(*big.Int), params.name)
  350. }
  351. switch v := value; v.Kind() {
  352. case reflect.Bool:
  353. if v.Bool() {
  354. return byteFFEncoder, nil
  355. }
  356. return byte00Encoder, nil
  357. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  358. return int64Encoder(v.Int()), nil
  359. case reflect.Struct:
  360. t := v.Type()
  361. for i := 0; i < t.NumField(); i++ {
  362. if t.Field(i).PkgPath != "" {
  363. return nil, StructuralError{"struct contains unexported fields", t.Field(i).Name}
  364. }
  365. }
  366. startingField := 0
  367. n := t.NumField()
  368. if n == 0 {
  369. return bytesEncoder(nil), nil
  370. }
  371. // If the first element of the structure is a non-empty
  372. // RawContents, then we don't bother serializing the rest.
  373. if t.Field(0).Type == rawContentsType {
  374. s := v.Field(0)
  375. if s.Len() > 0 {
  376. bytes := s.Bytes()
  377. /* The RawContents will contain the tag and
  378. * length fields but we'll also be writing
  379. * those ourselves, so we strip them out of
  380. * bytes */
  381. return bytesEncoder(stripTagAndLength(bytes)), nil
  382. }
  383. startingField = 1
  384. }
  385. switch n1 := n - startingField; n1 {
  386. case 0:
  387. return bytesEncoder(nil), nil
  388. case 1:
  389. return makeField(v.Field(startingField), parseFieldParameters(t.Field(startingField).Tag.Get("asn1")))
  390. default:
  391. m := make([]encoder, n1)
  392. for i := 0; i < n1; i++ {
  393. m[i], err = makeField(v.Field(i+startingField), parseFieldParameters(t.Field(i+startingField).Tag.Get("asn1")))
  394. if err != nil {
  395. return nil, err
  396. }
  397. }
  398. return multiEncoder(m), nil
  399. }
  400. case reflect.Slice:
  401. sliceType := v.Type()
  402. if sliceType.Elem().Kind() == reflect.Uint8 {
  403. return bytesEncoder(v.Bytes()), nil
  404. }
  405. var fp fieldParameters
  406. switch l := v.Len(); l {
  407. case 0:
  408. return bytesEncoder(nil), nil
  409. case 1:
  410. return makeField(v.Index(0), fp)
  411. default:
  412. m := make([]encoder, l)
  413. for i := 0; i < l; i++ {
  414. m[i], err = makeField(v.Index(i), fp)
  415. if err != nil {
  416. return nil, err
  417. }
  418. }
  419. return multiEncoder(m), nil
  420. }
  421. case reflect.String:
  422. switch params.stringType {
  423. case TagIA5String:
  424. return makeIA5String(v.String(), params.name)
  425. case TagPrintableString:
  426. return makePrintableString(v.String(), params.name)
  427. case TagNumericString:
  428. return makeNumericString(v.String(), params.name)
  429. default:
  430. return makeUTF8String(v.String()), nil
  431. }
  432. }
  433. return nil, StructuralError{"unknown Go type", params.name}
  434. }
  435. func makeField(v reflect.Value, params fieldParameters) (e encoder, err error) {
  436. if !v.IsValid() {
  437. return nil, fmt.Errorf("asn1: cannot marshal nil value")
  438. }
  439. // If the field is an interface{} then recurse into it.
  440. if v.Kind() == reflect.Interface && v.Type().NumMethod() == 0 {
  441. return makeField(v.Elem(), params)
  442. }
  443. if v.Kind() == reflect.Slice && v.Len() == 0 && params.omitEmpty {
  444. return bytesEncoder(nil), nil
  445. }
  446. if params.optional && params.defaultValue != nil && canHaveDefaultValue(v.Kind()) {
  447. defaultValue := reflect.New(v.Type()).Elem()
  448. defaultValue.SetInt(*params.defaultValue)
  449. if reflect.DeepEqual(v.Interface(), defaultValue.Interface()) {
  450. return bytesEncoder(nil), nil
  451. }
  452. }
  453. // If no default value is given then the zero value for the type is
  454. // assumed to be the default value. This isn't obviously the correct
  455. // behavior, but it's what Go has traditionally done.
  456. if params.optional && params.defaultValue == nil {
  457. if reflect.DeepEqual(v.Interface(), reflect.Zero(v.Type()).Interface()) {
  458. return bytesEncoder(nil), nil
  459. }
  460. }
  461. if v.Type() == rawValueType {
  462. rv := v.Interface().(RawValue)
  463. if len(rv.FullBytes) != 0 {
  464. return bytesEncoder(rv.FullBytes), nil
  465. }
  466. t := new(taggedEncoder)
  467. t.tag = bytesEncoder(appendTagAndLength(t.scratch[:0], tagAndLength{rv.Class, rv.Tag, len(rv.Bytes), rv.IsCompound}))
  468. t.body = bytesEncoder(rv.Bytes)
  469. return t, nil
  470. }
  471. matchAny, tag, isCompound, ok := getUniversalType(v.Type())
  472. if !ok || matchAny {
  473. return nil, StructuralError{fmt.Sprintf("unknown Go type: %v", v.Type()), params.name}
  474. }
  475. if params.timeType != 0 && tag != TagUTCTime {
  476. return nil, StructuralError{"explicit time type given to non-time member", params.name}
  477. }
  478. if params.stringType != 0 && tag != TagPrintableString {
  479. return nil, StructuralError{"explicit string type given to non-string member", params.name}
  480. }
  481. switch tag {
  482. case TagPrintableString:
  483. if params.stringType == 0 {
  484. // This is a string without an explicit string type. We'll use
  485. // a PrintableString if the character set in the string is
  486. // sufficiently limited, otherwise we'll use a UTF8String.
  487. for _, r := range v.String() {
  488. if r >= utf8.RuneSelf || !isPrintable(byte(r), rejectAsterisk, rejectAmpersand) {
  489. if !utf8.ValidString(v.String()) {
  490. return nil, errors.New("asn1: string not valid UTF-8")
  491. }
  492. tag = TagUTF8String
  493. break
  494. }
  495. }
  496. } else {
  497. tag = params.stringType
  498. }
  499. case TagUTCTime:
  500. if params.timeType == TagGeneralizedTime || outsideUTCRange(v.Interface().(time.Time)) {
  501. tag = TagGeneralizedTime
  502. }
  503. }
  504. if params.set {
  505. if tag != TagSequence {
  506. return nil, StructuralError{"non sequence tagged as set", params.name}
  507. }
  508. tag = TagSet
  509. }
  510. t := new(taggedEncoder)
  511. t.body, err = makeBody(v, params)
  512. if err != nil {
  513. return nil, err
  514. }
  515. bodyLen := t.body.Len()
  516. class := ClassUniversal
  517. if params.tag != nil {
  518. if params.application {
  519. class = ClassApplication
  520. } else {
  521. class = ClassContextSpecific
  522. }
  523. if params.explicit {
  524. t.tag = bytesEncoder(appendTagAndLength(t.scratch[:0], tagAndLength{ClassUniversal, tag, bodyLen, isCompound}))
  525. tt := new(taggedEncoder)
  526. tt.body = t
  527. tt.tag = bytesEncoder(appendTagAndLength(tt.scratch[:0], tagAndLength{
  528. class: class,
  529. tag: *params.tag,
  530. length: bodyLen + t.tag.Len(),
  531. isCompound: true,
  532. }))
  533. return tt, nil
  534. }
  535. // implicit tag.
  536. tag = *params.tag
  537. }
  538. t.tag = bytesEncoder(appendTagAndLength(t.scratch[:0], tagAndLength{class, tag, bodyLen, isCompound}))
  539. return t, nil
  540. }
  541. // Marshal returns the ASN.1 encoding of val.
  542. //
  543. // In addition to the struct tags recognised by Unmarshal, the following can be
  544. // used:
  545. //
  546. // ia5: causes strings to be marshaled as ASN.1, IA5String values
  547. // omitempty: causes empty slices to be skipped
  548. // printable: causes strings to be marshaled as ASN.1, PrintableString values
  549. // utf8: causes strings to be marshaled as ASN.1, UTF8String values
  550. // utc: causes time.Time to be marshaled as ASN.1, UTCTime values
  551. // generalized: causes time.Time to be marshaled as ASN.1, GeneralizedTime values
  552. func Marshal(val interface{}) ([]byte, error) {
  553. return MarshalWithParams(val, "")
  554. }
  555. // MarshalWithParams allows field parameters to be specified for the
  556. // top-level element. The form of the params is the same as the field tags.
  557. func MarshalWithParams(val interface{}, params string) ([]byte, error) {
  558. e, err := makeField(reflect.ValueOf(val), parseFieldParameters(params))
  559. if err != nil {
  560. return nil, err
  561. }
  562. b := make([]byte, e.Len())
  563. e.Encode(b)
  564. return b, nil
  565. }