| 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 | "crypto" |
| 21 | "crypto/rand" |
| 22 | "crypto/x509" |
| 23 | "crypto/x509/pkix" |
| 24 | "encoding/asn1" |
| 25 | "errors" |
| 26 | "fmt" |
| 27 | |
| 28 | "github.com/sassoftware/relic/v7/lib/x509tools" |
| 29 | ) |
| 30 | |
| 31 | type SignatureBuilder struct { |
| 32 | contentInfo ContentInfo |
| 33 | digest []byte |
| 34 | certs []*x509.Certificate |
| 35 | privateKey crypto.Signer |
| 36 | signerOpts crypto.SignerOpts |
| 37 | authAttrs AttributeList |
| 38 | } |
| 39 | |
| 40 | // Build a PKCS#7 signature procedurally. Returns a structure that can have |
| 41 | // content and attributes attached to it. |
| 42 | func NewBuilder(privKey crypto.Signer, certs []*x509.Certificate, opts crypto.SignerOpts) *SignatureBuilder { |
| 43 | return &SignatureBuilder{ |
| 44 | privateKey: privKey, |
| 45 | signerOpts: opts, |
| 46 | certs: certs, |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Embed bytes or a structure into the PKCS#7 content |
| 51 | func (sb *SignatureBuilder) SetContent(ctype asn1.ObjectIdentifier, data interface{}) error { |
| 52 | cinfo, err := NewContentInfo(ctype, data) |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | return sb.SetContentInfo(cinfo) |
| 57 | } |
| 58 | |
| 59 | // Set content to a generic "data" blob |
| 60 | func (sb *SignatureBuilder) SetContentData(data []byte) error { |
| 61 | return sb.SetContent(OidData, data) |
| 62 | } |
| 63 | |
| 64 | // Set a ContentInfo structure as the PKCS#7 content |
| 65 | func (sb *SignatureBuilder) SetContentInfo(cinfo ContentInfo) error { |
| 66 | blob, err := cinfo.Bytes() |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | d := sb.signerOpts.HashFunc().New() |
| 71 | d.Write(blob) |
| 72 | sb.contentInfo = cinfo |
| 73 | sb.digest = d.Sum(nil) |
| 74 | return nil |
| 75 | } |
| 76 | |
| 77 | // Set a "detached" content type, with digest |
| 78 | func (sb *SignatureBuilder) SetDetachedContent(ctype asn1.ObjectIdentifier, digest []byte) error { |
| 79 | if len(digest) != sb.signerOpts.HashFunc().Size() { |
| 80 | return errors.New("digest size mismatch") |
| 81 | } |
| 82 | cinfo, _ := NewContentInfo(ctype, nil) |
| 83 | sb.contentInfo = cinfo |
| 84 | sb.digest = digest |
| 85 | return nil |
| 86 | } |
| 87 | |
| 88 | // Add an authenticated attribute to SignerInfo |
| 89 | func (sb *SignatureBuilder) AddAuthenticatedAttribute(oid asn1.ObjectIdentifier, data interface{}) error { |
| 90 | return sb.authAttrs.Add(oid, data) |
| 91 | } |
| 92 | |
| 93 | // Complete the signature and return the full PKCS#7 structure |
| 94 | func (sb *SignatureBuilder) Sign() (*ContentInfoSignedData, error) { |
| 95 | if sb.digest == nil { |
| 96 | return nil, errors.New("SetContent was not called") |
| 97 | } |
| 98 | pubKey := sb.privateKey.Public() |
| 99 | digestAlg, pkeyAlg, err := x509tools.PkixAlgorithms(pubKey, sb.signerOpts) |
| 100 | if err != nil { |
| 101 | return nil, fmt.Errorf("pkcs7: %w", err) |
| 102 | } |
| 103 | if len(sb.certs) < 1 || !x509tools.SameKey(pubKey, sb.certs[0].PublicKey) { |
| 104 | return nil, errors.New("pkcs7: first certificate must match private key") |
| 105 | } |
| 106 | digest := sb.digest |
| 107 | if sb.authAttrs != nil { |
| 108 | // When authenticated attributes are present, then these are required. |
| 109 | if err := sb.authAttrs.Add(OidAttributeContentType, sb.contentInfo.ContentType); err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | if err := sb.authAttrs.Add(OidAttributeMessageDigest, sb.digest); err != nil { |
| 113 | return nil, err |
| 114 | } |
| 115 | // Now the signature is over the authenticated attributes instead of |
| 116 | // the content directly. |
| 117 | attrbytes, err := sb.authAttrs.Bytes() |
| 118 | if err != nil { |
| 119 | return nil, err |
| 120 | } |
| 121 | w := sb.signerOpts.HashFunc().New() |
| 122 | w.Write(attrbytes) |
| 123 | digest = w.Sum(nil) |
| 124 | } |
| 125 | sig, err := sb.privateKey.Sign(rand.Reader, digest, sb.signerOpts) |
| 126 | if err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | return &ContentInfoSignedData{ |
| 130 | ContentType: OidSignedData, |
| 131 | Content: SignedData{ |
| 132 | Version: 1, |
| 133 | DigestAlgorithmIdentifiers: []pkix.AlgorithmIdentifier{digestAlg}, |
| 134 | ContentInfo: sb.contentInfo, |
| 135 | Certificates: marshalCertificates(sb.certs), |
| 136 | CRLs: nil, |
| 137 | SignerInfos: []SignerInfo{{ |
| 138 | Version: 1, |
| 139 | IssuerAndSerialNumber: IssuerAndSerial{ |
| 140 | IssuerName: asn1.RawValue{FullBytes: sb.certs[0].RawIssuer}, |
| 141 | SerialNumber: sb.certs[0].SerialNumber, |
| 142 | }, |
| 143 | DigestAlgorithm: digestAlg, |
| 144 | DigestEncryptionAlgorithm: pkeyAlg, |
| 145 | AuthenticatedAttributes: sb.authAttrs, |
| 146 | EncryptedDigest: sig, |
| 147 | }}, |
| 148 | }, |
| 149 | }, nil |
| 150 | } |