| 1 |
package attributecert |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/ed25519" |
| 7 |
"crypto/elliptic" |
| 8 |
"crypto/rand" |
| 9 |
"crypto/rsa" |
| 10 |
"crypto/x509" |
| 11 |
"crypto/x509/pkix" |
| 12 |
"encoding/asn1" |
| 13 |
"errors" |
| 14 |
"fmt" |
| 15 |
"io" |
| 16 |
"math/big" |
| 17 |
"time" |
| 18 |
) |
| 19 |
|
| 20 |
// signingParamsForKey returns the signature algorithm and its Algorithm |
| 21 |
// Identifier to use for signing, based on the key type. |
| 22 |
// Shamelessly adapted from stdlib x509.go |
| 23 |
func signingParamsForKey(key crypto.Signer) (*signatureAlgorithmDetail, error) { |
| 24 |
var pubType x509.PublicKeyAlgorithm |
| 25 |
var sigAlgo x509.SignatureAlgorithm |
| 26 |
|
| 27 |
switch pub := key.Public().(type) { |
| 28 |
case *rsa.PublicKey: |
| 29 |
pubType = x509.RSA |
| 30 |
sigAlgo = x509.SHA256WithRSA |
| 31 |
|
| 32 |
case *ecdsa.PublicKey: |
| 33 |
pubType = x509.ECDSA |
| 34 |
switch pub.Curve { |
| 35 |
case elliptic.P224(), elliptic.P256(): |
| 36 |
sigAlgo = x509.ECDSAWithSHA256 |
| 37 |
case elliptic.P384(): |
| 38 |
sigAlgo = x509.ECDSAWithSHA384 |
| 39 |
case elliptic.P521(): |
| 40 |
sigAlgo = x509.ECDSAWithSHA512 |
| 41 |
default: |
| 42 |
return nil, errors.New("x509: unsupported elliptic curve") |
| 43 |
} |
| 44 |
|
| 45 |
case ed25519.PublicKey: |
| 46 |
pubType = x509.Ed25519 |
| 47 |
sigAlgo = x509.PureEd25519 |
| 48 |
|
| 49 |
default: |
| 50 |
return nil, errors.New("x509: only RSA, ECDSA and Ed25519 keys supported") |
| 51 |
} |
| 52 |
|
| 53 |
for _, details := range signatureAlgorithmDetails { |
| 54 |
if details.algo == sigAlgo { |
| 55 |
if details.pubKeyAlgo != pubType { |
| 56 |
return nil, errors.New("x509: requested SignatureAlgorithm does not match private key type") |
| 57 |
} |
| 58 |
if details.hash == crypto.MD5 { |
| 59 |
return nil, errors.New("x509: signing with MD5 is not supported") |
| 60 |
} |
| 61 |
|
| 62 |
return &details, nil |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
return nil, errors.New("x509: unknown SignatureAlgorithm") |
| 67 |
} |
| 68 |
|
| 69 |
// Based on signTBS() from stdlib x509.go |
| 70 |
func signTBS(tbs []byte, key crypto.Signer, sigDetails signatureAlgorithmDetail, rand io.Reader) ([]byte, error) { |
| 71 |
hashFunc := sigDetails.hash.HashFunc() |
| 72 |
|
| 73 |
var opts crypto.SignerOpts = hashFunc |
| 74 |
if sigDetails.isRSAPSS { |
| 75 |
opts = &rsa.PSSOptions{ |
| 76 |
SaltLength: rsa.PSSSaltLengthEqualsHash, |
| 77 |
Hash: hashFunc, |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
if hashFunc != 0 { |
| 82 |
h := hashFunc.New() |
| 83 |
if _, err := h.Write(tbs); err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
tbs = h.Sum(nil) |
| 87 |
} |
| 88 |
return key.Sign(rand, tbs, opts) |
| 89 |
} |
| 90 |
|
| 91 |
// nameToGeneralNames converts a x509 Name (as raw bytes) to a GeneralNames (as |
| 92 |
// a asn1.RawValue). |
| 93 |
func nameToGeneralNames(name []byte) (*asn1.RawValue, error) { |
| 94 |
gn := asn1.RawValue{ |
| 95 |
Class: asn1.ClassContextSpecific, |
| 96 |
Tag: 4, |
| 97 |
IsCompound: true, |
| 98 |
Bytes: name, |
| 99 |
} |
| 100 |
gnBytes, err := asn1.Marshal(gn) |
| 101 |
if err != nil { |
| 102 |
return nil, fmt.Errorf("failed to marshal general name: %w", err) |
| 103 |
} |
| 104 |
|
| 105 |
issuer := asn1.RawValue{ |
| 106 |
Class: asn1.ClassUniversal, |
| 107 |
Tag: asn1.TagSequence, |
| 108 |
IsCompound: true, |
| 109 |
Bytes: gnBytes, |
| 110 |
} |
| 111 |
return &issuer, nil |
| 112 |
} |
| 113 |
|
| 114 |
// CreateAttributeCertificate creates a new attribute certificate. |
| 115 |
// `holderIssuerName` is the DER encoded x509 Name of the holder issuer. |
| 116 |
// |
| 117 |
// Currently this only supports creating an attribute certificate with the bare minimum |
| 118 |
// fields to get it to parse and pass tests, it doesn't populate any attributes or extensions. |
| 119 |
func CreateAttributeCertificate(holderIssuerName []byte, holderSerial *big.Int, notBefore, notAfter time.Time, signingCert *x509.Certificate, priv any) ([]byte, error) { |
| 120 |
if len(holderIssuerName) == 0 { |
| 121 |
return nil, errors.New("holder issuer name cannot be empty") |
| 122 |
} |
| 123 |
if holderSerial == nil || holderSerial.Cmp(big.NewInt(0)) == 0 { |
| 124 |
return nil, errors.New("subject certificate serial number cannot be nil") |
| 125 |
} |
| 126 |
if priv == nil { |
| 127 |
return nil, errors.New("signer cannot be nil") |
| 128 |
} |
| 129 |
signer, ok := priv.(crypto.Signer) |
| 130 |
if !ok { |
| 131 |
return nil, errors.New("x509: certificate private key does not implement crypto.Signer") |
| 132 |
} |
| 133 |
if signingCert == nil { |
| 134 |
return nil, errors.New("signing certificate cannot be nil") |
| 135 |
} |
| 136 |
var serialNumber *big.Int |
| 137 |
// Generate a serial number following RFC 5280, Section 4.1.2.2 if one |
| 138 |
// is not provided. The serial number must be positive and at most 20 |
| 139 |
// octets *when encoded*. |
| 140 |
serialBytes := make([]byte, 20) |
| 141 |
if _, err := io.ReadFull(rand.Reader, serialBytes); err != nil { |
| 142 |
return nil, fmt.Errorf("failed to generate random serial number: %w", err) |
| 143 |
} |
| 144 |
// If the top bit is set, the serial will be padded with a leading zero |
| 145 |
// byte during encoding, so that it's not interpreted as a negative |
| 146 |
// integer. This padding would make the serial 21 octets so we clear the |
| 147 |
// top bit to ensure the correct length in all cases. |
| 148 |
serialBytes[0] &= 0b0111_1111 |
| 149 |
serialNumber = new(big.Int).SetBytes(serialBytes) |
| 150 |
|
| 151 |
// Determine the signature algorithm |
| 152 |
sigDetails, err := signingParamsForKey(signer) |
| 153 |
if err != nil { |
| 154 |
return nil, fmt.Errorf("failed to determine signature algorithm: %w", err) |
| 155 |
} |
| 156 |
algID := pkix.AlgorithmIdentifier{ |
| 157 |
Algorithm: sigDetails.oid, |
| 158 |
Parameters: sigDetails.params, |
| 159 |
} |
| 160 |
|
| 161 |
holderIssuer, err := nameToGeneralNames(holderIssuerName) |
| 162 |
if err != nil { |
| 163 |
return nil, fmt.Errorf("failed to create holder issuer: %w", err) |
| 164 |
} |
| 165 |
|
| 166 |
issuer, err := nameToGeneralNames(signingCert.RawSubject) |
| 167 |
if err != nil { |
| 168 |
return nil, fmt.Errorf("failed to create issuer: %w", err) |
| 169 |
} |
| 170 |
|
| 171 |
tbsCert := tbsAttributeCertificate{ |
| 172 |
Version: 1, // v2 |
| 173 |
SerialNumber: serialNumber, |
| 174 |
SignatureAlgorithm: algID, |
| 175 |
Holder: holder{ |
| 176 |
BaseCertificateID: issuerSerial{ |
| 177 |
Issuer: *holderIssuer, |
| 178 |
Serial: holderSerial, |
| 179 |
}, |
| 180 |
}, |
| 181 |
Issuer: attCertIssuer{ |
| 182 |
IssuerName: *issuer, |
| 183 |
}, |
| 184 |
Validity: validity{ |
| 185 |
NotBefore: notBefore, |
| 186 |
NotAfter: notAfter, |
| 187 |
}, |
| 188 |
Attributes: []attribute{}, |
| 189 |
Extensions: []pkix.Extension{}, |
| 190 |
} |
| 191 |
|
| 192 |
// Marshal the tbsCertificate |
| 193 |
tbsCertBytes, err := asn1.Marshal(tbsCert) |
| 194 |
if err != nil { |
| 195 |
return nil, fmt.Errorf("failed to marshal tbsCertificate: %w", err) |
| 196 |
} |
| 197 |
// Sign the tbsCertificate |
| 198 |
signature, err := signTBS(tbsCertBytes, signer, *sigDetails, rand.Reader) |
| 199 |
if err != nil { |
| 200 |
return nil, fmt.Errorf("failed to sign tbsCertificate: %w", err) |
| 201 |
} |
| 202 |
// Assemble the full attribute certificate |
| 203 |
attributeCert := attributeCertificate{ |
| 204 |
TBSAttributeCertificate: tbsCert, |
| 205 |
SignatureAlgorithm: algID, |
| 206 |
SignatureValue: asn1.BitString{Bytes: signature, BitLength: len(signature) * 8}, |
| 207 |
} |
| 208 |
|
| 209 |
// Marshal the full attribute certificate |
| 210 |
certBytes, err := asn1.Marshal(attributeCert) |
| 211 |
if err != nil { |
| 212 |
return nil, fmt.Errorf("failed to marshal attribute certificate: %w", err) |
| 213 |
} |
| 214 |
return certBytes, nil |
| 215 |
} |
| 216 |
|
| 217 |
// CreateAttributeCertificateFor creates a new attribute certificate for the |
| 218 |
// given subject certificate. See CreateAttributeCertificate for more details. |
| 219 |
func CreateAttributeCertificateFor(subjectCert *x509.Certificate, notBefore, notAfter time.Time, signingCert *x509.Certificate, priv any) ([]byte, error) { |
| 220 |
if subjectCert == nil { |
| 221 |
return nil, errors.New("subject certificate cannot be nil") |
| 222 |
} |
| 223 |
|
| 224 |
return CreateAttributeCertificate(subjectCert.RawIssuer, subjectCert.SerialNumber, notBefore, notAfter, signingCert, priv) |
| 225 |
} |