|
| 1 | +package signature |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto" |
| 6 | + "crypto/x509" |
| 7 | + "encoding/pem" |
| 8 | + |
| 9 | + cms "github.com/github/smimesign/ietf-cms" |
| 10 | + "github.com/pkg/errors" |
| 11 | +) |
| 12 | + |
| 13 | +type SignOptions struct { |
| 14 | + // Make a detached signature |
| 15 | + Detached bool |
| 16 | + // URL of RFC3161 timestamp authority to use for timestamping |
| 17 | + TimestampAuthority string |
| 18 | + // Create ascii armored output |
| 19 | + Armor bool |
| 20 | + // IncludeCerts specifies what certs to include in the resulting signature. |
| 21 | + // -3 is the same as -2, but ommits issuer when cert has Authority Information Access extension. |
| 22 | + // -2 includes all certs except root. |
| 23 | + // -1 includes all certs. |
| 24 | + // 0 includes no certs. |
| 25 | + // 1 includes leaf cert. |
| 26 | + // >1 includes n from the leaf. |
| 27 | + IncludeCerts int |
| 28 | +} |
| 29 | + |
| 30 | +// Identity is a copy of smimesign.Identity to allow for compatibility without |
| 31 | +// needing a dependency on the whole package. This can be removed once |
| 32 | +// https://github.com/github/smimesign/pull/108 is merged. |
| 33 | +type Identity interface { |
| 34 | + // Certificate gets the identity's certificate. |
| 35 | + Certificate() (*x509.Certificate, error) |
| 36 | + // CertificateChain attempts to get the identity's full certificate chain. |
| 37 | + CertificateChain() ([]*x509.Certificate, error) |
| 38 | + // Signer gets a crypto.Signer that uses the identity's private key. |
| 39 | + Signer() (crypto.Signer, error) |
| 40 | + // Delete deletes this identity from the system. |
| 41 | + Delete() error |
| 42 | + // Close any manually managed memory held by the Identity. |
| 43 | + Close() |
| 44 | +} |
| 45 | + |
| 46 | +// Sign signs a given payload for the given identity. |
| 47 | +// The resulting signature and cert used is returned. |
| 48 | +func Sign(ident Identity, body []byte, opts SignOptions) ([]byte, *x509.Certificate, error) { |
| 49 | + cert, err := ident.Certificate() |
| 50 | + if err != nil { |
| 51 | + return nil, nil, errors.Wrap(err, "failed to get idenity certificate") |
| 52 | + } |
| 53 | + signer, err := ident.Signer() |
| 54 | + if err != nil { |
| 55 | + return nil, nil, errors.Wrap(err, "failed to get idenity signer") |
| 56 | + } |
| 57 | + |
| 58 | + sd, err := cms.NewSignedData(body) |
| 59 | + if err != nil { |
| 60 | + return nil, nil, errors.Wrap(err, "failed to create signed data") |
| 61 | + } |
| 62 | + |
| 63 | + if err := sd.Sign([]*x509.Certificate{cert}, signer); err != nil { |
| 64 | + return nil, nil, errors.Wrap(err, "failed to sign message") |
| 65 | + } |
| 66 | + if opts.Detached { |
| 67 | + sd.Detached() |
| 68 | + } |
| 69 | + |
| 70 | + if len(opts.TimestampAuthority) > 0 { |
| 71 | + if err = sd.AddTimestamps(opts.TimestampAuthority); err != nil { |
| 72 | + return nil, nil, errors.Wrap(err, "failed to add timestamp") |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + chain, err := ident.CertificateChain() |
| 77 | + if err != nil { |
| 78 | + return nil, nil, errors.Wrap(err, "failed to get idenity certificate chain") |
| 79 | + } |
| 80 | + if chain, err = certsForSignature(chain, opts.IncludeCerts); err != nil { |
| 81 | + return nil, nil, err |
| 82 | + } |
| 83 | + if err := sd.SetCertificates(chain); err != nil { |
| 84 | + return nil, nil, errors.Wrap(err, "failed to set certificates") |
| 85 | + } |
| 86 | + |
| 87 | + der, err := sd.ToDER() |
| 88 | + if err != nil { |
| 89 | + return nil, nil, errors.Wrap(err, "failed to serialize signature") |
| 90 | + } |
| 91 | + |
| 92 | + if opts.Armor { |
| 93 | + return pem.EncodeToMemory(&pem.Block{ |
| 94 | + Type: "SIGNED MESSAGE", |
| 95 | + Bytes: der, |
| 96 | + }), cert, nil |
| 97 | + } else { |
| 98 | + return der, cert, nil |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// certsForSignature determines which certificates to include in the signature |
| 103 | +// based on the --include-certs option specified by the user. |
| 104 | +func certsForSignature(chain []*x509.Certificate, include int) ([]*x509.Certificate, error) { |
| 105 | + if include < -3 { |
| 106 | + include = -2 // default |
| 107 | + } |
| 108 | + if include > len(chain) { |
| 109 | + include = len(chain) |
| 110 | + } |
| 111 | + |
| 112 | + switch include { |
| 113 | + case -3: |
| 114 | + for i := len(chain) - 1; i > 0; i-- { |
| 115 | + issuer, cert := chain[i], chain[i-1] |
| 116 | + |
| 117 | + // remove issuer when cert has AIA extension |
| 118 | + if bytes.Equal(issuer.RawSubject, cert.RawIssuer) && len(cert.IssuingCertificateURL) > 0 { |
| 119 | + chain = chain[0:i] |
| 120 | + } |
| 121 | + } |
| 122 | + return chainWithoutRoot(chain), nil |
| 123 | + case -2: |
| 124 | + return chainWithoutRoot(chain), nil |
| 125 | + case -1: |
| 126 | + return chain, nil |
| 127 | + default: |
| 128 | + return chain[0:include], nil |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// Returns the provided chain, having removed the root certificate, if present. |
| 133 | +// This includes removing the cert itself if the chain is a single self-signed |
| 134 | +// cert. |
| 135 | +func chainWithoutRoot(chain []*x509.Certificate) []*x509.Certificate { |
| 136 | + if len(chain) == 0 { |
| 137 | + return chain |
| 138 | + } |
| 139 | + |
| 140 | + lastIdx := len(chain) - 1 |
| 141 | + last := chain[lastIdx] |
| 142 | + |
| 143 | + if bytes.Equal(last.RawIssuer, last.RawSubject) { |
| 144 | + return chain[0:lastIdx] |
| 145 | + } |
| 146 | + |
| 147 | + return chain |
| 148 | +} |
0 commit comments