-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrapperrsa.go
176 lines (143 loc) · 4.08 KB
/
wrapperrsa.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Package cryptowrap JSON/Gob/MsgPack-based Marshaler/Unmarshaler with AES encryption
package cryptowrap
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"encoding/json"
"fmt"
"hash"
)
// WrapperRSA is a struct with custom JSON/Gob/Binary marshaler and unmarshaler.
//
// Marshaler will encrypt Payload with RSA using EncKey as a public key.
// and hash function provided in Hash.
// sha256.New() will be used if no Hash provided.
//
// Unmarshaler will decrypt Payload with the DecKeys provided.
// Keys will be tryied one by one until success decryption.
// ErrUndecryptable will be returned in case no one key is suitable.
//
// Label must be the same for Marshaling and Umarshaling. If no label provided empty one is used.
//
// If Compress is true serialized Payload wil be compressed with LZ4.
//
// Note: there is a limit for the length of data could be encrypted with RSA:
// The message must be no longer than the length of the public modulus minus twice the hash length, minus a further 2.
// See https://golang.org/pkg/crypto/rsa/#EncryptOAEP for details (there no much though).
type WrapperRSA struct {
DecKeys []*rsa.PrivateKey
EncKey *rsa.PublicKey
Hash hash.Hash
Label []byte
Payload interface{}
Compress bool
}
type externalWrapperRSA struct {
Payload []byte
}
type internalWrapperRSA struct {
Compressed bool
Payload []byte
}
// MarshalJSON is a custom marshaler.
func (w *WrapperRSA) MarshalJSON() ([]byte, error) {
return w.marshal(json.Marshal)
}
// UnmarshalJSON is a custom unmarshaler.
func (w *WrapperRSA) UnmarshalJSON(data []byte) error {
return w.unmarshal(data, json.Unmarshal)
}
// GobEncode is a custom marshaler.
func (w *WrapperRSA) GobEncode() ([]byte, error) {
return w.marshal(gobMarshal)
}
// GobDecode is a custom unmarshaler.
func (w *WrapperRSA) GobDecode(data []byte) error {
return w.unmarshal(data, gobUnmarshal)
}
// MarshalBinary is a custom marshaler to be used with MsgPack (github.com/ugorji/go/codec).
func (w *WrapperRSA) MarshalBinary() (data []byte, err error) {
return w.marshal(binMarshal)
}
// UnmarshalBinary is a custom unmarshaler to be used with MsgPack (github.com/ugorji/go/codec).
func (w *WrapperRSA) UnmarshalBinary(data []byte) error {
return w.unmarshal(data, binUnmarshal)
}
var emptyLabel = []byte("") // nolint: gochecknoglobals
func (w *WrapperRSA) marshal(marshaler func(interface{}) ([]byte, error)) ([]byte, error) {
var (
intW internalWrapperRSA
extW externalWrapperRSA
err error
)
if w.Hash == nil {
w.Hash = sha256.New()
}
if w.Label == nil {
w.Label = emptyLabel
}
intW.Payload, err = marshaler(w.Payload)
if err != nil {
return nil, fmt.Errorf("marshaling payload: %w", err)
}
if w.Compress {
intW.Payload, err = compress(intW.Payload)
if err != nil {
return nil, err
}
intW.Compressed = true
}
extW.Payload, err = marshaler(&intW)
if err != nil {
return nil, fmt.Errorf("marshaling payload wrapper: %w", err)
}
extW.Payload, err = rsa.EncryptOAEP(w.Hash, rand.Reader, w.EncKey, extW.Payload, w.Label)
if err != nil {
return nil, fmt.Errorf("encrypting: %w", err)
}
data, err := marshaler(&extW)
if err != nil {
return nil, fmt.Errorf("marshaling: %w", err)
}
return data, err
}
func (w *WrapperRSA) unmarshal(data []byte, unmarshaler func([]byte, interface{}) error) error { // nolint: gocyclo
if len(w.DecKeys) < 1 {
return ErrNoKey
}
if w.Hash == nil {
w.Hash = sha256.New()
}
if w.Label == nil {
w.Label = emptyLabel
}
extW := externalWrapper{}
err := unmarshaler(data, &extW)
if err != nil {
return fmt.Errorf("unmarshaling: %w", err)
}
for _, key := range w.DecKeys {
data, err = rsa.DecryptOAEP(w.Hash, rand.Reader, key, extW.Payload, w.Label)
if err != nil {
continue
}
intW := internalWrapper{}
err = unmarshaler(data, &intW)
if err != nil {
continue
}
if intW.Compressed {
intW.Payload, err = decompress(intW.Payload)
if err != nil {
return err
}
}
err = unmarshaler(intW.Payload, w.Payload)
if err != nil {
return fmt.Errorf("unmarshaling wrapper: %w", err)
}
return nil
}
return ErrUndecryptable
}