| 1 |
package protocol |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
) |
| 6 |
|
| 7 |
var encodeIndent = 0 |
| 8 |
|
| 9 |
type asn1Object interface { |
| 10 |
encodeTo(writer *bytes.Buffer) error |
| 11 |
} |
| 12 |
|
| 13 |
type asn1Structured struct { |
| 14 |
tagBytes []byte |
| 15 |
content []asn1Object |
| 16 |
} |
| 17 |
|
| 18 |
func (s asn1Structured) encodeTo(out *bytes.Buffer) error { |
| 19 |
|
| 20 |
encodeIndent++ |
| 21 |
inner := new(bytes.Buffer) |
| 22 |
for _, obj := range s.content { |
| 23 |
err := obj.encodeTo(inner) |
| 24 |
if err != nil { |
| 25 |
return err |
| 26 |
} |
| 27 |
} |
| 28 |
encodeIndent-- |
| 29 |
out.Write(s.tagBytes) |
| 30 |
encodeLength(out, inner.Len()) |
| 31 |
out.Write(inner.Bytes()) |
| 32 |
return nil |
| 33 |
} |
| 34 |
|
| 35 |
type asn1Primitive struct { |
| 36 |
tagBytes []byte |
| 37 |
length int |
| 38 |
content []byte |
| 39 |
} |
| 40 |
|
| 41 |
func (p asn1Primitive) encodeTo(out *bytes.Buffer) error { |
| 42 |
_, err := out.Write(p.tagBytes) |
| 43 |
if err != nil { |
| 44 |
return err |
| 45 |
} |
| 46 |
if err = encodeLength(out, p.length); err != nil { |
| 47 |
return err |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
out.Write(p.content) |
| 52 |
|
| 53 |
return nil |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
func BER2DER(ber []byte) ([]byte, error) { |
| 58 |
if len(ber) == 0 { |
| 59 |
return nil, ASN1Error{"ber2der: input ber is empty"} |
| 60 |
} |
| 61 |
|
| 62 |
out := new(bytes.Buffer) |
| 63 |
|
| 64 |
obj, _, err := readObject(ber, 0) |
| 65 |
if err != nil { |
| 66 |
return nil, err |
| 67 |
} |
| 68 |
obj.encodeTo(out) |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
return out.Bytes(), nil |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
func marshalLongLength(out *bytes.Buffer, i int) (err error) { |
| 79 |
n := lengthLength(i) |
| 80 |
|
| 81 |
for ; n > 0; n-- { |
| 82 |
err = out.WriteByte(byte(i >> uint((n-1)*8))) |
| 83 |
if err != nil { |
| 84 |
return |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
return nil |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func lengthLength(i int) (numBytes int) { |
| 93 |
numBytes = 1 |
| 94 |
for i > 255 { |
| 95 |
numBytes++ |
| 96 |
i >>= 8 |
| 97 |
} |
| 98 |
return |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
func encodeLength(out *bytes.Buffer, length int) (err error) { |
| 116 |
if length >= 128 { |
| 117 |
l := lengthLength(length) |
| 118 |
err = out.WriteByte(0x80 | byte(l)) |
| 119 |
if err != nil { |
| 120 |
return |
| 121 |
} |
| 122 |
err = marshalLongLength(out, length) |
| 123 |
if err != nil { |
| 124 |
return |
| 125 |
} |
| 126 |
} else { |
| 127 |
err = out.WriteByte(byte(length)) |
| 128 |
if err != nil { |
| 129 |
return |
| 130 |
} |
| 131 |
} |
| 132 |
return |
| 133 |
} |
| 134 |
|
| 135 |
func readObject(ber []byte, offset int) (asn1Object, int, error) { |
| 136 |
|
| 137 |
tagStart := offset |
| 138 |
b := ber[offset] |
| 139 |
offset++ |
| 140 |
tag := b & 0x1F |
| 141 |
if tag == 0x1F { |
| 142 |
tag = 0 |
| 143 |
for ber[offset] >= 0x80 { |
| 144 |
tag = tag*128 + ber[offset] - 0x80 |
| 145 |
offset++ |
| 146 |
} |
| 147 |
tag = tag*128 + ber[offset] - 0x80 |
| 148 |
offset++ |
| 149 |
} |
| 150 |
tagEnd := offset |
| 151 |
|
| 152 |
kind := b & 0x20 |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
var length int |
| 162 |
l := ber[offset] |
| 163 |
offset++ |
| 164 |
indefinite := false |
| 165 |
if l > 0x80 { |
| 166 |
numberOfBytes := (int)(l & 0x7F) |
| 167 |
if numberOfBytes > 4 { |
| 168 |
return nil, 0, ASN1Error{"ber2der: BER tag length too long"} |
| 169 |
} |
| 170 |
if numberOfBytes == 4 && (int)(ber[offset]) > 0x7F { |
| 171 |
return nil, 0, ASN1Error{"ber2der: BER tag length is negative"} |
| 172 |
} |
| 173 |
if 0x0 == (int)(ber[offset]) { |
| 174 |
return nil, 0, ASN1Error{"ber2der: BER tag length has leading zero"} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
for i := 0; i < numberOfBytes; i++ { |
| 179 |
length = length*256 + (int)(ber[offset]) |
| 180 |
offset++ |
| 181 |
} |
| 182 |
} else if l == 0x80 { |
| 183 |
indefinite = true |
| 184 |
} else { |
| 185 |
length = (int)(l) |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
contentEnd := offset + length |
| 190 |
if contentEnd > len(ber) { |
| 191 |
return nil, 0, ASN1Error{"ber2der: BER tag length is more than available data"} |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
var obj asn1Object |
| 197 |
if indefinite && kind == 0 { |
| 198 |
return nil, 0, ASN1Error{"ber2der: Indefinite form tag must have constructed encoding"} |
| 199 |
} |
| 200 |
if kind == 0 { |
| 201 |
obj = asn1Primitive{ |
| 202 |
tagBytes: ber[tagStart:tagEnd], |
| 203 |
length: length, |
| 204 |
content: ber[offset:contentEnd], |
| 205 |
} |
| 206 |
} else { |
| 207 |
var subObjects []asn1Object |
| 208 |
for (offset < contentEnd) || indefinite { |
| 209 |
var subObj asn1Object |
| 210 |
var err error |
| 211 |
subObj, offset, err = readObject(ber, offset) |
| 212 |
if err != nil { |
| 213 |
return nil, 0, err |
| 214 |
} |
| 215 |
subObjects = append(subObjects, subObj) |
| 216 |
|
| 217 |
if indefinite { |
| 218 |
terminated, err := isIndefiniteTermination(ber, offset) |
| 219 |
if err != nil { |
| 220 |
return nil, 0, err |
| 221 |
} |
| 222 |
|
| 223 |
if terminated { |
| 224 |
break |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
obj = asn1Structured{ |
| 229 |
tagBytes: ber[tagStart:tagEnd], |
| 230 |
content: subObjects, |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
if indefinite { |
| 236 |
contentEnd = offset + 2 |
| 237 |
} |
| 238 |
|
| 239 |
return obj, contentEnd, nil |
| 240 |
} |
| 241 |
|
| 242 |
func isIndefiniteTermination(ber []byte, offset int) (bool, error) { |
| 243 |
if len(ber)-offset < 2 { |
| 244 |
return false, ASN1Error{"ber2der: Invalid BER format"} |
| 245 |
} |
| 246 |
|
| 247 |
return bytes.Index(ber[offset:], []byte{0x0, 0x0}) == 0, nil |
| 248 |
} |