-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmain.go
172 lines (140 loc) · 4.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"context"
"crypto"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/asn1"
"encoding/base64"
"encoding/pem"
"flag"
"fmt"
"io"
"log"
"os"
"golang.org/x/oauth2/google"
"google.golang.org/api/cloudkms/v1"
)
var (
oidEmailAddress = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1}
)
func main() {
keyFlag := flag.String("key", "", "")
commonNameFlag := flag.String("common-name", "", "")
orgFlag := flag.String("org", "", "")
emailFlag := flag.String("email", "", "")
outFlag := flag.String("out", "out.csr", "")
orgUnitFlag := flag.String("org-unit","", "")
countryFlag := flag.String("country","US", "")
provinceFlag := flag.String("province","California", "")
localityFlag := flag.String("locality","San Francisco", "")
flag.Parse()
oauthClient, err := google.DefaultClient(context.Background(), cloudkms.CloudPlatformScope)
if err != nil {
log.Fatal(err)
}
kmsService, err := cloudkms.New(oauthClient)
if err != nil {
log.Fatal(err)
}
s, err := NewGoogleKMSSigner(kmsService, *keyFlag)
if err != nil {
log.Fatal(err)
}
subj := pkix.Name{
CommonName: *commonNameFlag,
Organization: []string{*orgFlag},
OrganizationalUnit: []string{*orgUnitFlag},
Country: []string{*countryFlag},
Province: []string{*provinceFlag},
Locality: []string{*localityFlag},
}
rawSubj := subj.ToRDNSequence()
template := &x509.CertificateRequest{}
if *emailFlag != "" {
rawSubj = append(rawSubj, []pkix.AttributeTypeAndValue{
{Type: oidEmailAddress, Value: *emailFlag},
})
template.EmailAddresses = []string{*emailFlag}
}
asn1Subj, err := asn1.Marshal(rawSubj)
if err != nil {
log.Fatal(err)
}
template.RawSubject = asn1Subj
// TODO Make this a flag or read from s.PublicKey?
// https://cloud.google.com/kms/docs/algorithms
// https://cloud.google.com/kms/docs/reference/rest/v1/projects.locations.keyRings.cryptoKeys#CryptoKeyVersionTemplate
template.SignatureAlgorithm = x509.ECDSAWithSHA256 // x509.SHA256WithRSAPSS
f, err := os.Create(*outFlag)
if err != nil {
log.Fatal(err)
}
defer f.Close()
if err := CreateCertificateRequest(f, template, s); err != nil {
log.Fatal(err)
}
}
func CreateCertificateRequest(w io.Writer, template *x509.CertificateRequest, signer crypto.Signer) error {
out, err := x509.CreateCertificateRequest(rand.Reader, template, signer)
if err != nil {
return err
}
return pem.Encode(w, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: out})
}
type GoogleKMS struct {
Client *cloudkms.Service
keyResourceId string
publicKey crypto.PublicKey
}
func NewGoogleKMSSigner(client *cloudkms.Service, keyResourceId string) (*GoogleKMS, error) {
g := &GoogleKMS{
keyResourceId: keyResourceId,
Client: client,
}
err := g.getAsymmetricPublicKey()
if err != nil {
return nil, err
}
return g, nil
}
// Public returns the Public Key from Google Cloud KMS
func (g *GoogleKMS) Public() crypto.PublicKey {
return g.publicKey
}
// Sign calls Google Cloud KMS API and performs AsymmetricSign
func (g *GoogleKMS) Sign(rand io.Reader, digest []byte, opts crypto.SignerOpts) (signature []byte, err error) {
// API expects the digest to be base64 encoded
digest64 := base64.StdEncoding.EncodeToString(digest)
req := &cloudkms.AsymmetricSignRequest{
Digest: &cloudkms.Digest{
Sha256: digest64, // TODO: sha256 needs to follow sign algo
},
}
response, err := g.Client.Projects.Locations.KeyRings.CryptoKeys.CryptoKeyVersions.
AsymmetricSign(g.keyResourceId, req).Context(context.Background()).Do()
if err != nil {
return nil, err
}
// The response signature is base64 encoded
return base64.StdEncoding.DecodeString(response.Signature)
}
// getAsymmetricPublicKey pulls public key from Google Cloud KMS API
func (g *GoogleKMS) getAsymmetricPublicKey() error {
response, err := g.Client.Projects.Locations.KeyRings.CryptoKeys.CryptoKeyVersions.
GetPublicKey(g.keyResourceId).Context(context.Background()).Do()
if err != nil {
return err
}
block, _ := pem.Decode([]byte(response.Pem))
if block == nil || block.Type != "PUBLIC KEY" {
return fmt.Errorf("not a public key")
}
publicKey, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return err
}
g.publicKey = publicKey
return nil
}