85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
|
package pgp
|
||
|
|
||
|
import (
|
||
|
"crypto"
|
||
|
|
||
|
"golang.org/x/crypto/openpgp"
|
||
|
"golang.org/x/crypto/openpgp/packet"
|
||
|
)
|
||
|
|
||
|
func GetEntity(publicKey []byte, privateKey []byte) (*openpgp.Entity, error) {
|
||
|
publicKeyPacket, err := getPublicKeyPacket(publicKey)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
var privateKeyPacket *packet.PrivateKey
|
||
|
if len(privateKey) > 0 {
|
||
|
privateKeyPacket, err = getPrivateKeyPacket(privateKey)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return createEntityFromKeys(publicKeyPacket, privateKeyPacket)
|
||
|
}
|
||
|
|
||
|
// From https://gist.github.com/eliquious/9e96017f47d9bd43cdf9
|
||
|
func createEntityFromKeys(pubKey *packet.PublicKey, privKey *packet.PrivateKey) (*openpgp.Entity, error) {
|
||
|
config := packet.Config{
|
||
|
DefaultHash: crypto.SHA256,
|
||
|
DefaultCipher: packet.CipherAES256,
|
||
|
DefaultCompressionAlgo: packet.CompressionZLIB,
|
||
|
CompressionConfig: &packet.CompressionConfig{
|
||
|
Level: 9,
|
||
|
},
|
||
|
RSABits: 4096,
|
||
|
}
|
||
|
currentTime := config.Now()
|
||
|
uid := packet.NewUserId("", "", "")
|
||
|
|
||
|
e := openpgp.Entity{
|
||
|
PrimaryKey: pubKey,
|
||
|
PrivateKey: privKey,
|
||
|
Identities: make(map[string]*openpgp.Identity),
|
||
|
}
|
||
|
isPrimaryId := false
|
||
|
|
||
|
e.Identities[uid.Id] = &openpgp.Identity{
|
||
|
Name: uid.Name,
|
||
|
UserId: uid,
|
||
|
SelfSignature: &packet.Signature{
|
||
|
CreationTime: currentTime,
|
||
|
SigType: packet.SigTypePositiveCert,
|
||
|
PubKeyAlgo: packet.PubKeyAlgoRSA,
|
||
|
Hash: config.Hash(),
|
||
|
IsPrimaryId: &isPrimaryId,
|
||
|
FlagsValid: true,
|
||
|
FlagSign: true,
|
||
|
FlagCertify: true,
|
||
|
IssuerKeyId: &e.PrimaryKey.KeyId,
|
||
|
},
|
||
|
}
|
||
|
|
||
|
keyLifetimeSecs := uint32(86400 * 365)
|
||
|
|
||
|
e.Subkeys = make([]openpgp.Subkey, 1)
|
||
|
e.Subkeys[0] = openpgp.Subkey{
|
||
|
PublicKey: pubKey,
|
||
|
PrivateKey: privKey,
|
||
|
Sig: &packet.Signature{
|
||
|
CreationTime: currentTime,
|
||
|
SigType: packet.SigTypeSubkeyBinding,
|
||
|
PubKeyAlgo: packet.PubKeyAlgoRSA,
|
||
|
Hash: config.Hash(),
|
||
|
PreferredHash: []uint8{8}, // SHA-256
|
||
|
FlagsValid: true,
|
||
|
FlagEncryptStorage: true,
|
||
|
FlagEncryptCommunications: true,
|
||
|
IssuerKeyId: &e.PrimaryKey.KeyId,
|
||
|
KeyLifetimeSecs: &keyLifetimeSecs,
|
||
|
},
|
||
|
}
|
||
|
return &e, nil
|
||
|
}
|