cmsapitest.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 2018-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. #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 int test_encrypt_decrypt(const EVP_CIPHER *cipher)
  18. {
  19. int testresult = 0;
  20. STACK_OF(X509) *certstack = sk_X509_new_null();
  21. const char *msg = "Hello world";
  22. BIO *msgbio = BIO_new_mem_buf(msg, strlen(msg));
  23. BIO *outmsgbio = BIO_new(BIO_s_mem());
  24. CMS_ContentInfo* content = NULL;
  25. char buf[80];
  26. if (!TEST_ptr(certstack) || !TEST_ptr(msgbio) || !TEST_ptr(outmsgbio))
  27. goto end;
  28. if (!TEST_int_gt(sk_X509_push(certstack, cert), 0))
  29. goto end;
  30. content = CMS_encrypt(certstack, msgbio, cipher, CMS_TEXT);
  31. if (!TEST_ptr(content))
  32. goto end;
  33. if (!TEST_true(CMS_decrypt(content, privkey, cert, NULL, outmsgbio,
  34. CMS_TEXT)))
  35. goto end;
  36. /* Check we got the message we first started with */
  37. if (!TEST_int_eq(BIO_gets(outmsgbio, buf, sizeof(buf)), strlen(msg))
  38. || !TEST_int_eq(strcmp(buf, msg), 0))
  39. goto end;
  40. testresult = 1;
  41. end:
  42. sk_X509_free(certstack);
  43. BIO_free(msgbio);
  44. BIO_free(outmsgbio);
  45. CMS_ContentInfo_free(content);
  46. return testresult;
  47. }
  48. static int test_encrypt_decrypt_aes_cbc(void)
  49. {
  50. return test_encrypt_decrypt(EVP_aes_128_cbc());
  51. }
  52. static int test_encrypt_decrypt_aes_128_gcm(void)
  53. {
  54. return test_encrypt_decrypt(EVP_aes_128_gcm());
  55. }
  56. static int test_encrypt_decrypt_aes_192_gcm(void)
  57. {
  58. return test_encrypt_decrypt(EVP_aes_192_gcm());
  59. }
  60. static int test_encrypt_decrypt_aes_256_gcm(void)
  61. {
  62. return test_encrypt_decrypt(EVP_aes_256_gcm());
  63. }
  64. OPT_TEST_DECLARE_USAGE("certfile privkeyfile\n")
  65. int setup_tests(void)
  66. {
  67. char *certin = NULL, *privkeyin = NULL;
  68. BIO *certbio = NULL, *privkeybio = NULL;
  69. if (!test_skip_common_options()) {
  70. TEST_error("Error parsing test options\n");
  71. return 0;
  72. }
  73. if (!TEST_ptr(certin = test_get_argument(0))
  74. || !TEST_ptr(privkeyin = test_get_argument(1)))
  75. return 0;
  76. certbio = BIO_new_file(certin, "r");
  77. if (!TEST_ptr(certbio))
  78. return 0;
  79. if (!TEST_true(PEM_read_bio_X509(certbio, &cert, NULL, NULL))) {
  80. BIO_free(certbio);
  81. return 0;
  82. }
  83. BIO_free(certbio);
  84. privkeybio = BIO_new_file(privkeyin, "r");
  85. if (!TEST_ptr(privkeybio)) {
  86. X509_free(cert);
  87. cert = NULL;
  88. return 0;
  89. }
  90. if (!TEST_true(PEM_read_bio_PrivateKey(privkeybio, &privkey, NULL, NULL))) {
  91. BIO_free(privkeybio);
  92. X509_free(cert);
  93. cert = NULL;
  94. return 0;
  95. }
  96. BIO_free(privkeybio);
  97. ADD_TEST(test_encrypt_decrypt_aes_cbc);
  98. ADD_TEST(test_encrypt_decrypt_aes_128_gcm);
  99. ADD_TEST(test_encrypt_decrypt_aes_192_gcm);
  100. ADD_TEST(test_encrypt_decrypt_aes_256_gcm);
  101. return 1;
  102. }
  103. void cleanup_tests(void)
  104. {
  105. X509_free(cert);
  106. EVP_PKEY_free(privkey);
  107. }