2
0

pemtest.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright 2017-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/bio.h>
  11. #include <openssl/pem.h>
  12. #include "testutil.h"
  13. #include "internal/nelem.h"
  14. typedef struct {
  15. const char *raw;
  16. const char *encoded;
  17. } TESTDATA;
  18. static TESTDATA b64_pem_data[] = {
  19. { "hello world",
  20. "aGVsbG8gd29ybGQ=" },
  21. { "a very ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong input",
  22. "YSB2ZXJ5IG9vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29vb29uZyBpbnB1dA==" }
  23. };
  24. static const char *pemtype = "PEMTESTDATA";
  25. static char *pemfile;
  26. static int test_b64(int idx)
  27. {
  28. BIO *b = BIO_new(BIO_s_mem());
  29. char *name = NULL, *header = NULL;
  30. unsigned char *data = NULL;
  31. long len;
  32. int ret = 0;
  33. const char *raw = b64_pem_data[idx].raw;
  34. const char *encoded = b64_pem_data[idx].encoded;
  35. if (!TEST_ptr(b)
  36. || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
  37. || !TEST_true(BIO_printf(b, "%s\n", encoded))
  38. || !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype))
  39. || !TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len,
  40. PEM_FLAG_ONLY_B64)))
  41. goto err;
  42. if (!TEST_int_eq(memcmp(pemtype, name, strlen(pemtype)), 0)
  43. || !TEST_int_eq(len, strlen(raw))
  44. || !TEST_int_eq(memcmp(data, raw, strlen(raw)), 0))
  45. goto err;
  46. ret = 1;
  47. err:
  48. BIO_free(b);
  49. OPENSSL_free(name);
  50. OPENSSL_free(header);
  51. OPENSSL_free(data);
  52. return ret;
  53. }
  54. static int test_invalid(void)
  55. {
  56. BIO *b = BIO_new(BIO_s_mem());
  57. char *name = NULL, *header = NULL;
  58. unsigned char *data = NULL;
  59. long len;
  60. const char *encoded = b64_pem_data[0].encoded;
  61. if (!TEST_ptr(b)
  62. || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
  63. || !TEST_true(BIO_printf(b, "%c%s\n", '\t', encoded))
  64. || !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype))
  65. /* Expected to fail due to non-base64 character */
  66. || TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len,
  67. PEM_FLAG_ONLY_B64))) {
  68. BIO_free(b);
  69. return 0;
  70. }
  71. BIO_free(b);
  72. OPENSSL_free(name);
  73. OPENSSL_free(header);
  74. OPENSSL_free(data);
  75. return 1;
  76. }
  77. static int test_cert_key_cert(void)
  78. {
  79. EVP_PKEY *key;
  80. if (!TEST_ptr(key = load_pkey_pem(pemfile, NULL)))
  81. return 0;
  82. EVP_PKEY_free(key);
  83. return 1;
  84. }
  85. static int test_empty_payload(void)
  86. {
  87. BIO *b;
  88. static char *emptypay =
  89. "-----BEGIN CERTIFICATE-----\n"
  90. "-\n" /* Base64 EOF character */
  91. "-----END CERTIFICATE-----";
  92. char *name = NULL, *header = NULL;
  93. unsigned char *data = NULL;
  94. long len;
  95. int ret = 0;
  96. b = BIO_new_mem_buf(emptypay, strlen(emptypay));
  97. if (!TEST_ptr(b))
  98. return 0;
  99. /* Expected to fail because the payload is empty */
  100. if (!TEST_false(PEM_read_bio_ex(b, &name, &header, &data, &len, 0)))
  101. goto err;
  102. ret = 1;
  103. err:
  104. OPENSSL_free(name);
  105. OPENSSL_free(header);
  106. OPENSSL_free(data);
  107. BIO_free(b);
  108. return ret;
  109. }
  110. static int test_protected_params(void)
  111. {
  112. BIO *b;
  113. static char *protectedpay =
  114. "-----BEGIN RSA PRIVATE KEY-----\n"
  115. "Proc-Type: 4,ENCRYPTED\n"
  116. "DEK-Info: AES-256-CBC,4A44448ED28992710556549B35100CEA\n"
  117. "\n"
  118. "Xw3INxKeH+rUUF57mjATpvj6zknVhedwrlRmRvnwlLv5wqIy5Ae4UVLPh7SUswfC\n"
  119. "-----END RSA PRIVATE KEY-----\n";
  120. EVP_PKEY *pkey = NULL;
  121. int ret = 0;
  122. b = BIO_new_mem_buf(protectedpay, strlen(protectedpay));
  123. if (!TEST_ptr(b))
  124. return 0;
  125. /* Expected to fail because we cannot decrypt protected PEM files */
  126. pkey = PEM_read_bio_Parameters(b, NULL);
  127. if (!TEST_ptr_null(pkey))
  128. goto err;
  129. ret = 1;
  130. err:
  131. EVP_PKEY_free(pkey);
  132. BIO_free(b);
  133. return ret;
  134. }
  135. int setup_tests(void)
  136. {
  137. if (!TEST_ptr(pemfile = test_get_argument(0)))
  138. return 0;
  139. ADD_ALL_TESTS(test_b64, OSSL_NELEM(b64_pem_data));
  140. ADD_TEST(test_invalid);
  141. ADD_TEST(test_cert_key_cert);
  142. ADD_TEST(test_empty_payload);
  143. ADD_TEST(test_protected_params);
  144. return 1;
  145. }