| 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/elliptic" |
| 23 | "crypto/rand" |
| 24 | "crypto/rsa" |
| 25 | "crypto/sha1" |
| 26 | "crypto/x509" |
| 27 | "crypto/x509/pkix" |
| 28 | "encoding/asn1" |
| 29 | "errors" |
| 30 | "io" |
| 31 | "math/big" |
| 32 | ) |
| 33 | |
| 34 | // Make a random 12 byte big.Int |
| 35 | func MakeSerial() *big.Int { |
| 36 | blob := make([]byte, 12) |
| 37 | if _, err := io.ReadFull(rand.Reader, blob); err != nil { |
| 38 | panic(err) |
| 39 | } |
| 40 | return new(big.Int).SetBytes(blob) |
| 41 | } |
| 42 | |
| 43 | // Choose a X509 signature algorithm suitable for the specified public key |
| 44 | func X509SignatureAlgorithm(pub crypto.PublicKey) x509.SignatureAlgorithm { |
| 45 | switch k := pub.(type) { |
| 46 | case *rsa.PublicKey: |
| 47 | if ArgRSAPSS { |
| 48 | return x509.SHA256WithRSAPSS |
| 49 | } |
| 50 | return x509.SHA256WithRSA |
| 51 | case *ecdsa.PublicKey: |
| 52 | switch k.Curve { |
| 53 | case elliptic.P256(): |
| 54 | return x509.ECDSAWithSHA256 |
| 55 | case elliptic.P384(): |
| 56 | return x509.ECDSAWithSHA384 |
| 57 | case elliptic.P521(): |
| 58 | return x509.ECDSAWithSHA512 |
| 59 | } |
| 60 | return x509.ECDSAWithSHA256 |
| 61 | default: |
| 62 | return x509.UnknownSignatureAlgorithm |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | type pkixPublicKey struct { |
| 67 | Algo pkix.AlgorithmIdentifier |
| 68 | BitString asn1.BitString |
| 69 | } |
| 70 | |
| 71 | // Calculcate subject key identifier from a public key per RFC 3280 |
| 72 | func SubjectKeyID(pub crypto.PublicKey) ([]byte, error) { |
| 73 | der, err := x509.MarshalPKIXPublicKey(pub) |
| 74 | if err != nil { |
| 75 | return nil, err |
| 76 | } |
| 77 | // extract the raw "bit string" part of the public key bytes |
| 78 | var pki pkixPublicKey |
| 79 | if rest, err := asn1.Unmarshal(der, &pki); err != nil { |
| 80 | return nil, err |
| 81 | } else if len(rest) != 0 { |
| 82 | return nil, errors.New("trailing garbage on public key") |
| 83 | } |
| 84 | digest := sha1.Sum(pki.BitString.RightAlign()) |
| 85 | return digest[:], nil |
| 86 | } |
| 87 | |
| 88 | // Test whether two public or private keys have the same public key |
| 89 | func SameKey(pub1, pub2 interface{}) bool { |
| 90 | if privkey, ok := pub1.(crypto.Signer); ok { |
| 91 | pub1 = privkey.Public() |
| 92 | } |
| 93 | if privkey, ok := pub2.(crypto.Signer); ok { |
| 94 | pub2 = privkey.Public() |
| 95 | } |
| 96 | switch key1 := pub1.(type) { |
| 97 | case *rsa.PublicKey: |
| 98 | key2, ok := pub2.(*rsa.PublicKey) |
| 99 | return ok && key1.E == key2.E && key1.N.Cmp(key2.N) == 0 |
| 100 | case *ecdsa.PublicKey: |
| 101 | key2, ok := pub2.(*ecdsa.PublicKey) |
| 102 | return ok && key1.X.Cmp(key2.X) == 0 && key1.Y.Cmp(key2.Y) == 0 |
| 103 | default: |
| 104 | return false |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Verify an RSA or ECDSA signature |
| 109 | func Verify(pub interface{}, hash crypto.Hash, hashed []byte, sig []byte) error { |
| 110 | switch pubk := pub.(type) { |
| 111 | case *rsa.PublicKey: |
| 112 | return rsa.VerifyPKCS1v15(pubk, hash, hashed, sig) |
| 113 | case *ecdsa.PublicKey: |
| 114 | esig, err := UnmarshalEcdsaSignature(sig) |
| 115 | if err != nil { |
| 116 | return err |
| 117 | } |
| 118 | if !ecdsa.Verify(pubk, hashed, esig.R, esig.S) { |
| 119 | return errors.New("ECDSA verification failed") |
| 120 | } |
| 121 | return nil |
| 122 | } |
| 123 | return errors.New("unsupported public key algorithm") |
| 124 | } |
| 125 | |
| 126 | // Determine the type of a public or private key |
| 127 | func GetPublicKeyAlgorithm(key interface{}) x509.PublicKeyAlgorithm { |
| 128 | if privkey, ok := key.(crypto.Signer); ok { |
| 129 | key = privkey.Public() |
| 130 | } |
| 131 | switch key.(type) { |
| 132 | case *rsa.PublicKey: |
| 133 | return x509.RSA |
| 134 | case *ecdsa.PublicKey: |
| 135 | return x509.ECDSA |
| 136 | default: |
| 137 | return x509.UnknownPublicKeyAlgorithm |
| 138 | } |
| 139 | } |