...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package main
20
21 import (
22 "crypto"
23 "crypto/rsa"
24 "crypto/x509"
25 "encoding/gob"
26 "io"
27 "log"
28 "net/rpc"
29 "os"
30 "time"
31
32 "github.com/googleapis/enterprise-certificate-proxy/internal/signer/linux/pkcs11"
33 "github.com/googleapis/enterprise-certificate-proxy/internal/signer/util"
34 )
35
36
37
38 func enableECPLogging() bool {
39 if os.Getenv("ENABLE_ENTERPRISE_CERTIFICATE_LOGS") != "" {
40 return true
41 }
42
43 log.SetOutput(io.Discard)
44 return false
45 }
46
47 func init() {
48 gob.Register(crypto.SHA256)
49 gob.Register(crypto.SHA384)
50 gob.Register(crypto.SHA512)
51 gob.Register(&rsa.PSSOptions{})
52 gob.Register(&rsa.OAEPOptions{})
53 }
54
55
56 type SignArgs struct {
57 Digest []byte
58 Opts crypto.SignerOpts
59 }
60
61
62 type EncryptArgs struct {
63 Plaintext []byte
64 Opts any
65 }
66
67
68 type DecryptArgs struct {
69 Ciphertext []byte
70 Opts crypto.DecrypterOpts
71 }
72
73
74 type EnterpriseCertSigner struct {
75 key *pkcs11.Key
76 }
77
78
79 type Connection struct {
80 io.ReadCloser
81 io.WriteCloser
82 }
83
84
85 func (c *Connection) Close() error {
86 rerr := c.ReadCloser.Close()
87 werr := c.WriteCloser.Close()
88 if rerr != nil {
89 return rerr
90 }
91 return werr
92 }
93
94
95
96 func (k *EnterpriseCertSigner) CertificateChain(ignored struct{}, certificateChain *[][]byte) (err error) {
97 *certificateChain = k.key.CertificateChain()
98 return nil
99 }
100
101
102 func (k *EnterpriseCertSigner) Public(ignored struct{}, publicKey *[]byte) (err error) {
103 *publicKey, err = x509.MarshalPKIXPublicKey(k.key.Public())
104 return
105 }
106
107
108 func (k *EnterpriseCertSigner) Sign(args SignArgs, resp *[]byte) (err error) {
109 *resp, err = k.key.Sign(nil, args.Digest, args.Opts)
110 return
111 }
112
113
114 func (k *EnterpriseCertSigner) Encrypt(args EncryptArgs, resp *[]byte) (err error) {
115 *resp, err = k.key.Encrypt(args.Plaintext, args.Opts)
116 return
117 }
118
119
120 func (k *EnterpriseCertSigner) Decrypt(args DecryptArgs, resp *[]byte) (err error) {
121 *resp, err = k.key.Decrypt(args.Ciphertext, args.Opts)
122 return
123 }
124
125 func main() {
126 enableECPLogging()
127 if len(os.Args) != 2 {
128 log.Fatalln("Signer is not meant to be invoked manually, exiting...")
129 }
130 configFilePath := os.Args[1]
131 config, err := util.LoadConfig(configFilePath)
132 if err != nil {
133 log.Fatalf("Failed to load enterprise cert config: %v", err)
134 }
135
136 enterpriseCertSigner := new(EnterpriseCertSigner)
137 enterpriseCertSigner.key, err = pkcs11.Cred(config.CertConfigs.PKCS11.PKCS11Module, config.CertConfigs.PKCS11.Slot, config.CertConfigs.PKCS11.Label, config.CertConfigs.PKCS11.UserPin)
138 if err != nil {
139 log.Fatalf("Failed to initialize enterprise cert signer using pkcs11: %v", err)
140 }
141
142 if err := rpc.Register(enterpriseCertSigner); err != nil {
143 log.Fatalf("Failed to register enterprise cert signer with net/rpc: %v", err)
144 }
145
146
147
148
149 go func() {
150 for {
151 if os.Getppid() == 1 {
152 log.Fatalln("Enterprise cert signer's parent process died, exiting...")
153 }
154 time.Sleep(time.Second)
155 }
156 }()
157
158 rpc.ServeConn(&Connection{os.Stdin, os.Stdout})
159 }
160
View as plain text