| 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 pkcs7scip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/ |
| 18 | |
| 19 | import ( |
| 20 | "bytesscip-go gomod github.com/golang/go/src go1.18 bytes/" |
| 21 | "crypto/x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/" |
| 22 | "encoding/asn1scip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/" |
| 23 | "errorsscip-go gomod github.com/golang/go/src go1.18 errors/" |
| 24 | "fmtscip-go gomod github.com/golang/go/src go1.18 fmt/" |
| 25 | "timescip-go gomod github.com/golang/go/src go1.18 time/" |
| 26 | ) |
| 27 | |
| 28 | // Parse a signature from bytes |
| 29 | func Unmarshalscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/Unmarshal().(blob []byte) (*ContentInfoSignedDatascip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/ContentInfoSignedData#, error) { |
| 30 | psd := new(ContentInfoSignedDatascip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/ContentInfoSignedData#) |
| 31 | if rest, err := asn1scip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/.Unmarshalscip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/Unmarshal().(blob, psd); err != nil { |
| 32 | return nil, err |
| 33 | } else if len(bytesscip-go gomod github.com/golang/go/src go1.18 bytes/.TrimRightscip-go gomod github.com/golang/go/src go1.18 bytes/TrimRight().(rest, "\x00")) != 0 { |
| 34 | return nil, errorsscip-go gomod github.com/golang/go/src go1.18 errors/.Newscip-go gomod github.com/golang/go/src go1.18 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 *ContentInfoSignedDatascip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/ContentInfoSignedData#) Marshalscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/ContentInfoSignedData#Marshal().() ([]byte, error) { |
| 41 | return asn1scip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/.Marshalscip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/Marshal().(*psd) |
| 42 | } |
| 43 | |
| 44 | // Remove and return inlined content from the document, leaving a detached signature |
| 45 | func (psd *ContentInfoSignedDatascip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/ContentInfoSignedData#) Detachscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/ContentInfoSignedData#Detach().() ([]byte, error) { |
| 46 | content, err := psd.Contentscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/ContentInfoSignedData#Content..ContentInfoscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/SignedData#ContentInfo..Bytesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/ContentInfo#Bytes().() |
| 47 | if err != nil { |
| 48 | return nil, fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("pkcs7: %w", err) |
| 49 | } |
| 50 | psd.Contentscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/ContentInfoSignedData#Content..ContentInfoscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/SignedData#ContentInfo., _ = NewContentInfoscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/NewContentInfo().(psd.Contentscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/ContentInfoSignedData#Content..ContentInfoscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/SignedData#ContentInfo..ContentTypescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/ContentInfo#ContentType., nil) |
| 51 | return content, nil |
| 52 | } |
| 53 | |
| 54 | // dump raw certificates to structure |
| 55 | func marshalCertificatesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/marshalCertificates().(certs []*x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/Certificate#) RawCertificatesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/RawCertificates# { |
| 56 | c := make(RawCertificatesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/RawCertificates#, len(certs)) |
| 57 | for i, cert := range certs { |
| 58 | c[i] = asn1scip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/RawValue#{FullBytesscip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/RawValue#FullBytes.: cert.Rawscip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/Certificate#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 RawCertificatesscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/RawCertificates#) Parsescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/RawCertificates#Parse().() ([]*x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/Certificate#, error) { |
| 65 | var invalid CertificateErrorscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError# |
| 66 | var certs []*x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/.Certificatescip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/Certificate# |
| 67 | for _, rawCert := range raw { |
| 68 | cert, err := x509scip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/.ParseCertificatescip-go gomod github.com/golang/go/src go1.18 `crypto/x509`/ParseCertificate().(rawCert.FullBytesscip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/RawValue#FullBytes.) |
| 69 | if err != nil { |
| 70 | invalid.Invalidscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError#Invalid. = append(invalid.Invalidscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError#Invalid., rawCert.FullBytesscip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/RawValue#FullBytes.) |
| 71 | invalid.Errscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError#Err. = err |
| 72 | } else { |
| 73 | certs = append(certs, cert) |
| 74 | } |
| 75 | } |
| 76 | if invalid.Errscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError#Err. != nil { |
| 77 | return certs, invalid |
| 78 | } |
| 79 | return certs, nil |
| 80 | } |
| 81 | |
| 82 | type CertificateErrorscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError# struct { |
| 83 | Invalidscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError#Invalid. [][]byte |
| 84 | Errscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError#Err. error |
| 85 | } |
| 86 | |
| 87 | func (c CertificateErrorscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError#) Errorscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError#Error().() string { |
| 88 | return c.Errscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError#Err..Error() |
| 89 | } |
| 90 | |
| 91 | func (c CertificateErrorscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError#) Unwrapscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError#Unwrap().() error { |
| 92 | return c.Errscip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/CertificateError#Err. |
| 93 | } |
| 94 | |
| 95 | // ParseTime parses a GeneralizedTime or UTCTime value that potentially has a fractional seconds part |
| 96 | func ParseTimescip-go gomod github.com/sassoftware/relic/v7 . `github.com/sassoftware/relic/v7/lib/pkcs7`/ParseTime().(raw asn1scip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/.RawValuescip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/RawValue#) (ret timescip-go gomod github.com/golang/go/src go1.18 time/.Timescip-go gomod github.com/golang/go/src go1.18 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.Bytesscip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/RawValue#Bytes.) |
| 99 | switch raw.Tagscip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/RawValue#Tag. { |
| 100 | case asn1scip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/.TagGeneralizedTimescip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/TagGeneralizedTime.: |
| 101 | formatStr := "20060102150405Z0700" |
| 102 | return timescip-go gomod github.com/golang/go/src go1.18 time/.Parsescip-go gomod github.com/golang/go/src go1.18 time/Parse().(formatStr, s) |
| 103 | case asn1scip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/.TagUTCTimescip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/TagUTCTime.: |
| 104 | formatStr := "0601021504Z0700" |
| 105 | ret, err = timescip-go gomod github.com/golang/go/src go1.18 time/.Parsescip-go gomod github.com/golang/go/src go1.18 time/Parse().(formatStr, s) |
| 106 | if err != nil { |
| 107 | formatStr = "060102150405Z0700" |
| 108 | ret, err = timescip-go gomod github.com/golang/go/src go1.18 time/.Parsescip-go gomod github.com/golang/go/src go1.18 time/Parse().(formatStr, s) |
| 109 | } |
| 110 | return |
| 111 | default: |
| 112 | err = fmtscip-go gomod github.com/golang/go/src go1.18 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.18 fmt/Errorf().("unknown tag %d in timestamp field", raw.Tagscip-go gomod github.com/golang/go/src go1.18 `encoding/asn1`/RawValue#Tag.) |
| 113 | return |
| 114 | } |
| 115 | } |