From 293e53e15524518b254091a155c5d04f4305816d Mon Sep 17 00:00:00 2001 From: nsz Date: Mon, 5 Dec 2011 17:57:35 +0100 Subject: [PATCH] update genkey (+minor cleanups) --- cmd/genkey/genkey.go | 34 +++++++++++++++++++++++++------- document/document.go | 2 +- dsakey/dsakey.go | 46 +++++++++++++++++++++---------------------- dsakey/dsakey_test.go | 4 ++-- 4 files changed, 53 insertions(+), 33 deletions(-) diff --git a/cmd/genkey/genkey.go b/cmd/genkey/genkey.go index 6420306..3a8a256 100644 --- a/cmd/genkey/genkey.go +++ b/cmd/genkey/genkey.go @@ -1,14 +1,14 @@ package main import ( - "epoint/dsakey" "crypto/openpgp" + "epoint/dsakey" + "fmt" "log" "os" - "time" ) -const usage = "usage: ./genkey name comment email seckeyfile pubkeyfile < seed" +const usage = "usage: ./genkey [issuer] denomination seckeyfile pubkeyfile < seed > fingerprint" func serialize(e *openpgp.Entity, sk, pk string) (err error) { f, err := os.Create(sk) @@ -60,7 +60,22 @@ func serialize(e *openpgp.Entity, sk, pk string) (err error) { } func main() { - if len(os.Args) != 6 { + isIssuer := false + issuer := "" + denom := "" + sk := "" + pk := "" + if len(os.Args) == 4 { + isIssuer = true + denom = os.Args[1] + sk = os.Args[2] + pk = os.Args[3] + } else if len(os.Args) == 5 { + issuer = os.Args[1] + denom = os.Args[2] + sk = os.Args[3] + pk = os.Args[4] + } else { log.Fatal(usage) } b := make([]byte, 1000) @@ -69,13 +84,18 @@ func main() { log.Print(err) log.Fatal(usage) } - key := dsakey.PrivKey(b[:n]) - e, err := dsakey.NewEntity(key, time.Seconds(), os.Args[1], os.Args[2], os.Args[3]) + var e *openpgp.Entity + if isIssuer { + e, err = dsakey.NewIssuerEntity(b[:n], denom) + } else { + e, err = dsakey.NewHolderEntity(b[:n], issuer, denom) + } if err != nil { log.Fatal(err) } - err = serialize(e, os.Args[4], os.Args[5]) + err = serialize(e, sk, pk) if err != nil { log.Fatal(err) } + fmt.Fprintf(os.Stdout, "%X\n", e.PrimaryKey.Fingerprint) } diff --git a/document/document.go b/document/document.go index 1e26d28..7e98965 100644 --- a/document/document.go +++ b/document/document.go @@ -32,7 +32,7 @@ package document // TODO: fields of notice (last notice, serial, failure notice,..) // TODO: limits and cert type specific input validation // TODO: fix Cert mess -// TODO: nonce is id, id is even number of hex digits (require only draftid.nonce to be uniq) +// TODO: nonce is id, id is even number of hex digits (require only drawer.nonce to be uniq) // TODO: denom, issuer from key (key representation: armor?) import ( diff --git a/dsakey/dsakey.go b/dsakey/dsakey.go index f5fa9b5..23ab5fe 100644 --- a/dsakey/dsakey.go +++ b/dsakey/dsakey.go @@ -1,12 +1,12 @@ package dsakey import ( - "crypto/sha1" + "crypto" "crypto/dsa" "crypto/openpgp" "crypto/openpgp/packet" "crypto/rand" - "crypto" + "crypto/sha1" "fmt" "io" "math/big" @@ -18,9 +18,9 @@ const G = "502FF28CC4D7BC1100123C9227994341C29773BFBD8D7E8FFED6D87A9D82FE573744A func PrivKey(r []byte) *dsa.PrivateKey { priv := new(dsa.PrivateKey) - priv.Parameters.P,_ = new(big.Int).SetString(P, 16) - priv.Parameters.Q,_ = new(big.Int).SetString(Q, 16) - priv.Parameters.G,_ = new(big.Int).SetString(G, 16) + priv.Parameters.P, _ = new(big.Int).SetString(P, 16) + priv.Parameters.Q, _ = new(big.Int).SetString(Q, 16) + priv.Parameters.G, _ = new(big.Int).SetString(G, 16) // q > 2^159 prime // x = sha1(r) @@ -84,23 +84,23 @@ func NewEntity(priv *dsa.PrivateKey, currentTimeSecs int64, name, comment, email IssuerKeyId: &e.PrimaryKey.KeyId, }, } -/* - e.Subkeys = make([]Subkey, 1) - e.Subkeys[0] = Subkey{ - PublicKey: packet.NewRSAPublicKey(t, &encryptingPriv.PublicKey, true), - PrivateKey: packet.NewRSAPrivateKey(t, encryptingPriv, true), - Sig: &packet.Signature{ - CreationTime: t, - SigType: packet.SigTypeSubkeyBinding, - PubKeyAlgo: packet.PubKeyAlgoRSA, - Hash: crypto.SHA256, - FlagsValid: true, - FlagEncryptStorage: true, - FlagEncryptCommunications: true, - IssuerKeyId: &e.PrimaryKey.KeyId, - }, - } -*/ + /* + e.Subkeys = make([]Subkey, 1) + e.Subkeys[0] = Subkey{ + PublicKey: packet.NewRSAPublicKey(t, &encryptingPriv.PublicKey, true), + PrivateKey: packet.NewRSAPrivateKey(t, encryptingPriv, true), + Sig: &packet.Signature{ + CreationTime: t, + SigType: packet.SigTypeSubkeyBinding, + PubKeyAlgo: packet.PubKeyAlgoRSA, + Hash: crypto.SHA256, + FlagsValid: true, + FlagEncryptStorage: true, + FlagEncryptCommunications: true, + IssuerKeyId: &e.PrimaryKey.KeyId, + }, + } + */ return } @@ -110,7 +110,7 @@ func NewIssuerEntity(r []byte, denomination string) (e *openpgp.Entity, err erro } // simple key generation for obligation holder clients func NewHolderEntity(r []byte, issuer, denomination string) (e *openpgp.Entity, err error) { - return NewEntity(PrivKey(r), 0, "Holder of " + issuer, denomination, "") + return NewEntity(PrivKey(r), 0, "Holder of "+issuer, denomination, "") } // check the issuer and denomination associated with the given pgp key diff --git a/dsakey/dsakey_test.go b/dsakey/dsakey_test.go index 29ba725..34df359 100644 --- a/dsakey/dsakey_test.go +++ b/dsakey/dsakey_test.go @@ -1,11 +1,11 @@ package dsakey import ( + "bytes" "crypto/openpgp" "fmt" - "bytes" - "time" "testing" + "time" ) func testSignAndVerify(t *testing.T, priv *openpgp.Entity) { -- 2.20.1