1 | /*
|
---|
2 | * Copyright 2021-2023 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 |
|
---|
12 | #include "testutil.h"
|
---|
13 |
|
---|
14 | #include <openssl/evp.h>
|
---|
15 | #include <openssl/x509.h>
|
---|
16 | #include <openssl/rc4.h>
|
---|
17 | #include <openssl/md5.h>
|
---|
18 | #include <openssl/configuration.h>
|
---|
19 | #include <openssl/provider.h>
|
---|
20 |
|
---|
21 | #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5 \
|
---|
22 | || !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
|
---|
23 | static const char pbe_password[] = "MyVoiceIsMyPassport";
|
---|
24 |
|
---|
25 | static unsigned char pbe_salt[] = {
|
---|
26 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
|
---|
27 | };
|
---|
28 |
|
---|
29 | static const int pbe_iter = 1000;
|
---|
30 |
|
---|
31 | static unsigned char pbe_plaintext[] = {
|
---|
32 | 0x57, 0x65, 0x20, 0x61, 0x72, 0x65, 0x20, 0x61,
|
---|
33 | 0x6c, 0x6c, 0x20, 0x6d, 0x61, 0x64, 0x65, 0x20,
|
---|
34 | 0x6f, 0x66, 0x20, 0x73, 0x74, 0x61, 0x72, 0x73,
|
---|
35 | };
|
---|
36 | #endif
|
---|
37 |
|
---|
38 | /* Expected output generated using OpenSSL 1.1.1 */
|
---|
39 |
|
---|
40 | #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5
|
---|
41 | static const unsigned char pbe_ciphertext_rc4_md5[] = {
|
---|
42 | 0x21, 0x90, 0xfa, 0xee, 0x95, 0x66, 0x59, 0x45,
|
---|
43 | 0xfa, 0x1e, 0x9f, 0xe2, 0x25, 0xd2, 0xf9, 0x71,
|
---|
44 | 0x94, 0xe4, 0x3d, 0xc9, 0x7c, 0xb0, 0x07, 0x23,
|
---|
45 | };
|
---|
46 | #endif
|
---|
47 |
|
---|
48 | #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
|
---|
49 | static const unsigned char pbe_ciphertext_des_sha1[] = {
|
---|
50 | 0xce, 0x4b, 0xb0, 0x0a, 0x7b, 0x48, 0xd7, 0xe3,
|
---|
51 | 0x9a, 0x9f, 0x46, 0xd6, 0x41, 0x42, 0x4b, 0x44,
|
---|
52 | 0x36, 0x45, 0x5f, 0x60, 0x8f, 0x3c, 0xd0, 0x55,
|
---|
53 | 0xd0, 0x8d, 0xa9, 0xab, 0x78, 0x5b, 0x63, 0xaf,
|
---|
54 | };
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5 \
|
---|
58 | || !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
|
---|
59 | static int test_pkcs5_pbe(const EVP_CIPHER *cipher, const EVP_MD *md,
|
---|
60 | const unsigned char *exp, const int exp_len)
|
---|
61 | {
|
---|
62 | int ret = 0;
|
---|
63 | EVP_CIPHER_CTX *ctx;
|
---|
64 | X509_ALGOR *algor = NULL;
|
---|
65 | int i, outlen;
|
---|
66 | unsigned char out[32];
|
---|
67 |
|
---|
68 | ctx = EVP_CIPHER_CTX_new();
|
---|
69 | if (!TEST_ptr(ctx))
|
---|
70 | goto err;
|
---|
71 |
|
---|
72 | algor = X509_ALGOR_new();
|
---|
73 | if (!TEST_ptr(algor))
|
---|
74 | goto err;
|
---|
75 |
|
---|
76 | if (!TEST_true(PKCS5_pbe_set0_algor(algor, EVP_CIPHER_nid(cipher), pbe_iter,
|
---|
77 | pbe_salt, sizeof(pbe_salt)))
|
---|
78 | || !TEST_true(PKCS5_PBE_keyivgen(ctx, pbe_password, strlen(pbe_password),
|
---|
79 | algor->parameter, cipher, md, 1))
|
---|
80 | || !TEST_true(EVP_CipherUpdate(ctx, out, &i, pbe_plaintext,
|
---|
81 | sizeof(pbe_plaintext))))
|
---|
82 | goto err;
|
---|
83 | outlen = i;
|
---|
84 |
|
---|
85 | if (!TEST_true(EVP_CipherFinal_ex(ctx, out + i, &i)))
|
---|
86 | goto err;
|
---|
87 | outlen += i;
|
---|
88 |
|
---|
89 | if (!TEST_mem_eq(out, outlen, exp, exp_len))
|
---|
90 | goto err;
|
---|
91 |
|
---|
92 | /* Decrypt */
|
---|
93 |
|
---|
94 | if (!TEST_true(PKCS5_PBE_keyivgen(ctx, pbe_password, strlen(pbe_password),
|
---|
95 | algor->parameter, cipher, md, 0))
|
---|
96 | || !TEST_true(EVP_CipherUpdate(ctx, out, &i, exp, exp_len)))
|
---|
97 | goto err;
|
---|
98 |
|
---|
99 | outlen = i;
|
---|
100 | if (!TEST_true(EVP_CipherFinal_ex(ctx, out + i, &i)))
|
---|
101 | goto err;
|
---|
102 |
|
---|
103 | if (!TEST_mem_eq(out, outlen, pbe_plaintext, sizeof(pbe_plaintext)))
|
---|
104 | goto err;
|
---|
105 |
|
---|
106 | ret = 1;
|
---|
107 | err:
|
---|
108 | EVP_CIPHER_CTX_free(ctx);
|
---|
109 | X509_ALGOR_free(algor);
|
---|
110 | return ret;
|
---|
111 | }
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5
|
---|
115 | static int test_pkcs5_pbe_rc4_md5(void)
|
---|
116 | {
|
---|
117 | return test_pkcs5_pbe(EVP_rc4(), EVP_md5(), pbe_ciphertext_rc4_md5, sizeof(pbe_ciphertext_rc4_md5));
|
---|
118 | }
|
---|
119 | #endif
|
---|
120 |
|
---|
121 | #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
|
---|
122 | static int test_pkcs5_pbe_des_sha1(void)
|
---|
123 | {
|
---|
124 | return test_pkcs5_pbe(EVP_des_cbc(), EVP_sha1(), pbe_ciphertext_des_sha1, sizeof(pbe_ciphertext_des_sha1));
|
---|
125 | }
|
---|
126 | #endif
|
---|
127 |
|
---|
128 | #ifdef OPENSSL_NO_AUTOLOAD_CONFIG
|
---|
129 | /*
|
---|
130 | * For configurations where we are not autoloading configuration, we need
|
---|
131 | * to access the legacy provider. The easiest way is to load both the
|
---|
132 | * legacy and default providers directly and unload them on termination.
|
---|
133 | */
|
---|
134 | static OSSL_PROVIDER *legacy, *dflt;
|
---|
135 | #endif
|
---|
136 |
|
---|
137 | int setup_tests(void)
|
---|
138 | {
|
---|
139 | #ifdef OPENSSL_NO_AUTOLOAD_CONFIG
|
---|
140 | /* Load required providers if not done via configuration */
|
---|
141 | legacy = OSSL_PROVIDER_load(NULL, "legacy");
|
---|
142 | dflt = OSSL_PROVIDER_load(NULL, "default");
|
---|
143 | if (!TEST_ptr(legacy) || !TEST_ptr(dflt)) {
|
---|
144 | cleanup_tests();
|
---|
145 | return -1;
|
---|
146 | }
|
---|
147 | #endif
|
---|
148 |
|
---|
149 | #if !defined OPENSSL_NO_RC4 && !defined OPENSSL_NO_MD5
|
---|
150 | ADD_TEST(test_pkcs5_pbe_rc4_md5);
|
---|
151 | #endif
|
---|
152 | #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_SHA1
|
---|
153 | ADD_TEST(test_pkcs5_pbe_des_sha1);
|
---|
154 | #endif
|
---|
155 |
|
---|
156 | return 1;
|
---|
157 | }
|
---|
158 |
|
---|
159 | #ifdef OPENSSL_NO_AUTOLOAD_CONFIG
|
---|
160 | void cleanup_tests(void)
|
---|
161 | {
|
---|
162 | /* Dispose of providers */
|
---|
163 | OSSL_PROVIDER_unload(legacy);
|
---|
164 | OSSL_PROVIDER_unload(dflt);
|
---|
165 | legacy = dflt = NULL;
|
---|
166 | }
|
---|
167 | #endif
|
---|