2018-04-09 06:42:18 -04:00
|
|
|
package cert
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
|
|
|
"math/big"
|
|
|
|
"time"
|
2018-04-10 06:42:02 -04:00
|
|
|
|
|
|
|
"v2ray.com/core/common"
|
2018-04-09 06:42:18 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:generate go run $GOPATH/src/v2ray.com/core/common/errors/errorgen/main.go -pkg cert -path Protocol,TLS,Cert
|
|
|
|
|
|
|
|
type Certificate struct {
|
2018-04-10 06:42:02 -04:00
|
|
|
// Cerificate in ASN.1 DER format
|
2018-04-09 06:42:18 -04:00
|
|
|
Certificate []byte
|
2018-04-10 06:42:02 -04:00
|
|
|
// Private key in ASN.1 DER format
|
2018-04-09 06:42:18 -04:00
|
|
|
PrivateKey []byte
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:42:02 -04:00
|
|
|
func ParseCertificate(certPEM []byte, keyPEM []byte) (*Certificate, error) {
|
|
|
|
certBlock, _ := pem.Decode(certPEM)
|
|
|
|
if certBlock == nil {
|
|
|
|
return nil, newError("failed to decode certificate")
|
|
|
|
}
|
|
|
|
keyBlock, _ := pem.Decode(keyPEM)
|
|
|
|
if keyBlock == nil {
|
|
|
|
return nil, newError("failed to decode key")
|
|
|
|
}
|
|
|
|
return &Certificate{
|
|
|
|
Certificate: certBlock.Bytes,
|
|
|
|
PrivateKey: keyBlock.Bytes,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Certificate) ToPEM() ([]byte, []byte) {
|
|
|
|
return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: c.Certificate}),
|
|
|
|
pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: c.PrivateKey})
|
|
|
|
}
|
|
|
|
|
2018-04-09 06:42:18 -04:00
|
|
|
type Option func(*x509.Certificate)
|
|
|
|
|
|
|
|
func Authority(isCA bool) Option {
|
|
|
|
return func(cert *x509.Certificate) {
|
|
|
|
cert.IsCA = isCA
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-09 17:17:55 -04:00
|
|
|
func NotBefore(t time.Time) Option {
|
|
|
|
return func(c *x509.Certificate) {
|
|
|
|
c.NotBefore = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NotAfter(t time.Time) Option {
|
|
|
|
return func(c *x509.Certificate) {
|
|
|
|
c.NotAfter = t
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DNSNames(names ...string) Option {
|
|
|
|
return func(c *x509.Certificate) {
|
|
|
|
c.DNSNames = names
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func CommonName(name string) Option {
|
|
|
|
return func(c *x509.Certificate) {
|
|
|
|
c.Subject.CommonName = name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:42:02 -04:00
|
|
|
func KeyUsage(usage x509.KeyUsage) Option {
|
|
|
|
return func(c *x509.Certificate) {
|
|
|
|
c.KeyUsage = usage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-12 15:43:05 -04:00
|
|
|
func Organization(org string) Option {
|
|
|
|
return func(c *x509.Certificate) {
|
|
|
|
c.Subject.Organization = []string{org}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:42:02 -04:00
|
|
|
func MustGenerate(parent *Certificate, opts ...Option) *Certificate {
|
|
|
|
cert, err := Generate(parent, opts...)
|
|
|
|
common.Must(err)
|
|
|
|
return cert
|
|
|
|
}
|
|
|
|
|
|
|
|
func Generate(parent *Certificate, opts ...Option) (*Certificate, error) {
|
|
|
|
selfKey, err := rsa.GenerateKey(rand.Reader, 2048)
|
2018-04-09 17:17:55 -04:00
|
|
|
if err != nil {
|
2018-04-10 06:42:02 -04:00
|
|
|
return nil, newError("failed to generate self private key").Base(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
parentKey := selfKey
|
|
|
|
if parent != nil {
|
|
|
|
pKey, err := x509.ParsePKCS1PrivateKey(parent.PrivateKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to parse parent private key").Base(err)
|
|
|
|
}
|
|
|
|
parentKey = pKey
|
2018-04-09 17:17:55 -04:00
|
|
|
}
|
2018-04-09 06:42:18 -04:00
|
|
|
|
|
|
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
|
|
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
|
|
|
if err != nil {
|
2018-04-09 17:17:55 -04:00
|
|
|
return nil, newError("failed to generate serial number").Base(err)
|
2018-04-09 06:42:18 -04:00
|
|
|
}
|
|
|
|
|
2018-04-09 17:17:55 -04:00
|
|
|
template := &x509.Certificate{
|
|
|
|
SerialNumber: serialNumber,
|
2018-04-09 06:42:18 -04:00
|
|
|
NotBefore: time.Now().Add(time.Hour * -1),
|
|
|
|
NotAfter: time.Now().Add(time.Hour),
|
|
|
|
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
|
|
|
BasicConstraintsValid: true,
|
|
|
|
}
|
|
|
|
|
2018-04-09 17:17:55 -04:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(template)
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:42:02 -04:00
|
|
|
parentCert := template
|
|
|
|
if parent != nil {
|
|
|
|
pCert, err := x509.ParseCertificate(parent.Certificate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to parse parent certificate").Base(err)
|
|
|
|
}
|
|
|
|
parentCert = pCert
|
2018-04-09 17:17:55 -04:00
|
|
|
}
|
|
|
|
|
2018-04-10 06:42:02 -04:00
|
|
|
derBytes, err := x509.CreateCertificate(rand.Reader, template, parentCert, selfKey.Public(), parentKey)
|
2018-04-09 17:17:55 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, newError("failed to create certificate").Base(err)
|
|
|
|
}
|
2018-04-09 06:42:18 -04:00
|
|
|
|
2018-04-09 17:17:55 -04:00
|
|
|
return &Certificate{
|
2018-04-10 06:42:02 -04:00
|
|
|
Certificate: derBytes,
|
|
|
|
PrivateKey: x509.MarshalPKCS1PrivateKey(selfKey),
|
2018-04-09 06:42:18 -04:00
|
|
|
}, nil
|
|
|
|
}
|