@@ -12,8 +12,8 @@ import (
1212
1313 "github.com/plgd-dev/kit/security/generateCertificate"
1414
15- "github.com/plgd-dev/kit/security"
1615 flags "github.com/jessevdk/go-flags"
16+ "github.com/plgd-dev/kit/security"
1717)
1818
1919type Options struct {
@@ -22,10 +22,12 @@ type Options struct {
2222 GenerateIntermediateCA bool `long:"generateIntermediateCA"`
2323 GenerateCert bool `long:"generateCertificate"`
2424 GenerateIdentity string `long:"generateIdentityCertificate" description:"deviceID"`
25+ GenerateIdentityCsr string `long:"generateIdentityCsr" description:"deviceID"`
2526 } `group:"Command" namespace:"cmd"`
2627 Certificate generateCertificate.Configuration `group:"Certificate" namespace:"cert"`
2728 OutCert string `long:"outCert" default:"cert.pem"`
2829 OutKey string `long:"outKey" default:"cert.key"`
30+ OutCsr string `long:"outCsr" default:"req.csr"`
2931 SignerCert string `long:"signerCert"`
3032 SignerKey string `long:"signerKey"`
3133}
@@ -60,6 +62,8 @@ func main() {
6062 if err != nil {
6163 log .Fatal (err )
6264 }
65+ WriteCertOut (opts , cert )
66+ WritePrivateKey (opts , priv )
6367 case opts .Command .GenerateIntermediateCA :
6468 signerCert , err := security .LoadX509 (opts .SignerCert )
6569 if err != nil {
@@ -73,6 +77,8 @@ func main() {
7377 if err != nil {
7478 log .Fatal (err )
7579 }
80+ WriteCertOut (opts , cert )
81+ WritePrivateKey (opts , priv )
7682 case opts .Command .GenerateCert :
7783 signerCert , err := security .LoadX509 (opts .SignerCert )
7884 if err != nil {
@@ -86,6 +92,8 @@ func main() {
8692 if err != nil {
8793 log .Fatal (err )
8894 }
95+ WriteCertOut (opts , cert )
96+ WritePrivateKey (opts , priv )
8997 case opts .Command .GenerateIdentity != "" :
9098 signerCert , err := security .LoadX509 (opts .SignerCert )
9199 if err != nil {
@@ -99,12 +107,23 @@ func main() {
99107 if err != nil {
100108 log .Fatal (err )
101109 }
110+ WriteCertOut (opts , cert )
111+ WritePrivateKey (opts , priv )
112+ case opts .Command .GenerateIdentityCsr != "" :
113+ csr , err := generateCertificate .GenerateIdentityCSR (opts .Certificate , opts .Command .GenerateIdentityCsr , priv )
114+ if err != nil {
115+ log .Fatal (err )
116+ }
117+ WritePrivateKey (opts , priv )
118+ WriteCsrOut (opts , csr )
102119 default :
103120 fmt .Println ("invalid command" )
104121 parser .WriteHelp (os .Stdout )
105122 os .Exit (2 )
106123 }
124+ }
107125
126+ func WriteCertOut (opts Options , cert []byte ) {
108127 certOut , err := os .Create (opts .OutCert )
109128 if err != nil {
110129 log .Fatalf ("failed to open %v for writing: %s" , opts .OutCert , err )
@@ -116,7 +135,23 @@ func main() {
116135 if err := certOut .Close (); err != nil {
117136 log .Fatalf ("error closing %v: %s" , opts .OutCert , err )
118137 }
138+ }
139+
140+ func WriteCsrOut (opts Options , csr []byte ) {
141+ csrOut , err := os .Create (opts .OutCsr )
142+ if err != nil {
143+ log .Fatalf ("failed to open %v for writing: %s" , opts .OutCsr , err )
144+ }
145+ _ , err = csrOut .Write (csr )
146+ if err != nil {
147+ log .Fatalf ("failed to write %v: %s" , opts .OutCsr , err )
148+ }
149+ if err := csrOut .Close (); err != nil {
150+ log .Fatalf ("error closing %v: %s" , opts .OutCert , err )
151+ }
152+ }
119153
154+ func WritePrivateKey (opts Options , priv * ecdsa.PrivateKey ) {
120155 privBlock , err := pemBlockForKey (priv )
121156 if err != nil {
122157 log .Fatalf ("failed to encode priv key %v for writing: %v" , opts .OutKey , err )
@@ -134,3 +169,4 @@ func main() {
134169 log .Fatalf ("error closing %v: %s" , opts .OutKey , err )
135170 }
136171}
172+
0 commit comments