| // Copyright 2009 The Go Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style |
| // license that can be found in the LICENSE file. |
| |
| // This file is adapted from $GOROOT/src/crypto/tls/generate_cert.go |
| |
| // Generate a self-signed X.509 certificate for a TLS server. |
| |
| package main |
| |
| import ( |
| "crypto/ecdsa" |
| "crypto/elliptic" |
| "crypto/rand" |
| "crypto/rsa" |
| "crypto/x509" |
| "crypto/x509/pkix" |
| "encoding/pem" |
| "fmt" |
| "math/big" |
| "os" |
| "path/filepath" |
| "time" |
| ) |
| |
| func publicKey(priv interface{}) interface{} { |
| switch k := priv.(type) { |
| case *rsa.PrivateKey: |
| return &k.PublicKey |
| case *ecdsa.PrivateKey: |
| return &k.PublicKey |
| default: |
| return nil |
| } |
| } |
| |
| func pemBlockForKey(priv interface{}) *pem.Block { |
| switch k := priv.(type) { |
| case *rsa.PrivateKey: |
| return &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(k)} |
| case *ecdsa.PrivateKey: |
| b, err := x509.MarshalECPrivateKey(k) |
| if err != nil { |
| fmt.Fprintf(os.Stderr, "Unable to marshal ECDSA private key: %v", err) |
| os.Exit(2) |
| } |
| return &pem.Block{Type: "EC PRIVATE KEY", Bytes: b} |
| default: |
| return nil |
| } |
| } |
| |
| func generateCert(dir string) error { |
| priv, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader) |
| if err != nil { |
| return fmt.Errorf("failed to generate key: %s", err) |
| } |
| |
| notBefore := time.Now() |
| notAfter := notBefore.Add(24 * time.Hour) |
| |
| serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) |
| serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) |
| if err != nil { |
| return fmt.Errorf("failed to generate serial number: %s", err) |
| } |
| |
| template := x509.Certificate{ |
| SerialNumber: serialNumber, |
| Subject: pkix.Name{ |
| Organization: []string{"The Upspin Project"}, |
| }, |
| NotBefore: notBefore, |
| NotAfter: notAfter, |
| |
| KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, |
| ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
| BasicConstraintsValid: true, |
| |
| DNSNames: []string{"localhost"}, |
| } |
| |
| derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv) |
| if err != nil { |
| return fmt.Errorf("Failed to create certificate: %s", err) |
| } |
| |
| certOut, err := os.Create(filepath.Join(dir, "cert.pem")) |
| if err != nil { |
| return fmt.Errorf("failed to open cert.pem for writing: %s", err) |
| } |
| if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil { |
| return fmt.Errorf("failed to encode cert.pem: %s", err) |
| } |
| if err := certOut.Close(); err != nil { |
| return fmt.Errorf("failed to write cert.pem: %s", err) |
| } |
| |
| keyOut, err := os.OpenFile(filepath.Join(dir, "key.pem"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) |
| if err != nil { |
| return fmt.Errorf("failed to open key.pem for writing: %s", err) |
| } |
| if err := pem.Encode(keyOut, pemBlockForKey(priv)); err != nil { |
| return fmt.Errorf("failed to encode key.pem: %s", err) |
| } |
| if err := keyOut.Close(); err != nil { |
| return fmt.Errorf("failed to write cert.pem: %s", err) |
| } |
| |
| return nil |
| } |