Check that SCT timestamps are not in the future
[oweals/openssl.git] / test / ct_test.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <ctype.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include <openssl/ct.h>
16 #include <openssl/err.h>
17 #include <openssl/pem.h>
18 #include <openssl/x509.h>
19 #include <openssl/x509v3.h>
20 #include "testutil.h"
21 #include "test_main_custom.h"
22
23 #ifndef OPENSSL_NO_CT
24 /* Used when declaring buffers to read text files into */
25 #define CT_TEST_MAX_FILE_SIZE 8096
26
27 static char *certs_dir = NULL;
28 static char *ct_dir = NULL;
29
30 typedef struct ct_test_fixture {
31     const char *test_case_name;
32     /* The current time in milliseconds */
33     uint64_t epoch_time_in_ms;
34     /* The CT log store to use during tests */
35     CTLOG_STORE* ctlog_store;
36     /* Set the following to test handling of SCTs in X509 certificates */
37     const char *certs_dir;
38     char *certificate_file;
39     char *issuer_file;
40     /* Expected number of SCTs */
41     int expected_sct_count;
42     /* Expected number of valid SCTS */
43     int expected_valid_sct_count;
44     /* Set the following to test handling of SCTs in TLS format */
45     const unsigned char *tls_sct_list;
46     size_t tls_sct_list_len;
47     STACK_OF(SCT) *sct_list;
48     /*
49      * A file to load the expected SCT text from.
50      * This text will be compared to the actual text output during the test.
51      * A maximum of |CT_TEST_MAX_FILE_SIZE| bytes will be read of this file.
52      */
53     const char *sct_dir;
54     const char *sct_text_file;
55     /* Whether to test the validity of the SCT(s) */
56     int test_validity;
57 } CT_TEST_FIXTURE;
58
59 static CT_TEST_FIXTURE set_up(const char *const test_case_name)
60 {
61     CT_TEST_FIXTURE fixture;
62     int setup_ok = 1;
63     CTLOG_STORE *ctlog_store;
64
65     memset(&fixture, 0, sizeof(fixture));
66
67     ctlog_store = CTLOG_STORE_new();
68
69     if (ctlog_store == NULL) {
70         setup_ok = 0;
71         fprintf(stderr, "Failed to create a new CT log store\n");
72         goto end;
73     }
74
75     if (CTLOG_STORE_load_default_file(ctlog_store) != 1) {
76         setup_ok = 0;
77         fprintf(stderr, "Failed to load CT log list\n");
78         goto end;
79     }
80
81     fixture.test_case_name = test_case_name;
82     fixture.epoch_time_in_ms = 1473269626000; /* Sep 7 17:33:46 2016 GMT */
83     fixture.ctlog_store = ctlog_store;
84
85 end:
86     if (!setup_ok) {
87         exit(EXIT_FAILURE);
88     }
89     return fixture;
90 }
91
92 static void tear_down(CT_TEST_FIXTURE fixture)
93 {
94     CTLOG_STORE_free(fixture.ctlog_store);
95     SCT_LIST_free(fixture.sct_list);
96 }
97
98 static char *mk_file_path(const char *dir, const char *file)
99 {
100     char *full_file = NULL;
101     size_t full_file_l = 0;
102     const char *sep = "";
103 #ifndef OPENSSL_SYS_VMS
104     sep = "/";
105 #endif
106
107     full_file_l = strlen(dir) + strlen(sep) + strlen(file) + 1;
108     full_file = OPENSSL_zalloc(full_file_l);
109     if (full_file != NULL) {
110         OPENSSL_strlcpy(full_file, dir, full_file_l);
111         OPENSSL_strlcat(full_file, sep, full_file_l);
112         OPENSSL_strlcat(full_file, file, full_file_l);
113     }
114
115     return full_file;
116 }
117
118 static X509 *load_pem_cert(const char *dir, const char *file)
119 {
120     X509 *cert = NULL;
121     char *file_path = mk_file_path(dir, file);
122
123     if (file_path != NULL) {
124         BIO *cert_io = BIO_new_file(file_path, "r");
125         OPENSSL_free(file_path);
126
127         if (cert_io != NULL)
128             cert = PEM_read_bio_X509(cert_io, NULL, NULL, NULL);
129
130         BIO_free(cert_io);
131     }
132     return cert;
133 }
134
135 static int read_text_file(const char *dir, const char *file,
136                           char *buffer, int buffer_length)
137 {
138     int result = -1;
139     char *file_path = mk_file_path(dir, file);
140
141     if (file_path != NULL) {
142         BIO *file_io = BIO_new_file(file_path, "r");
143         OPENSSL_free(file_path);
144
145         if (file_io != NULL) {
146             result = BIO_read(file_io, buffer, buffer_length);
147             BIO_free(file_io);
148         }
149     }
150
151     return result;
152 }
153
154 static int compare_sct_list_printout(STACK_OF(SCT) *sct,
155     const char *expected_output)
156 {
157     BIO *text_buffer = NULL;
158     char *actual_output = NULL;
159     int result = 1;
160
161     text_buffer = BIO_new(BIO_s_mem());
162     if (text_buffer == NULL) {
163         fprintf(stderr, "Unable to allocate buffer\n");
164         goto end;
165     }
166
167     SCT_LIST_print(sct, text_buffer, 0, "\n", NULL);
168
169     /* Append null terminator because we're about to use the buffer contents
170     * as a string. */
171     if (BIO_write(text_buffer, "\0", 1) != 1) {
172         fprintf(stderr, "Failed to append null terminator to SCT text\n");
173         goto end;
174     }
175
176     BIO_get_mem_data(text_buffer, &actual_output);
177     result = strcmp(actual_output, expected_output);
178
179     if (result != 0) {
180         fprintf(stderr,
181             "Expected SCT printout:\n%s\nActual SCT printout:\n%s\n",
182             expected_output, actual_output);
183     }
184
185 end:
186     BIO_free(text_buffer);
187     return result;
188 }
189
190 static int compare_extension_printout(X509_EXTENSION *extension,
191                                       const char *expected_output)
192 {
193     BIO *text_buffer = NULL;
194     char *actual_output = NULL;
195     int result = 1;
196
197     text_buffer = BIO_new(BIO_s_mem());
198     if (text_buffer == NULL) {
199         fprintf(stderr, "Unable to allocate buffer\n");
200         goto end;
201     }
202
203     if (!X509V3_EXT_print(text_buffer, extension, X509V3_EXT_DEFAULT, 0)) {
204         fprintf(stderr, "Failed to print extension\n");
205         goto end;
206     }
207
208     /* Append null terminator because we're about to use the buffer contents
209      * as a string. */
210     if (BIO_write(text_buffer, "\0", 1) != 1) {
211         fprintf(stderr,
212                 "Failed to append null terminator to extension text\n");
213         goto end;
214     }
215
216     BIO_get_mem_data(text_buffer, &actual_output);
217     result = strcmp(actual_output, expected_output);
218
219     if (result != 0) {
220         fprintf(stderr,
221                 "Expected SCT printout:\n%s\nActual SCT printout:\n%s\n",
222                 expected_output, actual_output);
223     }
224
225 end:
226     BIO_free(text_buffer);
227     return result;
228 }
229
230 static int assert_validity(CT_TEST_FIXTURE fixture,
231                            STACK_OF(SCT) *scts,
232                            CT_POLICY_EVAL_CTX *policy_ctx) {
233     int invalid_sct_count = 0;
234     int valid_sct_count = 0;
235     int i;
236
237     if (SCT_LIST_validate(scts, policy_ctx) < 0) {
238         fprintf(stderr, "Error verifying SCTs\n");
239         return 0;
240     }
241
242     for (i = 0; i < sk_SCT_num(scts); ++i) {
243         SCT *sct_i = sk_SCT_value(scts, i);
244         switch (SCT_get_validation_status(sct_i)) {
245         case SCT_VALIDATION_STATUS_VALID:
246             ++valid_sct_count;
247             break;
248         case SCT_VALIDATION_STATUS_INVALID:
249             ++invalid_sct_count;
250             break;
251         case SCT_VALIDATION_STATUS_NOT_SET:
252         case SCT_VALIDATION_STATUS_UNKNOWN_LOG:
253         case SCT_VALIDATION_STATUS_UNVERIFIED:
254         case SCT_VALIDATION_STATUS_UNKNOWN_VERSION:
255             /* Ignore other validation statuses. */
256             break;
257         }
258     }
259
260     if (valid_sct_count != fixture.expected_valid_sct_count) {
261         int unverified_sct_count = sk_SCT_num(scts) -
262                 invalid_sct_count - valid_sct_count;
263
264         fprintf(stderr,
265                 "%d SCTs failed verification\n"
266                 "%d SCTs passed verification (%d expected)\n"
267                 "%d SCTs were unverified\n",
268                 invalid_sct_count,
269                 valid_sct_count,
270                 fixture.expected_valid_sct_count,
271                 unverified_sct_count);
272         return 0;
273     }
274
275     return 1;
276 }
277
278 static int execute_cert_test(CT_TEST_FIXTURE fixture)
279 {
280     int success = 0;
281     X509 *cert = NULL, *issuer = NULL;
282     STACK_OF(SCT) *scts = NULL;
283     SCT *sct = NULL;
284     char expected_sct_text[CT_TEST_MAX_FILE_SIZE];
285     int sct_text_len = 0;
286     unsigned char *tls_sct_list = NULL;
287     size_t tls_sct_list_len = 0;
288     CT_POLICY_EVAL_CTX *ct_policy_ctx = CT_POLICY_EVAL_CTX_new();
289
290     if (fixture.sct_text_file != NULL) {
291         sct_text_len = read_text_file(fixture.sct_dir, fixture.sct_text_file,
292                                       expected_sct_text,
293                                       CT_TEST_MAX_FILE_SIZE - 1);
294
295         if (sct_text_len < 0) {
296             fprintf(stderr, "Test data file not found: %s\n",
297                 fixture.sct_text_file);
298             goto end;
299         }
300
301         expected_sct_text[sct_text_len] = '\0';
302     }
303
304     CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(
305             ct_policy_ctx, fixture.ctlog_store);
306
307     CT_POLICY_EVAL_CTX_set_time(ct_policy_ctx, fixture.epoch_time_in_ms);
308
309     if (fixture.certificate_file != NULL) {
310         int sct_extension_index;
311         X509_EXTENSION *sct_extension = NULL;
312         cert = load_pem_cert(fixture.certs_dir, fixture.certificate_file);
313
314         if (cert == NULL) {
315             fprintf(stderr, "Unable to load certificate: %s\n",
316                 fixture.certificate_file);
317             goto end;
318         }
319
320         CT_POLICY_EVAL_CTX_set1_cert(ct_policy_ctx, cert);
321
322         if (fixture.issuer_file != NULL) {
323             issuer = load_pem_cert(fixture.certs_dir, fixture.issuer_file);
324
325             if (issuer == NULL) {
326                 fprintf(stderr, "Unable to load issuer certificate: %s\n",
327                         fixture.issuer_file);
328                 goto end;
329             }
330
331             CT_POLICY_EVAL_CTX_set1_issuer(ct_policy_ctx, issuer);
332         }
333
334         sct_extension_index =
335                 X509_get_ext_by_NID(cert, NID_ct_precert_scts, -1);
336         sct_extension = X509_get_ext(cert, sct_extension_index);
337         if (fixture.expected_sct_count > 0) {
338             if (sct_extension == NULL) {
339                 fprintf(stderr, "SCT extension not found in: %s\n",
340                     fixture.certificate_file);
341                 goto end;
342             }
343
344             if (fixture.sct_text_file
345                 && compare_extension_printout(sct_extension,
346                                               expected_sct_text)) {
347                     goto end;
348             }
349
350             if (fixture.test_validity) {
351                 int i;
352
353                 scts = X509V3_EXT_d2i(sct_extension);
354                 for (i = 0; i < sk_SCT_num(scts); ++i) {
355                     SCT *sct_i = sk_SCT_value(scts, i);
356
357                     if (!SCT_set_source(sct_i, SCT_SOURCE_X509V3_EXTENSION)) {
358                         fprintf(stderr,
359                                 "Error setting SCT source to X509v3 extension\n");
360                         goto end;
361                     }
362                 }
363
364                 if (!assert_validity(fixture, scts, ct_policy_ctx))
365                     goto end;
366             }
367         } else if (sct_extension != NULL) {
368             fprintf(stderr,
369                     "Expected no SCTs, but found SCT extension in: %s\n",
370                     fixture.certificate_file);
371             goto end;
372         }
373     }
374
375     if (fixture.tls_sct_list != NULL) {
376         const unsigned char *p = fixture.tls_sct_list;
377         if (o2i_SCT_LIST(&scts, &p, fixture.tls_sct_list_len) == NULL) {
378             fprintf(stderr, "Failed to decode SCTs from TLS format\n");
379             goto end;
380         }
381
382         if (fixture.test_validity && cert != NULL) {
383             if (!assert_validity(fixture, scts, ct_policy_ctx))
384                 goto end;
385         }
386
387         if (fixture.sct_text_file
388             && compare_sct_list_printout(scts, expected_sct_text)) {
389                 goto end;
390         }
391
392         tls_sct_list_len = i2o_SCT_LIST(scts, &tls_sct_list);
393         if (tls_sct_list_len != fixture.tls_sct_list_len ||
394             memcmp(fixture.tls_sct_list, tls_sct_list, tls_sct_list_len) != 0) {
395             fprintf(stderr,
396                     "Failed to encode SCTs into TLS format correctly\n");
397             goto end;
398         }
399     }
400     success = 1;
401
402 end:
403     X509_free(cert);
404     X509_free(issuer);
405     SCT_LIST_free(scts);
406     SCT_free(sct);
407     CT_POLICY_EVAL_CTX_free(ct_policy_ctx);
408     OPENSSL_free(tls_sct_list);
409     return success;
410 }
411
412 #define SETUP_CT_TEST_FIXTURE() SETUP_TEST_FIXTURE(CT_TEST_FIXTURE, set_up)
413 #define EXECUTE_CT_TEST() EXECUTE_TEST(execute_cert_test, tear_down)
414
415 static int test_no_scts_in_certificate()
416 {
417     SETUP_CT_TEST_FIXTURE();
418     fixture.certs_dir = certs_dir;
419     fixture.certificate_file = "leaf.pem";
420     fixture.issuer_file = "subinterCA.pem";
421     fixture.expected_sct_count = 0;
422     EXECUTE_CT_TEST();
423 }
424
425 static int test_one_sct_in_certificate()
426 {
427     SETUP_CT_TEST_FIXTURE();
428     fixture.certs_dir = certs_dir;
429     fixture.certificate_file = "embeddedSCTs1.pem";
430     fixture.issuer_file = "embeddedSCTs1_issuer.pem";
431     fixture.expected_sct_count = 1;
432     fixture.sct_dir = certs_dir;
433     fixture.sct_text_file = "embeddedSCTs1.sct";
434     EXECUTE_CT_TEST();
435 }
436
437 static int test_multiple_scts_in_certificate()
438 {
439     SETUP_CT_TEST_FIXTURE();
440     fixture.certs_dir = certs_dir;
441     fixture.certificate_file = "embeddedSCTs3.pem";
442     fixture.issuer_file = "embeddedSCTs3_issuer.pem";
443     fixture.expected_sct_count = 3;
444     fixture.sct_dir = certs_dir;
445     fixture.sct_text_file = "embeddedSCTs3.sct";
446     EXECUTE_CT_TEST();
447 }
448
449 static int test_verify_one_sct()
450 {
451     SETUP_CT_TEST_FIXTURE();
452     fixture.certs_dir = certs_dir;
453     fixture.certificate_file = "embeddedSCTs1.pem";
454     fixture.issuer_file = "embeddedSCTs1_issuer.pem";
455     fixture.expected_sct_count = fixture.expected_valid_sct_count = 1;
456     fixture.test_validity = 1;
457     EXECUTE_CT_TEST();
458 }
459
460 static int test_verify_multiple_scts()
461 {
462     SETUP_CT_TEST_FIXTURE();
463     fixture.certs_dir = certs_dir;
464     fixture.certificate_file = "embeddedSCTs3.pem";
465     fixture.issuer_file = "embeddedSCTs3_issuer.pem";
466     fixture.expected_sct_count = fixture.expected_valid_sct_count = 3;
467     fixture.test_validity = 1;
468     EXECUTE_CT_TEST();
469 }
470
471 static int test_verify_fails_for_future_sct()
472 {
473     SETUP_CT_TEST_FIXTURE();
474     fixture.epoch_time_in_ms = 1365094800000; /* Apr 4 17:00:00 2013 GMT */
475     fixture.certs_dir = certs_dir;
476     fixture.certificate_file = "embeddedSCTs1.pem";
477     fixture.issuer_file = "embeddedSCTs1_issuer.pem";
478     fixture.expected_sct_count = 1;
479     fixture.expected_valid_sct_count = 0;
480     fixture.test_validity = 1;
481     EXECUTE_CT_TEST();
482 }
483
484 static int test_decode_tls_sct()
485 {
486     const unsigned char tls_sct_list[] = "\x00\x78" /* length of list */
487         "\x00\x76"
488         "\x00" /* version */
489         /* log ID */
490         "\xDF\x1C\x2E\xC1\x15\x00\x94\x52\x47\xA9\x61\x68\x32\x5D\xDC\x5C\x79"
491         "\x59\xE8\xF7\xC6\xD3\x88\xFC\x00\x2E\x0B\xBD\x3F\x74\xD7\x64"
492         "\x00\x00\x01\x3D\xDB\x27\xDF\x93" /* timestamp */
493         "\x00\x00" /* extensions length */
494         "" /* extensions */
495         "\x04\x03" /* hash and signature algorithms */
496         "\x00\x47" /* signature length */
497         /* signature */
498         "\x30\x45\x02\x20\x48\x2F\x67\x51\xAF\x35\xDB\xA6\x54\x36\xBE\x1F\xD6"
499         "\x64\x0F\x3D\xBF\x9A\x41\x42\x94\x95\x92\x45\x30\x28\x8F\xA3\xE5\xE2"
500         "\x3E\x06\x02\x21\x00\xE4\xED\xC0\xDB\x3A\xC5\x72\xB1\xE2\xF5\xE8\xAB"
501         "\x6A\x68\x06\x53\x98\x7D\xCF\x41\x02\x7D\xFE\xFF\xA1\x05\x51\x9D\x89"
502         "\xED\xBF\x08";
503
504     SETUP_CT_TEST_FIXTURE();
505     fixture.tls_sct_list = tls_sct_list;
506     fixture.tls_sct_list_len = 0x7a;
507     fixture.sct_dir = ct_dir;
508     fixture.sct_text_file = "tls1.sct";
509     EXECUTE_CT_TEST();
510 }
511
512 static int test_encode_tls_sct()
513 {
514     const unsigned char log_id[] = "\xDF\x1C\x2E\xC1\x15\x00\x94\x52\x47\xA9"
515             "\x61\x68\x32\x5D\xDC\x5C\x79\x59\xE8\xF7\xC6\xD3\x88\xFC\x00\x2E"
516             "\x0B\xBD\x3F\x74\xD7\x64";
517
518     const unsigned char signature[] = "\x45\x02\x20\x48\x2F\x67\x51\xAF\x35"
519             "\xDB\xA6\x54\x36\xBE\x1F\xD6\x64\x0F\x3D\xBF\x9A\x41\x42\x94\x95"
520             "\x92\x45\x30\x28\x8F\xA3\xE5\xE2\x3E\x06\x02\x21\x00\xE4\xED\xC0"
521             "\xDB\x3A\xC5\x72\xB1\xE2\xF5\xE8\xAB\x6A\x68\x06\x53\x98\x7D\xCF"
522             "\x41\x02\x7D\xFE\xFF\xA1\x05\x51\x9D\x89\xED\xBF\x08";
523
524     SETUP_CT_TEST_FIXTURE();
525
526     STACK_OF(SCT) *sct_list = sk_SCT_new_null();
527     SCT *sct = SCT_new();
528     if (!SCT_set_version(sct, SCT_VERSION_V1)) {
529         fprintf(stderr, "Failed to set SCT version\n");
530         return 0;
531     }
532     if (!SCT_set1_log_id(sct, log_id, 32)) {
533         fprintf(stderr, "Failed to set SCT log ID\n");
534         return 0;
535     }
536     SCT_set_timestamp(sct, 1);
537     if (!SCT_set_signature_nid(sct, NID_ecdsa_with_SHA256)) {
538         fprintf(stderr, "Failed to set SCT signature NID\n");
539         return 0;
540     }
541     if (!SCT_set1_signature(sct, signature, 71)) {
542         fprintf(stderr, "Failed to set SCT signature\n");
543         return 0;
544     }
545     sk_SCT_push(sct_list, sct);
546
547     fixture.sct_list = sct_list;
548     fixture.sct_dir = ct_dir;
549     fixture.sct_text_file = "tls1.sct";
550     EXECUTE_CT_TEST();
551 }
552
553 int test_main(int argc, char *argv[])
554 {
555     int result = 0;
556     char *tmp_env;
557
558     tmp_env = getenv("CT_DIR");
559     ct_dir = OPENSSL_strdup(tmp_env != NULL ? tmp_env : "ct");
560     tmp_env = getenv("CERTS_DIR");
561     certs_dir = OPENSSL_strdup(tmp_env != NULL ? tmp_env : "certs");
562
563     ADD_TEST(test_no_scts_in_certificate);
564     ADD_TEST(test_one_sct_in_certificate);
565     ADD_TEST(test_multiple_scts_in_certificate);
566     ADD_TEST(test_verify_one_sct);
567     ADD_TEST(test_verify_multiple_scts);
568     ADD_TEST(test_verify_fails_for_future_sct);
569     ADD_TEST(test_decode_tls_sct);
570     ADD_TEST(test_encode_tls_sct);
571
572     result = run_tests(argv[0]);
573
574     OPENSSL_free(ct_dir);
575     OPENSSL_free(certs_dir);
576
577     return result;
578 }
579 #else
580 int test_main(int argc, char *argv[])
581 {
582     printf("No CT support\n");
583     return 0;
584 }
585 #endif