/
openpgp.go
111 lines (93 loc) · 2.81 KB
/
openpgp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package grant
import (
"errors"
"fmt"
"strconv"
"bytes"
"io"
"io/ioutil"
"github.com/monax/hoard/config/secrets"
"github.com/monax/hoard/reference"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/armor"
)
// OpenPGPGrant encrypts and signs a given reference
func OpenPGPGrant(ref *reference.Ref, public string, keyring *secrets.OpenPGPSecret) ([]byte, error) {
if keyring == nil {
return nil, fmt.Errorf("cannot encrypt because no private key was provided")
}
buf := bytes.NewBuffer(nil)
armorWriter, err := armor.Encode(buf, "PGP MESSAGE", nil)
if err != nil {
return nil, err
}
var to openpgp.EntityList
if public != "" {
// use public keyring
if to, err = openpgp.ReadArmoredKeyRing(bytes.NewBufferString(public)); err != nil {
return nil, fmt.Errorf("could not read public keyring: %s", err)
}
} else {
// default to configured keyring
if to, err = openpgp.ReadArmoredKeyRing(bytes.NewBuffer(keyring.Data)); err != nil {
return nil, fmt.Errorf("could not read private keyring: %s", err)
}
}
// read configured keyring
keys, err := openpgp.ReadArmoredKeyRing(bytes.NewBuffer(keyring.Data))
if err != nil {
return nil, err
}
id, err := strconv.ParseUint(keyring.PrivateID, 10, 64)
if err != nil {
return nil, err
}
// we can only sign with one key
from := keys.KeysById(id)[0].Entity
if from == nil {
return nil, fmt.Errorf("signing identity not found")
}
plaintextWriter, err := openpgp.Encrypt(armorWriter, to, from, nil, nil)
if err != nil {
return nil, fmt.Errorf("could not set up openpgp encryption: %s", err)
}
_, err = io.WriteString(plaintextWriter, ref.Plaintext(nil))
if err != nil {
return nil, err
}
plaintextWriter.Close()
armorWriter.Close()
return buf.Bytes(), nil
}
// OpenPGPReference decrypts a given grant
func OpenPGPReference(grant []byte, keyring *secrets.OpenPGPSecret) (*reference.Ref, error) {
if keyring == nil {
return nil, fmt.Errorf("cannot decrypt because no private key was provided")
}
// read local keyring
to, err := openpgp.ReadArmoredKeyRing(bytes.NewBuffer(keyring.Data))
block, err := armor.Decode(bytes.NewBuffer(grant))
if err != nil {
return nil, err
}
// consume grant message
messageReader, err := openpgp.ReadMessage(block.Body, to,
func(keys []openpgp.Key, symmetric bool) ([]byte, error) {
return nil, errors.New("OpenPGPGrantReference does not support password prompting")
}, nil)
if err != nil {
return nil, err
}
bs, err := ioutil.ReadAll(messageReader.UnverifiedBody)
if err != nil {
return nil, err
}
// verify that client knows signer
if !messageReader.IsSigned {
return nil, errors.New("grant is not signed")
}
if keys := to.KeysById(messageReader.SignedByKeyId); len(keys) == 0 {
return nil, errors.New("unknown message signature")
}
return reference.FromPlaintext(string(bs)), nil
}