test_pem.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* Copyright (c) 2001-2004, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. #include "orconfig.h"
  6. #include "lib/encoding/pem.h"
  7. #include "lib/cc/compat_compiler.h"
  8. #include "lib/malloc/malloc.h"
  9. #include "test/test.h"
  10. #include <string.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. static const char example_pre[] =
  14. "Lest you get the wrong impression, we wombats "
  15. "are not in the habit of tunneling madly about, without any supplies "
  16. "or even a map."; /* -- Ursula Vernon, _Digger_ */
  17. static const char expected[] =
  18. "-----BEGIN WOMBAT QUOTE-----\n"
  19. "TGVzdCB5b3UgZ2V0IHRoZSB3cm9uZyBpbXByZXNzaW9uLCB3ZSB3b21iYXRzIGFy\n"
  20. "ZSBub3QgaW4gdGhlIGhhYml0IG9mIHR1bm5lbGluZyBtYWRseSBhYm91dCwgd2l0\n"
  21. "aG91dCBhbnkgc3VwcGxpZXMgb3IgZXZlbiBhIG1hcC4=\n"
  22. "-----END WOMBAT QUOTE-----\n";
  23. static void
  24. test_crypto_pem_encode(void *arg)
  25. {
  26. (void)arg;
  27. char buf[4096];
  28. int n = (int) pem_encoded_size(strlen(example_pre), "WOMBAT QUOTE");
  29. int n2 = pem_encode(buf, sizeof(buf),
  30. (const unsigned char *)example_pre, strlen(example_pre),
  31. "WOMBAT QUOTE");
  32. tt_int_op(strlen(buf)+1, OP_EQ, n);
  33. tt_int_op(n2, OP_EQ, 0);
  34. tt_str_op(buf, OP_EQ, expected);
  35. /* Now make sure it succeeds if the buffer is exactly the length we want. */
  36. memset(buf, 0, sizeof(buf));
  37. n2 = pem_encode(buf, n, (const unsigned char *)example_pre,
  38. strlen(example_pre), "WOMBAT QUOTE");
  39. tt_int_op(n2, OP_EQ, 0);
  40. tt_str_op(buf, OP_EQ, expected);
  41. /* Make sure it fails if the buffer is too short. */
  42. memset(buf, 0, sizeof(buf));
  43. n2 = pem_encode(buf, n - 1, (const unsigned char *)example_pre,
  44. strlen(example_pre), "WOMBAT QUOTE");
  45. tt_int_op(n2, OP_EQ, -1);
  46. done:
  47. ;
  48. }
  49. static void
  50. test_crypto_pem_decode(void *arg)
  51. {
  52. (void)arg;
  53. unsigned char buf[4096];
  54. /* Try a straightforward decoding. */
  55. int n = pem_decode(buf, sizeof(buf),
  56. expected, strlen(expected),
  57. "WOMBAT QUOTE");
  58. tt_int_op(n, OP_EQ, strlen(example_pre));
  59. tt_mem_op(buf, OP_EQ, example_pre, n);
  60. /* Succeed if the buffer is exactly the right size. */
  61. memset(buf, 0xff, sizeof(buf));
  62. n = pem_decode(buf, strlen(example_pre),
  63. expected, strlen(expected),
  64. "WOMBAT QUOTE");
  65. tt_int_op(n, OP_EQ, strlen(example_pre));
  66. tt_mem_op(buf, OP_EQ, example_pre, n);
  67. tt_int_op(buf[n], OP_EQ, 0xff);
  68. /* Verify that it fails if the buffer is too small. */
  69. memset(buf, 0xff, sizeof(buf));
  70. n = pem_decode(buf, strlen(example_pre) - 1,
  71. expected, strlen(expected),
  72. "WOMBAT QUOTE");
  73. tt_int_op(n, OP_EQ, -1);
  74. /* Verify that it fails with an incorrect tag. */
  75. memset(buf, 0xff, sizeof(buf));
  76. n = pem_decode(buf, sizeof(buf),
  77. expected, strlen(expected),
  78. "QUOKKA VOTE");
  79. tt_int_op(n, OP_EQ, -1);
  80. /* Try truncated buffers of different sizes. */
  81. size_t i;
  82. for (i = 0; i <= strlen(expected); ++i) {
  83. char *truncated = tor_memdup(expected, i);
  84. n = pem_decode(buf, sizeof(buf),
  85. truncated, i,
  86. "WOMBAT QUOTE");
  87. tor_free(truncated);
  88. if (i < strlen(expected) - 1) {
  89. tt_int_op(n, OP_EQ, -1);
  90. } else {
  91. tt_int_op(n, OP_EQ, strlen(example_pre));
  92. }
  93. }
  94. done:
  95. ;
  96. }
  97. struct testcase_t pem_tests[] = {
  98. { "encode", test_crypto_pem_encode, 0, NULL, NULL },
  99. { "decode", test_crypto_pem_decode, 0, NULL, NULL },
  100. END_OF_TESTCASES
  101. };