Debian Codegraph @

openpgp/integration_tests/utils_test.go

raw
1 package integrationtests
2
3 import (
4 "bytes"
5 "crypto"
6 "crypto/rand"
7 mathrand "math/rand"
8 "strings"
9 "time"
10
11 "github.com/ProtonMail/go-crypto/openpgp"
12 "github.com/ProtonMail/go-crypto/openpgp/armor"
13 "github.com/ProtonMail/go-crypto/openpgp/packet"
14 "github.com/ProtonMail/go-crypto/openpgp/s2k"
15 )
16
17 // This function produces random test vectors: generates keys according to the
18 // given settings, associates a random message for each key. It returns the
19 // test vectors.
20 func generateFreshTestVectors(num int) (vectors []testVector, err error) {
21 mathrand.Seed(time.Now().UTC().UnixNano())
22 for i := 0; i < num; i++ {
23 config := randConfig()
24 // Sample random email, comment, password and message
25 name, email, comment, password, message := randEntityData()
26
27 // Only for verbose display
28 v := "v4"
29 if config.V6() {
30 v = "v6"
31 }
32 pkAlgoNames := map[packet.PublicKeyAlgorithm]string{
33 packet.PubKeyAlgoRSA: "rsa_" + v,
34 packet.PubKeyAlgoEdDSA: "EdDSA_" + v,
35 packet.PubKeyAlgoEd25519: "ed25519_" + v,
36 packet.PubKeyAlgoEd448: "ed448_" + v,
37 }
38
39 newVector := testVector{
40 config: config,
41 Name: pkAlgoNames[config.Algorithm],
42 Password: password,
43 Message: message,
44 }
45
46 // Generate keys
47 newEntity, errKG := openpgp.NewEntity(name, comment, email, config)
48 if errKG != nil {
49 panic(errKG)
50 }
51
52 // Encrypt private key of entity
53 rawPwd := []byte(password)
54 if newEntity.PrivateKey != nil && !newEntity.PrivateKey.Encrypted {
55 if err = newEntity.PrivateKey.Encrypt(rawPwd); err != nil {
56 panic(err)
57 }
58 }
59
60 // Encrypt subkeys of entity
61 for _, sub := range newEntity.Subkeys {
62 if sub.PrivateKey != nil && !sub.PrivateKey.Encrypted {
63 if err = sub.PrivateKey.Encrypt(rawPwd); err != nil {
64 panic(err)
65 }
66 }
67 }
68
69 w := bytes.NewBuffer(nil)
70 if err = newEntity.SerializePrivateWithoutSigning(w, nil); err != nil {
71 return nil, err
72 }
73
74 serialized := w.Bytes()
75
76 privateKey, _ := armorWithType(serialized, "PGP PRIVATE KEY BLOCK")
77 newVector.PrivateKey = privateKey
78 newVector.PublicKey, _ = publicKey(privateKey)
79 vectors = append(vectors, newVector)
80 }
81 return vectors, err
82 }
83
84 // armorWithType make bytes input to armor format
85 func armorWithType(input []byte, armorType string) (string, error) {
86 var b bytes.Buffer
87 w, err := armor.Encode(&b, armorType, nil)
88 if err != nil {
89 return "", err
90 }
91 if _, err = w.Write(input); err != nil {
92 return "", err
93 }
94 if err := w.Close(); err != nil {
95 return "", err
96 }
97 return b.String(), nil
98 }
99
100 func publicKey(privateKey string) (string, error) {
101 privKeyReader := strings.NewReader(privateKey)
102 entries, err := openpgp.ReadArmoredKeyRing(privKeyReader)
103 if err != nil {
104 return "", err
105 }
106
107 var outBuf bytes.Buffer
108 for _, e := range entries {
109 err := e.Serialize(&outBuf)
110 if err != nil {
111 return "", err
112 }
113 }
114
115 outString, err := armorWithType(outBuf.Bytes(), "PGP PUBLIC KEY BLOCK")
116 if err != nil {
117 return "", err
118 }
119
120 return outString, nil
121 }
122
123 var runes = []rune("abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKMNOPQRSTUVWXYZ.@-_:;?/!#$%^&*{}[]'\"+~()<>")
124
125 func randName() string {
126 firstName := make([]rune, 8)
127 lastName := make([]rune, 8)
128 nameRunes := runes[:26]
129
130 for i := range firstName {
131 firstName[i] = nameRunes[mathrand.Intn(len(nameRunes))]
132 }
133
134 for i := range lastName {
135 lastName[i] = nameRunes[mathrand.Intn(len(nameRunes))]
136 }
137
138 return string(firstName) + " " + string(lastName)
139 }
140
141 func randFileHints() *openpgp.FileHints {
142 fileNameRunes := runes[:66]
143 fileName := make([]rune, 1+mathrand.Intn(255))
144 for i := range fileName {
145 fileName[i] = fileNameRunes[mathrand.Intn(len(fileNameRunes))]
146 }
147
148 return &openpgp.FileHints{
149 IsBinary: mathrand.Intn(2) == 0,
150 FileName: string(fileName),
151 ModTime: time.Now(),
152 }
153 }
154
155 func randEmail() string {
156 address := make([]rune, 20)
157 addressRunes := runes[:38]
158 domain := make([]rune, 5)
159 domainRunes := runes[:36]
160 ext := make([]rune, 3)
161 for i := range address {
162 address[i] = addressRunes[mathrand.Intn(len(addressRunes))]
163 }
164 for i := range domain {
165 domain[i] = domainRunes[mathrand.Intn(len(domainRunes))]
166 }
167 for i := range ext {
168 ext[i] = domainRunes[mathrand.Intn(len(domainRunes))]
169 }
170 email := string(address) + "@" + string(domain) + "." + string(ext)
171 return email
172 }
173
174 // Comment does not allow the following characters: ()<>\x00
175 func randComment() string {
176 comment := make([]rune, 140)
177 commentRunes := runes[:84]
178 for i := range comment {
179 comment[i] = commentRunes[mathrand.Intn(len(commentRunes))]
180 }
181 return string(comment)
182 }
183
184 func randPassword() string {
185 maxPasswordLength := 64
186 password := make([]rune, mathrand.Intn(maxPasswordLength-1)+1)
187 for i := range password {
188 password[i] = runes[mathrand.Intn(len(runes))]
189 }
190 return string(password)
191 }
192
193 func randMessage() string {
194 maxMessageLength := 1 << 20
195 message := make([]byte, 1+mathrand.Intn(maxMessageLength-1))
196 if _, err := rand.Read(message); err != nil {
197 panic(err)
198 }
199 return string(message)
200 }
201
202 // Change one char of the input
203 func corrupt(input string) string {
204 if input == "" {
205 return string(runes[mathrand.Intn(len(runes))])
206 }
207 output := []rune(input)
208 for string(output) == input {
209 output[mathrand.Intn(len(output))] = runes[mathrand.Intn(len(runes))]
210 }
211 return string(output)
212 }
213
214 func randEntityData() (string, string, string, string, string) {
215 return randName(), randEmail(), randComment(), randPassword(), randMessage()
216 }
217
218 func randConfig() *packet.Config {
219 hashes := []crypto.Hash{
220 crypto.SHA256,
221 }
222 hash := hashes[mathrand.Intn(len(hashes))]
223
224 ciphers := []packet.CipherFunction{
225 packet.CipherAES256,
226 }
227 ciph := ciphers[mathrand.Intn(len(ciphers))]
228
229 compAlgos := []packet.CompressionAlgo{
230 packet.CompressionNone,
231 packet.CompressionZIP,
232 packet.CompressionZLIB,
233 }
234 compAlgo := compAlgos[mathrand.Intn(len(compAlgos))]
235
236 pkAlgos := []packet.PublicKeyAlgorithm{
237 packet.PubKeyAlgoRSA,
238 packet.PubKeyAlgoEdDSA,
239 packet.PubKeyAlgoEd25519,
240 packet.PubKeyAlgoEd448,
241 }
242 pkAlgo := pkAlgos[mathrand.Intn(len(pkAlgos))]
243
244 aeadModes := []packet.AEADMode{
245 packet.AEADModeOCB,
246 packet.AEADModeEAX,
247 packet.AEADModeGCM,
248 }
249 var aeadConf = packet.AEADConfig{
250 DefaultMode: aeadModes[mathrand.Intn(len(aeadModes))],
251 }
252
253 var rsaBits int
254 if pkAlgo == packet.PubKeyAlgoRSA {
255 switch mathrand.Int() % 4 {
256 case 0:
257 rsaBits = 2048
258 case 1:
259 rsaBits = 3072
260 case 2:
261 rsaBits = 4096
262 default:
263 rsaBits = 0
264 }
265 }
266
267 level := mathrand.Intn(11) - 1
268 compConf := &packet.CompressionConfig{Level: level}
269
270 var v6 bool
271 if mathrand.Int()%2 == 0 {
272 v6 = true
273 if pkAlgo == packet.PubKeyAlgoEdDSA {
274 pkAlgo = packet.PubKeyAlgoEd25519
275 }
276 }
277
278 var s2kConf *s2k.Config
279 if mathrand.Int()%2 == 0 {
280 s2kConf = &s2k.Config{
281 S2KMode: s2k.IteratedSaltedS2K,
282 Hash: hash,
283 S2KCount: 1024 + mathrand.Intn(65010689),
284 }
285 } else {
286 s2kConf = &s2k.Config{
287 S2KMode: s2k.Argon2S2K,
288 }
289 }
290
291 return &packet.Config{
292 V6Keys: v6,
293 Rand: rand.Reader,
294 DefaultHash: hash,
295 DefaultCipher: ciph,
296 DefaultCompressionAlgo: compAlgo,
297 CompressionConfig: compConf,
298 S2KConfig: s2kConf,
299 RSABits: rsaBits,
300 Algorithm: pkAlgo,
301 AEADConfig: &aeadConf,
302 }
303 }