| 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 x509tools |
| 18 | |
| 19 | import ( |
| 20 | "crypto" |
| 21 | "crypto/ecdsa" |
| 22 | "crypto/rsa" |
| 23 | "crypto/x509" |
| 24 | "crypto/x509/pkix" |
| 25 | "encoding/asn1" |
| 26 | "errors" |
| 27 | "fmt" |
| 28 | ) |
| 29 | |
| 30 | var ( |
| 31 | // RFC 3279 |
| 32 | OidPublicKeyRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 1} |
| 33 | OidPublicKeyDSA = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 1} |
| 34 | OidPublicKeyECDSA = asn1.ObjectIdentifier{1, 2, 840, 10045, 2, 1} |
| 35 | |
| 36 | oidSignatureMD5WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 4} |
| 37 | oidSignatureSHA1WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 5} |
| 38 | oidSignatureSHA256WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 11} |
| 39 | oidSignatureSHA384WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 12} |
| 40 | oidSignatureSHA512WithRSA = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 13} |
| 41 | oidSignatureDSAWithSHA1 = asn1.ObjectIdentifier{1, 2, 840, 10040, 4, 3} |
| 42 | oidSignatureDSAWithSHA256 = asn1.ObjectIdentifier{2, 16, 840, 1, 101, 3, 4, 3, 2} |
| 43 | oidSignatureECDSAWithSHA1 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 1} |
| 44 | oidSignatureECDSAWithSHA256 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 2} |
| 45 | oidSignatureECDSAWithSHA384 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 3} |
| 46 | oidSignatureECDSAWithSHA512 = asn1.ObjectIdentifier{1, 2, 840, 10045, 4, 3, 4} |
| 47 | oidISOSignatureSHA1WithRSA = asn1.ObjectIdentifier{1, 3, 14, 3, 2, 29} |
| 48 | |
| 49 | // RFC 4055 |
| 50 | OidMGF1 = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 8} |
| 51 | OidSignatureRSAPSS = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 1, 10} |
| 52 | |
| 53 | oidExtensionSubjectKeyId = asn1.ObjectIdentifier{2, 5, 29, 14} |
| 54 | oidExtensionKeyUsage = asn1.ObjectIdentifier{2, 5, 29, 15} |
| 55 | oidExtensionSubjectAltName = asn1.ObjectIdentifier{2, 5, 29, 17} |
| 56 | oidExtensionBasicConstraints = asn1.ObjectIdentifier{2, 5, 29, 19} |
| 57 | oidExtensionAuthorityKeyId = asn1.ObjectIdentifier{2, 5, 29, 35} |
| 58 | oidExtensionExtendedKeyUsage = asn1.ObjectIdentifier{2, 5, 29, 37} |
| 59 | ) |
| 60 | |
| 61 | type sigAlgInfo struct { |
| 62 | oid asn1.ObjectIdentifier |
| 63 | pubKeyAlgo x509.PublicKeyAlgorithm |
| 64 | hash crypto.Hash |
| 65 | } |
| 66 | |
| 67 | var sigAlgInfos = []sigAlgInfo{ |
| 68 | // without a digest |
| 69 | {OidPublicKeyRSA, x509.RSA, 0}, |
| 70 | {OidSignatureRSAPSS, x509.RSA, 0}, |
| 71 | {OidPublicKeyDSA, x509.DSA, 0}, |
| 72 | {OidPublicKeyECDSA, x509.ECDSA, 0}, |
| 73 | // with a digest |
| 74 | {oidSignatureMD5WithRSA, x509.RSA, crypto.MD5}, |
| 75 | {oidSignatureSHA1WithRSA, x509.RSA, crypto.SHA1}, |
| 76 | {oidISOSignatureSHA1WithRSA, x509.RSA, crypto.SHA1}, |
| 77 | {oidSignatureSHA256WithRSA, x509.RSA, crypto.SHA256}, |
| 78 | {oidSignatureSHA384WithRSA, x509.RSA, crypto.SHA384}, |
| 79 | {oidSignatureSHA512WithRSA, x509.RSA, crypto.SHA512}, |
| 80 | {oidSignatureDSAWithSHA1, x509.DSA, crypto.SHA1}, |
| 81 | {oidSignatureDSAWithSHA256, x509.DSA, crypto.SHA256}, |
| 82 | {oidSignatureECDSAWithSHA1, x509.ECDSA, crypto.SHA1}, |
| 83 | {oidSignatureECDSAWithSHA256, x509.ECDSA, crypto.SHA256}, |
| 84 | {oidSignatureECDSAWithSHA384, x509.ECDSA, crypto.SHA384}, |
| 85 | {oidSignatureECDSAWithSHA512, x509.ECDSA, crypto.SHA512}, |
| 86 | } |
| 87 | |
| 88 | // Given a public key and signer options, return the appropriate X.509 digest and signature algorithms |
| 89 | func PkixAlgorithms(pub crypto.PublicKey, opts crypto.SignerOpts) (digestAlg, sigAlg pkix.AlgorithmIdentifier, err error) { |
| 90 | digestAlg, ok := PkixDigestAlgorithm(opts.HashFunc()) |
| 91 | if !ok { |
| 92 | err = errors.New("unsupported digest algorithm") |
| 93 | return |
| 94 | } |
| 95 | if pss, ok := opts.(*rsa.PSSOptions); ok { |
| 96 | rsapub, ok := pub.(*rsa.PublicKey) |
| 97 | if !ok { |
| 98 | err = errors.New("RSA-PSS is only valid for RSA keys") |
| 99 | return |
| 100 | } |
| 101 | var params asn1.RawValue |
| 102 | params, err = MarshalRSAPSSParameters(rsapub, pss) |
| 103 | if err != nil { |
| 104 | return |
| 105 | } |
| 106 | sigAlg = pkix.AlgorithmIdentifier{ |
| 107 | Algorithm: OidSignatureRSAPSS, |
| 108 | Parameters: params, |
| 109 | } |
| 110 | return |
| 111 | } |
| 112 | switch pub.(type) { |
| 113 | case *rsa.PublicKey: |
| 114 | sigAlg.Algorithm = OidPublicKeyRSA |
| 115 | case *ecdsa.PublicKey: |
| 116 | sigAlg.Algorithm = OidPublicKeyECDSA |
| 117 | default: |
| 118 | err = errors.New("unsupported public key algorithm") |
| 119 | return |
| 120 | } |
| 121 | sigAlg.Parameters = asn1.NullRawValue |
| 122 | return |
| 123 | } |
| 124 | |
| 125 | // Convert a crypto.Hash to a X.509 AlgorithmIdentifier |
| 126 | func PkixDigestAlgorithm(hash crypto.Hash) (alg pkix.AlgorithmIdentifier, ok bool) { |
| 127 | if oid, ok2 := HashOids[hash]; ok2 { |
| 128 | alg.Algorithm = oid |
| 129 | alg.Parameters = asn1.NullRawValue |
| 130 | ok = true |
| 131 | } |
| 132 | return |
| 133 | } |
| 134 | |
| 135 | // Convert a X.509 AlgorithmIdentifier to a crypto.Hash |
| 136 | func PkixDigestToHash(alg pkix.AlgorithmIdentifier) (hash crypto.Hash, ok bool) { |
| 137 | for hash, oid := range HashOids { |
| 138 | if alg.Algorithm.Equal(oid) { |
| 139 | return hash, true |
| 140 | } |
| 141 | } |
| 142 | return 0, false |
| 143 | } |
| 144 | |
| 145 | // Convert a X.509 AlgorithmIdentifier to a crypto.Hash |
| 146 | func PkixDigestToHashE(alg pkix.AlgorithmIdentifier) (hash crypto.Hash, err error) { |
| 147 | hash, ok := PkixDigestToHash(alg) |
| 148 | if ok && hash.Available() { |
| 149 | return hash, nil |
| 150 | } |
| 151 | return 0, UnknownDigestError{Algorithm: alg.Algorithm} |
| 152 | } |
| 153 | |
| 154 | // Convert a crypto.PublicKey to a X.509 AlgorithmIdentifier |
| 155 | func PkixPublicKeyAlgorithm(pub crypto.PublicKey) (alg pkix.AlgorithmIdentifier, ok bool) { |
| 156 | _, alg, err := PkixAlgorithms(pub, nil) |
| 157 | return alg, err == nil |
| 158 | } |
| 159 | |
| 160 | // Verify a signature using the algorithm specified by the given X.509 AlgorithmIdentifier |
| 161 | func PkixVerify(pub crypto.PublicKey, digestAlg, sigAlg pkix.AlgorithmIdentifier, digest, sig []byte) error { |
| 162 | hash, err := PkixDigestToHashE(digestAlg) |
| 163 | if err != nil { |
| 164 | return err |
| 165 | } |
| 166 | var info sigAlgInfo |
| 167 | for _, a := range sigAlgInfos { |
| 168 | if sigAlg.Algorithm.Equal(a.oid) { |
| 169 | info = a |
| 170 | } |
| 171 | } |
| 172 | if info.hash != 0 && info.hash != hash { |
| 173 | return errors.New("signature type does not match digest type") |
| 174 | } |
| 175 | switch info.pubKeyAlgo { |
| 176 | case x509.RSA: |
| 177 | key, ok := pub.(*rsa.PublicKey) |
| 178 | if !ok { |
| 179 | return errors.New("incorrect key type for signature") |
| 180 | } |
| 181 | if sigAlg.Algorithm.Equal(OidSignatureRSAPSS) { |
| 182 | opts, err := UnmarshalRSAPSSParameters(hash, sigAlg.Parameters) |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | return rsa.VerifyPSS(key, hash, digest, sig, opts) |
| 187 | } |
| 188 | return rsa.VerifyPKCS1v15(key, hash, digest, sig) |
| 189 | case x509.ECDSA: |
| 190 | key, ok := pub.(*ecdsa.PublicKey) |
| 191 | if !ok { |
| 192 | return errors.New("incorrect key type for signature") |
| 193 | } |
| 194 | esig, err := UnmarshalEcdsaSignature(sig) |
| 195 | if err != nil { |
| 196 | return err |
| 197 | } |
| 198 | if !ecdsa.Verify(key, digest, esig.R, esig.S) { |
| 199 | return errors.New("ECDSA verification failed") |
| 200 | } |
| 201 | return nil |
| 202 | default: |
| 203 | return errors.New("unsupported public key algorithm") |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | type UnknownDigestError struct { |
| 208 | Algorithm asn1.ObjectIdentifier |
| 209 | } |
| 210 | |
| 211 | func (e UnknownDigestError) Error() string { |
| 212 | return fmt.Sprintf("unsupported hash algorithm %s", e.Algorithm) |
| 213 | } |