1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2020, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
8 #include "lib/encoding/pem.h"
9 #include "lib/cc/compat_compiler.h"
10 #include "lib/malloc/malloc.h"
12 #include "test/test.h"
18 static const char example_pre
[] =
19 "Lest you get the wrong impression, we wombats "
20 "are not in the habit of tunneling madly about, without any supplies "
21 "or even a map."; /* -- Ursula Vernon, _Digger_ */
22 static const char expected
[] =
23 "-----BEGIN WOMBAT QUOTE-----\n"
24 "TGVzdCB5b3UgZ2V0IHRoZSB3cm9uZyBpbXByZXNzaW9uLCB3ZSB3b21iYXRzIGFy\n"
25 "ZSBub3QgaW4gdGhlIGhhYml0IG9mIHR1bm5lbGluZyBtYWRseSBhYm91dCwgd2l0\n"
26 "aG91dCBhbnkgc3VwcGxpZXMgb3IgZXZlbiBhIG1hcC4=\n"
27 "-----END WOMBAT QUOTE-----\n";
30 test_crypto_pem_encode(void *arg
)
36 int n
= (int) pem_encoded_size(strlen(example_pre
), "WOMBAT QUOTE");
38 int n2
= pem_encode(buf
, sizeof(buf
),
39 (const unsigned char *)example_pre
, strlen(example_pre
),
41 tt_int_op(strlen(buf
)+1, OP_EQ
, n
);
42 tt_int_op(n2
, OP_EQ
, 0);
43 tt_str_op(buf
, OP_EQ
, expected
);
45 /* Now make sure it succeeds if the buffer is exactly the length we want. */
46 memset(buf
, 0, sizeof(buf
));
47 n2
= pem_encode(buf
, n
, (const unsigned char *)example_pre
,
48 strlen(example_pre
), "WOMBAT QUOTE");
49 tt_int_op(n2
, OP_EQ
, 0);
50 tt_str_op(buf
, OP_EQ
, expected
);
52 /* Make sure it fails if the buffer is too short. */
53 memset(buf
, 0, sizeof(buf
));
54 n2
= pem_encode(buf
, n
- 1, (const unsigned char *)example_pre
,
55 strlen(example_pre
), "WOMBAT QUOTE");
56 tt_int_op(n2
, OP_EQ
, -1);
63 test_crypto_pem_decode(void *arg
)
67 unsigned char buf
[4096];
69 /* Try a straightforward decoding. */
70 int n
= pem_decode(buf
, sizeof(buf
),
71 expected
, strlen(expected
),
73 tt_int_op(n
, OP_EQ
, strlen(example_pre
));
74 tt_mem_op(buf
, OP_EQ
, example_pre
, n
);
76 /* Succeed if the buffer is exactly the right size. */
77 memset(buf
, 0xff, sizeof(buf
));
78 n
= pem_decode(buf
, strlen(example_pre
),
79 expected
, strlen(expected
),
81 tt_int_op(n
, OP_EQ
, strlen(example_pre
));
82 tt_mem_op(buf
, OP_EQ
, example_pre
, n
);
83 tt_int_op(buf
[n
], OP_EQ
, 0xff);
85 /* Verify that it fails if the buffer is too small. */
86 memset(buf
, 0xff, sizeof(buf
));
87 n
= pem_decode(buf
, strlen(example_pre
) - 1,
88 expected
, strlen(expected
),
90 tt_int_op(n
, OP_EQ
, -1);
92 /* Verify that it fails with an incorrect tag. */
93 memset(buf
, 0xff, sizeof(buf
));
94 n
= pem_decode(buf
, sizeof(buf
),
95 expected
, strlen(expected
),
97 tt_int_op(n
, OP_EQ
, -1);
99 /* Try truncated buffers of different sizes. */
101 for (i
= 0; i
<= strlen(expected
); ++i
) {
102 char *truncated
= tor_memdup(expected
, i
);
103 n
= pem_decode(buf
, sizeof(buf
),
107 if (i
< strlen(expected
) - 1) {
108 tt_int_op(n
, OP_EQ
, -1);
110 tt_int_op(n
, OP_EQ
, strlen(example_pre
));
119 test_crypto_pem_decode_crlf(void *arg
)
122 char crlf_version
[4096];
125 /* Convert 'expected' to a version with CRLF instead of LF. */
126 const char *inp
= expected
;
127 char *outp
= crlf_version
;
136 /* Decoding should succeed (or else we have bug 33032 again) */
137 int n
= pem_decode(buf
, sizeof(buf
),
138 crlf_version
, strlen(crlf_version
),
140 tt_int_op(n
, OP_EQ
, strlen(example_pre
));
141 tt_mem_op(buf
, OP_EQ
, example_pre
, n
);
147 struct testcase_t pem_tests
[] = {
148 { "encode", test_crypto_pem_encode
, 0, NULL
, NULL
},
149 { "decode", test_crypto_pem_decode
, 0, NULL
, NULL
},
150 { "decode_crlf", test_crypto_pem_decode_crlf
, 0, NULL
, NULL
},