2
0

pemtest.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2017-2018 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 int test_b64(int idx)
  26. {
  27. BIO *b = BIO_new(BIO_s_mem());
  28. char *name = NULL, *header = NULL;
  29. unsigned char *data = NULL;
  30. long len;
  31. int ret = 0;
  32. const char *raw = b64_pem_data[idx].raw;
  33. const char *encoded = b64_pem_data[idx].encoded;
  34. if (!TEST_ptr(b)
  35. || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
  36. || !TEST_true(BIO_printf(b, "%s\n", encoded))
  37. || !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype))
  38. || !TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len,
  39. PEM_FLAG_ONLY_B64)))
  40. goto err;
  41. if (!TEST_int_eq(memcmp(pemtype, name, strlen(pemtype)), 0)
  42. || !TEST_int_eq(len, strlen(raw))
  43. || !TEST_int_eq(memcmp(data, raw, strlen(raw)), 0))
  44. goto err;
  45. ret = 1;
  46. err:
  47. BIO_free(b);
  48. OPENSSL_free(name);
  49. OPENSSL_free(header);
  50. OPENSSL_free(data);
  51. return ret;
  52. }
  53. static int test_invalid(void)
  54. {
  55. BIO *b = BIO_new(BIO_s_mem());
  56. char *name = NULL, *header = NULL;
  57. unsigned char *data = NULL;
  58. long len;
  59. const char *encoded = b64_pem_data[0].encoded;
  60. if (!TEST_ptr(b)
  61. || !TEST_true(BIO_printf(b, "-----BEGIN %s-----\n", pemtype))
  62. || !TEST_true(BIO_printf(b, "%c%s\n", '\t', encoded))
  63. || !TEST_true(BIO_printf(b, "-----END %s-----\n", pemtype))
  64. /* Expected to fail due to non-base64 character */
  65. || TEST_true(PEM_read_bio_ex(b, &name, &header, &data, &len,
  66. PEM_FLAG_ONLY_B64))) {
  67. BIO_free(b);
  68. return 0;
  69. }
  70. BIO_free(b);
  71. OPENSSL_free(name);
  72. OPENSSL_free(header);
  73. OPENSSL_free(data);
  74. return 1;
  75. }
  76. int setup_tests(void)
  77. {
  78. ADD_ALL_TESTS(test_b64, OSSL_NELEM(b64_pem_data));
  79. ADD_TEST(test_invalid);
  80. return 1;
  81. }