1
2
3
4
5 package cryptobyte
6
7 import (
8 encoding_asn1 "encoding/asn1"
9 "fmt"
10 "math/big"
11 "reflect"
12 "time"
13
14 "golang.org/x/crypto/cryptobyte/asn1"
15 )
16
17
18
19
20
21
22 func (b *Builder) AddASN1Int64(v int64) {
23 b.addASN1Signed(asn1.INTEGER, v)
24 }
25
26
27
28 func (b *Builder) AddASN1Int64WithTag(v int64, tag asn1.Tag) {
29 b.addASN1Signed(tag, v)
30 }
31
32
33 func (b *Builder) AddASN1Enum(v int64) {
34 b.addASN1Signed(asn1.ENUM, v)
35 }
36
37 func (b *Builder) addASN1Signed(tag asn1.Tag, v int64) {
38 b.AddASN1(tag, func(c *Builder) {
39 length := 1
40 for i := v; i >= 0x80 || i < -0x80; i >>= 8 {
41 length++
42 }
43
44 for ; length > 0; length-- {
45 i := v >> uint((length-1)*8) & 0xff
46 c.AddUint8(uint8(i))
47 }
48 })
49 }
50
51
52 func (b *Builder) AddASN1Uint64(v uint64) {
53 b.AddASN1(asn1.INTEGER, func(c *Builder) {
54 length := 1
55 for i := v; i >= 0x80; i >>= 8 {
56 length++
57 }
58
59 for ; length > 0; length-- {
60 i := v >> uint((length-1)*8) & 0xff
61 c.AddUint8(uint8(i))
62 }
63 })
64 }
65
66
67 func (b *Builder) AddASN1BigInt(n *big.Int) {
68 if b.err != nil {
69 return
70 }
71
72 b.AddASN1(asn1.INTEGER, func(c *Builder) {
73 if n.Sign() < 0 {
74
75
76
77
78 nMinus1 := new(big.Int).Neg(n)
79 nMinus1.Sub(nMinus1, bigOne)
80 bytes := nMinus1.Bytes()
81 for i := range bytes {
82 bytes[i] ^= 0xff
83 }
84 if len(bytes) == 0 || bytes[0]&0x80 == 0 {
85 c.add(0xff)
86 }
87 c.add(bytes...)
88 } else if n.Sign() == 0 {
89 c.add(0)
90 } else {
91 bytes := n.Bytes()
92 if bytes[0]&0x80 != 0 {
93 c.add(0)
94 }
95 c.add(bytes...)
96 }
97 })
98 }
99
100
101 func (b *Builder) AddASN1OctetString(bytes []byte) {
102 b.AddASN1(asn1.OCTET_STRING, func(c *Builder) {
103 c.AddBytes(bytes)
104 })
105 }
106
107 const generalizedTimeFormatStr = "20060102150405Z0700"
108
109
110 func (b *Builder) AddASN1GeneralizedTime(t time.Time) {
111 if t.Year() < 0 || t.Year() > 9999 {
112 b.err = fmt.Errorf("cryptobyte: cannot represent %v as a GeneralizedTime", t)
113 return
114 }
115 b.AddASN1(asn1.GeneralizedTime, func(c *Builder) {
116 c.AddBytes([]byte(t.Format(generalizedTimeFormatStr)))
117 })
118 }
119
120
121 func (b *Builder) AddASN1UTCTime(t time.Time) {
122 b.AddASN1(asn1.UTCTime, func(c *Builder) {
123
124
125 if t.Year() < 1950 || t.Year() >= 2050 {
126 b.err = fmt.Errorf("cryptobyte: cannot represent %v as a UTCTime", t)
127 return
128 }
129 c.AddBytes([]byte(t.Format(defaultUTCTimeFormatStr)))
130 })
131 }
132
133
134
135 func (b *Builder) AddASN1BitString(data []byte) {
136 b.AddASN1(asn1.BIT_STRING, func(b *Builder) {
137 b.AddUint8(0)
138 b.AddBytes(data)
139 })
140 }
141
142 func (b *Builder) addBase128Int(n int64) {
143 var length int
144 if n == 0 {
145 length = 1
146 } else {
147 for i := n; i > 0; i >>= 7 {
148 length++
149 }
150 }
151
152 for i := length - 1; i >= 0; i-- {
153 o := byte(n >> uint(i*7))
154 o &= 0x7f
155 if i != 0 {
156 o |= 0x80
157 }
158
159 b.add(o)
160 }
161 }
162
163 func isValidOID(oid encoding_asn1.ObjectIdentifier) bool {
164 if len(oid) < 2 {
165 return false
166 }
167
168 if oid[0] > 2 || (oid[0] <= 1 && oid[1] >= 40) {
169 return false
170 }
171
172 for _, v := range oid {
173 if v < 0 {
174 return false
175 }
176 }
177
178 return true
179 }
180
181 func (b *Builder) AddASN1ObjectIdentifier(oid encoding_asn1.ObjectIdentifier) {
182 b.AddASN1(asn1.OBJECT_IDENTIFIER, func(b *Builder) {
183 if !isValidOID(oid) {
184 b.err = fmt.Errorf("cryptobyte: invalid OID: %v", oid)
185 return
186 }
187
188 b.addBase128Int(int64(oid[0])*40 + int64(oid[1]))
189 for _, v := range oid[2:] {
190 b.addBase128Int(int64(v))
191 }
192 })
193 }
194
195 func (b *Builder) AddASN1Boolean(v bool) {
196 b.AddASN1(asn1.BOOLEAN, func(b *Builder) {
197 if v {
198 b.AddUint8(0xff)
199 } else {
200 b.AddUint8(0)
201 }
202 })
203 }
204
205 func (b *Builder) AddASN1NULL() {
206 b.add(uint8(asn1.NULL), 0)
207 }
208
209
210
211 func (b *Builder) MarshalASN1(v interface{}) {
212
213
214
215 if b.err != nil {
216 return
217 }
218 bytes, err := encoding_asn1.Marshal(v)
219 if err != nil {
220 b.err = err
221 return
222 }
223 b.AddBytes(bytes)
224 }
225
226
227
228
229
230 func (b *Builder) AddASN1(tag asn1.Tag, f BuilderContinuation) {
231 if b.err != nil {
232 return
233 }
234
235
236 if tag&0x1f == 0x1f {
237 b.err = fmt.Errorf("cryptobyte: high-tag number identifier octects not supported: 0x%x", tag)
238 return
239 }
240 b.AddUint8(uint8(tag))
241 b.addLengthPrefixed(1, true, f)
242 }
243
244
245
246
247
248
249 func (s *String) ReadASN1Boolean(out *bool) bool {
250 var bytes String
251 if !s.ReadASN1(&bytes, asn1.BOOLEAN) || len(bytes) != 1 {
252 return false
253 }
254
255 switch bytes[0] {
256 case 0:
257 *out = false
258 case 0xff:
259 *out = true
260 default:
261 return false
262 }
263
264 return true
265 }
266
267
268
269
270
271
272
273 func (s *String) ReadASN1Integer(out interface{}) bool {
274 switch out := out.(type) {
275 case *int, *int8, *int16, *int32, *int64:
276 var i int64
277 if !s.readASN1Int64(&i) || reflect.ValueOf(out).Elem().OverflowInt(i) {
278 return false
279 }
280 reflect.ValueOf(out).Elem().SetInt(i)
281 return true
282 case *uint, *uint8, *uint16, *uint32, *uint64:
283 var u uint64
284 if !s.readASN1Uint64(&u) || reflect.ValueOf(out).Elem().OverflowUint(u) {
285 return false
286 }
287 reflect.ValueOf(out).Elem().SetUint(u)
288 return true
289 case *big.Int:
290 return s.readASN1BigInt(out)
291 case *[]byte:
292 return s.readASN1Bytes(out)
293 default:
294 panic("out does not point to an integer type")
295 }
296 }
297
298 func checkASN1Integer(bytes []byte) bool {
299 if len(bytes) == 0 {
300
301 return false
302 }
303 if len(bytes) == 1 {
304 return true
305 }
306 if bytes[0] == 0 && bytes[1]&0x80 == 0 || bytes[0] == 0xff && bytes[1]&0x80 == 0x80 {
307
308 return false
309 }
310 return true
311 }
312
313 var bigOne = big.NewInt(1)
314
315 func (s *String) readASN1BigInt(out *big.Int) bool {
316 var bytes String
317 if !s.ReadASN1(&bytes, asn1.INTEGER) || !checkASN1Integer(bytes) {
318 return false
319 }
320 if bytes[0]&0x80 == 0x80 {
321
322 neg := make([]byte, len(bytes))
323 for i, b := range bytes {
324 neg[i] = ^b
325 }
326 out.SetBytes(neg)
327 out.Add(out, bigOne)
328 out.Neg(out)
329 } else {
330 out.SetBytes(bytes)
331 }
332 return true
333 }
334
335 func (s *String) readASN1Bytes(out *[]byte) bool {
336 var bytes String
337 if !s.ReadASN1(&bytes, asn1.INTEGER) || !checkASN1Integer(bytes) {
338 return false
339 }
340 if bytes[0]&0x80 == 0x80 {
341 return false
342 }
343 for len(bytes) > 1 && bytes[0] == 0 {
344 bytes = bytes[1:]
345 }
346 *out = bytes
347 return true
348 }
349
350 func (s *String) readASN1Int64(out *int64) bool {
351 var bytes String
352 if !s.ReadASN1(&bytes, asn1.INTEGER) || !checkASN1Integer(bytes) || !asn1Signed(out, bytes) {
353 return false
354 }
355 return true
356 }
357
358 func asn1Signed(out *int64, n []byte) bool {
359 length := len(n)
360 if length > 8 {
361 return false
362 }
363 for i := 0; i < length; i++ {
364 *out <<= 8
365 *out |= int64(n[i])
366 }
367
368 *out <<= 64 - uint8(length)*8
369 *out >>= 64 - uint8(length)*8
370 return true
371 }
372
373 func (s *String) readASN1Uint64(out *uint64) bool {
374 var bytes String
375 if !s.ReadASN1(&bytes, asn1.INTEGER) || !checkASN1Integer(bytes) || !asn1Unsigned(out, bytes) {
376 return false
377 }
378 return true
379 }
380
381 func asn1Unsigned(out *uint64, n []byte) bool {
382 length := len(n)
383 if length > 9 || length == 9 && n[0] != 0 {
384
385 return false
386 }
387 if n[0]&0x80 != 0 {
388
389 return false
390 }
391 for i := 0; i < length; i++ {
392 *out <<= 8
393 *out |= uint64(n[i])
394 }
395 return true
396 }
397
398
399
400
401 func (s *String) ReadASN1Int64WithTag(out *int64, tag asn1.Tag) bool {
402 var bytes String
403 return s.ReadASN1(&bytes, tag) && checkASN1Integer(bytes) && asn1Signed(out, bytes)
404 }
405
406
407
408 func (s *String) ReadASN1Enum(out *int) bool {
409 var bytes String
410 var i int64
411 if !s.ReadASN1(&bytes, asn1.ENUM) || !checkASN1Integer(bytes) || !asn1Signed(&i, bytes) {
412 return false
413 }
414 if int64(int(i)) != i {
415 return false
416 }
417 *out = int(i)
418 return true
419 }
420
421 func (s *String) readBase128Int(out *int) bool {
422 ret := 0
423 for i := 0; len(*s) > 0; i++ {
424 if i == 5 {
425 return false
426 }
427
428
429 if ret >= 1<<(31-7) {
430 return false
431 }
432 ret <<= 7
433 b := s.read(1)[0]
434
435
436
437
438 if i == 0 && b == 0x80 {
439 return false
440 }
441
442 ret |= int(b & 0x7f)
443 if b&0x80 == 0 {
444 *out = ret
445 return true
446 }
447 }
448 return false
449 }
450
451
452
453 func (s *String) ReadASN1ObjectIdentifier(out *encoding_asn1.ObjectIdentifier) bool {
454 var bytes String
455 if !s.ReadASN1(&bytes, asn1.OBJECT_IDENTIFIER) || len(bytes) == 0 {
456 return false
457 }
458
459
460
461 components := make([]int, len(bytes)+1)
462
463
464
465
466
467 var v int
468 if !bytes.readBase128Int(&v) {
469 return false
470 }
471 if v < 80 {
472 components[0] = v / 40
473 components[1] = v % 40
474 } else {
475 components[0] = 2
476 components[1] = v - 80
477 }
478
479 i := 2
480 for ; len(bytes) > 0; i++ {
481 if !bytes.readBase128Int(&v) {
482 return false
483 }
484 components[i] = v
485 }
486 *out = components[:i]
487 return true
488 }
489
490
491
492 func (s *String) ReadASN1GeneralizedTime(out *time.Time) bool {
493 var bytes String
494 if !s.ReadASN1(&bytes, asn1.GeneralizedTime) {
495 return false
496 }
497 t := string(bytes)
498 res, err := time.Parse(generalizedTimeFormatStr, t)
499 if err != nil {
500 return false
501 }
502 if serialized := res.Format(generalizedTimeFormatStr); serialized != t {
503 return false
504 }
505 *out = res
506 return true
507 }
508
509 const defaultUTCTimeFormatStr = "060102150405Z0700"
510
511
512
513 func (s *String) ReadASN1UTCTime(out *time.Time) bool {
514 var bytes String
515 if !s.ReadASN1(&bytes, asn1.UTCTime) {
516 return false
517 }
518 t := string(bytes)
519
520 formatStr := defaultUTCTimeFormatStr
521 var err error
522 res, err := time.Parse(formatStr, t)
523 if err != nil {
524
525
526
527 formatStr = "0601021504Z0700"
528 res, err = time.Parse(formatStr, t)
529 }
530 if err != nil {
531 return false
532 }
533
534 if serialized := res.Format(formatStr); serialized != t {
535 return false
536 }
537
538 if res.Year() >= 2050 {
539
540
541
542 res = res.AddDate(-100, 0, 0)
543 }
544 *out = res
545 return true
546 }
547
548
549
550 func (s *String) ReadASN1BitString(out *encoding_asn1.BitString) bool {
551 var bytes String
552 if !s.ReadASN1(&bytes, asn1.BIT_STRING) || len(bytes) == 0 ||
553 len(bytes)*8/8 != len(bytes) {
554 return false
555 }
556
557 paddingBits := bytes[0]
558 bytes = bytes[1:]
559 if paddingBits > 7 ||
560 len(bytes) == 0 && paddingBits != 0 ||
561 len(bytes) > 0 && bytes[len(bytes)-1]&(1<<paddingBits-1) != 0 {
562 return false
563 }
564
565 out.BitLength = len(bytes)*8 - int(paddingBits)
566 out.Bytes = bytes
567 return true
568 }
569
570
571
572
573 func (s *String) ReadASN1BitStringAsBytes(out *[]byte) bool {
574 var bytes String
575 if !s.ReadASN1(&bytes, asn1.BIT_STRING) || len(bytes) == 0 {
576 return false
577 }
578
579 paddingBits := bytes[0]
580 if paddingBits != 0 {
581 return false
582 }
583 *out = bytes[1:]
584 return true
585 }
586
587
588
589
590 func (s *String) ReadASN1Bytes(out *[]byte, tag asn1.Tag) bool {
591 return s.ReadASN1((*String)(out), tag)
592 }
593
594
595
596
597
598
599 func (s *String) ReadASN1(out *String, tag asn1.Tag) bool {
600 var t asn1.Tag
601 if !s.ReadAnyASN1(out, &t) || t != tag {
602 return false
603 }
604 return true
605 }
606
607
608
609
610
611
612 func (s *String) ReadASN1Element(out *String, tag asn1.Tag) bool {
613 var t asn1.Tag
614 if !s.ReadAnyASN1Element(out, &t) || t != tag {
615 return false
616 }
617 return true
618 }
619
620
621
622
623
624
625 func (s *String) ReadAnyASN1(out *String, outTag *asn1.Tag) bool {
626 return s.readASN1(out, outTag, true )
627 }
628
629
630
631
632
633
634 func (s *String) ReadAnyASN1Element(out *String, outTag *asn1.Tag) bool {
635 return s.readASN1(out, outTag, false )
636 }
637
638
639
640 func (s String) PeekASN1Tag(tag asn1.Tag) bool {
641 if len(s) == 0 {
642 return false
643 }
644 return asn1.Tag(s[0]) == tag
645 }
646
647
648
649 func (s *String) SkipASN1(tag asn1.Tag) bool {
650 var unused String
651 return s.ReadASN1(&unused, tag)
652 }
653
654
655
656
657
658 func (s *String) ReadOptionalASN1(out *String, outPresent *bool, tag asn1.Tag) bool {
659 present := s.PeekASN1Tag(tag)
660 if outPresent != nil {
661 *outPresent = present
662 }
663 if present && !s.ReadASN1(out, tag) {
664 return false
665 }
666 return true
667 }
668
669
670
671 func (s *String) SkipOptionalASN1(tag asn1.Tag) bool {
672 if !s.PeekASN1Tag(tag) {
673 return true
674 }
675 var unused String
676 return s.ReadASN1(&unused, tag)
677 }
678
679
680
681
682
683 func (s *String) ReadOptionalASN1Integer(out interface{}, tag asn1.Tag, defaultValue interface{}) bool {
684 var present bool
685 var i String
686 if !s.ReadOptionalASN1(&i, &present, tag) {
687 return false
688 }
689 if !present {
690 switch out.(type) {
691 case *int, *int8, *int16, *int32, *int64,
692 *uint, *uint8, *uint16, *uint32, *uint64, *[]byte:
693 reflect.ValueOf(out).Elem().Set(reflect.ValueOf(defaultValue))
694 case *big.Int:
695 if defaultValue, ok := defaultValue.(*big.Int); ok {
696 out.(*big.Int).Set(defaultValue)
697 } else {
698 panic("out points to big.Int, but defaultValue does not")
699 }
700 default:
701 panic("invalid integer type")
702 }
703 return true
704 }
705 if !i.ReadASN1Integer(out) || !i.Empty() {
706 return false
707 }
708 return true
709 }
710
711
712
713
714
715 func (s *String) ReadOptionalASN1OctetString(out *[]byte, outPresent *bool, tag asn1.Tag) bool {
716 var present bool
717 var child String
718 if !s.ReadOptionalASN1(&child, &present, tag) {
719 return false
720 }
721 if outPresent != nil {
722 *outPresent = present
723 }
724 if present {
725 var oct String
726 if !child.ReadASN1(&oct, asn1.OCTET_STRING) || !child.Empty() {
727 return false
728 }
729 *out = oct
730 } else {
731 *out = nil
732 }
733 return true
734 }
735
736
737
738
739
740 func (s *String) ReadOptionalASN1Boolean(out *bool, tag asn1.Tag, defaultValue bool) bool {
741 var present bool
742 var child String
743 if !s.ReadOptionalASN1(&child, &present, tag) {
744 return false
745 }
746
747 if !present {
748 *out = defaultValue
749 return true
750 }
751
752 return child.ReadASN1Boolean(out)
753 }
754
755 func (s *String) readASN1(out *String, outTag *asn1.Tag, skipHeader bool) bool {
756 if len(*s) < 2 {
757 return false
758 }
759 tag, lenByte := (*s)[0], (*s)[1]
760
761 if tag&0x1f == 0x1f {
762
763
764
765
766
767 return false
768 }
769
770 if outTag != nil {
771 *outTag = asn1.Tag(tag)
772 }
773
774
775
776
777
778 var length, headerLen uint32
779 if lenByte&0x80 == 0 {
780
781 length = uint32(lenByte) + 2
782 headerLen = 2
783 } else {
784
785
786 lenLen := lenByte & 0x7f
787 var len32 uint32
788
789 if lenLen == 0 || lenLen > 4 || len(*s) < int(2+lenLen) {
790 return false
791 }
792
793 lenBytes := String((*s)[2 : 2+lenLen])
794 if !lenBytes.readUnsigned(&len32, int(lenLen)) {
795 return false
796 }
797
798
799
800 if len32 < 128 {
801
802 return false
803 }
804 if len32>>((lenLen-1)*8) == 0 {
805
806 return false
807 }
808
809 headerLen = 2 + uint32(lenLen)
810 if headerLen+len32 < len32 {
811
812 return false
813 }
814 length = headerLen + len32
815 }
816
817 if int(length) < 0 || !s.ReadBytes((*[]byte)(out), int(length)) {
818 return false
819 }
820 if skipHeader && !out.Skip(int(headerLen)) {
821 panic("cryptobyte: internal error")
822 }
823
824 return true
825 }
826
View as plain text