|
5 | 5 | package asn1
|
6 | 6 |
|
7 | 7 | import (
|
| 8 | + "bytes" |
8 | 9 | "errors"
|
9 | 10 | "fmt"
|
10 | 11 | "math/big"
|
11 | 12 | "reflect"
|
| 13 | + "sort" |
12 | 14 | "time"
|
13 | 15 | "unicode/utf8"
|
14 | 16 | )
|
@@ -78,6 +80,48 @@ func (m multiEncoder) Encode(dst []byte) {
|
78 | 80 | }
|
79 | 81 | }
|
80 | 82 |
|
| 83 | +type setEncoder []encoder |
| 84 | + |
| 85 | +func (s setEncoder) Len() int { |
| 86 | + var size int |
| 87 | + for _, e := range s { |
| 88 | + size += e.Len() |
| 89 | + } |
| 90 | + return size |
| 91 | +} |
| 92 | + |
| 93 | +func (s setEncoder) Encode(dst []byte) { |
| 94 | + // Per X690 Section 11.6: The encodings of the component values of a |
| 95 | + // set-of value shall appear in ascending order, the encodings being |
| 96 | + // compared as octet strings with the shorter components being padded |
| 97 | + // at their trailing end with 0-octets. |
| 98 | + // |
| 99 | + // First we encode each element to its TLV encoding and then use |
| 100 | + // octetSort to get the ordering expected by X690 DER rules before |
| 101 | + // writing the sorted encodings out to dst. |
| 102 | + l := make([][]byte, len(s)) |
| 103 | + for i, e := range s { |
| 104 | + l[i] = make([]byte, e.Len()) |
| 105 | + e.Encode(l[i]) |
| 106 | + } |
| 107 | + |
| 108 | + sort.Slice(l, func(i, j int) bool { |
| 109 | + // Since we are using bytes.Compare to compare TLV encodings we |
| 110 | + // don't need to right pad s[i] and s[j] to the same length as |
| 111 | + // suggested in X690. If len(s[i]) < len(s[j]) the length octet of |
| 112 | + // s[i], which is the first determining byte, will inherently be |
| 113 | + // smaller than the length octet of s[j]. This lets us skip the |
| 114 | + // padding step. |
| 115 | + return bytes.Compare(l[i], l[j]) < 0 |
| 116 | + }) |
| 117 | + |
| 118 | + var off int |
| 119 | + for _, b := range l { |
| 120 | + copy(dst[off:], b) |
| 121 | + off += len(b) |
| 122 | + } |
| 123 | +} |
| 124 | + |
81 | 125 | type taggedEncoder struct {
|
82 | 126 | // scratch contains temporary space for encoding the tag and length of
|
83 | 127 | // an element in order to avoid extra allocations.
|
@@ -511,6 +555,9 @@ func makeBody(value reflect.Value, params fieldParameters) (e encoder, err error
|
511 | 555 | }
|
512 | 556 | }
|
513 | 557 |
|
| 558 | + if params.set { |
| 559 | + return setEncoder(m), nil |
| 560 | + } |
514 | 561 | return multiEncoder(m), nil
|
515 | 562 | }
|
516 | 563 | case reflect.String:
|
@@ -618,6 +665,15 @@ func makeField(v reflect.Value, params fieldParameters) (e encoder, err error) {
|
618 | 665 | tag = TagSet
|
619 | 666 | }
|
620 | 667 |
|
| 668 | + // makeField can be called for a slice that should be treated as a SET |
| 669 | + // but doesn't have params.set set, for instance when using a slice |
| 670 | + // with the SET type name suffix. In this case getUniversalType returns |
| 671 | + // TagSet, but makeBody doesn't know about that so will treat the slice |
| 672 | + // as a sequence. To work around this we set params.set. |
| 673 | + if tag == TagSet && !params.set { |
| 674 | + params.set = true |
| 675 | + } |
| 676 | + |
621 | 677 | t := new(taggedEncoder)
|
622 | 678 |
|
623 | 679 | t.body, err = makeBody(v, params)
|
|
0 commit comments