Debian Codegraph @

cmdline/token/newpgpkeycmd.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"
21 "crypto/ecdsa"
22 "crypto/rsa"
23 "encoding/hex"
24 "errors"
25 "fmt"
26 "os"
27 "strings"
28 "time"
29
30 "github.com/spf13/cobra"
31 "golang.org/x/crypto/openpgp"
32 "golang.org/x/crypto/openpgp/armor"
33 "golang.org/x/crypto/openpgp/packet"
34
35 "github.com/sassoftware/relic/v7/cmdline/shared"
36 "github.com/sassoftware/relic/v7/token"
37 )
38
39 var NewPgpKeyCmd = &cobra.Command{
40 Use: "pgp-generate",
41 Short: "Generate a new PGP key from token",
42 RunE: newPgpKeyCmd,
43 }
44
45 var (
46 argUserName string
47 argUserComment string
48 argUserEmail string
49 )
50
51 func init() {
52 shared.RootCmd.AddCommand(NewPgpKeyCmd)
53 NewPgpKeyCmd.Flags().StringVarP(&argUserName, "name", "n", "", "Name of user identity")
54 NewPgpKeyCmd.Flags().StringVarP(&argUserComment, "comment", "C", "", "Comment of user identity")
55 NewPgpKeyCmd.Flags().StringVarP(&argUserEmail, "email", "E", "", "Email of user identity")
56 addSelectOrGenerateFlags(NewPgpKeyCmd)
57 }
58
59 func makeKey(key token.Key, uids []*packet.UserId) (*openpgp.Entity, error) {
60 creationTime := time.Now()
61 var pubKey *packet.PublicKey
62 switch pub := key.Public().(type) {
63 case *rsa.PublicKey:
64 pubKey = packet.NewRSAPublicKey(creationTime, pub)
65 case *ecdsa.PublicKey:
66 pubKey = packet.NewECDSAPublicKey(creationTime, pub)
67 default:
68 return nil, errors.New("Unsupported key type")
69 }
70 entity := &openpgp.Entity{
71 PrimaryKey: pubKey,
72 PrivateKey: &packet.PrivateKey{
73 PublicKey: *pubKey,
74 Encrypted: false,
75 PrivateKey: key,
76 },
77 Identities: make(map[string]*openpgp.Identity),
78 }
79 isPrimaryID := true
80 for _, uid := range uids {
81 sig := &packet.Signature{
82 SigType: packet.SigTypePositiveCert,
83 CreationTime: creationTime,
84 PubKeyAlgo: pubKey.PubKeyAlgo,
85 Hash: crypto.SHA512,
86 IsPrimaryId: &isPrimaryID,
87 FlagsValid: true,
88 FlagSign: true,
89 FlagCertify: true,
90 IssuerKeyId: &pubKey.KeyId,
91 }
92 err := sig.SignUserId(uid.Id, entity.PrimaryKey, entity.PrivateKey, nil)
93 if err != nil {
94 return nil, err
95 }
96 entity.Identities[uid.Id] = &openpgp.Identity{
97 Name: uid.Name,
98 UserId: uid,
99 SelfSignature: sig,
100 }
101 }
102 return entity, nil
103 }
104
105 func newPgpKeyCmd(cmd *cobra.Command, args []string) error {
106 if argUserName == "" {
107 return errors.New("--name is required")
108 }
109 uid := packet.NewUserId(argUserName, argUserComment, argUserEmail)
110 if uid == nil {
111 return errors.New("Invalid user ID")
112 }
113 key, err := selectOrGenerate()
114 if err != nil {
115 return err
116 }
117 entity, err := makeKey(key, []*packet.UserId{uid})
118 if err != nil {
119 return err
120 }
121 fingerprint := hex.EncodeToString(entity.PrimaryKey.Fingerprint[:])
122 fmt.Fprintln(os.Stderr, "Token CKA_ID: ", formatKeyID(key.GetID()))
123 fmt.Fprintln(os.Stderr, "PGP ID: ", strings.ToUpper(fingerprint))
124 writer, err := armor.Encode(os.Stdout, openpgp.PublicKeyType, nil)
125 if err != nil {
126 return err
127 }
128 err = entity.Serialize(writer)
129 if err != nil {
130 return err
131 }
132 writer.Close()
133 fmt.Println()
134 return nil
135 }