Debian Codegraph @

cmdline/token/x509cmd.go

raw symbols SCIP
1 //
2 // Copyright (c) SAS Institute Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 package token
18
19 import (
20 "crypto/rand"
21 "errors"
22 "fmt"
23 "io/ioutil"
24 "os"
25
26 "github.com/spf13/cobra"
27
28 "github.com/sassoftware/relic/v7/cmdline/shared"
29 "github.com/sassoftware/relic/v7/lib/certloader"
30 "github.com/sassoftware/relic/v7/lib/x509tools"
31 )
32
33 var (
34 argCopyExtensions bool
35 argCrossSign bool
36 )
37
38 var ReqCmd = &cobra.Command{
39 Use: "x509-request",
40 Short: "Generate PKCS#10 certificate signing request",
41 }
42
43 var SelfSignCmd = &cobra.Command{
44 Use: "x509-self-sign",
45 Short: "Generate self-signed X509 certificate",
46 }
47
48 var SignCsrCmd = &cobra.Command{
49 Use: "x509-sign",
50 Short: "Create a X509 certificate from a certificate signing request",
51 RunE: signCsrCmd,
52 }
53
54 func init() {
55 ReqCmd.RunE = x509Cmd
56 shared.RootCmd.AddCommand(ReqCmd)
57 addSelectOrGenerateFlags(ReqCmd)
58 x509tools.AddRequestFlags(ReqCmd)
59
60 SelfSignCmd.RunE = x509Cmd
61 shared.RootCmd.AddCommand(SelfSignCmd)
62 addSelectOrGenerateFlags(SelfSignCmd)
63 x509tools.AddCertFlags(SelfSignCmd)
64
65 shared.RootCmd.AddCommand(SignCsrCmd)
66 addKeyFlags(SignCsrCmd)
67 x509tools.AddCertFlags(SignCsrCmd)
68 SignCsrCmd.Flags().BoolVar(&argCopyExtensions, "copy-extensions", false, "Copy extensions verbabim from CSR")
69 SignCsrCmd.Flags().BoolVar(&argCrossSign, "cross-sign", false, "Input is an existing certificate (implies --copy-extensions)")
70 }
71
72 func x509Cmd(cmd *cobra.Command, args []string) error {
73 if x509tools.ArgCommonName == "" {
74 return errors.New("--commonName is required")
75 }
76 key, err := selectOrGenerate()
77 if err != nil {
78 return err
79 }
80 var result string
81 if cmd == ReqCmd {
82 result, err = x509tools.MakeRequest(rand.Reader, key)
83 } else {
84 result, err = x509tools.MakeCertificate(rand.Reader, key)
85 }
86 if err != nil {
87 return err
88 }
89 os.Stdout.WriteString(result)
90 if ckaID := key.GetID(); len(ckaID) != 0 {
91 fmt.Println("CKA_ID:", formatKeyID(ckaID))
92 }
93 return nil
94 }
95
96 func signCsrCmd(cmd *cobra.Command, args []string) error {
97 if len(args) != 1 {
98 return errors.New("expected a CSR file as input")
99 }
100 csr, err := ioutil.ReadFile(args[0])
101 if err != nil {
102 return err
103 }
104 key, err := openKey(argKeyName)
105 if err != nil {
106 return err
107 }
108 parsedCert := key.Certificate()
109 certPath := key.Config().X509Certificate
110 if certPath == "" && len(parsedCert) == 0 {
111 return errors.New("token key has no x509 certificate")
112 }
113 cert, err := certloader.LoadTokenCertificates(key, certPath, "", parsedCert)
114 if err != nil {
115 return err
116 }
117 if argCrossSign {
118 result, err := x509tools.CrossSign(csr, rand.Reader, key, cert.Leaf)
119 if err != nil {
120 return err
121 }
122 os.Stdout.WriteString(result)
123 } else {
124 result, err := x509tools.SignCSR(csr, rand.Reader, key, cert.Leaf, argCopyExtensions)
125 if err != nil {
126 return err
127 }
128 os.Stdout.WriteString(result)
129 }
130 return nil
131 }