aboutsummaryrefslogtreecommitdiff
path: root/src/test/test_pem.c
blob: 9772be124b3bf413bc2eaec0f3c014c527ec927b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* Copyright (c) 2001-2004, Roger Dingledine.
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
 * Copyright (c) 2007-2020, The Tor Project, Inc. */
/* See LICENSE for licensing information */

#include "orconfig.h"

#include "lib/encoding/pem.h"
#include "lib/cc/compat_compiler.h"
#include "lib/malloc/malloc.h"

#include "test/test.h"

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

static const char example_pre[] =
    "Lest you get the wrong impression, we wombats "
    "are not in the habit of tunneling madly about, without any supplies "
    "or even a map."; /* -- Ursula Vernon, _Digger_ */
static const char expected[] =
    "-----BEGIN WOMBAT QUOTE-----\n"
    "TGVzdCB5b3UgZ2V0IHRoZSB3cm9uZyBpbXByZXNzaW9uLCB3ZSB3b21iYXRzIGFy\n"
    "ZSBub3QgaW4gdGhlIGhhYml0IG9mIHR1bm5lbGluZyBtYWRseSBhYm91dCwgd2l0\n"
    "aG91dCBhbnkgc3VwcGxpZXMgb3IgZXZlbiBhIG1hcC4=\n"
    "-----END WOMBAT QUOTE-----\n";

static void
test_crypto_pem_encode(void *arg)
{
  (void)arg;

  char buf[4096];

  int n = (int) pem_encoded_size(strlen(example_pre), "WOMBAT QUOTE");

  int n2 = pem_encode(buf, sizeof(buf),
                      (const unsigned char *)example_pre, strlen(example_pre),
                      "WOMBAT QUOTE");
  tt_int_op(strlen(buf)+1, OP_EQ, n);
  tt_int_op(n2, OP_EQ, 0);
  tt_str_op(buf, OP_EQ, expected);

  /* Now make sure it succeeds if the buffer is exactly the length we want. */
  memset(buf, 0, sizeof(buf));
  n2 = pem_encode(buf, n, (const unsigned char *)example_pre,
                      strlen(example_pre), "WOMBAT QUOTE");
  tt_int_op(n2, OP_EQ, 0);
  tt_str_op(buf, OP_EQ, expected);

  /* Make sure it fails if the buffer is too short. */
  memset(buf, 0, sizeof(buf));
  n2 = pem_encode(buf, n - 1, (const unsigned char *)example_pre,
                  strlen(example_pre), "WOMBAT QUOTE");
  tt_int_op(n2, OP_EQ, -1);

 done:
  ;
}

static void
test_crypto_pem_decode(void *arg)
{
  (void)arg;

  unsigned char buf[4096];

  /* Try a straightforward decoding. */
  int n = pem_decode(buf, sizeof(buf),
                     expected, strlen(expected),
                     "WOMBAT QUOTE");
  tt_int_op(n, OP_EQ, strlen(example_pre));
  tt_mem_op(buf, OP_EQ, example_pre, n);

  /* Succeed if the buffer is exactly the right size. */
  memset(buf, 0xff, sizeof(buf));
  n = pem_decode(buf, strlen(example_pre),
                 expected, strlen(expected),
                 "WOMBAT QUOTE");
  tt_int_op(n, OP_EQ, strlen(example_pre));
  tt_mem_op(buf, OP_EQ, example_pre, n);
  tt_int_op(buf[n], OP_EQ, 0xff);

  /* Verify that it fails if the buffer is too small. */
  memset(buf, 0xff, sizeof(buf));
  n = pem_decode(buf, strlen(example_pre) - 1,
                 expected, strlen(expected),
                 "WOMBAT QUOTE");
  tt_int_op(n, OP_EQ, -1);

  /* Verify that it fails with an incorrect tag. */
  memset(buf, 0xff, sizeof(buf));
  n = pem_decode(buf, sizeof(buf),
                 expected, strlen(expected),
                 "QUOKKA VOTE");
  tt_int_op(n, OP_EQ, -1);

  /* Try truncated buffers of different sizes. */
  size_t i;
  for (i = 0; i <= strlen(expected); ++i) {
    char *truncated = tor_memdup(expected, i);
    n = pem_decode(buf, sizeof(buf),
                   truncated, i,
                   "WOMBAT QUOTE");
    tor_free(truncated);
    if (i < strlen(expected) - 1) {
      tt_int_op(n, OP_EQ, -1);
    } else {
      tt_int_op(n, OP_EQ, strlen(example_pre));
    }
  }

 done:
  ;
}

static void
test_crypto_pem_decode_crlf(void *arg)
{
  (void)arg;
  char crlf_version[4096];
  uint8_t buf[4096];

  /* Convert 'expected' to a version with CRLF instead of LF. */
  const char *inp = expected;
  char *outp = crlf_version;
  while (*inp) {
    if (*inp == '\n') {
      *outp++ = '\r';
    }
    *outp++ = *inp++;
  }
  *outp = 0;

  /* Decoding should succeed (or else we have bug 33032 again) */
  int n = pem_decode(buf, sizeof(buf),
                     crlf_version, strlen(crlf_version),
                     "WOMBAT QUOTE");
  tt_int_op(n, OP_EQ, strlen(example_pre));
  tt_mem_op(buf, OP_EQ, example_pre, n);

 done:
  ;
}

struct testcase_t pem_tests[] = {
  { "encode", test_crypto_pem_encode, 0, NULL, NULL },
  { "decode", test_crypto_pem_decode, 0, NULL, NULL },
  { "decode_crlf", test_crypto_pem_decode_crlf, 0, NULL, NULL },
  END_OF_TESTCASES
};