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 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
|
/*
* Copyright 2024-2025 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
#include "testutil.h"
#include <openssl/bio.h>
#include <openssl/pem.h>
/* dummy data that needs to be passed to the callback */
typedef struct CallbackData {
char magic;
int result;
} CALLBACK_DATA;
/* constants */
static const char weak_password[] = "weak_password";
static const char a0a_password[] = "aaaaaaaa\0aaaaaaaa";
static const char a0b_password[] = "aaaaaaaa\0bbbbbbbb";
static const char cb_magic = 'p';
/* shared working data for all tests */
static char *key_file = NULL;
static EVP_PKEY *original_pkey = NULL;
/* the test performed by the callback */
typedef enum CallbackTest {
CB_TEST_NEGATIVE = 0,
CB_TEST_ZERO_LENGTH,
CB_TEST_WEAK,
CB_TEST_16ZERO,
CB_TEST_A0A,
CB_TEST_A0B,
CB_TEST_MATCH_SIZE,
CB_TEST_EXCEED_SIZE
} CALLBACK_TEST;
static CALLBACK_TEST callback_test = CB_TEST_NEGATIVE;
typedef enum KeyEncoding {
KE_PEM = 0,
KE_PKCS8
} KEY_ENCODING;
typedef enum ExpectedResult {
ER_FAILURE = 0,
ER_SUCCESS
} EXPECTED_RESULT;
typedef enum OPTION_choice {
OPT_ERR = -1,
OPT_EOF = 0,
OPT_KEY_FILE,
OPT_TEST_ENUM
} OPTION_CHOICE;
const OPTIONS *test_get_options(void)
{
static const OPTIONS test_options[] = {
OPT_TEST_OPTIONS_DEFAULT_USAGE,
{ "keyfile", OPT_KEY_FILE, '<',
"The PEM file with the encrypted key to load" },
{ NULL }
};
return test_options;
}
static int callback_copy_password(char *buf, int size)
{
int ret = -1;
switch (callback_test) {
case CB_TEST_NEGATIVE:
break;
case CB_TEST_ZERO_LENGTH:
ret = 0;
break;
case CB_TEST_WEAK:
ret = sizeof(weak_password) - 1;
memcpy(buf, weak_password, ret);
break;
case CB_TEST_16ZERO:
memset(buf, 0, 16);
ret = 16;
break;
case CB_TEST_A0A:
ret = sizeof(a0a_password) - 1;
memcpy(buf, a0a_password, ret);
break;
case CB_TEST_A0B:
ret = sizeof(a0b_password) - 1;
memcpy(buf, a0b_password, ret);
break;
case CB_TEST_MATCH_SIZE:
memset(buf, 'e', size);
ret = size;
break;
case CB_TEST_EXCEED_SIZE:
memset(buf, 'e', size);
ret = 1000000;
break;
}
return ret;
}
static int read_callback(char *buf, int size, int rwflag, void *u)
{
CALLBACK_DATA *cb_data = (CALLBACK_DATA *)u;
int ret = -1;
/* basic verification of the received data */
if (!TEST_ptr(cb_data))
goto err;
if (!TEST_char_eq(cb_data->magic, cb_magic))
goto err;
if (!TEST_ptr(buf))
goto err;
if (!TEST_int_gt(size, 0))
goto err;
if (!TEST_int_eq(rwflag, 0))
goto err;
ret = callback_copy_password(buf, size);
cb_data->result = 1;
err:
return ret;
}
static int write_callback(char *buf, int size, int rwflag, void *u)
{
CALLBACK_DATA *cb_data = (CALLBACK_DATA *)u;
int ret = -1;
/* basic verification of the received data */
if (!TEST_ptr(cb_data))
goto err;
if (!TEST_char_eq(cb_data->magic, cb_magic))
goto err;
if (!TEST_ptr(buf))
goto err;
if (!TEST_int_gt(size, 0))
goto err;
if (!TEST_int_eq(rwflag, 1))
goto err;
ret = callback_copy_password(buf, size);
cb_data->result = 1;
err:
return ret;
}
static int re_encrypt_key(char **enc_data, int *enc_data_size,
KEY_ENCODING key_encoding)
{
CALLBACK_DATA cb_data;
int w_ret = 0;
BUF_MEM *bptr = NULL;
BIO *bio = NULL;
int ret = 0;
if (!TEST_ptr(enc_data))
goto err;
if (!TEST_ptr(enc_data_size))
goto err;
if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
goto err;
cb_data.magic = cb_magic;
cb_data.result = 0;
switch (key_encoding) {
case KE_PEM:
w_ret = PEM_write_bio_PrivateKey(bio, original_pkey, EVP_aes_256_cbc(),
NULL, 0, write_callback, &cb_data);
break;
case KE_PKCS8:
w_ret = i2d_PKCS8PrivateKey_bio(bio, original_pkey, EVP_aes_256_cbc(),
NULL, 0, write_callback, &cb_data);
break;
}
if (!TEST_int_ne(w_ret, 0))
goto err;
if (!TEST_char_eq(cb_data.magic, cb_magic))
goto err;
if (!TEST_int_eq(cb_data.result, 1))
goto err;
*enc_data_size = BIO_get_mem_data(bio, enc_data);
BIO_get_mem_ptr(bio, &bptr);
if (!BIO_set_close(bio, BIO_NOCLOSE))
goto err;
bptr->data = NULL;
ret = 1;
err:
BUF_MEM_free(bptr);
BIO_free(bio);
return ret;
}
static int decrypt_key(char *enc_data, int enc_data_size,
KEY_ENCODING key_encoding,
EXPECTED_RESULT expected_result)
{
CALLBACK_DATA cb_data;
EVP_PKEY *r_ret = NULL;
BIO *bio = NULL;
EVP_PKEY *pkey = NULL;
int ret = 0;
if (!TEST_ptr(bio = BIO_new_mem_buf(enc_data, enc_data_size)))
goto err;
cb_data.magic = cb_magic;
cb_data.result = 0;
switch (key_encoding) {
case KE_PEM:
r_ret = PEM_read_bio_PrivateKey(bio, &pkey, read_callback, &cb_data);
break;
case KE_PKCS8:
r_ret = d2i_PKCS8PrivateKey_bio(bio, &pkey, read_callback, &cb_data);
break;
}
if (expected_result == ER_SUCCESS) {
if (!TEST_ptr(r_ret))
goto err;
} else {
if (!TEST_ptr_null(r_ret))
goto err;
}
if (!TEST_char_eq(cb_data.magic, cb_magic))
goto err;
if (!TEST_int_eq(cb_data.result, 1))
goto err;
ret = 1;
err:
EVP_PKEY_free(pkey);
BIO_free(bio);
return ret;
}
static int full_cycle_test(KEY_ENCODING key_encoding, CALLBACK_TEST write_test,
CALLBACK_TEST read_test,
EXPECTED_RESULT expected_read_result)
{
char *enc_data = NULL;
int enc_data_size = 0;
int ret = 0;
callback_test = write_test;
if (!re_encrypt_key(&enc_data, &enc_data_size, key_encoding))
goto err;
callback_test = read_test;
if (!decrypt_key(enc_data, enc_data_size, key_encoding,
expected_read_result))
goto err;
ret = 1;
err:
OPENSSL_free(enc_data);
return ret;
}
static int test_pem_negative(void)
{
return full_cycle_test(KE_PEM, CB_TEST_WEAK, CB_TEST_NEGATIVE, ER_FAILURE);
}
static int test_pem_zero_length(void)
{
return full_cycle_test(KE_PEM, CB_TEST_ZERO_LENGTH, CB_TEST_ZERO_LENGTH,
ER_SUCCESS);
}
static int test_pem_weak(void)
{
return full_cycle_test(KE_PEM, CB_TEST_WEAK, CB_TEST_WEAK, ER_SUCCESS);
}
static int test_pem_16zero(void)
{
return full_cycle_test(KE_PEM, CB_TEST_16ZERO, CB_TEST_16ZERO, ER_SUCCESS);
}
static int test_pem_a0a(void)
{
return full_cycle_test(KE_PEM, CB_TEST_A0A, CB_TEST_A0A, ER_SUCCESS);
}
static int test_pem_a0a_a0b(void)
{
return full_cycle_test(KE_PEM, CB_TEST_A0A, CB_TEST_A0B, ER_FAILURE);
}
static int test_pem_match_size(void)
{
return full_cycle_test(KE_PEM, CB_TEST_MATCH_SIZE, CB_TEST_MATCH_SIZE,
ER_SUCCESS);
}
static int test_pem_exceed_size(void)
{
return full_cycle_test(KE_PEM, CB_TEST_MATCH_SIZE, CB_TEST_EXCEED_SIZE,
ER_FAILURE);
}
static int test_pkcs8_negative(void)
{
return full_cycle_test(KE_PKCS8, CB_TEST_WEAK, CB_TEST_NEGATIVE, ER_FAILURE);
}
static int test_pkcs8_zero_length(void)
{
return full_cycle_test(KE_PKCS8, CB_TEST_ZERO_LENGTH, CB_TEST_ZERO_LENGTH,
ER_SUCCESS);
}
static int test_pkcs8_weak(void)
{
return full_cycle_test(KE_PKCS8, CB_TEST_WEAK, CB_TEST_WEAK, ER_SUCCESS);
}
static int test_pkcs8_16zero(void)
{
return full_cycle_test(KE_PKCS8, CB_TEST_16ZERO, CB_TEST_16ZERO,
ER_SUCCESS);
}
static int test_pkcs8_a0a(void)
{
return full_cycle_test(KE_PKCS8, CB_TEST_A0A, CB_TEST_A0A, ER_SUCCESS);
}
static int test_pkcs8_a0a_a0b(void)
{
return full_cycle_test(KE_PKCS8, CB_TEST_A0A, CB_TEST_A0B, ER_FAILURE);
}
static int test_pkcs8_match_size(void)
{
return full_cycle_test(KE_PKCS8, CB_TEST_MATCH_SIZE, CB_TEST_MATCH_SIZE,
ER_SUCCESS);
}
static int test_pkcs8_exceed_size(void)
{
return full_cycle_test(KE_PKCS8, CB_TEST_MATCH_SIZE, CB_TEST_EXCEED_SIZE,
ER_FAILURE);
}
static int callback_original_pw(char *buf, int size, int rwflag, void *u)
{
memcpy(buf, weak_password, sizeof(weak_password) - 1);
return sizeof(weak_password) - 1;
}
int setup_tests(void)
{
OPTION_CHOICE o;
BIO *bio = NULL;
while ((o = opt_next()) != OPT_EOF) {
switch (o) {
case OPT_KEY_FILE:
key_file = opt_arg();
break;
case OPT_TEST_CASES:
break;
default:
case OPT_ERR:
return 0;
}
}
/* read the original key */
if (!TEST_ptr(bio = BIO_new_file(key_file, "r")))
return 0;
if (!TEST_ptr(PEM_read_bio_PrivateKey(bio, &original_pkey,
callback_original_pw, NULL))) {
BIO_free(bio);
return 0;
}
BIO_free(bio);
/* add all tests */
ADD_TEST(test_pem_negative);
ADD_TEST(test_pem_zero_length);
ADD_TEST(test_pem_weak);
ADD_TEST(test_pem_16zero);
ADD_TEST(test_pem_a0a);
ADD_TEST(test_pem_a0a_a0b);
ADD_TEST(test_pem_match_size);
ADD_TEST(test_pem_exceed_size);
ADD_TEST(test_pkcs8_negative);
ADD_TEST(test_pkcs8_zero_length);
ADD_TEST(test_pkcs8_weak);
ADD_TEST(test_pkcs8_16zero);
ADD_TEST(test_pkcs8_a0a);
ADD_TEST(test_pkcs8_a0a_a0b);
ADD_TEST(test_pkcs8_match_size);
ADD_TEST(test_pkcs8_exceed_size);
return 1;
}
void cleanup_tests(void)
{
EVP_PKEY_free(original_pkey);
}
|