cmsapitest.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /*
  2. * Copyright 2018-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. #include <string.h>
  10. #include <openssl/cms.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/pem.h>
  14. #include "testutil.h"
  15. static X509 *cert = NULL;
  16. static EVP_PKEY *privkey = NULL;
  17. static char *derin = NULL;
  18. static int test_encrypt_decrypt(const EVP_CIPHER *cipher)
  19. {
  20. int testresult = 0;
  21. STACK_OF(X509) *certstack = sk_X509_new_null();
  22. const char *msg = "Hello world";
  23. BIO *msgbio = BIO_new_mem_buf(msg, strlen(msg));
  24. BIO *outmsgbio = BIO_new(BIO_s_mem());
  25. CMS_ContentInfo* content = NULL;
  26. char buf[80];
  27. if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio))
  28. goto end;
  29. if (!TEST_int_gt(sk_X509_push(certstack, cert), 0))
  30. goto end;
  31. content = CMS_encrypt(certstack, msgbio, cipher, CMS_TEXT);
  32. if (!TEST_ptr(content))
  33. goto end;
  34. if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio,
  35. CMS_TEXT)))
  36. goto end;
  37. /* Check we got the message we first started with */
  38. if (!TEST_int_eq(BIO_gets(outmsgbio, buf, sizeof(buf)), strlen(msg))
  39. || !TEST_int_eq(strcmp(buf, msg), 0))
  40. goto end;
  41. testresult = 1;
  42. end:
  43. sk_X509_free(certstack);
  44. BIO_free(msgbio);
  45. BIO_free(outmsgbio);
  46. CMS_ContentInfo_free(content);
  47. return testresult && TEST_int_eq(ERR_peek_error(), 0);
  48. }
  49. static int test_encrypt_decrypt_aes_cbc(void)
  50. {
  51. return test_encrypt_decrypt(EVP_aes_128_cbc());
  52. }
  53. static int test_encrypt_decrypt_aes_128_gcm(void)
  54. {
  55. return test_encrypt_decrypt(EVP_aes_128_gcm());
  56. }
  57. static int test_encrypt_decrypt_aes_192_gcm(void)
  58. {
  59. return test_encrypt_decrypt(EVP_aes_192_gcm());
  60. }
  61. static int test_encrypt_decrypt_aes_256_gcm(void)
  62. {
  63. return test_encrypt_decrypt(EVP_aes_256_gcm());
  64. }
  65. static int test_d2i_CMS_bio_NULL(void)
  66. {
  67. BIO *bio;
  68. CMS_ContentInfo *cms = NULL;
  69. int ret = 0;
  70. /*
  71. * Test data generated using:
  72. * openssl cms -sign -md sha256 -signer ./test/certs/rootCA.pem -inkey \
  73. * ./test/certs/rootCA.key -nodetach -outform DER -in ./in.txt -out out.der \
  74. * -nosmimecap
  75. */
  76. static const unsigned char cms_data[] = {
  77. 0x30, 0x82, 0x05, 0xc5, 0x06, 0x09, 0x2a, 0x86,
  78. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0,
  79. 0x82, 0x05, 0xb6, 0x30, 0x82, 0x05, 0xb2, 0x02,
  80. 0x01, 0x01, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x09,
  81. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02,
  82. 0x01, 0x30, 0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48,
  83. 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x0f,
  84. 0x04, 0x0d, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20,
  85. 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x0d, 0x0a, 0xa0,
  86. 0x82, 0x03, 0x83, 0x30, 0x82, 0x03, 0x7f, 0x30,
  87. 0x82, 0x02, 0x67, 0xa0, 0x03, 0x02, 0x01, 0x02,
  88. 0x02, 0x09, 0x00, 0x88, 0x43, 0x29, 0xcb, 0xc2,
  89. 0xeb, 0x15, 0x9a, 0x30, 0x0d, 0x06, 0x09, 0x2a,
  90. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
  91. 0x05, 0x00, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09,
  92. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41,
  93. 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  94. 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
  95. 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21,
  96. 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  97. 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
  98. 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74,
  99. 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74,
  100. 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
  101. 0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74,
  102. 0x43, 0x41, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x35,
  103. 0x30, 0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35,
  104. 0x31, 0x31, 0x5a, 0x17, 0x0d, 0x33, 0x35, 0x30,
  105. 0x37, 0x30, 0x32, 0x31, 0x33, 0x31, 0x35, 0x31,
  106. 0x31, 0x5a, 0x30, 0x56, 0x31, 0x0b, 0x30, 0x09,
  107. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x41,
  108. 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
  109. 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d, 0x65,
  110. 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31, 0x21,
  111. 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c,
  112. 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e, 0x65,
  113. 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69, 0x74,
  114. 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c, 0x74,
  115. 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03, 0x55,
  116. 0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f, 0x74,
  117. 0x43, 0x41, 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d,
  118. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  119. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
  120. 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82,
  121. 0x01, 0x01, 0x00, 0xc0, 0xf1, 0x6b, 0x77, 0x88,
  122. 0xac, 0x35, 0xdf, 0xfb, 0x73, 0x53, 0x2f, 0x92,
  123. 0x80, 0x2f, 0x74, 0x16, 0x32, 0x4d, 0xf5, 0x10,
  124. 0x20, 0x6f, 0x6c, 0x3a, 0x8e, 0xd1, 0xdc, 0x6b,
  125. 0xe1, 0x2e, 0x3e, 0xc3, 0x04, 0x0f, 0xbf, 0x9b,
  126. 0xc4, 0xc9, 0x12, 0xd1, 0xe4, 0x0b, 0x45, 0x97,
  127. 0xe5, 0x06, 0xcd, 0x66, 0x3a, 0xe1, 0xe0, 0xe2,
  128. 0x2b, 0xdf, 0xa2, 0xc4, 0xec, 0x7b, 0xd3, 0x3d,
  129. 0x3c, 0x8a, 0xff, 0x5e, 0x74, 0xa0, 0xab, 0xa7,
  130. 0x03, 0x6a, 0x16, 0x5b, 0x5e, 0x92, 0xc4, 0x7e,
  131. 0x5b, 0x79, 0x8a, 0x69, 0xd4, 0xbc, 0x83, 0x5e,
  132. 0xae, 0x42, 0x92, 0x74, 0xa5, 0x2b, 0xe7, 0x00,
  133. 0xc1, 0xa9, 0xdc, 0xd5, 0xb1, 0x53, 0x07, 0x0f,
  134. 0x73, 0xf7, 0x8e, 0xad, 0x14, 0x3e, 0x25, 0x9e,
  135. 0xe5, 0x1e, 0xe6, 0xcc, 0x91, 0xcd, 0x95, 0x0c,
  136. 0x80, 0x44, 0x20, 0xc3, 0xfd, 0x17, 0xcf, 0x91,
  137. 0x3d, 0x63, 0x10, 0x1c, 0x14, 0x5b, 0xfb, 0xc3,
  138. 0xa8, 0xc1, 0x88, 0xb2, 0x77, 0xff, 0x9c, 0xdb,
  139. 0xfc, 0x6a, 0x44, 0x44, 0x44, 0xf7, 0x85, 0xec,
  140. 0x08, 0x2c, 0xd4, 0xdf, 0x81, 0xa3, 0x79, 0xc9,
  141. 0xfe, 0x1e, 0x9b, 0x93, 0x16, 0x53, 0xb7, 0x97,
  142. 0xab, 0xbe, 0x4f, 0x1a, 0xa5, 0xe2, 0xfa, 0x46,
  143. 0x05, 0xe4, 0x0d, 0x9c, 0x2a, 0xa4, 0xcc, 0xb9,
  144. 0x1e, 0x21, 0xa0, 0x6c, 0xc4, 0xab, 0x59, 0xb0,
  145. 0x40, 0x39, 0xbb, 0xf9, 0x88, 0xad, 0xfd, 0xdf,
  146. 0x8d, 0xb4, 0x0b, 0xaf, 0x7e, 0x41, 0xe0, 0x21,
  147. 0x3c, 0xc8, 0x33, 0x45, 0x49, 0x84, 0x2f, 0x93,
  148. 0x06, 0xee, 0xfd, 0x4f, 0xed, 0x4f, 0xf3, 0xbc,
  149. 0x9b, 0xde, 0xfc, 0x25, 0x5e, 0x55, 0xd5, 0x75,
  150. 0xd4, 0xc5, 0x7b, 0x3a, 0x40, 0x35, 0x06, 0x9f,
  151. 0xc4, 0x84, 0xb4, 0x6c, 0x93, 0x0c, 0xaf, 0x37,
  152. 0x5a, 0xaf, 0xb6, 0x41, 0x4d, 0x26, 0x23, 0x1c,
  153. 0xb8, 0x02, 0xb3, 0x02, 0x03, 0x01, 0x00, 0x01,
  154. 0xa3, 0x50, 0x30, 0x4e, 0x30, 0x0c, 0x06, 0x03,
  155. 0x55, 0x1d, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  156. 0x01, 0xff, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d,
  157. 0x0e, 0x04, 0x16, 0x04, 0x14, 0x85, 0x56, 0x89,
  158. 0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86, 0x03,
  159. 0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33, 0x6d,
  160. 0xfd, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23,
  161. 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0x85, 0x56,
  162. 0x89, 0x35, 0xe2, 0x9f, 0x00, 0x1a, 0xe1, 0x86,
  163. 0x03, 0x0b, 0x4b, 0xaf, 0x76, 0x12, 0x6b, 0x33,
  164. 0x6d, 0xfd, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  165. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  166. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x32, 0x0a,
  167. 0xbf, 0x2a, 0x0a, 0xe2, 0xbb, 0x4f, 0x43, 0xce,
  168. 0x88, 0xda, 0x5a, 0x39, 0x10, 0x37, 0x80, 0xbb,
  169. 0x37, 0x2d, 0x5e, 0x2d, 0x88, 0xdd, 0x26, 0x69,
  170. 0x9c, 0xe7, 0xb4, 0x98, 0x20, 0xb1, 0x25, 0xe6,
  171. 0x61, 0x59, 0x6d, 0x12, 0xec, 0x9b, 0x87, 0xbe,
  172. 0x57, 0xe1, 0x12, 0x05, 0xc5, 0x04, 0xf1, 0x17,
  173. 0xce, 0x14, 0xb8, 0x1c, 0x92, 0xd4, 0x95, 0x95,
  174. 0x2c, 0x5b, 0x28, 0x89, 0xfb, 0x72, 0x9c, 0x20,
  175. 0xd3, 0x32, 0x81, 0xa8, 0x85, 0xec, 0xc8, 0x08,
  176. 0x7b, 0xa8, 0x59, 0x5b, 0x3a, 0x6c, 0x31, 0xab,
  177. 0x52, 0xe2, 0x66, 0xcd, 0x14, 0x49, 0x5c, 0xf3,
  178. 0xd3, 0x3e, 0x62, 0xbc, 0x91, 0x16, 0xb4, 0x1c,
  179. 0xf5, 0xdd, 0x54, 0xaa, 0x3c, 0x61, 0x97, 0x79,
  180. 0xac, 0xe4, 0xc8, 0x43, 0x35, 0xc3, 0x0f, 0xfc,
  181. 0xf3, 0x70, 0x1d, 0xaf, 0xf0, 0x9c, 0x8a, 0x2a,
  182. 0x92, 0x93, 0x48, 0xaa, 0xd0, 0xe8, 0x47, 0xbe,
  183. 0x35, 0xc1, 0xc6, 0x7b, 0x6d, 0xda, 0xfa, 0x5d,
  184. 0x57, 0x45, 0xf3, 0xea, 0x41, 0x8f, 0x36, 0xc1,
  185. 0x3c, 0xf4, 0x52, 0x7f, 0x6e, 0x31, 0xdd, 0xba,
  186. 0x9a, 0xbc, 0x70, 0x56, 0x71, 0x38, 0xdc, 0x49,
  187. 0x57, 0x0c, 0xfd, 0x91, 0x17, 0xc5, 0xea, 0x87,
  188. 0xe5, 0x23, 0x74, 0x19, 0xb2, 0xb6, 0x99, 0x0c,
  189. 0x6b, 0xa2, 0x05, 0xf8, 0x51, 0x68, 0xed, 0x97,
  190. 0xe0, 0xdf, 0x62, 0xf9, 0x7e, 0x7a, 0x3a, 0x44,
  191. 0x71, 0x83, 0x57, 0x28, 0x49, 0x88, 0x69, 0xb5,
  192. 0x14, 0x1e, 0xda, 0x46, 0xe3, 0x6e, 0x78, 0xe1,
  193. 0xcb, 0x8f, 0xb5, 0x98, 0xb3, 0x2d, 0x6e, 0x5b,
  194. 0xb7, 0xf6, 0x93, 0x24, 0x14, 0x1f, 0xa4, 0xf6,
  195. 0x69, 0xbd, 0xff, 0x4c, 0x52, 0x50, 0x02, 0xc5,
  196. 0x43, 0x8d, 0x14, 0xe2, 0xd0, 0x75, 0x9f, 0x12,
  197. 0x5e, 0x94, 0x89, 0xd1, 0xef, 0x77, 0x89, 0x7d,
  198. 0x89, 0xd9, 0x9e, 0x76, 0x99, 0x24, 0x31, 0x82,
  199. 0x01, 0xf7, 0x30, 0x82, 0x01, 0xf3, 0x02, 0x01,
  200. 0x01, 0x30, 0x63, 0x30, 0x56, 0x31, 0x0b, 0x30,
  201. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  202. 0x41, 0x55, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
  203. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x53, 0x6f, 0x6d,
  204. 0x65, 0x2d, 0x53, 0x74, 0x61, 0x74, 0x65, 0x31,
  205. 0x21, 0x30, 0x1f, 0x06, 0x03, 0x55, 0x04, 0x0a,
  206. 0x0c, 0x18, 0x49, 0x6e, 0x74, 0x65, 0x72, 0x6e,
  207. 0x65, 0x74, 0x20, 0x57, 0x69, 0x64, 0x67, 0x69,
  208. 0x74, 0x73, 0x20, 0x50, 0x74, 0x79, 0x20, 0x4c,
  209. 0x74, 0x64, 0x31, 0x0f, 0x30, 0x0d, 0x06, 0x03,
  210. 0x55, 0x04, 0x03, 0x0c, 0x06, 0x72, 0x6f, 0x6f,
  211. 0x74, 0x43, 0x41, 0x02, 0x09, 0x00, 0x88, 0x43,
  212. 0x29, 0xcb, 0xc2, 0xeb, 0x15, 0x9a, 0x30, 0x0b,
  213. 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  214. 0x04, 0x02, 0x01, 0xa0, 0x69, 0x30, 0x18, 0x06,
  215. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
  216. 0x09, 0x03, 0x31, 0x0b, 0x06, 0x09, 0x2a, 0x86,
  217. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30,
  218. 0x1c, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
  219. 0x0d, 0x01, 0x09, 0x05, 0x31, 0x0f, 0x17, 0x0d,
  220. 0x32, 0x30, 0x31, 0x32, 0x31, 0x31, 0x30, 0x39,
  221. 0x30, 0x30, 0x31, 0x33, 0x5a, 0x30, 0x2f, 0x06,
  222. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
  223. 0x09, 0x04, 0x31, 0x22, 0x04, 0x20, 0xb0, 0x80,
  224. 0x22, 0xd3, 0x15, 0xcf, 0x1e, 0xb1, 0x2d, 0x26,
  225. 0x65, 0xbd, 0xed, 0x0e, 0x6a, 0xf4, 0x06, 0x53,
  226. 0xc0, 0xa0, 0xbe, 0x97, 0x52, 0x32, 0xfb, 0x49,
  227. 0xbc, 0xbd, 0x02, 0x1c, 0xfc, 0x36, 0x30, 0x0d,
  228. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  229. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82, 0x01,
  230. 0x00, 0x37, 0x44, 0x39, 0x08, 0xb2, 0x19, 0x52,
  231. 0x35, 0x9c, 0xd0, 0x67, 0x87, 0xae, 0xb8, 0x1c,
  232. 0x80, 0xf4, 0x03, 0x29, 0x2e, 0xe3, 0x76, 0x4a,
  233. 0xb0, 0x98, 0x10, 0x00, 0x9a, 0x30, 0xdb, 0x05,
  234. 0x28, 0x53, 0x34, 0x31, 0x14, 0xbd, 0x87, 0xb9,
  235. 0x4d, 0x45, 0x07, 0x97, 0xa3, 0x57, 0x0b, 0x7e,
  236. 0xd1, 0x67, 0xfb, 0x4e, 0x0f, 0x5b, 0x90, 0xb2,
  237. 0x6f, 0xe6, 0xce, 0x49, 0xdd, 0x72, 0x46, 0x71,
  238. 0x26, 0xa1, 0x1b, 0x98, 0x23, 0x7d, 0x69, 0x73,
  239. 0x84, 0xdc, 0xf9, 0xd2, 0x1c, 0x6d, 0xf6, 0xf5,
  240. 0x17, 0x49, 0x6e, 0x9d, 0x4d, 0xf1, 0xe2, 0x43,
  241. 0x29, 0x53, 0x55, 0xa5, 0x22, 0x1e, 0x89, 0x2c,
  242. 0xaf, 0xf2, 0x43, 0x47, 0xd5, 0xfa, 0xad, 0xe7,
  243. 0x89, 0x60, 0xbf, 0x96, 0x35, 0x6f, 0xc2, 0x99,
  244. 0xb7, 0x55, 0xc5, 0xe3, 0x04, 0x25, 0x1b, 0xf6,
  245. 0x7e, 0xf2, 0x2b, 0x14, 0xa9, 0x57, 0x96, 0xbe,
  246. 0xbd, 0x6e, 0x95, 0x44, 0x94, 0xbd, 0xaf, 0x9a,
  247. 0x6d, 0x77, 0x55, 0x5e, 0x6c, 0xf6, 0x32, 0x37,
  248. 0xec, 0xef, 0xe5, 0x81, 0xb0, 0xe3, 0x35, 0xc7,
  249. 0x86, 0xea, 0x47, 0x59, 0x38, 0xb6, 0x16, 0xfb,
  250. 0x1d, 0x10, 0x55, 0x48, 0xb1, 0x44, 0x33, 0xde,
  251. 0xf6, 0x29, 0xbe, 0xbf, 0xbc, 0x71, 0x3e, 0x49,
  252. 0xba, 0xe7, 0x9f, 0x4d, 0x6c, 0xfb, 0xec, 0xd2,
  253. 0xe0, 0x12, 0xa9, 0x7c, 0xc9, 0x9a, 0x7b, 0x85,
  254. 0x83, 0xb8, 0xca, 0xdd, 0xf6, 0xb7, 0x15, 0x75,
  255. 0x7b, 0x4a, 0x69, 0xcf, 0x0a, 0xc7, 0x80, 0x01,
  256. 0xe7, 0x94, 0x16, 0x7f, 0x8d, 0x3c, 0xfa, 0x1f,
  257. 0x05, 0x71, 0x76, 0x15, 0xb0, 0xf6, 0x61, 0x30,
  258. 0x58, 0x16, 0xbe, 0x1b, 0xd1, 0x93, 0xc4, 0x1a,
  259. 0x91, 0x0c, 0x48, 0xe2, 0x1c, 0x8e, 0xa5, 0xc5,
  260. 0xa7, 0x81, 0x44, 0x48, 0x3b, 0x10, 0xc2, 0x74,
  261. 0x07, 0xdf, 0xa8, 0xae, 0x57, 0xee, 0x7f, 0xe3,
  262. 0x6a
  263. };
  264. ret = TEST_ptr(bio = BIO_new_mem_buf(cms_data, sizeof(cms_data)))
  265. && TEST_ptr(cms = d2i_CMS_bio(bio, NULL))
  266. && TEST_true(CMS_verify(cms, NULL, NULL, NULL, NULL,
  267. CMS_NO_SIGNER_CERT_VERIFY));
  268. CMS_ContentInfo_free(cms);
  269. BIO_free(bio);
  270. return ret && TEST_int_eq(ERR_peek_error(), 0);
  271. }
  272. static unsigned char *read_all(BIO *bio, long *p_len)
  273. {
  274. const int step = 256;
  275. unsigned char *buf = NULL;
  276. unsigned char *tmp = NULL;
  277. int ret;
  278. *p_len = 0;
  279. for (;;) {
  280. tmp = OPENSSL_realloc(buf, *p_len + step);
  281. if (tmp == NULL)
  282. break;
  283. buf = tmp;
  284. ret = BIO_read(bio, buf + *p_len, step);
  285. if (ret < 0)
  286. break;
  287. *p_len += ret;
  288. if (ret < step)
  289. return buf;
  290. }
  291. /* Error */
  292. OPENSSL_free(buf);
  293. *p_len = 0;
  294. return NULL;
  295. }
  296. static int test_d2i_CMS_decode(const int idx)
  297. {
  298. BIO *bio = NULL;
  299. CMS_ContentInfo *cms = NULL;
  300. unsigned char *buf = NULL;
  301. const unsigned char *tmp = NULL;
  302. long buf_len = 0;
  303. int ret = 0;
  304. if (!TEST_ptr(bio = BIO_new_file(derin, "r")))
  305. goto end;
  306. switch (idx) {
  307. case 0:
  308. if (!TEST_ptr(cms = d2i_CMS_bio(bio, NULL)))
  309. goto end;
  310. break;
  311. case 1:
  312. if (!TEST_ptr(buf = read_all(bio, &buf_len)))
  313. goto end;
  314. tmp = buf;
  315. if (!TEST_ptr(cms = d2i_CMS_ContentInfo(NULL, &tmp, buf_len)))
  316. goto end;
  317. break;
  318. }
  319. if (!TEST_int_eq(ERR_peek_error(), 0))
  320. goto end;
  321. ret = 1;
  322. end:
  323. CMS_ContentInfo_free(cms);
  324. BIO_free(bio);
  325. OPENSSL_free(buf);
  326. return ret;
  327. }
  328. OPT_TEST_DECLARE_USAGE("certfile privkeyfile derfile\n")
  329. int setup_tests(void)
  330. {
  331. char *certin = NULL, *privkeyin = NULL;
  332. BIO *certbio = NULL, *privkeybio = NULL;
  333. if (!test_skip_common_options()) {
  334. TEST_error("Error parsing test options\n");
  335. return 0;
  336. }
  337. if (!TEST_ptr(certin = test_get_argument(0))
  338. || !TEST_ptr(privkeyin = test_get_argument(1))
  339. || !TEST_ptr(derin = test_get_argument(2)))
  340. return 0;
  341. certbio = BIO_new_file(certin, "r");
  342. if (!TEST_ptr(certbio))
  343. return 0;
  344. if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) {
  345. BIO_free(certbio);
  346. return 0;
  347. }
  348. BIO_free(certbio);
  349. privkeybio = BIO_new_file(privkeyin, "r");
  350. if (!TEST_ptr(privkeybio)) {
  351. X509_free(cert);
  352. cert = NULL;
  353. return 0;
  354. }
  355. if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) {
  356. BIO_free(privkeybio);
  357. X509_free(cert);
  358. cert = NULL;
  359. return 0;
  360. }
  361. BIO_free(privkeybio);
  362. ADD_TEST(test_encrypt_decrypt_aes_cbc);
  363. ADD_TEST(test_encrypt_decrypt_aes_128_gcm);
  364. ADD_TEST(test_encrypt_decrypt_aes_192_gcm);
  365. ADD_TEST(test_encrypt_decrypt_aes_256_gcm);
  366. ADD_TEST(test_d2i_CMS_bio_NULL);
  367. ADD_ALL_TESTS(test_d2i_CMS_decode, 2);
  368. return 1;
  369. }
  370. void cleanup_tests(void)
  371. {
  372. X509_free(cert);
  373. EVP_PKEY_free(privkey);
  374. }