|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/rsa" |
| 6 | + "crypto/x509" |
| 7 | + "crypto/x509/pkix" |
| 8 | + "encoding/pem" |
| 9 | + "fmt" |
| 10 | + "io/ioutil" |
| 11 | + "math/big" |
| 12 | + "net" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +// CreateCertificateOptions configures how to create a certificate. |
| 17 | +type CreateCertificateOptions struct { |
| 18 | + Hosts []string // Host names and/or IP addresses |
| 19 | + ValidFor time.Duration |
| 20 | + RSABits int |
| 21 | + Organization string |
| 22 | +} |
| 23 | + |
| 24 | +const ( |
| 25 | + defaultValidFor = time.Hour * 24 * 365 // 1year |
| 26 | +) |
| 27 | + |
| 28 | +func publicKey(priv interface{}) interface{} { |
| 29 | + switch k := priv.(type) { |
| 30 | + case *rsa.PrivateKey: |
| 31 | + return &k.PublicKey |
| 32 | + default: |
| 33 | + return nil |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func pemBlockForKey(priv interface{}) *pem.Block { |
| 38 | + switch k := priv.(type) { |
| 39 | + case *rsa.PrivateKey: |
| 40 | + return &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(k)} |
| 41 | + default: |
| 42 | + return nil |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +// CreateCertificate creates a self-signed certificate according to the given configuration. |
| 47 | +// The resulting certificate + private key will be written into a single file in the given folder. |
| 48 | +// The path of that single file is returned. |
| 49 | +func CreateCertificate(options CreateCertificateOptions, folder string) (string, error) { |
| 50 | + priv, err := rsa.GenerateKey(rand.Reader, options.RSABits) |
| 51 | + if err != nil { |
| 52 | + return "", maskAny(err) |
| 53 | + } |
| 54 | + |
| 55 | + notBefore := time.Now() |
| 56 | + if options.ValidFor == 0 { |
| 57 | + options.ValidFor = defaultValidFor |
| 58 | + } |
| 59 | + notAfter := notBefore.Add(options.ValidFor) |
| 60 | + |
| 61 | + serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) |
| 62 | + serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) |
| 63 | + if err != nil { |
| 64 | + return "", maskAny(fmt.Errorf("failed to generate serial number: %v", err)) |
| 65 | + } |
| 66 | + |
| 67 | + template := x509.Certificate{ |
| 68 | + SerialNumber: serialNumber, |
| 69 | + Subject: pkix.Name{ |
| 70 | + Organization: []string{options.Organization}, |
| 71 | + }, |
| 72 | + NotBefore: notBefore, |
| 73 | + NotAfter: notAfter, |
| 74 | + |
| 75 | + KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, |
| 76 | + ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| 77 | + BasicConstraintsValid: true, |
| 78 | + } |
| 79 | + |
| 80 | + for _, h := range options.Hosts { |
| 81 | + if ip := net.ParseIP(h); ip != nil { |
| 82 | + template.IPAddresses = append(template.IPAddresses, ip) |
| 83 | + } else { |
| 84 | + template.DNSNames = append(template.DNSNames, h) |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Create the certificate |
| 89 | + derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv) |
| 90 | + if err != nil { |
| 91 | + return "", maskAny(fmt.Errorf("Failed to create certificate: %v", err)) |
| 92 | + } |
| 93 | + |
| 94 | + // Write the certificate to disk |
| 95 | + f, err := ioutil.TempFile(folder, "key-") |
| 96 | + if err != nil { |
| 97 | + return "", maskAny(err) |
| 98 | + } |
| 99 | + defer f.Close() |
| 100 | + // Public key |
| 101 | + pem.Encode(f, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) |
| 102 | + // Private key |
| 103 | + pem.Encode(f, pemBlockForKey(priv)) |
| 104 | + |
| 105 | + return f.Name(), nil |
| 106 | +} |
0 commit comments