2015-08-25 21:24:19 +00:00
|
|
|
package pgpkeys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
2015-08-26 14:59:50 +00:00
|
|
|
"encoding/hex"
|
2015-08-25 21:24:19 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"golang.org/x/crypto/openpgp"
|
|
|
|
"golang.org/x/crypto/openpgp/packet"
|
|
|
|
)
|
|
|
|
|
|
|
|
// EncryptShares takes an ordered set of Shamir key share fragments and
|
|
|
|
// PGP public keys and encrypts each Shamir key fragment with the corresponding
|
|
|
|
// public key
|
|
|
|
//
|
|
|
|
// Note: There is no corresponding test function; this functionality is
|
|
|
|
// thoroughly tested in the init and rekey command unit tests
|
2015-08-25 22:33:58 +00:00
|
|
|
func EncryptShares(secretShares [][]byte, pgpKeys []string) ([][]byte, error) {
|
|
|
|
if len(secretShares) != len(pgpKeys) {
|
|
|
|
return nil, fmt.Errorf("Mismatch between number of generated shares and number of PGP keys")
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
2015-08-25 22:33:58 +00:00
|
|
|
encryptedShares := [][]byte{}
|
|
|
|
for i, keystring := range pgpKeys {
|
2015-08-25 21:24:19 +00:00
|
|
|
data, err := base64.StdEncoding.DecodeString(keystring)
|
|
|
|
if err != nil {
|
2015-08-25 22:33:58 +00:00
|
|
|
return nil, fmt.Errorf("Error decoding given PGP key: %s", err)
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
|
|
|
entity, err := openpgp.ReadEntity(packet.NewReader(bytes.NewBuffer(data)))
|
|
|
|
if err != nil {
|
2015-08-25 22:33:58 +00:00
|
|
|
return nil, fmt.Errorf("Error parsing given PGP key: %s", err)
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
|
|
|
ctBuf := bytes.NewBuffer(nil)
|
|
|
|
pt, err := openpgp.Encrypt(ctBuf, []*openpgp.Entity{entity}, nil, nil, nil)
|
|
|
|
if err != nil {
|
2015-08-25 22:33:58 +00:00
|
|
|
return nil, fmt.Errorf("Error setting up encryption for PGP message: %s", err)
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
2015-08-26 14:59:50 +00:00
|
|
|
_, err = pt.Write([]byte(hex.EncodeToString(secretShares[i])))
|
2015-08-25 21:24:19 +00:00
|
|
|
if err != nil {
|
2015-08-25 22:33:58 +00:00
|
|
|
return nil, fmt.Errorf("Error encrypting PGP message: %s", err)
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
|
|
|
pt.Close()
|
2015-08-25 22:33:58 +00:00
|
|
|
encryptedShares = append(encryptedShares, ctBuf.Bytes())
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
2015-08-25 22:33:58 +00:00
|
|
|
return encryptedShares, nil
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|