1 /* 2 * Copyright 2018-2026 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 <openssl/pem.h> 13 #include <openssl/cms.h> 14 #include <openssl/bio.h> 15 #include <openssl/x509.h> 16 #include "../crypto/cms/cms_local.h" /* for d.signedData and d.envelopedData */ 17 18 #include "testutil.h" 19 20 static X509 *cert = NULL; 21 static EVP_PKEY *privkey = NULL; 22 static char *derin = NULL; 23 static char *too_long_iv_cms_in = NULL; 24 25 static int test_encrypt_decrypt(const EVP_CIPHER *cipher) 26 { 27 int testresult = 0; 28 STACK_OF(X509) *certstack = sk_X509_new_null(); 29 const char *msg = "Hello world"; 30 BIO *msgbio = BIO_new_mem_buf(msg, strlen(msg)); 31 BIO *outmsgbio = BIO_new(BIO_s_mem()); 32 CMS_ContentInfo *content = NULL; 33 BIO *contentbio = NULL; 34 char buf[80]; 35 36 if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio)) 37 goto end; 38 39 if (!TEST_int_gt(sk_X509_push(certstack, cert), 0)) 40 goto end; 41 42 content = CMS_encrypt(certstack, msgbio, cipher, CMS_TEXT); 43 if (!TEST_ptr(content)) 44 goto end; 45 46 if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio, 47 CMS_TEXT))) 48 goto end; 49 50 if (!(EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) 51 && !TEST_ptr(contentbio = CMS_EnvelopedData_decrypt(content->d.envelopedData, 52 NULL, privkey, cert, NULL, 53 CMS_TEXT, NULL, NULL))) 54 goto end; 55 56 /* Check we got the message we first started with */ 57 if (!TEST_int_eq(BIO_gets(outmsgbio, buf, sizeof(buf)), strlen(msg)) 58 || !TEST_int_eq(strcmp(buf, msg), 0)) 59 goto end; 60 61 testresult = 1; 62 end: 63 BIO_free(contentbio); 64 sk_X509_free(certstack); 65 BIO_free(msgbio); 66 BIO_free(outmsgbio); 67 CMS_ContentInfo_free(content); 68 69 return testresult && TEST_int_eq(ERR_peek_error(), 0); 70 } 71 72 static int test_encrypt_decrypt_aes_cbc(void) 73 { 74 return test_encrypt_decrypt(EVP_aes_128_cbc()); 75 } 76 77 static int test_encrypt_decrypt_aes_128_gcm(void) 78 { 79 return test_encrypt_decrypt(EVP_aes_128_gcm()); 80 } 81 82 static int test_encrypt_decrypt_aes_192_gcm(void) 83 { 84 return test_encrypt_decrypt(EVP_aes_192_gcm()); 85 } 86 87 static int test_encrypt_decrypt_aes_256_gcm(void) 88 { 89 return test_encrypt_decrypt(EVP_aes_256_gcm()); 90 } 91 92 static int test_CMS_add1_cert(void) 93 { 94 CMS_ContentInfo *cms = NULL; 95 int ret = 0; 96 97 ret = TEST_ptr(cms = CMS_ContentInfo_new()) 98 && TEST_ptr(CMS_add1_signer(cms, cert, privkey, NULL, 0)) 99 && TEST_true(CMS_add1_cert(cms, cert)); /* add cert again */ 100 101 CMS_ContentInfo_free(cms); 102 return ret; 103 } 104 105 static int test_d2i_CMS_bio_NULL(void) 106 { 107 BIO *bio, *content = NULL; 108 CMS_ContentInfo *cms = NULL; 109 unsigned int flags = CMS_NO_SIGNER_CERT_VERIFY; 110 int ret = 0; 111 112 /* 113 * Test data generated using: 114 * openssl cms -sign -md sha256 -signer ./test/certs/rootCA.pem -inkey \ 115 * ./test/certs/rootCA.key -nodetach -outform DER -in ./in.txt -out out.der \ 116 * -nosmimecap 117 */ 118 static const unsigned char cms_data[] = { 119 0x30, 0x82, 0x05, 0xc5, 0x06, 0x09, 0x2a, 0x86, 120 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0, 121 0x82, 0x05, 0xb6, 0x30, 0x82, 0x05, 0xb2, 0x02, 122 0x01, 0x01, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x09, 123 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 124 0x01, 0x30, 0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48, 125 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x0f, 126 0x04, 0x0d, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 127 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0d, 0x0a, 0xa0, 128 0x82, 0x03, 0x83, 0x30, 0x82, 0x03, 0x7f, 0x30, 129 0x82, 0x02, 0x67, 0xa0, 0x03, 0x02, 0x01, 0x02, 130 0x02, 0x09, 0x00, 0x88, 0x43, 0x29, 0xcb, 0xc2, 131 0xeb, 0x15, 0x9a, 0x30, 0x0d, 0x06, 0x09, 0x2a, 132 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 133 0x05, 0x00, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09, 134 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 135 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 136 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 137 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 138 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 139 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 140 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 141 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 142 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 143 0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74, 144 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x35, 145 0x30, 0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35, 146 0x31, 0x31, 0x5a, 0x17, 0x0d, 0x33, 0x35, 0x30, 147 0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35, 0x31, 148 0x31, 0x5a, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09, 149 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41, 150 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 151 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65, 152 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21, 153 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 154 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65, 155 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74, 156 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74, 157 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55, 158 0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74, 159 0x43, 0x41, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 160 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 161 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 162 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 163 0x01, 0x01, 0x00, 0xc0, 0xf1, 0x6b, 0x77, 0x88, 164 0xac, 0x35, 0xdf, 0xfb, 0x73, 0x53, 0x2f, 0x92, 165 0x80, 0x2f, 0x74, 0x16, 0x32, 0x4d, 0xf5, 0x10, 166 0x20, 0x6f, 0x6c, 0x3a, 0x8e, 0xd1, 0xdc, 0x6b, 167 0xe1, 0x2e, 0x3e, 0xc3, 0x04, 0x0f, 0xbf, 0x9b, 168 0xc4, 0xc9, 0x12, 0xd1, 0xe4, 0x0b, 0x45, 0x97, 169 0xe5, 0x06, 0xcd, 0x66, 0x3a, 0xe1, 0xe0, 0xe2, 170 0x2b, 0xdf, 0xa2, 0xc4, 0xec, 0x7b, 0xd3, 0x3d, 171 0x3c, 0x8a, 0xff, 0x5e, 0x74, 0xa0, 0xab, 0xa7, 172 0x03, 0x6a, 0x16, 0x5b, 0x5e, 0x92, 0xc4, 0x7e, 173 0x5b, 0x79, 0x8a, 0x69, 0xd4, 0xbc, 0x83, 0x5e, 174 0xae, 0x42, 0x92, 0x74, 0xa5, 0x2b, 0xe7, 0x00, 175 0xc1, 0xa9, 0xdc, 0xd5, 0xb1, 0x53, 0x07, 0x0f, 176 0x73, 0xf7, 0x8e, 0xad, 0x14, 0x3e, 0x25, 0x9e, 177 0xe5, 0x1e, 0xe6, 0xcc, 0x91, 0xcd, 0x95, 0x0c, 178 0x80, 0x44, 0x20, 0xc3, 0xfd, 0x17, 0xcf, 0x91, 179 0x3d, 0x63, 0x10, 0x1c, 0x14, 0x5b, 0xfb, 0xc3, 180 0xa8, 0xc1, 0x88, 0xb2, 0x77, 0xff, 0x9c, 0xdb, 181 0xfc, 0x6a, 0x44, 0x44, 0x44, 0xf7, 0x85, 0xec, 182 0x08, 0x2c, 0xd4, 0xdf, 0x81, 0xa3, 0x79, 0xc9, 183 0xfe, 0x1e, 0x9b, 0x93, 0x16, 0x53, 0xb7, 0x97, 184 0xab, 0xbe, 0x4f, 0x1a, 0xa5, 0xe2, 0xfa, 0x46, 185 0x05, 0xe4, 0x0d, 0x9c, 0x2a, 0xa4, 0xcc, 0xb9, 186 0x1e, 0x21, 0xa0, 0x6c, 0xc4, 0xab, 0x59, 0xb0, 187 0x40, 0x39, 0xbb, 0xf9, 0x88, 0xad, 0xfd, 0xdf, 188 0x8d, 0xb4, 0x0b, 0xaf, 0x7e, 0x41, 0xe0, 0x21, 189 0x3c, 0xc8, 0x33, 0x45, 0x49, 0x84, 0x2f, 0x93, 190 0x06, 0xee, 0xfd, 0x4f, 0xed, 0x4f, 0xf3, 0xbc, 191 0x9b, 0xde, 0xfc, 0x25, 0x5e, 0x55, 0xd5, 0x75, 192 0xd4, 0xc5, 0x7b, 0x3a, 0x40, 0x35, 0x06, 0x9f, 193 0xc4, 0x84, 0xb4, 0x6c, 0x93, 0x0c, 0xaf, 0x37, 194 0x5a, 0xaf, 0xb6, 0x41, 0x4d, 0x26, 0x23, 0x1c, 195 0xb8, 0x02, 0xb3, 0x02, 0x03, 0x01, 0x00, 0x01, 196 0xa3, 0x50, 0x30, 0x4e, 0x30, 0x0c, 0x06, 0x03, 197 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 198 0x01, 0xff, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 199 0x0e, 0x04, 0x16, 0x04, 0x14, 0x85, 0x56, 0x89, 200 0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86, 0x03, 201 0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33, 0x6d, 202 0xfd, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 203 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x85, 0x56, 204 0x89, 0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86, 205 0x03, 0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33, 206 0x6d, 0xfd, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 207 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 208 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x32, 0x0a, 209 0xbf, 0x2a, 0x0a, 0xe2, 0xbb, 0x4f, 0x43, 0xce, 210 0x88, 0xda, 0x5a, 0x39, 0x10, 0x37, 0x80, 0xbb, 211 0x37, 0x2d, 0x5e, 0x2d, 0x88, 0xdd, 0x26, 0x69, 212 0x9c, 0xe7, 0xb4, 0x98, 0x20, 0xb1, 0x25, 0xe6, 213 0x61, 0x59, 0x6d, 0x12, 0xec, 0x9b, 0x87, 0xbe, 214 0x57, 0xe1, 0x12, 0x05, 0xc5, 0x04, 0xf1, 0x17, 215 0xce, 0x14, 0xb8, 0x1c, 0x92, 0xd4, 0x95, 0x95, 216 0x2c, 0x5b, 0x28, 0x89, 0xfb, 0x72, 0x9c, 0x20, 217 0xd3, 0x32, 0x81, 0xa8, 0x85, 0xec, 0xc8, 0x08, 218 0x7b, 0xa8, 0x59, 0x5b, 0x3a, 0x6c, 0x31, 0xab, 219 0x52, 0xe2, 0x66, 0xcd, 0x14, 0x49, 0x5c, 0xf3, 220 0xd3, 0x3e, 0x62, 0xbc, 0x91, 0x16, 0xb4, 0x1c, 221 0xf5, 0xdd, 0x54, 0xaa, 0x3c, 0x61, 0x97, 0x79, 222 0xac, 0xe4, 0xc8, 0x43, 0x35, 0xc3, 0x0f, 0xfc, 223 0xf3, 0x70, 0x1d, 0xaf, 0xf0, 0x9c, 0x8a, 0x2a, 224 0x92, 0x93, 0x48, 0xaa, 0xd0, 0xe8, 0x47, 0xbe, 225 0x35, 0xc1, 0xc6, 0x7b, 0x6d, 0xda, 0xfa, 0x5d, 226 0x57, 0x45, 0xf3, 0xea, 0x41, 0x8f, 0x36, 0xc1, 227 0x3c, 0xf4, 0x52, 0x7f, 0x6e, 0x31, 0xdd, 0xba, 228 0x9a, 0xbc, 0x70, 0x56, 0x71, 0x38, 0xdc, 0x49, 229 0x57, 0x0c, 0xfd, 0x91, 0x17, 0xc5, 0xea, 0x87, 230 0xe5, 0x23, 0x74, 0x19, 0xb2, 0xb6, 0x99, 0x0c, 231 0x6b, 0xa2, 0x05, 0xf8, 0x51, 0x68, 0xed, 0x97, 232 0xe0, 0xdf, 0x62, 0xf9, 0x7e, 0x7a, 0x3a, 0x44, 233 0x71, 0x83, 0x57, 0x28, 0x49, 0x88, 0x69, 0xb5, 234 0x14, 0x1e, 0xda, 0x46, 0xe3, 0x6e, 0x78, 0xe1, 235 0xcb, 0x8f, 0xb5, 0x98, 0xb3, 0x2d, 0x6e, 0x5b, 236 0xb7, 0xf6, 0x93, 0x24, 0x14, 0x1f, 0xa4, 0xf6, 237 0x69, 0xbd, 0xff, 0x4c, 0x52, 0x50, 0x02, 0xc5, 238 0x43, 0x8d, 0x14, 0xe2, 0xd0, 0x75, 0x9f, 0x12, 239 0x5e, 0x94, 0x89, 0xd1, 0xef, 0x77, 0x89, 0x7d, 240 0x89, 0xd9, 0x9e, 0x76, 0x99, 0x24, 0x31, 0x82, 241 0x01, 0xf7, 0x30, 0x82, 0x01, 0xf3, 0x02, 0x01, 242 0x01, 0x30, 0x63, 0x30, 0x56, 0x31, 0x0b, 0x30, 243 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 244 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 245 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 246 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 247 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 248 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 249 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 250 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 251 0x74, 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 252 0x55, 0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 253 0x74, 0x43, 0x41, 0x02, 0x09, 0x00, 0x88, 0x43, 254 0x29, 0xcb, 0xc2, 0xeb, 0x15, 0x9a, 0x30, 0x0b, 255 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 256 0x04, 0x02, 0x01, 0xa0, 0x69, 0x30, 0x18, 0x06, 257 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 258 0x09, 0x03, 0x31, 0x0b, 0x06, 0x09, 0x2a, 0x86, 259 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30, 260 0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 261 0x0d, 0x01, 0x09, 0x05, 0x31, 0x0f, 0x17, 0x0d, 262 0x32, 0x30, 0x31, 0x32, 0x31, 0x31, 0x30, 0x39, 263 0x30, 0x30, 0x31, 0x33, 0x5a, 0x30, 0x2f, 0x06, 264 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 265 0x09, 0x04, 0x31, 0x22, 0x04, 0x20, 0xb0, 0x80, 266 0x22, 0xd3, 0x15, 0xcf, 0x1e, 0xb1, 0x2d, 0x26, 267 0x65, 0xbd, 0xed, 0x0e, 0x6a, 0xf4, 0x06, 0x53, 268 0xc0, 0xa0, 0xbe, 0x97, 0x52, 0x32, 0xfb, 0x49, 269 0xbc, 0xbd, 0x02, 0x1c, 0xfc, 0x36, 0x30, 0x0d, 270 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 271 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x01, 272 0x00, 0x37, 0x44, 0x39, 0x08, 0xb2, 0x19, 0x52, 273 0x35, 0x9c, 0xd0, 0x67, 0x87, 0xae, 0xb8, 0x1c, 274 0x80, 0xf4, 0x03, 0x29, 0x2e, 0xe3, 0x76, 0x4a, 275 0xb0, 0x98, 0x10, 0x00, 0x9a, 0x30, 0xdb, 0x05, 276 0x28, 0x53, 0x34, 0x31, 0x14, 0xbd, 0x87, 0xb9, 277 0x4d, 0x45, 0x07, 0x97, 0xa3, 0x57, 0x0b, 0x7e, 278 0xd1, 0x67, 0xfb, 0x4e, 0x0f, 0x5b, 0x90, 0xb2, 279 0x6f, 0xe6, 0xce, 0x49, 0xdd, 0x72, 0x46, 0x71, 280 0x26, 0xa1, 0x1b, 0x98, 0x23, 0x7d, 0x69, 0x73, 281 0x84, 0xdc, 0xf9, 0xd2, 0x1c, 0x6d, 0xf6, 0xf5, 282 0x17, 0x49, 0x6e, 0x9d, 0x4d, 0xf1, 0xe2, 0x43, 283 0x29, 0x53, 0x55, 0xa5, 0x22, 0x1e, 0x89, 0x2c, 284 0xaf, 0xf2, 0x43, 0x47, 0xd5, 0xfa, 0xad, 0xe7, 285 0x89, 0x60, 0xbf, 0x96, 0x35, 0x6f, 0xc2, 0x99, 286 0xb7, 0x55, 0xc5, 0xe3, 0x04, 0x25, 0x1b, 0xf6, 287 0x7e, 0xf2, 0x2b, 0x14, 0xa9, 0x57, 0x96, 0xbe, 288 0xbd, 0x6e, 0x95, 0x44, 0x94, 0xbd, 0xaf, 0x9a, 289 0x6d, 0x77, 0x55, 0x5e, 0x6c, 0xf6, 0x32, 0x37, 290 0xec, 0xef, 0xe5, 0x81, 0xb0, 0xe3, 0x35, 0xc7, 291 0x86, 0xea, 0x47, 0x59, 0x38, 0xb6, 0x16, 0xfb, 292 0x1d, 0x10, 0x55, 0x48, 0xb1, 0x44, 0x33, 0xde, 293 0xf6, 0x29, 0xbe, 0xbf, 0xbc, 0x71, 0x3e, 0x49, 294 0xba, 0xe7, 0x9f, 0x4d, 0x6c, 0xfb, 0xec, 0xd2, 295 0xe0, 0x12, 0xa9, 0x7c, 0xc9, 0x9a, 0x7b, 0x85, 296 0x83, 0xb8, 0xca, 0xdd, 0xf6, 0xb7, 0x15, 0x75, 297 0x7b, 0x4a, 0x69, 0xcf, 0x0a, 0xc7, 0x80, 0x01, 298 0xe7, 0x94, 0x16, 0x7f, 0x8d, 0x3c, 0xfa, 0x1f, 299 0x05, 0x71, 0x76, 0x15, 0xb0, 0xf6, 0x61, 0x30, 300 0x58, 0x16, 0xbe, 0x1b, 0xd1, 0x93, 0xc4, 0x1a, 301 0x91, 0x0c, 0x48, 0xe2, 0x1c, 0x8e, 0xa5, 0xc5, 302 0xa7, 0x81, 0x44, 0x48, 0x3b, 0x10, 0xc2, 0x74, 303 0x07, 0xdf, 0xa8, 0xae, 0x57, 0xee, 0x7f, 0xe3, 304 0x6a 305 }; 306 307 ret = TEST_ptr(bio = BIO_new_mem_buf(cms_data, sizeof(cms_data))) 308 && TEST_ptr(cms = d2i_CMS_bio(bio, NULL)) 309 && TEST_true(CMS_verify(cms, NULL, NULL, NULL, NULL, flags)) 310 && TEST_ptr(content = CMS_SignedData_verify(cms->d.signedData, NULL, NULL, NULL, 311 NULL, NULL, flags, NULL, NULL)); 312 BIO_free(content); 313 CMS_ContentInfo_free(cms); 314 BIO_free(bio); 315 return ret && TEST_int_eq(ERR_peek_error(), 0); 316 } 317 318 static unsigned char *read_all(BIO *bio, long *p_len) 319 { 320 const int step = 256; 321 unsigned char *buf = NULL; 322 unsigned char *tmp = NULL; 323 int ret; 324 325 *p_len = 0; 326 for (;;) { 327 tmp = OPENSSL_realloc(buf, *p_len + step); 328 if (tmp == NULL) 329 break; 330 buf = tmp; 331 ret = BIO_read(bio, buf + *p_len, step); 332 if (ret < 0) 333 break; 334 335 if (LONG_MAX - ret < *p_len) 336 break; 337 338 *p_len += ret; 339 340 if (ret < step) 341 return buf; 342 } 343 344 /* Error */ 345 OPENSSL_free(buf); 346 *p_len = 0; 347 return NULL; 348 } 349 350 static int test_d2i_CMS_decode(const int idx) 351 { 352 BIO *bio = NULL; 353 CMS_ContentInfo *cms = NULL; 354 unsigned char *buf = NULL; 355 const unsigned char *tmp = NULL; 356 long buf_len = 0; 357 int ret = 0; 358 359 if (!TEST_ptr(bio = BIO_new_file(derin, "r"))) 360 goto end; 361 362 switch (idx) { 363 case 0: 364 if (!TEST_ptr(cms = d2i_CMS_bio(bio, NULL))) 365 goto end; 366 break; 367 case 1: 368 if (!TEST_ptr(buf = read_all(bio, &buf_len))) 369 goto end; 370 tmp = buf; 371 if (!TEST_ptr(cms = d2i_CMS_ContentInfo(NULL, &tmp, buf_len))) 372 goto end; 373 break; 374 } 375 376 if (!TEST_int_eq(ERR_peek_error(), 0)) 377 goto end; 378 379 ret = 1; 380 end: 381 CMS_ContentInfo_free(cms); 382 BIO_free(bio); 383 OPENSSL_free(buf); 384 385 return ret; 386 } 387 388 static int test_CMS_set1_key_mem_leak(void) 389 { 390 CMS_ContentInfo *cms; 391 unsigned char key[32] = { 0 }; 392 int ret = 0; 393 394 if (!TEST_ptr(cms = CMS_ContentInfo_new())) 395 return 0; 396 397 if (!TEST_true(CMS_EncryptedData_set1_key(cms, EVP_aes_256_cbc(), 398 key, 32))) 399 goto end; 400 401 if (!TEST_true(CMS_EncryptedData_set1_key(cms, EVP_aes_128_cbc(), 402 key, 16))) 403 goto end; 404 405 ret = 1; 406 end: 407 CMS_ContentInfo_free(cms); 408 return ret; 409 } 410 411 static int test_encrypted_data(void) 412 { 413 const char *msg = "Hello world"; 414 BIO *msgbio = BIO_new_mem_buf(msg, (int)strlen(msg)); 415 uint8_t key[16] = { 0 }; 416 size_t keylen = 16; 417 CMS_ContentInfo *cms; 418 BIO *decryptbio = BIO_new(BIO_s_mem()); 419 char buf[80]; 420 int ret = 0; 421 422 cms = CMS_EncryptedData_encrypt(msgbio, EVP_aes_128_cbc(), key, keylen, SMIME_BINARY); 423 if (!TEST_ptr(cms)) 424 goto end; 425 426 if (!TEST_true(CMS_EncryptedData_decrypt(cms, key, keylen, NULL, decryptbio, SMIME_BINARY))) 427 goto end; 428 429 /* Check we got the message we first started with */ 430 if (!TEST_int_eq(BIO_gets(decryptbio, buf, sizeof(buf)), (int)strlen(msg)) 431 || !TEST_int_eq(strcmp(buf, msg), 0)) 432 goto end; 433 434 ret = 1; 435 end: 436 CMS_ContentInfo_free(cms); 437 BIO_free(msgbio); 438 BIO_free(decryptbio); 439 return ret; 440 } 441 442 static int test_encrypted_data_aead(void) 443 { 444 const char *msg = "Hello world"; 445 BIO *msgbio = BIO_new_mem_buf(msg, (int)strlen(msg)); 446 uint8_t key[16] = { 0 }; 447 size_t keylen = 16; 448 CMS_ContentInfo *cms; 449 BIO *decryptbio = BIO_new(BIO_s_mem()); 450 int ret = 0; 451 452 cms = CMS_ContentInfo_new(); 453 if (!TEST_ptr(cms)) 454 goto end; 455 456 /* 457 * AEAD algorithms are not supported by the CMS EncryptedData so setting 458 * the cipher to AES GCM 128 will result in a failure 459 */ 460 if (!TEST_false(CMS_EncryptedData_set1_key(cms, EVP_aes_128_gcm(), key, keylen))) 461 goto end; 462 463 CMS_ContentInfo_free(cms); 464 cms = NULL; 465 466 /* 467 * AEAD algorithms are not supported by the CMS EncryptedData so setting 468 * the cipher to AES GCM 128 will result in a failure 469 */ 470 cms = CMS_EncryptedData_encrypt(msgbio, EVP_aes_128_gcm(), key, keylen, SMIME_BINARY); 471 if (!TEST_ptr_null(cms)) 472 goto end; 473 474 ret = 1; 475 476 end: 477 CMS_ContentInfo_free(cms); 478 BIO_free(msgbio); 479 BIO_free(decryptbio); 480 return ret; 481 } 482 483 static int test_cms_aesgcm_iv_too_long(void) 484 { 485 int ret = 0; 486 BIO *cmsbio = NULL, *out = NULL; 487 CMS_ContentInfo *cms = NULL; 488 unsigned long err = 0; 489 490 if (!TEST_ptr(cmsbio = BIO_new_file(too_long_iv_cms_in, "r"))) 491 goto end; 492 493 if (!TEST_ptr(cms = PEM_read_bio_CMS(cmsbio, NULL, NULL, NULL))) 494 goto end; 495 496 /* Must fail cleanly (no crash) */ 497 if (!TEST_false(CMS_decrypt(cms, privkey, cert, NULL, out, 0))) 498 goto end; 499 err = ERR_peek_last_error(); 500 if (!TEST_ulong_ne(err, 0)) 501 goto end; 502 if (!TEST_int_eq(ERR_GET_LIB(err), ERR_LIB_CMS)) 503 goto end; 504 if (!TEST_int_eq(ERR_GET_REASON(err), CMS_R_CIPHER_PARAMETER_INITIALISATION_ERROR)) 505 goto end; 506 507 ret = 1; 508 end: 509 CMS_ContentInfo_free(cms); 510 BIO_free(cmsbio); 511 BIO_free(out); 512 return ret; 513 } 514 515 OPT_TEST_DECLARE_USAGE("certfile privkeyfile derfile\n") 516 517 int setup_tests(void) 518 { 519 char *certin = NULL, *privkeyin = NULL; 520 BIO *certbio = NULL, *privkeybio = NULL; 521 522 if (!test_skip_common_options()) { 523 TEST_error("Error parsing test options\n"); 524 return 0; 525 } 526 527 if (!TEST_ptr(certin = test_get_argument(0)) 528 || !TEST_ptr(privkeyin = test_get_argument(1)) 529 || !TEST_ptr(derin = test_get_argument(2)) 530 || !TEST_ptr(too_long_iv_cms_in = test_get_argument(3))) 531 return 0; 532 533 certbio = BIO_new_file(certin, "r"); 534 if (!TEST_ptr(certbio)) 535 return 0; 536 if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) { 537 BIO_free(certbio); 538 return 0; 539 } 540 BIO_free(certbio); 541 542 privkeybio = BIO_new_file(privkeyin, "r"); 543 if (!TEST_ptr(privkeybio)) { 544 X509_free(cert); 545 cert = NULL; 546 return 0; 547 } 548 if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) { 549 BIO_free(privkeybio); 550 X509_free(cert); 551 cert = NULL; 552 return 0; 553 } 554 BIO_free(privkeybio); 555 556 ADD_TEST(test_encrypt_decrypt_aes_cbc); 557 ADD_TEST(test_encrypt_decrypt_aes_128_gcm); 558 ADD_TEST(test_encrypt_decrypt_aes_192_gcm); 559 ADD_TEST(test_encrypt_decrypt_aes_256_gcm); 560 ADD_TEST(test_CMS_add1_cert); 561 ADD_TEST(test_d2i_CMS_bio_NULL); 562 ADD_TEST(test_CMS_set1_key_mem_leak); 563 ADD_TEST(test_encrypted_data); 564 ADD_TEST(test_encrypted_data_aead); 565 ADD_ALL_TESTS(test_d2i_CMS_decode, 2); 566 ADD_TEST(test_cms_aesgcm_iv_too_long); 567 return 1; 568 } 569 570 void cleanup_tests(void) 571 { 572 X509_free(cert); 573 EVP_PKEY_free(privkey); 574 } 575