update server and genkey according to new key pkg
[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         "crypto"
20         "crypto/dsa"
21         "crypto/openpgp"
22         "crypto/openpgp/packet"
23         "crypto/rand"
24         "crypto/sha1"
25         "fmt"
26         "io"
27         "math/big"
28 )
29
30 // TODO: keep denomination only in issuer key?
31 // TODO: cleanup
32 // TODO: server key
33
34 const P = "A4D2B9575C25F0E622B8694387128A793E1AD27D12FFF4B5BA11A37CEFD31C935BCBB0A944581A6E6DA12986FCBA9D666607D71D365C286B9BCB57F6D938BE74982B7D770CE438F03B0A20ABA02E5691458C39D96E6E86AE564176ED1A6DFBAFB6EE7674CC5EDCF9FEB6158471FB3FAB53BA1CE1BA64C5626B9E8585FCEF5D31"
35 const Q = "FFFFFFFFFFFFFFFFFFFF254EAF9E7916D607AAAF"
36 const G = "7EA5C898777BE4BB29DCDC47289E718F7274C9CD7E570D3D552F3B3EE43C3DEF7BA68E57786926520CCAC71DBA13F37C4064395D5AF3334A04ABD8CED5E7FF476C661953936E8ADDE96A39D8C4AC1080A2BE3FE863A24B08BD43827E54AFADA72433704EA3C12E50E5BD08C130C68A1402FC20DA79CFE0DE931C414348D32B10"
37
38 // Calculate DSA private key from given random seed r
39 func DsaKey(r []byte) *dsa.PrivateKey {
40         priv := new(dsa.PrivateKey)
41         priv.Parameters.P, _ = new(big.Int).SetString(P, 16)
42         priv.Parameters.Q, _ = new(big.Int).SetString(Q, 16)
43         priv.Parameters.G, _ = new(big.Int).SetString(G, 16)
44
45         x := new(big.Int)
46 loop:
47         h := sha1.New()
48         h.Write(r)
49         r = h.Sum()
50         x.SetBytes(r)
51         // TODO: zero out r and h ?
52         if x.Sign() == 0 || x.Cmp(priv.Q) >= 0 {
53                 // very rare
54                 goto loop
55         }
56         priv.X = x
57         priv.Y = new(big.Int)
58         priv.Y.Exp(priv.G, x, priv.P)
59         return priv
60 }
61
62 // Generate a random DSA private key
63 func RandomDsaKey() (priv *dsa.PrivateKey, err error) {
64         r := make([]byte, sha1.Size)
65         _, err = io.ReadFull(rand.Reader, r)
66         priv = DsaKey(r)
67         return
68 }
69
70 // New returns an openpgp.Entity that contains a fresh DSA private key with a
71 // single identity composed of the given full name, comment and email, any of
72 // which may be empty but must not contain any of "()<>\x00".
73 func New(priv *dsa.PrivateKey, currentTimeSecs int64, name, comment, email string) (e *openpgp.Entity, err error) {
74         uid := packet.NewUserId(name, comment, email)
75         if uid == nil {
76                 return nil, fmt.Errorf("NewEntity: invalid argument: user id field contained invalid characters")
77         }
78         t := uint32(currentTimeSecs)
79         e = &openpgp.Entity{
80                 PrimaryKey: packet.NewDSAPublicKey(t, &priv.PublicKey, false /* not a subkey */ ),
81                 PrivateKey: packet.NewDSAPrivateKey(t, priv, false /* not a subkey */ ),
82                 Identities: make(map[string]*openpgp.Identity),
83         }
84         isPrimaryId := true
85         e.Identities[uid.Id] = &openpgp.Identity{
86                 Name:   uid.Name,
87                 UserId: uid,
88                 SelfSignature: &packet.Signature{
89                         CreationTime: t,
90                         SigType:      packet.SigTypePositiveCert,
91                         PubKeyAlgo:   packet.PubKeyAlgoDSA,
92                         Hash:         crypto.SHA256,
93                         IsPrimaryId:  &isPrimaryId,
94                         FlagsValid:   true,
95                         FlagSign:     true,
96                         FlagCertify:  true,
97                         IssuerKeyId:  &e.PrimaryKey.KeyId,
98                 },
99         }
100         return
101 }
102
103 // Issuer generates a key for obligation issuer clients from random seed r
104 func Issuer(r []byte, denomination string) (e *openpgp.Entity, err error) {
105         return New(DsaKey(r), 0, "Issuer", denomination, "")
106 }
107 // Holder generates a key for obligation holder clients from random seed r
108 func Holder(r []byte, issuer, denomination string) (e *openpgp.Entity, err error) {
109         return New(DsaKey(r), 0, "Holder of "+issuer, denomination, "")
110 }
111
112 // Check the issuer and denomination associated with the given pgp key
113 func Check(e *openpgp.Entity) (isIssuer bool, issuer, denomination string, err error) {
114         // allow multiple identities, use the first one that looks like an epoint uid
115         for _, id := range e.Identities {
116                 denomination = id.UserId.Comment
117                 if id.UserId.Name == "Issuer" {
118                         isIssuer = true
119                         issuer = fmt.Sprintf("%X", e.PrimaryKey.Fingerprint)
120                         return
121                 }
122                 const prefix = "Holder of "
123                 if id.UserId.Name[:len(prefix)] == prefix {
124                         issuer = id.UserId.Name[len(prefix):]
125                         return
126                 }
127         }
128         err = fmt.Errorf("Check: no valid userid was found")
129         return
130 }