Debian Codegraph @

fakeca/identity.go

raw symbols SCIP
1 package fakeca
2
3 import (
4 "bytes"
5 "crypto"
6 "crypto/ecdsa"
7 "crypto/rsa"
8 "crypto/x509"
9 "encoding/pem"
10 "errors"
11 "fmt"
12 "os/exec"
13 )
14
15 // Identity is a certificate and private key.
16 type Identity struct {
17 Issuer *Identity
18 PrivateKey crypto.Signer
19 Certificate *x509.Certificate
20 NextSN int64
21 }
22
23 // New creates a new CA.
24 func New(opts ...Option) *Identity {
25 c := &configuration{}
26
27 for _, opt := range opts {
28 option(opt)(c)
29 }
30
31 return c.generate()
32 }
33
34 // Issue issues a new Identity with this one as its parent.
35 func (id *Identity) Issue(opts ...Option) *Identity {
36 opts = append(opts, Issuer(id))
37 return New(opts...)
38 }
39
40 // PFX wraps the certificate and private key in an encrypted PKCS#12 packet. The
41 // provided password must be alphanumeric.
42 func (id *Identity) PFX(password string) []byte {
43 return toPFX(id.Certificate, id.PrivateKey, password)
44 }
45
46 // Chain builds a slice of *x509.Certificate from this CA and its issuers.
47 func (id *Identity) Chain() []*x509.Certificate {
48 chain := []*x509.Certificate{}
49 for this := id; this != nil; this = this.Issuer {
50 chain = append(chain, this.Certificate)
51 }
52
53 return chain
54 }
55
56 // ChainPool builds an *x509.CertPool from this CA and its issuers.
57 func (id *Identity) ChainPool() *x509.CertPool {
58 chain := x509.NewCertPool()
59 for this := id; this != nil; this = this.Issuer {
60 chain.AddCert(this.Certificate)
61 }
62
63 return chain
64 }
65
66 // IncrementSN returns the next serial number.
67 func (id *Identity) IncrementSN() int64 {
68 defer func() {
69 id.NextSN++
70 }()
71
72 return id.NextSN
73 }
74
75 func toPFX(cert *x509.Certificate, priv interface{}, password string) []byte {
76 // only allow alphanumeric passwords
77 for _, c := range password {
78 switch {
79 case c >= 'a' && c <= 'z':
80 case c >= 'A' && c <= 'Z':
81 case c >= '0' && c <= '9':
82 default:
83 panic("password must be alphanumeric")
84 }
85 }
86
87 passout := fmt.Sprintf("pass:%s", password)
88 cmd := exec.Command("openssl", "pkcs12", "-export", "-passout", passout)
89
90 cmd.Stdin = bytes.NewReader(append(append(toPKCS8(priv), '\n'), toPEM(cert)...))
91
92 out := new(bytes.Buffer)
93 cmd.Stdout = out
94
95 if err := cmd.Run(); err != nil {
96 panic(err)
97 }
98
99 return out.Bytes()
100 }
101
102 func toPEM(cert *x509.Certificate) []byte {
103 buf := new(bytes.Buffer)
104 if err := pem.Encode(buf, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}); err != nil {
105 panic(err)
106 }
107
108 return buf.Bytes()
109 }
110
111 func toDER(priv interface{}) []byte {
112 var (
113 der []byte
114 err error
115 )
116 switch p := priv.(type) {
117 case *rsa.PrivateKey:
118 der = x509.MarshalPKCS1PrivateKey(p)
119 case *ecdsa.PrivateKey:
120 der, err = x509.MarshalECPrivateKey(p)
121 default:
122 err = errors.New("unknown key type")
123 }
124 if err != nil {
125 panic(err)
126 }
127
128 return der
129 }
130
131 func toPKCS8(priv interface{}) []byte {
132 cmd := exec.Command("openssl", "pkcs8", "-topk8", "-nocrypt", "-inform", "DER")
133
134 cmd.Stdin = bytes.NewReader(toDER(priv))
135
136 out := new(bytes.Buffer)
137 cmd.Stdout = out
138
139 if err := cmd.Run(); err != nil {
140 panic(err)
141 }
142
143 return out.Bytes()
144 }