Ejemplo n.º 1
0
// Name implements storage.Contacts.
func (c *contacts) Name(key *sf.PublicKey) (string, error) {
	var name []byte
	var fail string
	err := c.db.View(func(tx *bolt.Tx) error {
		contactsBucket := tx.Bucket([]byte("contacts"))
		if contactsBucket == nil {
			return errgo.New("key not found")
		}
		name = contactsBucket.Get(key[:])
		return nil
	})
	if err != nil {
		return fail, err
	}
	if len(name) == 0 {
		return "", errgo.Newf("no contact found for %q", key.Encode())
	}
	return string(name), nil
}
Ejemplo n.º 2
0
// Get implements storage.Vault.
func (v *vault) Get(key *sf.PublicKey) (*sf.KeyPair, error) {
	var keyPair sf.KeyPair
	err := v.db.View(func(tx *bolt.Tx) error {
		keysBucket := tx.Bucket([]byte("keys"))
		if keysBucket == nil {
			return errgo.New("empty vault")
		}
		logBucket := tx.Bucket([]byte("log"))
		if logBucket == nil {
			return errgo.New("empty vault")
		}

		seqBytes := keysBucket.Get(key[:])
		if seqBytes == nil {
			return errgo.Newf("key pair not found for %q", key.Encode())
		}
		seqInt := new(big.Int)
		seqInt.SetBytes(seqBytes)
		encBytes := logBucket.Get(seqBytes)
		if encBytes == nil {
			return errgo.Newf("missing expected key #%s", seqInt.String())
		}
		seq := new(sf.Nonce)
		copy(seq[:], seqBytes)

		keyPairBytes, ok := secretbox.Open(nil, encBytes, (*[24]byte)(seq), (*[32]byte)(v.secretKey))
		if !ok {
			return errgo.Newf("error opening key pair #%s", seqInt.String())
		}
		keyPair.PublicKey = new(sf.PublicKey)
		copy(keyPair.PublicKey[:], keyPairBytes[:32])
		keyPair.PrivateKey = new(sf.PrivateKey)
		copy(keyPair.PrivateKey[:], keyPairBytes[32:])
		// TODO: mprotect private key
		// TODO: zeroize keyPairBytes

		return nil
	})
	if err != nil {
		return nil, errgo.Mask(err)
	}
	return &keyPair, nil
}