package key
import (
+ "bytes"
"crypto"
"crypto/dsa"
"crypto/openpgp"
"fmt"
"io"
"math/big"
+ "time"
)
// TODO: keep denomination only in issuer key?
loop:
h := sha1.New()
h.Write(r)
- r = h.Sum()
+ r = h.Sum(nil)
x.SetBytes(r)
// TODO: zero out r and h ?
if x.Sign() == 0 || x.Cmp(priv.Q) >= 0 {
// New returns an openpgp.Entity that contains a fresh DSA private key with a
// single identity composed of the given full name, comment and email, any of
// which may be empty but must not contain any of "()<>\x00".
-func New(priv *dsa.PrivateKey, currentTimeSecs int64, name, comment, email string) (e *openpgp.Entity, err error) {
+func New(priv *dsa.PrivateKey, t time.Time, name, comment, email string) (e *openpgp.Entity, err error) {
uid := packet.NewUserId(name, comment, email)
if uid == nil {
return nil, fmt.Errorf("NewEntity: invalid argument: user id field contained invalid characters")
}
- t := uint32(currentTimeSecs)
e = &openpgp.Entity{
- PrimaryKey: packet.NewDSAPublicKey(t, &priv.PublicKey, false /* not a subkey */ ),
- PrivateKey: packet.NewDSAPrivateKey(t, priv, false /* not a subkey */ ),
+ PrimaryKey: packet.NewDSAPublicKey(t, &priv.PublicKey),
+ PrivateKey: packet.NewDSAPrivateKey(t, priv),
Identities: make(map[string]*openpgp.Identity),
}
isPrimaryId := true
return
}
+// Parse armored or binary openpgp public or private key
+func Parse(d []byte) (e *openpgp.Entity, err error) {
+ elist, err := openpgp.ReadArmoredKeyRing(bytes.NewBuffer(d))
+ if err != nil {
+ elist1, err1 := openpgp.ReadKeyRing(bytes.NewBuffer(d))
+ if err1 != nil {
+ return
+ }
+ err = nil
+ elist = elist1
+ }
+ if len(elist) != 1 {
+ err = fmt.Errorf("Parse: expected exactly one key")
+ return
+ }
+ e = elist[0]
+ return
+}
+
// Issuer generates a key for obligation issuer clients from random seed r
func Issuer(r []byte, denomination string) (e *openpgp.Entity, err error) {
- return New(DsaKey(r), 0, "Issuer", denomination, "")
+ return New(DsaKey(r), time.Unix(0,0), "Issuer", denomination, "")
}
// Holder generates a key for obligation holder clients from random seed r
func Holder(r []byte, issuer, denomination string) (e *openpgp.Entity, err error) {
- return New(DsaKey(r), 0, "Holder of "+issuer, denomination, "")
+ return New(DsaKey(r), time.Unix(0,0), "Holder of "+issuer, denomination, "")
+}
+// Server generates a key for the server from random seed r
+func Server(r []byte) (e *openpgp.Entity, err error) {
+ return New(DsaKey(r), time.Now(), "Server", "", "")
}
// Key id (fingerprint)