mirror of https://github.com/hashicorp/consul
Paul Banks
7 years ago
committed by
Mitchell Hashimoto
10 changed files with 136 additions and 31 deletions
@ -0,0 +1,59 @@
|
||||
package connect |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto" |
||||
"crypto/ecdsa" |
||||
"crypto/elliptic" |
||||
"crypto/rand" |
||||
"crypto/x509" |
||||
"encoding/pem" |
||||
"fmt" |
||||
"net/url" |
||||
) |
||||
|
||||
// CreateCSR returns a CSR to sign the given service along with the PEM-encoded
|
||||
// private key for this certificate.
|
||||
func CreateCSR(uri CertURI, privateKey crypto.Signer) (string, error) { |
||||
template := &x509.CertificateRequest{ |
||||
URIs: []*url.URL{uri.URI()}, |
||||
SignatureAlgorithm: x509.ECDSAWithSHA256, |
||||
} |
||||
|
||||
// Create the CSR itself
|
||||
var csrBuf bytes.Buffer |
||||
bs, err := x509.CreateCertificateRequest(rand.Reader, template, privateKey) |
||||
if err != nil { |
||||
return "", err |
||||
} |
||||
|
||||
err = pem.Encode(&csrBuf, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: bs}) |
||||
if err != nil { |
||||
return "", err |
||||
} |
||||
|
||||
return csrBuf.String(), nil |
||||
} |
||||
|
||||
// GeneratePrivateKey generates a new Private key
|
||||
func GeneratePrivateKey() (crypto.Signer, string, error) { |
||||
var pk *ecdsa.PrivateKey |
||||
|
||||
pk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
||||
if err != nil { |
||||
return nil, "", fmt.Errorf("error generating private key: %s", err) |
||||
} |
||||
|
||||
bs, err := x509.MarshalECPrivateKey(pk) |
||||
if err != nil { |
||||
return nil, "", fmt.Errorf("error generating private key: %s", err) |
||||
} |
||||
|
||||
var buf bytes.Buffer |
||||
err = pem.Encode(&buf, &pem.Block{Type: "EC PRIVATE KEY", Bytes: bs}) |
||||
if err != nil { |
||||
return nil, "", fmt.Errorf("error encoding private key: %s", err) |
||||
} |
||||
|
||||
return pk, buf.String(), nil |
||||
} |
Loading…
Reference in new issue