TEST: Update the serialization/deserialization test with legacy PEM encryption
[openssl.git] / test / serdes_test.c
1 /*
2  * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <string.h>
11 #include <openssl/evp.h>
12 #include <openssl/pem.h>
13 #include <openssl/rsa.h>
14 #include <openssl/x509.h>
15 #include <openssl/serializer.h>
16 #include <openssl/deserializer.h>
17
18 #include "internal/cryptlib.h"   /* ossl_assert */
19
20 #include "testutil.h"
21
22 /*
23  * TODO(3.0) Modify PEM_write_bio_PrivateKey_traditional() to handle
24  * provider side EVP_PKEYs (which don't necessarily have an ameth)
25  *
26  * In the mean time, we use separate "downgraded" EVP_PKEYs to test
27  * serializing/deserializing with "traditional" keys.
28  */
29
30 static EVP_PKEY *key_RSA = NULL;
31 static EVP_PKEY *legacy_key_RSA = NULL;
32
33 static EVP_PKEY *make_RSA(const char *rsa_type, int make_legacy)
34 {
35     EVP_PKEY *pkey = NULL;
36     EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_name(NULL, rsa_type, NULL);
37
38     /*
39      * No real need to check the errors other than for the cascade
40      * effect.  |pkey| will imply remain NULL if something goes wrong.
41      */
42     (void)(ctx != NULL
43            && EVP_PKEY_keygen_init(ctx) > 0
44            && EVP_PKEY_keygen(ctx, &pkey) > 0);
45     EVP_PKEY_CTX_free(ctx);
46     if (make_legacy && EVP_PKEY_get0(pkey) == NULL) {
47         EVP_PKEY_free(pkey);
48         pkey = NULL;
49     }
50
51     return pkey;
52 }
53
54 /* Main test driver */
55
56 typedef int (serializer)(void **serialized, long *serialized_len,
57                          void *object,
58                          const char *pass, const char *pcipher,
59                          const char *ser_propq);
60 typedef int (deserializer)(void **object,
61                            void *serialized, long serialized_len,
62                            const char *pass, const char *pcipher);
63 typedef int (checker)(int type, const void *data, size_t data_len);
64 typedef void (dumper)(const char *label, const void *data, size_t data_len);
65
66 static int test_serialize_deserialize(EVP_PKEY *pkey,
67                                       const char *pass, const char *pcipher,
68                                       serializer *serialize_cb,
69                                       deserializer *deserialize_cb,
70                                       checker *check_cb, dumper *dump_cb,
71                                       const char *ser_propq, int make_legacy)
72 {
73     void *serialized = NULL;
74     long serialized_len = 0;
75     EVP_PKEY *pkey2 = NULL;
76     void *serialized2 = NULL;
77     long serialized2_len = 0;
78     int ok = 0;
79
80     if (!serialize_cb(&serialized, &serialized_len, pkey,
81                       pass, pcipher, ser_propq)
82         || !check_cb(EVP_PKEY_base_id(pkey), serialized, serialized_len)
83         || !deserialize_cb((void **)&pkey2, serialized, serialized_len,
84                            pass, pcipher)
85         || !TEST_int_eq(EVP_PKEY_eq(pkey, pkey2), 1))
86         goto end;
87
88     /*
89      * TODO(3.0) Remove this when PEM_write_bio_PrivateKey_traditional()
90      * handles provider side keys.
91      */
92     if (make_legacy
93         && !TEST_ptr(EVP_PKEY_get0(pkey2)))
94         goto end;
95
96     /*
97      * Double check the serialization, but only for unprotected keys,
98      * as protected keys have a random component, which makes the output
99      * differ.
100      */
101     if ((pass == NULL && pcipher == NULL)
102         && (!serialize_cb(&serialized2, &serialized2_len, pkey2,
103                           pass, pcipher, ser_propq)
104             || !TEST_mem_eq(serialized, serialized_len,
105                             serialized2, serialized2_len)))
106         goto end;
107
108     ok = 1;
109  end:
110     if (!ok)
111         dump_cb("serialized result", serialized, serialized_len);
112
113     OPENSSL_free(serialized);
114     OPENSSL_free(serialized2);
115     EVP_PKEY_free(pkey2);
116     return ok;
117 }
118
119 /* Serializing and desserializing methods */
120
121 static int serialize_EVP_PKEY_prov(void **serialized, long *serialized_len,
122                                    void *object,
123                                    const char *pass, const char *pcipher,
124                                    const char *ser_propq)
125 {
126     EVP_PKEY *pkey = object;
127     OSSL_SERIALIZER_CTX *sctx = NULL;
128     BIO *mem_ser = NULL;
129     BUF_MEM *mem_buf = NULL;
130     const unsigned char *upass = (const unsigned char *)pass;
131     int ok = 0;
132
133     if (!TEST_ptr(sctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pkey, ser_propq))
134         || (pass != NULL
135             && !TEST_true(OSSL_SERIALIZER_CTX_set_passphrase(sctx, upass,
136                                                              strlen(pass))))
137         || (pcipher != NULL
138             && !TEST_true(OSSL_SERIALIZER_CTX_set_cipher(sctx, pcipher, NULL)))
139         || !TEST_ptr(mem_ser = BIO_new(BIO_s_mem()))
140         || !TEST_true(OSSL_SERIALIZER_to_bio(sctx, mem_ser))
141         || !TEST_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0)
142         || !TEST_ptr(*serialized = mem_buf->data)
143         || !TEST_long_gt(*serialized_len = mem_buf->length, 0))
144         goto end;
145
146     /* Detach the serialized output */
147     mem_buf->data = NULL;
148     mem_buf->length = 0;
149     ok = 1;
150  end:
151     BIO_free(mem_ser);
152     OSSL_SERIALIZER_CTX_free(sctx);
153     return ok;
154 }
155
156 static int deserialize_EVP_PKEY_prov(void **object,
157                                      void *serialized, long serialized_len,
158                                      const char *pass, const char *pcipher)
159 {
160     EVP_PKEY *pkey = NULL;
161     OSSL_DESERIALIZER_CTX *dctx = NULL;
162     BIO *mem_deser = NULL;
163     const unsigned char *upass = (const unsigned char *)pass;
164     int ok = 0;
165
166     if (!TEST_ptr(dctx = OSSL_DESERIALIZER_CTX_new_by_EVP_PKEY(&pkey, NULL,
167                                                                NULL, NULL))
168         || (pass != NULL
169             && !OSSL_DESERIALIZER_CTX_set_passphrase(dctx, upass,
170                                                      strlen(pass)))
171         || (pcipher != NULL
172             && !OSSL_DESERIALIZER_CTX_set_cipher(dctx, pcipher, NULL))
173         || !TEST_ptr(mem_deser = BIO_new_mem_buf(serialized, serialized_len))
174         || !TEST_true(OSSL_DESERIALIZER_from_bio(dctx, mem_deser)))
175         goto end;
176     ok = 1;
177     *object = pkey;
178  end:
179     BIO_free(mem_deser);
180     OSSL_DESERIALIZER_CTX_free(dctx);
181     return ok;
182 }
183
184 static int serialize_EVP_PKEY_legacy_PEM(void **serialized,
185                                          long *serialized_len,
186                                          void *object,
187                                          const char *pass, const char *pcipher,
188                                          ossl_unused const char *ser_propq)
189 {
190     EVP_PKEY *pkey = object;
191     EVP_CIPHER *cipher = NULL;
192     BIO *mem_ser = NULL;
193     BUF_MEM *mem_buf = NULL;
194     const unsigned char *upass = (const unsigned char *)pass;
195     size_t passlen = 0;
196     int ok = 0;
197
198     if (pcipher != NULL && pass != NULL) {
199         passlen = strlen(pass);
200         if (!TEST_ptr(cipher = EVP_CIPHER_fetch(NULL, pcipher, NULL)))
201             goto end;
202     }
203     if (!TEST_ptr(mem_ser = BIO_new(BIO_s_mem()))
204         || !TEST_true(PEM_write_bio_PrivateKey_traditional(mem_ser, pkey,
205                                                            cipher,
206                                                            upass, passlen,
207                                                            NULL, NULL))
208         || !TEST_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0)
209         || !TEST_ptr(*serialized = mem_buf->data)
210         || !TEST_long_gt(*serialized_len = mem_buf->length, 0))
211         goto end;
212
213     /* Detach the serialized output */
214     mem_buf->data = NULL;
215     mem_buf->length = 0;
216     ok = 1;
217  end:
218     BIO_free(mem_ser);
219     EVP_CIPHER_free(cipher);
220     return ok;
221 }
222
223 /* Test cases and their dumpers / checkers */
224
225 static void dump_der(const char *label, const void *data, size_t data_len)
226 {
227     test_output_memory(label, data, data_len);
228 }
229
230 static void dump_pem(const char *label, const void *data, size_t data_len)
231 {
232     test_output_string(label, data, data_len - 1);
233 }
234
235 static int check_unprotected_PKCS8_DER(int type,
236                                        const void *data, size_t data_len)
237 {
238     const unsigned char *datap = data;
239     PKCS8_PRIV_KEY_INFO *p8inf =
240         d2i_PKCS8_PRIV_KEY_INFO(NULL, &datap, data_len);
241     int ok = 0;
242
243     if (TEST_ptr(p8inf)) {
244         EVP_PKEY *pkey = EVP_PKCS82PKEY(p8inf);
245
246         ok = (TEST_ptr(pkey) && TEST_true(EVP_PKEY_is_a(pkey, "RSA")));
247         EVP_PKEY_free(pkey);
248     }
249     PKCS8_PRIV_KEY_INFO_free(p8inf);
250     return ok;
251 }
252
253 static int test_unprotected_RSA_via_DER(void)
254 {
255     return test_serialize_deserialize(key_RSA, NULL, NULL,
256                                       serialize_EVP_PKEY_prov,
257                                       deserialize_EVP_PKEY_prov,
258                                       check_unprotected_PKCS8_DER, dump_der,
259                                       OSSL_SERIALIZER_PrivateKey_TO_DER_PQ,
260                                       0);
261 }
262
263 static int check_unprotected_PKCS8_PEM(int type,
264                                        const void *data, size_t data_len)
265 {
266     static const char pem_header[] = "-----BEGIN " PEM_STRING_PKCS8INF "-----";
267
268     return TEST_strn_eq(data, pem_header, sizeof(pem_header) - 1);
269 }
270
271 static int test_unprotected_RSA_via_PEM(void)
272 {
273     return test_serialize_deserialize(key_RSA, NULL, NULL,
274                                       serialize_EVP_PKEY_prov,
275                                       deserialize_EVP_PKEY_prov,
276                                       check_unprotected_PKCS8_PEM, dump_pem,
277                                       OSSL_SERIALIZER_PrivateKey_TO_PEM_PQ,
278                                       0);
279 }
280
281 static int check_unprotected_legacy_PEM(int type,
282                                         const void *data, size_t data_len)
283 {
284     static const char pem_header[] = "-----BEGIN " PEM_STRING_RSA "-----";
285
286     return TEST_strn_eq(data, pem_header, sizeof(pem_header) - 1);
287 }
288
289 static int test_unprotected_RSA_via_legacy_PEM(void)
290 {
291     return test_serialize_deserialize(legacy_key_RSA, NULL, NULL,
292                                       serialize_EVP_PKEY_legacy_PEM,
293                                       deserialize_EVP_PKEY_prov,
294                                       check_unprotected_legacy_PEM, dump_pem,
295                                       NULL, 1);
296 }
297
298 static const char *pass_cipher = "AES-256-CBC";
299 static const char *pass = "the holy handgrenade of antioch";
300
301 static int check_protected_PKCS8_DER(int type,
302                                      const void *data, size_t data_len)
303 {
304     const unsigned char *datap = data;
305     X509_SIG *p8 = d2i_X509_SIG(NULL, &datap, data_len);
306     int ok = TEST_ptr(p8);
307
308     X509_SIG_free(p8);
309     return ok;
310 }
311
312 static int test_protected_RSA_via_DER(void)
313 {
314     return test_serialize_deserialize(key_RSA, pass, pass_cipher,
315                                       serialize_EVP_PKEY_prov,
316                                       deserialize_EVP_PKEY_prov,
317                                       check_protected_PKCS8_DER, dump_der,
318                                       OSSL_SERIALIZER_PrivateKey_TO_DER_PQ,
319                                       0);
320 }
321
322 static int check_protected_PKCS8_PEM(int type,
323                                      const void *data, size_t data_len)
324 {
325     static const char pem_header[] = "-----BEGIN " PEM_STRING_PKCS8 "-----";
326
327     return TEST_strn_eq(data, pem_header, sizeof(pem_header) - 1);
328 }
329
330 static int test_protected_RSA_via_PEM(void)
331 {
332     return test_serialize_deserialize(key_RSA, pass, pass_cipher,
333                                       serialize_EVP_PKEY_prov,
334                                       deserialize_EVP_PKEY_prov,
335                                       check_protected_PKCS8_PEM, dump_pem,
336                                       OSSL_SERIALIZER_PrivateKey_TO_PEM_PQ,
337                                       0);
338 }
339
340 static int check_protected_legacy_PEM(int type,
341                                       const void *data, size_t data_len)
342 {
343     static const char pem_header[] = "-----BEGIN " PEM_STRING_RSA "-----";
344
345     return
346         TEST_strn_eq(data, pem_header, sizeof(pem_header) - 1)
347         && TEST_ptr(strstr(data, "\nDEK-Info: "));
348 }
349
350 static int test_protected_RSA_via_legacy_PEM(void)
351 {
352     return test_serialize_deserialize(legacy_key_RSA, pass, pass_cipher,
353                                       serialize_EVP_PKEY_legacy_PEM,
354                                       deserialize_EVP_PKEY_prov,
355                                       check_protected_legacy_PEM, dump_pem,
356                                       NULL, 1);
357 }
358
359 int setup_tests(void)
360 {
361     TEST_info("Generating keys...");
362     if (!TEST_ptr(key_RSA = make_RSA("RSA", 0))
363         || !TEST_ptr(legacy_key_RSA = make_RSA("RSA", 1))) {
364         EVP_PKEY_free(key_RSA);
365         EVP_PKEY_free(legacy_key_RSA);
366         return 0;
367     }
368     TEST_info("Generating key... done");
369
370     ADD_TEST(test_unprotected_RSA_via_DER);
371     ADD_TEST(test_unprotected_RSA_via_PEM);
372     ADD_TEST(test_unprotected_RSA_via_legacy_PEM);
373     ADD_TEST(test_protected_RSA_via_DER);
374     ADD_TEST(test_protected_RSA_via_PEM);
375     ADD_TEST(test_protected_RSA_via_legacy_PEM);
376
377     return 1;
378 }