|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2017 All Rights Reserved. |
| 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 | +package ca |
| 17 | + |
| 18 | +import ( |
| 19 | + "crypto" |
| 20 | + "crypto/ecdsa" |
| 21 | + "crypto/rand" |
| 22 | + "crypto/x509" |
| 23 | + "crypto/x509/pkix" |
| 24 | + "encoding/pem" |
| 25 | + "math/big" |
| 26 | + "os" |
| 27 | + "time" |
| 28 | + |
| 29 | + "path/filepath" |
| 30 | + |
| 31 | + "github.com/hyperledger/fabric/common/tools/cryptogen/csp" |
| 32 | +) |
| 33 | + |
| 34 | +type CA struct { |
| 35 | + Name string |
| 36 | + //SignKey *ecdsa.PrivateKey |
| 37 | + Signer crypto.Signer |
| 38 | + SignCert *x509.Certificate |
| 39 | +} |
| 40 | + |
| 41 | +// NewCA creates an instance of CA and saves the signing key pair in |
| 42 | +// baseDir/name |
| 43 | +func NewCA(baseDir, name string) (*CA, error) { |
| 44 | + |
| 45 | + err := os.MkdirAll(baseDir, 0755) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + |
| 50 | + //key, err := genKeyECDSA(caDir, name) |
| 51 | + priv, signer, err := csp.GeneratePrivateKey(baseDir) |
| 52 | + // get public signing certificate |
| 53 | + ecPubKey, err := csp.GetECPublicKey(priv) |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + template, err := x509Template() |
| 59 | + |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + //this is a CA |
| 65 | + template.IsCA = true |
| 66 | + template.KeyUsage |= x509.KeyUsageCertSign | x509.KeyUsageCRLSign |
| 67 | + template.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageAny} |
| 68 | + |
| 69 | + //set the organization for the subject |
| 70 | + subject := subjectTemplate() |
| 71 | + subject.Organization = []string{name} |
| 72 | + subject.CommonName = name |
| 73 | + |
| 74 | + template.Subject = subject |
| 75 | + template.SubjectKeyId = priv.SKI() |
| 76 | + |
| 77 | + x509Cert, err := genCertificateECDSA(baseDir, name, &template, &template, |
| 78 | + ecPubKey, signer) |
| 79 | + |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + ca := &CA{ |
| 85 | + Name: name, |
| 86 | + Signer: signer, |
| 87 | + SignCert: x509Cert, |
| 88 | + } |
| 89 | + |
| 90 | + return ca, nil |
| 91 | +} |
| 92 | + |
| 93 | +// SignCertificate creates a signed certificate based on a built-in template |
| 94 | +// and saves it in baseDir/name |
| 95 | +func (ca *CA) SignCertificate(baseDir, name string, pub *ecdsa.PublicKey) error { |
| 96 | + |
| 97 | + template, err := x509Template() |
| 98 | + |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + template.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth} |
| 104 | + |
| 105 | + //set the organization for the subject |
| 106 | + subject := subjectTemplate() |
| 107 | + subject.CommonName = name |
| 108 | + |
| 109 | + template.Subject = subject |
| 110 | + |
| 111 | + _, err = genCertificateECDSA(baseDir, name, &template, ca.SignCert, |
| 112 | + pub, ca.Signer) |
| 113 | + |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +// default template for X509 subject |
| 122 | +func subjectTemplate() pkix.Name { |
| 123 | + return pkix.Name{ |
| 124 | + Country: []string{"US"}, |
| 125 | + Locality: []string{"San Francisco"}, |
| 126 | + Province: []string{"California"}, |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// default template for X509 certificates |
| 131 | +func x509Template() (x509.Certificate, error) { |
| 132 | + |
| 133 | + //generate a serial number |
| 134 | + serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) |
| 135 | + serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) |
| 136 | + if err != nil { |
| 137 | + return x509.Certificate{}, err |
| 138 | + } |
| 139 | + |
| 140 | + now := time.Now() |
| 141 | + //basic template to use |
| 142 | + x509 := x509.Certificate{ |
| 143 | + SerialNumber: serialNumber, |
| 144 | + NotBefore: now, |
| 145 | + NotAfter: now.Add(3650 * 24 * time.Hour), //~ten years |
| 146 | + KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, |
| 147 | + BasicConstraintsValid: true, |
| 148 | + } |
| 149 | + return x509, nil |
| 150 | + |
| 151 | +} |
| 152 | + |
| 153 | +// generate a signed X509 certficate using ECDSA |
| 154 | +func genCertificateECDSA(baseDir, name string, template, parent *x509.Certificate, pub *ecdsa.PublicKey, |
| 155 | + priv interface{}) (*x509.Certificate, error) { |
| 156 | + |
| 157 | + //create the x509 public cert |
| 158 | + certBytes, err := x509.CreateCertificate(rand.Reader, template, parent, pub, priv) |
| 159 | + if err != nil { |
| 160 | + return nil, err |
| 161 | + } |
| 162 | + |
| 163 | + //write cert out to file |
| 164 | + fileName := filepath.Join(baseDir, name+"-cert.pem") |
| 165 | + certFile, err := os.Create(fileName) |
| 166 | + if err != nil { |
| 167 | + return nil, err |
| 168 | + } |
| 169 | + //pem encode the cert |
| 170 | + err = pem.Encode(certFile, &pem.Block{Type: "CERTIFICATE", Bytes: certBytes}) |
| 171 | + certFile.Close() |
| 172 | + if err != nil { |
| 173 | + return nil, err |
| 174 | + } |
| 175 | + |
| 176 | + x509Cert, err := x509.ParseCertificate(certBytes) |
| 177 | + if err != nil { |
| 178 | + return nil, err |
| 179 | + } |
| 180 | + return x509Cert, nil |
| 181 | +} |
0 commit comments