|
| 1 | +// Package sslcert contains a reworked version of https://golang.org/src/crypto/tls/generate_cert.go |
| 2 | +package sslcert |
| 3 | + |
| 4 | +import ( |
| 5 | + "bufio" |
| 6 | + "bytes" |
| 7 | + "crypto/ecdsa" |
| 8 | + "crypto/ed25519" |
| 9 | + "crypto/elliptic" |
| 10 | + "crypto/rand" |
| 11 | + "crypto/rsa" |
| 12 | + "crypto/x509" |
| 13 | + "crypto/x509/pkix" |
| 14 | + "encoding/pem" |
| 15 | + "errors" |
| 16 | + "fmt" |
| 17 | + "math/big" |
| 18 | + "net" |
| 19 | + "strings" |
| 20 | + "time" |
| 21 | +) |
| 22 | + |
| 23 | +func pubKey(priv interface{}) interface{} { |
| 24 | + switch k := priv.(type) { |
| 25 | + case *rsa.PrivateKey: |
| 26 | + return &k.PublicKey |
| 27 | + case *ecdsa.PrivateKey: |
| 28 | + return &k.PublicKey |
| 29 | + case ed25519.PrivateKey: |
| 30 | + return k.Public().(ed25519.PublicKey) |
| 31 | + default: |
| 32 | + return nil |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func Generate(options Options) (privateKey, publicKey []byte, err error) { |
| 37 | + if options.Host == "" { |
| 38 | + return nil, nil, errors.New("Empty host value") |
| 39 | + } |
| 40 | + |
| 41 | + var priv interface{} |
| 42 | + switch options.EcdsaCurve { |
| 43 | + case "": |
| 44 | + if options.Ed25519Key { |
| 45 | + _, priv, err = ed25519.GenerateKey(rand.Reader) |
| 46 | + } else { |
| 47 | + priv, err = rsa.GenerateKey(rand.Reader, options.RSABits) |
| 48 | + } |
| 49 | + case "P224": |
| 50 | + priv, err = ecdsa.GenerateKey(elliptic.P224(), rand.Reader) |
| 51 | + case "P256": |
| 52 | + priv, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 53 | + case "P384": |
| 54 | + priv, err = ecdsa.GenerateKey(elliptic.P384(), rand.Reader) |
| 55 | + case "P521": |
| 56 | + priv, err = ecdsa.GenerateKey(elliptic.P521(), rand.Reader) |
| 57 | + default: |
| 58 | + err = fmt.Errorf("Unrecognized elliptic curve: %q", options.EcdsaCurve) |
| 59 | + return |
| 60 | + } |
| 61 | + if err != nil { |
| 62 | + err = fmt.Errorf("Failed to generate private key: %v", err) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + // ECDSA, ED25519 and RSA subject keys should have the DigitalSignature |
| 67 | + // KeyUsage bits set in the x509.Certificate template |
| 68 | + keyUsage := x509.KeyUsageDigitalSignature |
| 69 | + // Only RSA subject keys should have the KeyEncipherment KeyUsage bits set. In |
| 70 | + // the context of TLS this KeyUsage is particular to RSA key exchange and |
| 71 | + // authentication. |
| 72 | + if _, isRSA := priv.(*rsa.PrivateKey); isRSA { |
| 73 | + keyUsage |= x509.KeyUsageKeyEncipherment |
| 74 | + } |
| 75 | + |
| 76 | + var notBefore time.Time |
| 77 | + if len(options.ValidFrom) == 0 { |
| 78 | + notBefore = time.Now() |
| 79 | + } else { |
| 80 | + notBefore, err = time.Parse("Jan 2 15:04:05 2006", options.ValidFrom) |
| 81 | + if err != nil { |
| 82 | + err = fmt.Errorf("Failed to parse creation date: %v", err) |
| 83 | + return |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + notAfter := notBefore.Add(options.ValidFor) |
| 88 | + |
| 89 | + serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) |
| 90 | + serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) |
| 91 | + if err != nil { |
| 92 | + err = fmt.Errorf("Failed to generate serial number: %v", err) |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + template := x509.Certificate{ |
| 97 | + SerialNumber: serialNumber, |
| 98 | + Subject: pkix.Name{ |
| 99 | + Organization: []string{options.Organization}, |
| 100 | + }, |
| 101 | + NotBefore: notBefore, |
| 102 | + NotAfter: notAfter, |
| 103 | + |
| 104 | + KeyUsage: keyUsage, |
| 105 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 106 | + BasicConstraintsValid: true, |
| 107 | + } |
| 108 | + |
| 109 | + hosts := strings.Split(options.Host, ",") |
| 110 | + for _, h := range hosts { |
| 111 | + if ip := net.ParseIP(h); ip != nil { |
| 112 | + template.IPAddresses = append(template.IPAddresses, ip) |
| 113 | + } else { |
| 114 | + template.DNSNames = append(template.DNSNames, h) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if options.IsCA { |
| 119 | + template.IsCA = true |
| 120 | + template.KeyUsage |= x509.KeyUsageCertSign |
| 121 | + } |
| 122 | + |
| 123 | + derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, pubKey(priv), priv) |
| 124 | + if err != nil { |
| 125 | + err = fmt.Errorf("Failed to create certificate: %v", err) |
| 126 | + return |
| 127 | + } |
| 128 | + |
| 129 | + var pubKeyBuf bytes.Buffer |
| 130 | + pubKeyBufb := bufio.NewWriter(&pubKeyBuf) |
| 131 | + err = pem.Encode(pubKeyBufb, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) |
| 132 | + if err != nil { |
| 133 | + err = fmt.Errorf("Failed to write data to cert.pem: %v", err) |
| 134 | + return |
| 135 | + } |
| 136 | + pubKeyBufb.Flush() |
| 137 | + |
| 138 | + privBytes, err := x509.MarshalPKCS8PrivateKey(priv) |
| 139 | + if err != nil { |
| 140 | + err = fmt.Errorf("Unable to marshal private key: %v", err) |
| 141 | + return |
| 142 | + } |
| 143 | + var privKeyBuf bytes.Buffer |
| 144 | + privKeyBufb := bufio.NewWriter(&privKeyBuf) |
| 145 | + err = pem.Encode(privKeyBufb, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}) |
| 146 | + if err != nil { |
| 147 | + err = fmt.Errorf("Failed to write data to key.pem: %v", err) |
| 148 | + return |
| 149 | + } |
| 150 | + privKeyBufb.Flush() |
| 151 | + |
| 152 | + return pubKeyBuf.Bytes(), privKeyBuf.Bytes(), nil |
| 153 | +} |
0 commit comments