pemtest.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright 2017-2021 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. int setup_tests(void)
  86. {
  87. if (!TEST_ptr(pemfile = test_get_argument(0)))
  88. return 0;
  89. ADD_ALL_TESTS(test_b64, OSSL_NELEM(b64_pem_data));
  90. ADD_TEST(test_invalid);
  91. ADD_TEST(test_cert_key_cert);
  92. return 1;
  93. }