Fuzzing module for various string operations, currently focusing on
[tor.git] / src / test / test_introduce.c
blob4d2d909945b8d0b4b42636a246a0a22b8d2de05b
1 /* Copyright (c) 2012-2018, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
4 #include "orconfig.h"
5 #include "lib/crypt_ops/crypto_cipher.h"
6 #include "core/or/or.h"
7 #include "test/test.h"
9 #define RENDSERVICE_PRIVATE
10 #include "feature/rend/rendservice.h"
12 static uint8_t v0_test_plaintext[] =
13 /* 20 bytes of rendezvous point nickname */
14 { 0x4e, 0x69, 0x63, 0x6b, 0x6e, 0x61, 0x6d, 0x65,
15 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
16 0x00, 0x00, 0x00, 0x00,
17 /* 20 bytes dummy rendezvous cookie */
18 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
19 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
20 0x10, 0x11, 0x12, 0x13,
21 /* 128 bytes dummy DH handshake data */
22 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
23 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
24 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
25 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
26 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
27 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
28 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
29 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
30 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
31 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
32 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
33 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
34 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
35 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
36 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
37 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
39 static uint8_t v1_test_plaintext[] =
40 /* Version byte */
41 { 0x01,
42 /* 42 bytes of dummy rendezvous point hex digest */
43 0x24, 0x30, 0x30, 0x30, 0x31, 0x30, 0x32, 0x30,
44 0x33, 0x30, 0x34, 0x30, 0x35, 0x30, 0x36, 0x30,
45 0x37, 0x30, 0x38, 0x30, 0x39, 0x30, 0x41, 0x30,
46 0x42, 0x30, 0x43, 0x30, 0x44, 0x30, 0x45, 0x30,
47 0x46, 0x31, 0x30, 0x31, 0x31, 0x31, 0x32, 0x31,
48 0x33, 0x00,
49 /* 20 bytes dummy rendezvous cookie */
50 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
51 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
52 0x10, 0x11, 0x12, 0x13,
53 /* 128 bytes dummy DH handshake data */
54 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
55 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
56 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
57 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
58 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
59 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
60 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
61 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
62 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
63 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
64 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
65 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
66 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
67 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
68 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
69 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
71 static uint8_t v2_test_plaintext[] =
72 /* Version byte */
73 { 0x02,
74 /* 4 bytes rendezvous point's IP address */
75 0xc0, 0xa8, 0x00, 0x01,
76 /* 2 bytes rendezvous point's OR port */
77 0x23, 0x5a,
78 /* 20 bytes dummy rendezvous point's identity digest */
79 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
80 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
81 0x10, 0x11, 0x12, 0x13,
82 /* 2 bytes length of onion key */
83 0x00, 0x8c,
84 /* Onion key (140 bytes taken from live test) */
85 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xb1,
86 0xcd, 0x46, 0xa9, 0x18, 0xd2, 0x0f, 0x01, 0xf8,
87 0xb2, 0xad, 0xa4, 0x79, 0xb4, 0xbb, 0x4b, 0xf4,
88 0x54, 0x1e, 0x3f, 0x03, 0x54, 0xcf, 0x7c, 0xb6,
89 0xb5, 0xf0, 0xfe, 0xed, 0x4b, 0x7d, 0xd7, 0x61,
90 0xdb, 0x6d, 0xd9, 0x19, 0xe2, 0x72, 0x04, 0xaa,
91 0x3e, 0x89, 0x26, 0x14, 0x62, 0x9a, 0x6c, 0x11,
92 0x0b, 0x35, 0x99, 0x2c, 0x9f, 0x2c, 0x64, 0xa1,
93 0xd9, 0xe2, 0x88, 0xce, 0xf6, 0x54, 0xfe, 0x1d,
94 0x37, 0x5e, 0x6d, 0x73, 0x95, 0x54, 0x90, 0xf0,
95 0x7b, 0xfa, 0xd4, 0x44, 0xac, 0xb2, 0x23, 0x9f,
96 0x75, 0x36, 0xe2, 0x78, 0x62, 0x82, 0x80, 0xa4,
97 0x23, 0x22, 0xc9, 0xbf, 0xc4, 0x36, 0xd1, 0x31,
98 0x33, 0x8e, 0x64, 0xb4, 0xa9, 0x74, 0xa1, 0xcb,
99 0x42, 0x8d, 0x60, 0xc7, 0xbb, 0x8e, 0x6e, 0x0f,
100 0x36, 0x74, 0x8e, 0xf4, 0x08, 0x99, 0x06, 0x92,
101 0xb1, 0x3f, 0xb3, 0xdd, 0xed, 0xf7, 0xc9, 0x02,
102 0x03, 0x01, 0x00, 0x01,
103 /* 20 bytes dummy rendezvous cookie */
104 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
105 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
106 0x10, 0x11, 0x12, 0x13,
107 /* 128 bytes dummy DH handshake data */
108 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
109 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
110 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
111 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
112 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
113 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
114 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
115 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
116 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
117 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
118 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
119 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
120 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
121 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
122 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
123 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
125 static uint8_t v3_no_auth_test_plaintext[] =
126 /* Version byte */
127 { 0x03,
128 /* Auth type (0 for no auth len/auth data) */
129 0x00,
130 /* Timestamp */
131 0x50, 0x0b, 0xb5, 0xaa,
132 /* 4 bytes rendezvous point's IP address */
133 0xc0, 0xa8, 0x00, 0x01,
134 /* 2 bytes rendezvous point's OR port */
135 0x23, 0x5a,
136 /* 20 bytes dummy rendezvous point's identity digest */
137 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
138 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
139 0x10, 0x11, 0x12, 0x13,
140 /* 2 bytes length of onion key */
141 0x00, 0x8c,
142 /* Onion key (140 bytes taken from live test) */
143 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xb1,
144 0xcd, 0x46, 0xa9, 0x18, 0xd2, 0x0f, 0x01, 0xf8,
145 0xb2, 0xad, 0xa4, 0x79, 0xb4, 0xbb, 0x4b, 0xf4,
146 0x54, 0x1e, 0x3f, 0x03, 0x54, 0xcf, 0x7c, 0xb6,
147 0xb5, 0xf0, 0xfe, 0xed, 0x4b, 0x7d, 0xd7, 0x61,
148 0xdb, 0x6d, 0xd9, 0x19, 0xe2, 0x72, 0x04, 0xaa,
149 0x3e, 0x89, 0x26, 0x14, 0x62, 0x9a, 0x6c, 0x11,
150 0x0b, 0x35, 0x99, 0x2c, 0x9f, 0x2c, 0x64, 0xa1,
151 0xd9, 0xe2, 0x88, 0xce, 0xf6, 0x54, 0xfe, 0x1d,
152 0x37, 0x5e, 0x6d, 0x73, 0x95, 0x54, 0x90, 0xf0,
153 0x7b, 0xfa, 0xd4, 0x44, 0xac, 0xb2, 0x23, 0x9f,
154 0x75, 0x36, 0xe2, 0x78, 0x62, 0x82, 0x80, 0xa4,
155 0x23, 0x22, 0xc9, 0xbf, 0xc4, 0x36, 0xd1, 0x31,
156 0x33, 0x8e, 0x64, 0xb4, 0xa9, 0x74, 0xa1, 0xcb,
157 0x42, 0x8d, 0x60, 0xc7, 0xbb, 0x8e, 0x6e, 0x0f,
158 0x36, 0x74, 0x8e, 0xf4, 0x08, 0x99, 0x06, 0x92,
159 0xb1, 0x3f, 0xb3, 0xdd, 0xed, 0xf7, 0xc9, 0x02,
160 0x03, 0x01, 0x00, 0x01,
161 /* 20 bytes dummy rendezvous cookie */
162 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
163 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
164 0x10, 0x11, 0x12, 0x13,
165 /* 128 bytes dummy DH handshake data */
166 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
167 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
168 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
169 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
170 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
171 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
172 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
173 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
174 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
175 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
176 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
177 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
178 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
179 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
180 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
181 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
183 static uint8_t v3_basic_auth_test_plaintext[] =
184 /* Version byte */
185 { 0x03,
186 /* Auth type (1 for REND_BASIC_AUTH) */
187 0x01,
188 /* Auth len (must be 16 bytes for REND_BASIC_AUTH) */
189 0x00, 0x10,
190 /* Auth data (a 16-byte dummy descriptor cookie) */
191 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
192 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
193 /* Timestamp */
194 0x50, 0x0b, 0xb5, 0xaa,
195 /* 4 bytes rendezvous point's IP address */
196 0xc0, 0xa8, 0x00, 0x01,
197 /* 2 bytes rendezvous point's OR port */
198 0x23, 0x5a,
199 /* 20 bytes dummy rendezvous point's identity digest */
200 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
201 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
202 0x10, 0x11, 0x12, 0x13,
203 /* 2 bytes length of onion key */
204 0x00, 0x8c,
205 /* Onion key (140 bytes taken from live test) */
206 0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xb1,
207 0xcd, 0x46, 0xa9, 0x18, 0xd2, 0x0f, 0x01, 0xf8,
208 0xb2, 0xad, 0xa4, 0x79, 0xb4, 0xbb, 0x4b, 0xf4,
209 0x54, 0x1e, 0x3f, 0x03, 0x54, 0xcf, 0x7c, 0xb6,
210 0xb5, 0xf0, 0xfe, 0xed, 0x4b, 0x7d, 0xd7, 0x61,
211 0xdb, 0x6d, 0xd9, 0x19, 0xe2, 0x72, 0x04, 0xaa,
212 0x3e, 0x89, 0x26, 0x14, 0x62, 0x9a, 0x6c, 0x11,
213 0x0b, 0x35, 0x99, 0x2c, 0x9f, 0x2c, 0x64, 0xa1,
214 0xd9, 0xe2, 0x88, 0xce, 0xf6, 0x54, 0xfe, 0x1d,
215 0x37, 0x5e, 0x6d, 0x73, 0x95, 0x54, 0x90, 0xf0,
216 0x7b, 0xfa, 0xd4, 0x44, 0xac, 0xb2, 0x23, 0x9f,
217 0x75, 0x36, 0xe2, 0x78, 0x62, 0x82, 0x80, 0xa4,
218 0x23, 0x22, 0xc9, 0xbf, 0xc4, 0x36, 0xd1, 0x31,
219 0x33, 0x8e, 0x64, 0xb4, 0xa9, 0x74, 0xa1, 0xcb,
220 0x42, 0x8d, 0x60, 0xc7, 0xbb, 0x8e, 0x6e, 0x0f,
221 0x36, 0x74, 0x8e, 0xf4, 0x08, 0x99, 0x06, 0x92,
222 0xb1, 0x3f, 0xb3, 0xdd, 0xed, 0xf7, 0xc9, 0x02,
223 0x03, 0x01, 0x00, 0x01,
224 /* 20 bytes dummy rendezvous cookie */
225 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
226 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
227 0x10, 0x11, 0x12, 0x13,
228 /* 128 bytes dummy DH handshake data */
229 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
230 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
231 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
232 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
233 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
234 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
235 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
236 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
237 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
238 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
239 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
240 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00,
241 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
242 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
243 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08,
244 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
246 static void do_decrypt_test(uint8_t *plaintext, size_t plaintext_len);
247 static void do_early_parse_test(uint8_t *plaintext, size_t plaintext_len);
248 static void do_late_parse_test(uint8_t *plaintext, size_t plaintext_len);
249 static void do_parse_test(uint8_t *plaintext, size_t plaintext_len, int phase);
250 static ssize_t make_intro_from_plaintext(
251 void *buf, size_t len, crypto_pk_t *key, void **cell_out);
253 #define EARLY_PARSE_ONLY 1
254 #define DECRYPT_ONLY 2
255 #define ALL_PARSING 3
257 static void
258 do_early_parse_test(uint8_t *plaintext, size_t plaintext_len)
260 do_parse_test(plaintext, plaintext_len, EARLY_PARSE_ONLY);
263 static void
264 do_decrypt_test(uint8_t *plaintext, size_t plaintext_len)
266 do_parse_test(plaintext, plaintext_len, DECRYPT_ONLY);
269 static void
270 do_late_parse_test(uint8_t *plaintext, size_t plaintext_len)
272 do_parse_test(plaintext, plaintext_len, ALL_PARSING);
275 /** Test utility function: checks that the <b>plaintext_len</b>-byte string at
276 * <b>plaintext</b> is at least superficially parseable.
278 static void
279 do_parse_test(uint8_t *plaintext, size_t plaintext_len, int phase)
281 crypto_pk_t *k = NULL;
282 ssize_t r;
283 uint8_t *cell = NULL;
284 size_t cell_len;
285 rend_intro_cell_t *parsed_req = NULL;
286 char *err_msg = NULL;
287 char digest[DIGEST_LEN];
289 /* Get a key */
290 k = crypto_pk_new();
291 tt_assert(k);
292 r = crypto_pk_read_private_key_from_string(k, AUTHORITY_SIGNKEY_1, -1);
293 tt_assert(!r);
295 /* Get digest for future comparison */
296 r = crypto_pk_get_digest(k, digest);
297 tt_assert(r >= 0);
299 /* Make a cell out of it */
300 r = make_intro_from_plaintext(
301 plaintext, plaintext_len,
302 k, (void **)(&cell));
303 tt_assert(r > 0);
304 tt_assert(cell);
305 cell_len = r;
307 /* Do early parsing */
308 parsed_req = rend_service_begin_parse_intro(cell, cell_len, 2, &err_msg);
309 tt_assert(parsed_req);
310 tt_ptr_op(err_msg, OP_EQ, NULL);
311 tt_mem_op(parsed_req->pk,OP_EQ, digest, DIGEST_LEN);
312 tt_assert(parsed_req->ciphertext);
313 tt_assert(parsed_req->ciphertext_len > 0);
315 if (phase == EARLY_PARSE_ONLY)
316 goto done;
318 /* Do decryption */
319 r = rend_service_decrypt_intro(parsed_req, k, &err_msg);
320 tt_assert(!r);
321 tt_ptr_op(err_msg, OP_EQ, NULL);
322 tt_assert(parsed_req->plaintext);
323 tt_assert(parsed_req->plaintext_len > 0);
325 if (phase == DECRYPT_ONLY)
326 goto done;
328 /* Do late parsing */
329 r = rend_service_parse_intro_plaintext(parsed_req, &err_msg);
330 tt_assert(!r);
331 tt_ptr_op(err_msg, OP_EQ, NULL);
332 tt_assert(parsed_req->parsed);
334 done:
335 tor_free(cell);
336 crypto_pk_free(k);
337 rend_service_free_intro(parsed_req);
338 tor_free(err_msg);
341 /** Given the plaintext of the encrypted part of an INTRODUCE1/2 and a key,
342 * construct the encrypted cell for testing.
345 static ssize_t
346 make_intro_from_plaintext(
347 void *buf, size_t len, crypto_pk_t *key, void **cell_out)
349 char *cell = NULL;
350 ssize_t cell_len = -1, r;
351 /* Assemble key digest and ciphertext, then construct the cell */
352 ssize_t ciphertext_size;
354 if (!(buf && key && len > 0 && cell_out)) goto done;
357 * Figure out an upper bound on how big the ciphertext will be
358 * (see crypto_pk_obsolete_public_hybrid_encrypt())
360 ciphertext_size = PKCS1_OAEP_PADDING_OVERHEAD;
361 ciphertext_size += crypto_pk_keysize(key);
362 ciphertext_size += CIPHER_KEY_LEN;
363 ciphertext_size += len;
366 * Allocate space for the cell
368 cell = tor_malloc(DIGEST_LEN + ciphertext_size);
370 /* Compute key digest (will be first DIGEST_LEN octets of cell) */
371 r = crypto_pk_get_digest(key, cell);
372 tt_assert(r >= 0);
374 /* Do encryption */
375 r = crypto_pk_obsolete_public_hybrid_encrypt(
376 key, cell + DIGEST_LEN, ciphertext_size,
377 buf, len,
378 PK_PKCS1_OAEP_PADDING, 0);
379 tt_assert(r >= 0);
381 /* Figure out cell length */
382 cell_len = DIGEST_LEN + r;
384 /* Output the cell */
385 *cell_out = cell;
387 done:
388 return cell_len;
391 /** Test v0 INTRODUCE2 parsing through decryption only
394 static void
395 test_introduce_decrypt_v0(void *arg)
397 (void)arg;
398 do_decrypt_test(v0_test_plaintext, sizeof(v0_test_plaintext));
401 /** Test v1 INTRODUCE2 parsing through decryption only
404 static void
405 test_introduce_decrypt_v1(void *arg)
407 (void)arg;
408 do_decrypt_test(v1_test_plaintext, sizeof(v1_test_plaintext));
411 /** Test v2 INTRODUCE2 parsing through decryption only
414 static void
415 test_introduce_decrypt_v2(void *arg)
417 (void)arg;
418 do_decrypt_test(v2_test_plaintext, sizeof(v2_test_plaintext));
421 /** Test v3 INTRODUCE2 parsing through decryption only
424 static void
425 test_introduce_decrypt_v3(void *arg)
427 (void)arg;
428 do_decrypt_test(
429 v3_no_auth_test_plaintext, sizeof(v3_no_auth_test_plaintext));
430 do_decrypt_test(
431 v3_basic_auth_test_plaintext, sizeof(v3_basic_auth_test_plaintext));
434 /** Test v0 INTRODUCE2 parsing through early parsing only
437 static void
438 test_introduce_early_parse_v0(void *arg)
440 (void)arg;
441 do_early_parse_test(v0_test_plaintext, sizeof(v0_test_plaintext));
444 /** Test v1 INTRODUCE2 parsing through early parsing only
447 static void
448 test_introduce_early_parse_v1(void *arg)
450 (void)arg;
451 do_early_parse_test(v1_test_plaintext, sizeof(v1_test_plaintext));
454 /** Test v2 INTRODUCE2 parsing through early parsing only
457 static void
458 test_introduce_early_parse_v2(void *arg)
460 (void)arg;
461 do_early_parse_test(v2_test_plaintext, sizeof(v2_test_plaintext));
464 /** Test v3 INTRODUCE2 parsing through early parsing only
467 static void
468 test_introduce_early_parse_v3(void *arg)
470 (void)arg;
471 do_early_parse_test(
472 v3_no_auth_test_plaintext, sizeof(v3_no_auth_test_plaintext));
473 do_early_parse_test(
474 v3_basic_auth_test_plaintext, sizeof(v3_basic_auth_test_plaintext));
477 /** Test v0 INTRODUCE2 parsing
480 static void
481 test_introduce_late_parse_v0(void *arg)
483 (void)arg;
484 do_late_parse_test(v0_test_plaintext, sizeof(v0_test_plaintext));
487 /** Test v1 INTRODUCE2 parsing
490 static void
491 test_introduce_late_parse_v1(void *arg)
493 (void)arg;
494 do_late_parse_test(v1_test_plaintext, sizeof(v1_test_plaintext));
497 /** Test v2 INTRODUCE2 parsing
500 static void
501 test_introduce_late_parse_v2(void *arg)
503 (void)arg;
504 do_late_parse_test(v2_test_plaintext, sizeof(v2_test_plaintext));
507 /** Test v3 INTRODUCE2 parsing
510 static void
511 test_introduce_late_parse_v3(void *arg)
513 (void)arg;
514 do_late_parse_test(
515 v3_no_auth_test_plaintext, sizeof(v3_no_auth_test_plaintext));
516 do_late_parse_test(
517 v3_basic_auth_test_plaintext, sizeof(v3_basic_auth_test_plaintext));
520 #define INTRODUCE_LEGACY(name) \
521 { #name, test_introduce_ ## name , 0, NULL, NULL }
523 struct testcase_t introduce_tests[] = {
524 INTRODUCE_LEGACY(early_parse_v0),
525 INTRODUCE_LEGACY(early_parse_v1),
526 INTRODUCE_LEGACY(early_parse_v2),
527 INTRODUCE_LEGACY(early_parse_v3),
528 INTRODUCE_LEGACY(decrypt_v0),
529 INTRODUCE_LEGACY(decrypt_v1),
530 INTRODUCE_LEGACY(decrypt_v2),
531 INTRODUCE_LEGACY(decrypt_v3),
532 INTRODUCE_LEGACY(late_parse_v0),
533 INTRODUCE_LEGACY(late_parse_v1),
534 INTRODUCE_LEGACY(late_parse_v2),
535 INTRODUCE_LEGACY(late_parse_v3),
536 END_OF_TESTCASES