19537d6d4c2cd1254d00c6ac9f8f20bd9ae7d1cb
[epoint] / pkg / key / key.go
1 // Package key implements epoint key pair generation and handling.
2 //
3 // An epoint key is an OpenPGP signing key that contains a self-signed
4 // user id packet which matches
5 //     "Issuer (<denomination>)"
6 // or
7 //     "Holder of <issuer fpr> (<denomination>)"
8 //
9 // The OpenPGP DSA key material is generated from a random seed using
10 // a deterministic algorithm. (The self-signature is not deterministic
11 // but the key material and thus the fingerprint is.)
12 // This makes it possible to represent an obligation issuer or holder key
13 // pair with a few bits of secret random seed.
14 // (The user id only needs to be set up correctly when the key is uploaded
15 // to the epoint server, it is not required for signing draft documents.)
16 package key
17
18 import (
19         "bytes"
20         "crypto"
21         "crypto/dsa"
22         "crypto/openpgp"
23         "crypto/openpgp/packet"
24         "crypto/rand"
25         "crypto/sha1"
26         "fmt"
27         "io"
28         "math/big"
29         "time"
30 )
31
32 // TODO: keep denomination only in issuer key?
33 // TODO: cleanup
34 // TODO: server key
35
36 const P = "A4D2B9575C25F0E622B8694387128A793E1AD27D12FFF4B5BA11A37CEFD31C935BCBB0A944581A6E6DA12986FCBA9D666607D71D365C286B9BCB57F6D938BE74982B7D770CE438F03B0A20ABA02E5691458C39D96E6E86AE564176ED1A6DFBAFB6EE7674CC5EDCF9FEB6158471FB3FAB53BA1CE1BA64C5626B9E8585FCEF5D31"
37 const Q = "FFFFFFFFFFFFFFFFFFFF254EAF9E7916D607AAAF"
38 const G = "7EA5C898777BE4BB29DCDC47289E718F7274C9CD7E570D3D552F3B3EE43C3DEF7BA68E57786926520CCAC71DBA13F37C4064395D5AF3334A04ABD8CED5E7FF476C661953936E8ADDE96A39D8C4AC1080A2BE3FE863A24B08BD43827E54AFADA72433704EA3C12E50E5BD08C130C68A1402FC20DA79CFE0DE931C414348D32B10"
39
40 // Calculate DSA private key from given random seed r
41 func DsaKey(r []byte) *dsa.PrivateKey {
42         priv := new(dsa.PrivateKey)
43         priv.Parameters.P, _ = new(big.Int).SetString(P, 16)
44         priv.Parameters.Q, _ = new(big.Int).SetString(Q, 16)
45         priv.Parameters.G, _ = new(big.Int).SetString(G, 16)
46
47         x := new(big.Int)
48 loop:
49         h := sha1.New()
50         h.Write(r)
51         r = h.Sum(nil)
52         x.SetBytes(r)
53         // TODO: zero out r and h ?
54         if x.Sign() == 0 || x.Cmp(priv.Q) >= 0 {
55                 // very rare
56                 goto loop
57         }
58         priv.X = x
59         priv.Y = new(big.Int)
60         priv.Y.Exp(priv.G, x, priv.P)
61         return priv
62 }
63
64 // Generate a random DSA private key
65 func RandomDsaKey() (priv *dsa.PrivateKey, err error) {
66         r := make([]byte, sha1.Size)
67         _, err = io.ReadFull(rand.Reader, r)
68         priv = DsaKey(r)
69         return
70 }
71
72 // New returns an openpgp.Entity that contains a fresh DSA private key with a
73 // single identity composed of the given full name, comment and email, any of
74 // which may be empty but must not contain any of "()<>\x00".
75 func New(priv *dsa.PrivateKey, t time.Time, name, comment, email string) (e *openpgp.Entity, err error) {
76         uid := packet.NewUserId(name, comment, email)
77         if uid == nil {
78                 return nil, fmt.Errorf("NewEntity: invalid argument: user id field contained invalid characters")
79         }
80         e = &openpgp.Entity{
81                 PrimaryKey: packet.NewDSAPublicKey(t, &priv.PublicKey),
82                 PrivateKey: packet.NewDSAPrivateKey(t, priv),
83                 Identities: make(map[string]*openpgp.Identity),
84         }
85         isPrimaryId := true
86         e.Identities[uid.Id] = &openpgp.Identity{
87                 Name:   uid.Name,
88                 UserId: uid,
89                 SelfSignature: &packet.Signature{
90                         CreationTime: t,
91                         SigType:      packet.SigTypePositiveCert,
92                         PubKeyAlgo:   packet.PubKeyAlgoDSA,
93                         Hash:         crypto.SHA256,
94                         IsPrimaryId:  &isPrimaryId,
95                         FlagsValid:   true,
96                         FlagSign:     true,
97                         FlagCertify:  true,
98                         IssuerKeyId:  &e.PrimaryKey.KeyId,
99                 },
100         }
101         return
102 }
103
104 // Parse armored or binary openpgp public or private key
105 func Parse(d []byte) (e *openpgp.Entity, err error) {
106         elist, err := openpgp.ReadArmoredKeyRing(bytes.NewBuffer(d))
107         if err != nil {
108                 elist1, err1 := openpgp.ReadKeyRing(bytes.NewBuffer(d))
109                 if err1 != nil {
110                         return
111                 }
112                 err = nil
113                 elist = elist1
114         }
115         if len(elist) != 1 {
116                 err = fmt.Errorf("Parse: expected exactly one key")
117                 return
118         }
119         e = elist[0]
120         return
121 }
122
123 // Issuer generates a key for obligation issuer clients from random seed r
124 func Issuer(r []byte, denomination string) (e *openpgp.Entity, err error) {
125         return New(DsaKey(r), time.Unix(0,0), "Issuer", denomination, "")
126 }
127 // Holder generates a key for obligation holder clients from random seed r
128 func Holder(r []byte, issuer, denomination string) (e *openpgp.Entity, err error) {
129         return New(DsaKey(r), time.Unix(0,0), "Holder of "+issuer, denomination, "")
130 }
131 // Server generates a key for the server from random seed r
132 func Server(r []byte) (e *openpgp.Entity, err error) {
133         return New(DsaKey(r), time.Now(), "Server", "", "")
134 }
135
136 // Key id (fingerprint)
137 func Id(e *openpgp.Entity) string {
138         return fmt.Sprintf("%X", e.PrimaryKey.Fingerprint)
139 }
140
141 // Check the issuer and denomination associated with the given pgp key
142 func Check(e *openpgp.Entity) (isIssuer bool, issuer, denomination string, err error) {
143         // allow multiple identities, use the first one that looks like an epoint uid
144         for _, id := range e.Identities {
145                 denomination = id.UserId.Comment
146                 if id.UserId.Name == "Issuer" {
147                         isIssuer = true
148                         issuer = Id(e)
149                         return
150                 }
151                 const prefix = "Holder of "
152                 if id.UserId.Name[:len(prefix)] == prefix {
153                         issuer = id.UserId.Name[len(prefix):]
154                         return
155                 }
156         }
157         err = fmt.Errorf("Check: no valid userid was found")
158         return
159 }