forked from cisco/libacvp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acvp_transport.c
443 lines (383 loc) · 13.3 KB
/
acvp_transport.c
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/*****************************************************************************
* Copyright (c) 2016, Cisco Systems, Inc.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#ifdef USE_MURL
# include <murl/murl.h>
#else
# include <curl/curl.h>
#endif
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "acvp.h"
#include "acvp_lcl.h"
#define MAX_TOKEN_LEN 512
static struct curl_slist* acvp_add_auth_hdr (ACVP_CTX *ctx, struct curl_slist *slist)
{
int bearer_size;
char *bearer;
/*
* Create the Authorzation header if needed
*/
if (ctx->jwt_token) {
bearer_size = strnlen(ctx->jwt_token, MAX_TOKEN_LEN) + 23;
bearer = calloc(1, bearer_size);
if (!bearer) {
acvp_log_msg(ctx, "ERROR: unable to allocate memory.");
return slist;
}
snprintf(bearer, bearer_size + 1, "Authorization: Bearer %s", ctx->jwt_token);
slist = curl_slist_append(slist, bearer);
free(bearer);
}
return slist;
}
/*
* This function uses libcurl to send a simple HTTP GET
* request with no Content-Type header.
* TLS peer verification is enabled, but not HTTP authentication.
* The parameters are:
*
* ctx: Ptr to ACVP_CTX, which contains the server name
* url: URL to use for the GET request
* writefunc: Function pointer to handle writing the data
* from the HTTP body received from the server.
*
* Return value is the HTTP status value from the server
* (e.g. 200 for HTTP OK)
*/
static long acvp_curl_http_get (ACVP_CTX *ctx, char *url, void *writefunc)
{
long http_code = 0;
CURL *hnd;
struct curl_slist *slist;
slist = NULL;
/*
* Create the Authorzation header if needed
*/
slist = acvp_add_auth_hdr(ctx, slist);
ctx->read_ctr = 0;
/*
* Setup Curl
*/
hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_URL, url);
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.27.0");
if (slist) {
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist);
}
if (ctx->verify_peer && ctx->cacerts_file) {
curl_easy_setopt(hnd, CURLOPT_CAINFO, ctx->cacerts_file);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 1L);
} else {
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
acvp_log_msg(ctx, "WARNING: TLS peer verification has not been enabled.");
}
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
if (ctx->tls_cert && ctx->tls_key) {
curl_easy_setopt(hnd, CURLOPT_SSLCERTTYPE, "PEM");
curl_easy_setopt(hnd, CURLOPT_SSLCERT, ctx->tls_cert);
curl_easy_setopt(hnd, CURLOPT_SSLKEYTYPE, "PEM");
curl_easy_setopt(hnd, CURLOPT_SSLKEY, ctx->tls_key);
}
/*
* If the caller wants the HTTP data from the server
* set the callback function
*/
if (writefunc) {
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, ctx);
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, writefunc);
}
/*
* Send the HTTP GET request
*/
curl_easy_perform(hnd);
/*
* Get the HTTP reponse status code from the server
*/
curl_easy_getinfo (hnd, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code != 200) {
acvp_log_msg(ctx, "HTTP response: %d\n", (int)http_code);
}
curl_easy_cleanup(hnd);
hnd = NULL;
if (slist) {
curl_slist_free_all(slist);
slist = NULL;
}
return (http_code);
}
/*
* This function uses libcurl to send a simple HTTP POST
* request with no Content-Type header.
* TLS peer verification is enabled, but not HTTP authentication.
* The parameters are:
*
* ctx: Ptr to ACVP_CTX, which contains the server name
* url: URL to use for the GET request
* data: data to POST to the server
* writefunc: Function pointer to handle writing the data
* from the HTTP body received from the server.
*
* Return value is the HTTP status value from the server
* (e.g. 200 for HTTP OK)
*/
static long acvp_curl_http_post (ACVP_CTX *ctx, char *url, char *data, void *writefunc)
{
long http_code = 0;
CURL *hnd;
CURLcode crv;
struct curl_slist *slist;
/*
* Set the Content-Type header in the HTTP request
*/
slist = NULL;
slist = curl_slist_append(slist, "Content-Type:application/octet-stream");
//FIXME: v0.2 spec says to use application/json
//slist = curl_slist_append(slist, "Content-Type:application/json");
/*
* Create the Authorzation header if needed
*/
slist = acvp_add_auth_hdr(ctx, slist);
ctx->read_ctr = 0;
/*
* Setup Curl
*/
hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_URL, url);
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "libacvp");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist);
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_POST, 1L);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)strlen(data));
//FIXME: we should always to TLS peer auth
if (ctx->verify_peer && ctx->cacerts_file) {
curl_easy_setopt(hnd, CURLOPT_CAINFO, ctx->cacerts_file);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 1L);
} else {
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYPEER, 0L);
acvp_log_msg(ctx, "WARNING: TLS peer verification has not been enabled.");
}
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
if (ctx->tls_cert && ctx->tls_key) {
curl_easy_setopt(hnd, CURLOPT_SSLCERTTYPE, "PEM");
curl_easy_setopt(hnd, CURLOPT_SSLCERT, ctx->tls_cert);
curl_easy_setopt(hnd, CURLOPT_SSLKEYTYPE, "PEM");
curl_easy_setopt(hnd, CURLOPT_SSLKEY, ctx->tls_key);
}
/*
* If the caller wants the HTTP data from the server
* set the callback function
*/
if (writefunc) {
curl_easy_setopt(hnd, CURLOPT_WRITEDATA, ctx);
curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, writefunc);
}
/*
* Send the HTTP POST request
*/
crv = curl_easy_perform(hnd);
if (crv != CURLE_OK) {
acvp_log_msg(ctx, "Curl failed with code %d (%s)\n", crv, curl_easy_strerror(crv));
}
/*
* Get the HTTP reponse status code from the server
*/
curl_easy_getinfo (hnd, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code != 200) {
acvp_log_msg(ctx, "HTTP response: %d\n", (int)http_code);
}
curl_easy_cleanup(hnd);
hnd = NULL;
curl_slist_free_all(slist);
slist = NULL;
return (http_code);
}
/*
* This is a callback used by curl to send the HTTP body
* to the application (us). We will store the HTTP body
* on the ACVP_CTX in one of the transitory fields.
*/
static size_t acvp_curl_write_upld_func(void *ptr, size_t size, size_t nmemb, void *userdata)
{
ACVP_CTX *ctx = (ACVP_CTX*)userdata;
char *http_buf;
if (size != 1) {
fprintf(stderr, "\ncurl size not 1\n");
return 0;
}
if (!ctx->upld_buf) {
ctx->upld_buf = calloc(1, ACVP_KAT_BUF_MAX);
if (!ctx->upld_buf) {
fprintf(stderr, "\nmalloc failed in curl write upld func\n");
return 0;
}
}
http_buf = ctx->upld_buf;
if ((ctx->read_ctr + nmemb) > ACVP_KAT_BUF_MAX) {
fprintf(stderr, "\nKAT is too large\n");
return 0;
}
memcpy(&http_buf[ctx->read_ctr], ptr, nmemb);
http_buf[ctx->read_ctr+nmemb] = 0;
ctx->read_ctr += nmemb;
return nmemb;
}
/*
* This is a callback used by curl to send the HTTP body
* to the application (us). We will store the HTTP body
* on the ACVP_CTX in one of the transitory fields.
*/
static size_t acvp_curl_write_kat_func(void *ptr, size_t size, size_t nmemb, void *userdata)
{
ACVP_CTX *ctx = (ACVP_CTX*)userdata;
char *json_buf;
if (size != 1) {
fprintf(stderr, "\ncurl size not 1\n");
return 0;
}
if (!ctx->kat_buf) {
ctx->kat_buf = calloc(1, ACVP_KAT_BUF_MAX);
if (!ctx->kat_buf) {
fprintf(stderr, "\nmalloc failed in curl write kat func\n");
return 0;
}
}
json_buf = ctx->kat_buf;
if ((ctx->read_ctr + nmemb) > ACVP_KAT_BUF_MAX) {
fprintf(stderr, "\nKAT is too large\n");
return 0;
}
memcpy(&json_buf[ctx->read_ctr], ptr, nmemb);
json_buf[ctx->read_ctr+nmemb] = 0;
ctx->read_ctr += nmemb;
return nmemb;
}
/*
* This is a callback used by curl to send the HTTP body
* to the application (us). We will store the HTTP body
* on the ACVP_CTX in one of the transitory fields.
*/
static size_t acvp_curl_write_register_func(void *ptr, size_t size, size_t nmemb, void *userdata)
{
ACVP_CTX *ctx = (ACVP_CTX*)userdata;
char *json_buf;
if (size != 1) {
fprintf(stderr, "\ncurl size not 1\n");
return 0;
}
if (!ctx->reg_buf) {
ctx->reg_buf = calloc(1, ACVP_REG_BUF_MAX);
if (!ctx->reg_buf) {
fprintf(stderr, "\nmalloc failed in curl write reg func\n");
return 0;
}
}
json_buf = ctx->reg_buf;
if ((ctx->read_ctr + nmemb) > ACVP_REG_BUF_MAX) {
fprintf(stderr, "\nRegister response is too large\n");
return 0;
}
memcpy(&json_buf[ctx->read_ctr], ptr, nmemb);
json_buf[ctx->read_ctr+nmemb] = 0;
ctx->read_ctr += nmemb;
return nmemb;
}
/*
* This is the transport function used within libacvp to register
* the DUT with the ACVP server.
*
* The reg parameter is the JSON encoded registration message that
* will be sent to the server.
*/
ACVP_RESULT acvp_send_register(ACVP_CTX *ctx, char *reg)
{
ACVP_RESULT rv;
char url[512]; //TODO: 512 is an arbitrary limit
memset(url, 0x0, 512);
snprintf(url, 511, "https://%s:%d/%svalidation/acvp/register", ctx->server_name, ctx->server_port, ctx->path_segment);
rv = acvp_curl_http_post(ctx, url, reg, &acvp_curl_write_register_func);
if (rv != 200) {
acvp_log_msg(ctx, "Unable to register with ACVP server. curl rv=%d\n", rv);
acvp_log_msg(ctx, "%s\n", ctx->reg_buf);
return ACVP_TRANSPORT_FAIL;
}
/*
* Update user with status
*/
acvp_log_msg(ctx,"Successfully received registration response from ACVP server");
return ACVP_SUCCESS;
}
/*
* This is the top level function used within libacvp to retrieve
* a KAT vector set from the ACVP server.
*/
ACVP_RESULT acvp_retrieve_vector_set(ACVP_CTX *ctx, int vs_id)
{
ACVP_RESULT rv;
char url[512]; //TODO: 512 is an arbitrary limit
memset(url, 0x0, 512);
snprintf(url, 511, "https://%s:%d/%svalidation/acvp/vectors?vs_id=%d", ctx->server_name, ctx->server_port, ctx->path_segment, vs_id);
if (ctx->kat_buf) {
memset(ctx->kat_buf, 0x0, ACVP_KAT_BUF_MAX);
}
rv = acvp_curl_http_get(ctx, url, &acvp_curl_write_kat_func);
if (rv != 200) {
acvp_log_msg(ctx, "Unable to get vectors from server. curl rv=%d\n", rv);
acvp_log_msg(ctx, "%s\n", ctx->kat_buf);
return ACVP_TRANSPORT_FAIL;
}
/*
* Update user with status
*/
acvp_log_msg(ctx,"Successfully retrieved KAT vector set");
return ACVP_SUCCESS;
}
/*
* This function is used to submit a vector set response
* to the ACV server.
*/
ACVP_RESULT acvp_submit_vector_responses(ACVP_CTX *ctx)
{
ACVP_RESULT rv;
char url[512]; //TODO: 512 is an arbitrary limit
char *resp;
memset(url, 0x0, 512);
snprintf(url, 511, "https://%s:%d/%svalidation/acvp/vectors?vs_id=%d", ctx->server_name, ctx->server_port, ctx->path_segment, ctx->vs_id);
resp = json_serialize_to_string_pretty(ctx->kat_resp);
rv = acvp_curl_http_post(ctx, url, resp, &acvp_curl_write_upld_func);
json_value_free(ctx->kat_resp);
ctx->kat_resp = NULL;
json_free_serialized_string(resp);
if (rv != 200) {
acvp_log_msg(ctx, "Unable to upload vector set to ACVP server. curl rv=%d\n", rv);
acvp_log_msg(ctx, "%s\n", ctx->upld_buf);
return ACVP_TRANSPORT_FAIL;
}
acvp_log_msg(ctx, "Successfully submitted KAT vector responses");
return ACVP_SUCCESS;
}