| 1 | package pkcs7scip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ |
| 2 | |
| 3 | import ( |
| 4 | "bytesscip-go gomod github.com/golang/go/src go1.14 bytes/" |
| 5 | "errorsscip-go gomod github.com/golang/go/src go1.14 errors/" |
| 6 | ) |
| 7 | |
| 8 | type asn1Objectscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Object# interface { |
| 9 | EncodeToscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Object#EncodeTo.(writer *bytesscip-go gomod github.com/golang/go/src go1.14 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#) error |
| 10 | } |
| 11 | |
| 12 | type asn1Structuredscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Structured# struct { |
| 13 | tagBytesscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Structured#tagBytes. []byte |
| 14 | contentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Structured#content. []asn1Objectscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Object# |
| 15 | } |
| 16 | |
| 17 | func (s asn1Structuredscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Structured#) EncodeToscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Structured#EncodeTo().(out *bytesscip-go gomod github.com/golang/go/src go1.14 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#) error { |
| 18 | inner := new(bytesscip-go gomod github.com/golang/go/src go1.14 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#) |
| 19 | for _, obj := range s.contentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Structured#content. { |
| 20 | err := obj.EncodeToscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Object#EncodeTo.(inner) |
| 21 | if err != nil { |
| 22 | return err |
| 23 | } |
| 24 | } |
| 25 | out.Writescip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#Write().(s.tagBytesscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Structured#tagBytes.) |
| 26 | encodeLengthscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encodeLength().(out, inner.Lenscip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#Len().()) |
| 27 | out.Writescip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#Write().(inner.Bytesscip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#Bytes().()) |
| 28 | return nil |
| 29 | } |
| 30 | |
| 31 | type asn1Primitivescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Primitive# struct { |
| 32 | tagBytesscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Primitive#tagBytes. []byte |
| 33 | lengthscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Primitive#length. int |
| 34 | contentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Primitive#content. []byte |
| 35 | } |
| 36 | |
| 37 | func (p asn1Primitivescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Primitive#) EncodeToscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Primitive#EncodeTo().(out *bytesscip-go gomod github.com/golang/go/src go1.14 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#) error { |
| 38 | _, err := out.Writescip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#Write().(p.tagBytesscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Primitive#tagBytes.) |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | if err = encodeLengthscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encodeLength().(out, p.lengthscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Primitive#length.); err != nil { |
| 43 | return err |
| 44 | } |
| 45 | // fmt.Printf("%s--> tag: % X length: %d\n", strings.Repeat("| ", encodeIndent), p.tagBytes, p.length) |
| 46 | // fmt.Printf("%s--> content length: %d\n", strings.Repeat("| ", encodeIndent), len(p.content)) |
| 47 | out.Writescip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#Write().(p.contentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Primitive#content.) |
| 48 | |
| 49 | return nil |
| 50 | } |
| 51 | |
| 52 | func ber2derscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/ber2der().(ber []byte) ([]byte, error) { |
| 53 | if len(ber) == 0 { |
| 54 | return nil, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: input ber is empty") |
| 55 | } |
| 56 | // fmt.Printf("--> ber2der: Transcoding %d bytes\n", len(ber)) |
| 57 | out := new(bytesscip-go gomod github.com/golang/go/src go1.14 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#) |
| 58 | |
| 59 | obj, _, err := readObjectscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/readObject().(ber, 0) |
| 60 | if err != nil { |
| 61 | return nil, err |
| 62 | } |
| 63 | obj.EncodeToscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Object#EncodeTo.(out) |
| 64 | |
| 65 | // if offset < len(ber) { |
| 66 | // return nil, fmt.Errorf("ber2der: Content longer than expected. Got %d, expected %d", offset, len(ber)) |
| 67 | // } |
| 68 | |
| 69 | return out.Bytesscip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#Bytes().(), nil |
| 70 | } |
| 71 | |
| 72 | // encodes lengths that are longer than 127 into string of bytes |
| 73 | func marshalLongLengthscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/marshalLongLength().(out *bytesscip-go gomod github.com/golang/go/src go1.14 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#, i int) (err error) { |
| 74 | n := lengthLengthscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/lengthLength().(i) |
| 75 | |
| 76 | for ; n > 0; n-- { |
| 77 | err = out.WriteBytescip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#WriteByte().(byte(i >> uint((n-1)*8))) |
| 78 | if err != nil { |
| 79 | return |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return nil |
| 84 | } |
| 85 | |
| 86 | // computes the byte length of an encoded length value |
| 87 | func lengthLengthscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/lengthLength().(i int) (numBytes int) { |
| 88 | numBytes = 1 |
| 89 | for i > 255 { |
| 90 | numBytes++ |
| 91 | i >>= 8 |
| 92 | } |
| 93 | return |
| 94 | } |
| 95 | |
| 96 | // encodes the length in DER format |
| 97 | // If the length fits in 7 bits, the value is encoded directly. |
| 98 | // |
| 99 | // Otherwise, the number of bytes to encode the length is first determined. |
| 100 | // This number is likely to be 4 or less for a 32bit length. This number is |
| 101 | // added to 0x80. The length is encoded in big endian encoding follow after |
| 102 | // |
| 103 | // Examples: |
| 104 | // |
| 105 | // length | byte 1 | bytes n |
| 106 | // 0 | 0x00 | - |
| 107 | // 120 | 0x78 | - |
| 108 | // 200 | 0x81 | 0xC8 |
| 109 | // 500 | 0x82 | 0x01 0xF4 |
| 110 | func encodeLengthscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/encodeLength().(out *bytesscip-go gomod github.com/golang/go/src go1.14 bytes/.Bufferscip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#, length int) (err error) { |
| 111 | if length >= 128 { |
| 112 | l := lengthLengthscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/lengthLength().(length) |
| 113 | err = out.WriteBytescip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#WriteByte().(0x80 | byte(l)) |
| 114 | if err != nil { |
| 115 | return |
| 116 | } |
| 117 | err = marshalLongLengthscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/marshalLongLength().(out, length) |
| 118 | if err != nil { |
| 119 | return |
| 120 | } |
| 121 | } else { |
| 122 | err = out.WriteBytescip-go gomod github.com/golang/go/src go1.14 bytes/Buffer#WriteByte().(byte(length)) |
| 123 | if err != nil { |
| 124 | return |
| 125 | } |
| 126 | } |
| 127 | return |
| 128 | } |
| 129 | |
| 130 | func readObjectscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/readObject().(ber []byte, offset int) (asn1Objectscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Object#, int, error) { |
| 131 | berLen := len(ber) |
| 132 | if offset >= berLen { |
| 133 | return nil, 0, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: offset is after end of ber data") |
| 134 | } |
| 135 | tagStart := offset |
| 136 | b := ber[offset] |
| 137 | offset++ |
| 138 | if offset >= berLen { |
| 139 | return nil, 0, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: cannot move offset forward, end of ber data reached") |
| 140 | } |
| 141 | tag := b & 0x1F // last 5 bits |
| 142 | if tag == 0x1F { |
| 143 | tag = 0 |
| 144 | for ber[offset] >= 0x80 { |
| 145 | tag = tag*128 + ber[offset] - 0x80 |
| 146 | offset++ |
| 147 | if offset >= berLen { |
| 148 | return nil, 0, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: cannot move offset forward, end of ber data reached") |
| 149 | } |
| 150 | } |
| 151 | // jvehent 20170227: this doesn't appear to be used anywhere... |
| 152 | // tag = tag*128 + ber[offset] - 0x80 |
| 153 | offset++ |
| 154 | if offset >= berLen { |
| 155 | return nil, 0, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: cannot move offset forward, end of ber data reached") |
| 156 | } |
| 157 | } |
| 158 | tagEnd := offset |
| 159 | |
| 160 | kind := b & 0x20 |
| 161 | if kind == 0 { |
| 162 | debugprintscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/debugprint().("--> Primitive\n") |
| 163 | } else { |
| 164 | debugprintscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/debugprint().("--> Constructed\n") |
| 165 | } |
| 166 | // read length |
| 167 | var length int |
| 168 | l := ber[offset] |
| 169 | offset++ |
| 170 | if offset > berLen { |
| 171 | return nil, 0, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: cannot move offset forward, end of ber data reached") |
| 172 | } |
| 173 | indefinite := false |
| 174 | if l > 0x80 { |
| 175 | numberOfBytes := (int)(l & 0x7F) |
| 176 | if numberOfBytes > 4 { // int is only guaranteed to be 32bit |
| 177 | return nil, 0, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: BER tag length too long") |
| 178 | } |
| 179 | if offset+numberOfBytes > berLen { |
| 180 | return nil, 0, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: BER tag length is more than available data") |
| 181 | } |
| 182 | if numberOfBytes == 4 && (int)(ber[offset]) > 0x7F { |
| 183 | return nil, 0, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: BER tag length is negative") |
| 184 | } |
| 185 | if (int)(ber[offset]) == 0x0 { |
| 186 | return nil, 0, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: BER tag length has leading zero") |
| 187 | } |
| 188 | debugprintscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/debugprint().("--> (compute length) indicator byte: %x\n", l) |
| 189 | debugprintscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/debugprint().("--> (compute length) length bytes: % X\n", ber[offset:offset+numberOfBytes]) |
| 190 | for i := 0; i < numberOfBytes; i++ { |
| 191 | length = length*256 + (int)(ber[offset]) |
| 192 | offset++ |
| 193 | if offset > berLen { |
| 194 | return nil, 0, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: cannot move offset forward, end of ber data reached") |
| 195 | } |
| 196 | } |
| 197 | } else if l == 0x80 { |
| 198 | indefinite = true |
| 199 | } else { |
| 200 | length = (int)(l) |
| 201 | } |
| 202 | if length < 0 { |
| 203 | return nil, 0, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: invalid negative value found in BER tag length") |
| 204 | } |
| 205 | // fmt.Printf("--> length : %d\n", length) |
| 206 | contentEnd := offset + length |
| 207 | if contentEnd > len(ber) { |
| 208 | return nil, 0, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: BER tag length is more than available data") |
| 209 | } |
| 210 | debugprintscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/debugprint().("--> content start : %d\n", offset) |
| 211 | debugprintscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/debugprint().("--> content end : %d\n", contentEnd) |
| 212 | debugprintscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/debugprint().("--> content : % X\n", ber[offset:contentEnd]) |
| 213 | var obj asn1Objectscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Object# |
| 214 | if indefinite && kind == 0 { |
| 215 | return nil, 0, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: Indefinite form tag must have constructed encoding") |
| 216 | } |
| 217 | if kind == 0 { |
| 218 | obj = asn1Primitivescip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Primitive#{ |
| 219 | tagBytesscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Primitive#tagBytes.: ber[tagStart:tagEnd], |
| 220 | lengthscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Primitive#length.: length, |
| 221 | contentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Primitive#content.: ber[offset:contentEnd], |
| 222 | } |
| 223 | } else { |
| 224 | var subObjects []asn1Objectscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Object# |
| 225 | for (offset < contentEnd) || indefinite { |
| 226 | var subObj asn1Objectscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Object# |
| 227 | var err error |
| 228 | subObj, offset, err = readObjectscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/readObject().(ber, offset) |
| 229 | if err != nil { |
| 230 | return nil, 0, err |
| 231 | } |
| 232 | subObjects = append(subObjects, subObj) |
| 233 | |
| 234 | if indefinite { |
| 235 | terminated, err := isIndefiniteTerminationscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/isIndefiniteTermination().(ber, offset) |
| 236 | if err != nil { |
| 237 | return nil, 0, err |
| 238 | } |
| 239 | |
| 240 | if terminated { |
| 241 | break |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | obj = asn1Structuredscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Structured#{ |
| 246 | tagBytesscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Structured#tagBytes.: ber[tagStart:tagEnd], |
| 247 | contentscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/asn1Structured#content.: subObjects, |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // Apply indefinite form length with 0x0000 terminator. |
| 252 | if indefinite { |
| 253 | contentEnd = offset + 2 |
| 254 | } |
| 255 | |
| 256 | return obj, contentEnd, nil |
| 257 | } |
| 258 | |
| 259 | func isIndefiniteTerminationscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/isIndefiniteTermination().(ber []byte, offset int) (bool, error) { |
| 260 | if len(ber)-offset < 2 { |
| 261 | return false, errorsscip-go gomod github.com/golang/go/src go1.14 errors/.Newscip-go gomod github.com/golang/go/src go1.14 errors/New().("ber2der: Invalid BER format") |
| 262 | } |
| 263 | |
| 264 | return bytesscip-go gomod github.com/golang/go/src go1.14 bytes/.Indexscip-go gomod github.com/golang/go/src go1.14 bytes/Index().(ber[offset:], []byte{0x0, 0x0}) == 0, nil |
| 265 | } |
| 266 | |
| 267 | func debugprintscip-go gomod github.com/smallstep/pkcs7 . `github.com/smallstep/pkcs7`/debugprint().(format string, a ...interface{}) { |
| 268 | // fmt.Printf(format, a) |
| 269 | } |