2020-04-21 17:03:33 +03:00
|
|
|
// Package pki provides PKI all the PKI functions necessary to run micro over an untrusted network
|
|
|
|
// including a CA
|
2021-10-02 19:55:07 +03:00
|
|
|
package pki // import "go.unistack.org/micro/v3/util/pki"
|
2020-04-21 17:03:33 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/ed25519"
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2020-08-25 13:44:41 +03:00
|
|
|
"errors"
|
2021-04-27 08:32:47 +03:00
|
|
|
"fmt"
|
2020-04-21 17:03:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// GenerateKey returns an ed25519 key
|
|
|
|
func GenerateKey() (ed25519.PublicKey, ed25519.PrivateKey, error) {
|
|
|
|
return ed25519.GenerateKey(rand.Reader)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CA generates a self signed CA and returns cert, key in PEM format
|
|
|
|
func CA(opts ...CertOption) ([]byte, []byte, error) {
|
|
|
|
opts = append(opts, IsCA())
|
|
|
|
options := CertOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
template := &x509.Certificate{
|
|
|
|
SignatureAlgorithm: x509.PureEd25519,
|
|
|
|
Subject: options.Subject,
|
|
|
|
DNSNames: options.DNSNames,
|
|
|
|
IPAddresses: options.IPAddresses,
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
NotBefore: options.NotBefore,
|
|
|
|
NotAfter: options.NotAfter,
|
|
|
|
SerialNumber: options.SerialNumber,
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
}
|
|
|
|
if options.IsCA {
|
|
|
|
template.IsCA = true
|
|
|
|
template.KeyUsage |= x509.KeyUsageCertSign
|
|
|
|
}
|
|
|
|
x509Cert, err := x509.CreateCertificate(rand.Reader, template, template, options.Pub, options.Priv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
cert, key := &bytes.Buffer{}, &bytes.Buffer{}
|
2021-04-27 08:32:47 +03:00
|
|
|
if err = pem.Encode(cert, &pem.Block{Type: "CERTIFICATE", Bytes: x509Cert}); err != nil {
|
2020-04-21 17:03:33 +03:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
x509Key, err := x509.MarshalPKCS8PrivateKey(options.Priv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2021-04-27 08:32:47 +03:00
|
|
|
if err = pem.Encode(key, &pem.Block{Type: "PRIVATE KEY", Bytes: x509Key}); err != nil {
|
2020-04-21 17:03:33 +03:00
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return cert.Bytes(), key.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CSR generates a certificate request in PEM format
|
|
|
|
func CSR(opts ...CertOption) ([]byte, error) {
|
|
|
|
options := CertOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
csrTemplate := &x509.CertificateRequest{
|
|
|
|
Subject: options.Subject,
|
|
|
|
SignatureAlgorithm: x509.PureEd25519,
|
|
|
|
DNSNames: options.DNSNames,
|
|
|
|
IPAddresses: options.IPAddresses,
|
|
|
|
}
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
csr, err := x509.CreateCertificateRequest(rand.Reader, csrTemplate, options.Priv)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := pem.Encode(out, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csr}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return out.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign decodes a CSR and signs it with the CA
|
2021-02-13 15:35:56 +03:00
|
|
|
func Sign(crt, key, csr []byte, opts ...CertOption) ([]byte, error) {
|
2020-04-21 17:03:33 +03:00
|
|
|
options := CertOptions{}
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2021-02-13 15:35:56 +03:00
|
|
|
asn1CACrt, err := decodePEM(crt)
|
2020-04-21 17:03:33 +03:00
|
|
|
if err != nil {
|
2020-08-25 13:44:41 +03:00
|
|
|
return nil, fmt.Errorf("failed to decode CA Crt PEM: %w", err)
|
2020-04-21 17:03:33 +03:00
|
|
|
}
|
|
|
|
if len(asn1CACrt) != 1 {
|
2020-08-25 13:44:41 +03:00
|
|
|
return nil, fmt.Errorf("expected 1 CA Crt, got %d", len(asn1CACrt))
|
2020-04-21 17:03:33 +03:00
|
|
|
}
|
|
|
|
caCrt, err := x509.ParseCertificate(asn1CACrt[0].Bytes)
|
|
|
|
if err != nil {
|
2020-08-25 13:44:41 +03:00
|
|
|
return nil, fmt.Errorf("ca is not a valid certificate: %w", err)
|
2020-04-21 17:03:33 +03:00
|
|
|
}
|
2021-02-13 15:35:56 +03:00
|
|
|
asn1CAKey, err := decodePEM(key)
|
2020-04-21 17:03:33 +03:00
|
|
|
if err != nil {
|
2020-08-25 13:44:41 +03:00
|
|
|
return nil, fmt.Errorf("failed to decode CA Key PEM: %w", err)
|
2020-04-21 17:03:33 +03:00
|
|
|
}
|
|
|
|
if len(asn1CAKey) != 1 {
|
2020-08-25 13:44:41 +03:00
|
|
|
return nil, fmt.Errorf("expected 1 CA Key, got %d", len(asn1CACrt))
|
2020-04-21 17:03:33 +03:00
|
|
|
}
|
|
|
|
caKey, err := x509.ParsePKCS8PrivateKey(asn1CAKey[0].Bytes)
|
|
|
|
if err != nil {
|
2020-08-25 13:44:41 +03:00
|
|
|
return nil, fmt.Errorf("ca key is not a valid private key: %w", err)
|
2020-04-21 17:03:33 +03:00
|
|
|
}
|
2021-02-13 15:35:56 +03:00
|
|
|
asn1CSR, err := decodePEM(csr)
|
2020-04-21 17:03:33 +03:00
|
|
|
if err != nil {
|
2020-08-25 13:44:41 +03:00
|
|
|
return nil, fmt.Errorf("failed to decode CSR PEM: %w", err)
|
2020-04-21 17:03:33 +03:00
|
|
|
}
|
|
|
|
if len(asn1CSR) != 1 {
|
2020-08-25 13:44:41 +03:00
|
|
|
return nil, fmt.Errorf("expected 1 CSR, got %d", len(asn1CSR))
|
2020-04-21 17:03:33 +03:00
|
|
|
}
|
2021-02-13 15:35:56 +03:00
|
|
|
caCsr, err := x509.ParseCertificateRequest(asn1CSR[0].Bytes)
|
2020-04-21 17:03:33 +03:00
|
|
|
if err != nil {
|
2020-08-25 13:44:41 +03:00
|
|
|
return nil, fmt.Errorf("csr is invalid: %w", err)
|
2020-04-21 17:03:33 +03:00
|
|
|
}
|
|
|
|
template := &x509.Certificate{
|
|
|
|
SignatureAlgorithm: x509.PureEd25519,
|
2021-02-13 15:35:56 +03:00
|
|
|
Subject: caCsr.Subject,
|
|
|
|
DNSNames: caCsr.DNSNames,
|
|
|
|
IPAddresses: caCsr.IPAddresses,
|
2020-04-21 17:03:33 +03:00
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
NotBefore: options.NotBefore,
|
|
|
|
NotAfter: options.NotAfter,
|
|
|
|
SerialNumber: options.SerialNumber,
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
x509Cert, err := x509.CreateCertificate(rand.Reader, template, caCrt, caCrt.PublicKey, caKey)
|
|
|
|
if err != nil {
|
2020-08-25 13:44:41 +03:00
|
|
|
return nil, fmt.Errorf("Couldn't sign certificate: %w", err)
|
2020-04-21 17:03:33 +03:00
|
|
|
}
|
|
|
|
out := &bytes.Buffer{}
|
|
|
|
if err := pem.Encode(out, &pem.Block{Type: "CERTIFICATE", Bytes: x509Cert}); err != nil {
|
2020-08-25 13:44:41 +03:00
|
|
|
return nil, fmt.Errorf("couldn't encode cert: %w", err)
|
2020-04-21 17:03:33 +03:00
|
|
|
}
|
|
|
|
return out.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2020-08-25 15:41:48 +03:00
|
|
|
//nolint:gocritic
|
2020-04-21 17:03:33 +03:00
|
|
|
func decodePEM(PEM []byte) ([]*pem.Block, error) {
|
|
|
|
var blocks []*pem.Block
|
|
|
|
var asn1 *pem.Block
|
|
|
|
var rest []byte
|
|
|
|
for {
|
|
|
|
asn1, rest = pem.Decode(PEM)
|
|
|
|
if asn1 == nil {
|
|
|
|
return nil, errors.New("PEM is not valid")
|
|
|
|
}
|
|
|
|
blocks = append(blocks, asn1)
|
|
|
|
if len(rest) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return blocks, nil
|
|
|
|
}
|