/
rsa.go
162 lines (138 loc) · 3.93 KB
/
rsa.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package keys
import (
"bytes"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"io"
"github.com/theupdateframework/go-tuf/data"
)
func init() {
VerifierMap.Store(data.KeyTypeRSASSA_PSS_SHA256, NewRsaVerifier)
SignerMap.Store(data.KeyTypeRSASSA_PSS_SHA256, newRsaSigner)
}
func NewRsaVerifier() Verifier {
return &rsaVerifier{}
}
func newRsaSigner() Signer {
return &rsaSigner{}
}
type rsaVerifier struct {
PublicKey *PKIXPublicKey `json:"public"`
rsaKey *rsa.PublicKey
key *data.PublicKey
}
func (p *rsaVerifier) Public() string {
// This is already verified to succeed when unmarshalling a public key.
r, err := x509.MarshalPKIXPublicKey(p.rsaKey)
if err != nil {
// TODO: Gracefully handle these errors.
// See https://github.com/theupdateframework/go-tuf/issues/363
panic(err)
}
return string(r)
}
func (p *rsaVerifier) Verify(msg, sigBytes []byte) error {
hash := sha256.Sum256(msg)
return rsa.VerifyPSS(p.rsaKey, crypto.SHA256, hash[:], sigBytes, &rsa.PSSOptions{})
}
func (p *rsaVerifier) MarshalPublicKey() *data.PublicKey {
return p.key
}
func (p *rsaVerifier) UnmarshalPublicKey(key *data.PublicKey) error {
// Prepare decoder limited to 512Kb
dec := json.NewDecoder(io.LimitReader(bytes.NewReader(key.Value), MaxJSONKeySize))
// Unmarshal key value
if err := dec.Decode(p); err != nil {
if errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
return fmt.Errorf("tuf: the public key is truncated or too large: %w", err)
}
return err
}
rsaKey, ok := p.PublicKey.PublicKey.(*rsa.PublicKey)
if !ok {
return fmt.Errorf("invalid public key")
}
if _, err := x509.MarshalPKIXPublicKey(rsaKey); err != nil {
return fmt.Errorf("marshalling to PKIX key: invalid public key")
}
p.rsaKey = rsaKey
p.key = key
return nil
}
type rsaSigner struct {
*rsa.PrivateKey
}
type rsaPrivateKeyValue struct {
Private string `json:"private"`
Public *PKIXPublicKey `json:"public"`
}
func (s *rsaSigner) PublicData() *data.PublicKey {
keyValBytes, _ := json.Marshal(rsaVerifier{PublicKey: &PKIXPublicKey{PublicKey: s.Public()}})
return &data.PublicKey{
Type: data.KeyTypeRSASSA_PSS_SHA256,
Scheme: data.KeySchemeRSASSA_PSS_SHA256,
Algorithms: data.HashAlgorithms,
Value: keyValBytes,
}
}
func (s *rsaSigner) SignMessage(message []byte) ([]byte, error) {
hash := sha256.Sum256(message)
return rsa.SignPSS(rand.Reader, s.PrivateKey, crypto.SHA256, hash[:], &rsa.PSSOptions{})
}
func (s *rsaSigner) ContainsID(id string) bool {
return s.PublicData().ContainsID(id)
}
func (s *rsaSigner) MarshalPrivateKey() (*data.PrivateKey, error) {
priv := x509.MarshalPKCS1PrivateKey(s.PrivateKey)
pemKey := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: priv})
val, err := json.Marshal(rsaPrivateKeyValue{
Private: string(pemKey),
Public: &PKIXPublicKey{PublicKey: s.Public()},
})
if err != nil {
return nil, err
}
return &data.PrivateKey{
Type: data.KeyTypeRSASSA_PSS_SHA256,
Scheme: data.KeySchemeRSASSA_PSS_SHA256,
Algorithms: data.HashAlgorithms,
Value: val,
}, nil
}
func (s *rsaSigner) UnmarshalPrivateKey(key *data.PrivateKey) error {
val := rsaPrivateKeyValue{}
if err := json.Unmarshal(key.Value, &val); err != nil {
return err
}
block, _ := pem.Decode([]byte(val.Private))
if block == nil {
return errors.New("invalid PEM value")
}
if block.Type != "RSA PRIVATE KEY" {
return fmt.Errorf("invalid block type: %s", block.Type)
}
k, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return err
}
if _, err := json.Marshal(rsaVerifier{
PublicKey: &PKIXPublicKey{PublicKey: k.Public()}}); err != nil {
return fmt.Errorf("invalid public key: %s", err)
}
s.PrivateKey = k
return nil
}
func GenerateRsaKey() (*rsaSigner, error) {
privkey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
}
return &rsaSigner{privkey}, nil
}