| 1 | // |
| 2 | // Copyright (c) SAS Institute Inc. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
| 16 | |
| 17 | package pkcs7 |
| 18 | |
| 19 | import ( |
| 20 | "bytes" |
| 21 | "crypto/x509" |
| 22 | "encoding/asn1" |
| 23 | "errors" |
| 24 | "fmt" |
| 25 | "time" |
| 26 | ) |
| 27 | |
| 28 | // Parse a signature from bytes |
| 29 | func Unmarshal(blob []byte) (*ContentInfoSignedData, error) { |
| 30 | psd := new(ContentInfoSignedData) |
| 31 | if rest, err := asn1.Unmarshal(blob, psd); err != nil { |
| 32 | return nil, err |
| 33 | } else if len(bytes.TrimRight(rest, "\x00")) != 0 { |
| 34 | return nil, errors.New("pkcs7: trailing garbage after PKCS#7 structure") |
| 35 | } |
| 36 | return psd, nil |
| 37 | } |
| 38 | |
| 39 | // Marshal the signature to bytes |
| 40 | func (psd *ContentInfoSignedData) Marshal() ([]byte, error) { |
| 41 | return asn1.Marshal(*psd) |
| 42 | } |
| 43 | |
| 44 | // Remove and return inlined content from the document, leaving a detached signature |
| 45 | func (psd *ContentInfoSignedData) Detach() ([]byte, error) { |
| 46 | content, err := psd.Content.ContentInfo.Bytes() |
| 47 | if err != nil { |
| 48 | return nil, fmt.Errorf("pkcs7: %w", err) |
| 49 | } |
| 50 | psd.Content.ContentInfo, _ = NewContentInfo(psd.Content.ContentInfo.ContentType, nil) |
| 51 | return content, nil |
| 52 | } |
| 53 | |
| 54 | // dump raw certificates to structure |
| 55 | func marshalCertificates(certs []*x509.Certificate) RawCertificates { |
| 56 | c := make(RawCertificates, len(certs)) |
| 57 | for i, cert := range certs { |
| 58 | c[i] = asn1.RawValue{FullBytes: cert.Raw} |
| 59 | } |
| 60 | return c |
| 61 | } |
| 62 | |
| 63 | // Parse raw certificates from structure. If any cert is invalid, the remaining valid certs are returned along with a CertificateError. |
| 64 | func (raw RawCertificates) Parse() ([]*x509.Certificate, error) { |
| 65 | var invalid CertificateError |
| 66 | var certs []*x509.Certificate |
| 67 | for _, rawCert := range raw { |
| 68 | cert, err := x509.ParseCertificate(rawCert.FullBytes) |
| 69 | if err != nil { |
| 70 | invalid.Invalid = append(invalid.Invalid, rawCert.FullBytes) |
| 71 | invalid.Err = err |
| 72 | } else { |
| 73 | certs = append(certs, cert) |
| 74 | } |
| 75 | } |
| 76 | if invalid.Err != nil { |
| 77 | return certs, invalid |
| 78 | } |
| 79 | return certs, nil |
| 80 | } |
| 81 | |
| 82 | type CertificateError struct { |
| 83 | Invalid [][]byte |
| 84 | Err error |
| 85 | } |
| 86 | |
| 87 | func (c CertificateError) Error() string { |
| 88 | return c.Err.Error() |
| 89 | } |
| 90 | |
| 91 | func (c CertificateError) Unwrap() error { |
| 92 | return c.Err |
| 93 | } |
| 94 | |
| 95 | // ParseTime parses a GeneralizedTime or UTCTime value that potentially has a fractional seconds part |
| 96 | func ParseTime(raw asn1.RawValue) (ret time.Time, err error) { |
| 97 | // as of go 1.12 fractional timestamps fail to parse with a "did not serialize back to the original value" error, so this implementation without the serialize check is needed |
| 98 | s := string(raw.Bytes) |
| 99 | switch raw.Tag { |
| 100 | case asn1.TagGeneralizedTime: |
| 101 | formatStr := "20060102150405Z0700" |
| 102 | return time.Parse(formatStr, s) |
| 103 | case asn1.TagUTCTime: |
| 104 | formatStr := "0601021504Z0700" |
| 105 | ret, err = time.Parse(formatStr, s) |
| 106 | if err != nil { |
| 107 | formatStr = "060102150405Z0700" |
| 108 | ret, err = time.Parse(formatStr, s) |
| 109 | } |
| 110 | return |
| 111 | default: |
| 112 | err = fmt.Errorf("unknown tag %d in timestamp field", raw.Tag) |
| 113 | return |
| 114 | } |
| 115 | } |