ossl_shim: add deprecation guards around the -use-ticket-callback option.
[openssl.git] / test / ossl_shim / ossl_shim.cc
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 /*
11  * HMAC low level APIs are deprecated for public use but might be used here.
12  */
13 #define OPENSSL_SUPPRESS_DEPRECATED
14
15 #if !defined(__STDC_FORMAT_MACROS)
16 #define __STDC_FORMAT_MACROS
17 #endif
18
19 #include "packeted_bio.h"
20 #include <openssl/e_os2.h>
21
22 #if !defined(OPENSSL_SYS_WINDOWS)
23 #include <arpa/inet.h>
24 #include <netinet/in.h>
25 #include <netinet/tcp.h>
26 #include <signal.h>
27 #include <sys/socket.h>
28 #include <sys/time.h>
29 #include <unistd.h>
30 #else
31 #include <io.h>
32 OPENSSL_MSVC_PRAGMA(warning(push, 3))
33 #include <winsock2.h>
34 #include <ws2tcpip.h>
35 OPENSSL_MSVC_PRAGMA(warning(pop))
36
37 OPENSSL_MSVC_PRAGMA(comment(lib, "Ws2_32.lib"))
38 #endif
39
40 #include <assert.h>
41 #include <inttypes.h>
42 #include <string.h>
43
44 #include <openssl/bio.h>
45 #include <openssl/buffer.h>
46 #include <openssl/bn.h>
47 #include <openssl/crypto.h>
48 #include <openssl/dh.h>
49 #include <openssl/err.h>
50 #include <openssl/evp.h>
51 #include <openssl/hmac.h>
52 #include <openssl/objects.h>
53 #include <openssl/rand.h>
54 #include <openssl/ssl.h>
55 #include <openssl/x509.h>
56
57 #include <memory>
58 #include <string>
59 #include <vector>
60
61 #include "async_bio.h"
62 #include "test_config.h"
63
64 namespace bssl {
65
66 #if !defined(OPENSSL_SYS_WINDOWS)
67 static int closesocket(int sock) {
68   return close(sock);
69 }
70
71 static void PrintSocketError(const char *func) {
72   perror(func);
73 }
74 #else
75 static void PrintSocketError(const char *func) {
76   fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
77 }
78 #endif
79
80 static int Usage(const char *program) {
81   fprintf(stderr, "Usage: %s [flags...]\n", program);
82   return 1;
83 }
84
85 struct TestState {
86   // async_bio is async BIO which pauses reads and writes.
87   BIO *async_bio = nullptr;
88   // packeted_bio is the packeted BIO which simulates read timeouts.
89   BIO *packeted_bio = nullptr;
90   bool cert_ready = false;
91   bool handshake_done = false;
92   // private_key is the underlying private key used when testing custom keys.
93   bssl::UniquePtr<EVP_PKEY> private_key;
94   bool got_new_session = false;
95   bssl::UniquePtr<SSL_SESSION> new_session;
96   bool ticket_decrypt_done = false;
97   bool alpn_select_done = false;
98 };
99
100 static void TestStateExFree(void *parent, void *ptr, CRYPTO_EX_DATA *ad,
101                             int index, long argl, void *argp) {
102   delete ((TestState *)ptr);
103 }
104
105 static int g_config_index = 0;
106 static int g_state_index = 0;
107
108 static bool SetTestConfig(SSL *ssl, const TestConfig *config) {
109   return SSL_set_ex_data(ssl, g_config_index, (void *)config) == 1;
110 }
111
112 static const TestConfig *GetTestConfig(const SSL *ssl) {
113   return (const TestConfig *)SSL_get_ex_data(ssl, g_config_index);
114 }
115
116 static bool SetTestState(SSL *ssl, std::unique_ptr<TestState> state) {
117   // |SSL_set_ex_data| takes ownership of |state| only on success.
118   if (SSL_set_ex_data(ssl, g_state_index, state.get()) == 1) {
119     state.release();
120     return true;
121   }
122   return false;
123 }
124
125 static TestState *GetTestState(const SSL *ssl) {
126   return (TestState *)SSL_get_ex_data(ssl, g_state_index);
127 }
128
129 static bssl::UniquePtr<X509> LoadCertificate(const std::string &file) {
130   bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file()));
131   if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
132     return nullptr;
133   }
134   return bssl::UniquePtr<X509>(PEM_read_bio_X509(bio.get(), NULL, NULL, NULL));
135 }
136
137 static bssl::UniquePtr<EVP_PKEY> LoadPrivateKey(const std::string &file) {
138   bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_file()));
139   if (!bio || !BIO_read_filename(bio.get(), file.c_str())) {
140     return nullptr;
141   }
142   return bssl::UniquePtr<EVP_PKEY>(
143       PEM_read_bio_PrivateKey(bio.get(), NULL, NULL, NULL));
144 }
145
146 template<typename T>
147 struct Free {
148   void operator()(T *buf) {
149     free(buf);
150   }
151 };
152
153 static bool GetCertificate(SSL *ssl, bssl::UniquePtr<X509> *out_x509,
154                            bssl::UniquePtr<EVP_PKEY> *out_pkey) {
155   const TestConfig *config = GetTestConfig(ssl);
156
157   if (!config->key_file.empty()) {
158     *out_pkey = LoadPrivateKey(config->key_file.c_str());
159     if (!*out_pkey) {
160       return false;
161     }
162   }
163   if (!config->cert_file.empty()) {
164     *out_x509 = LoadCertificate(config->cert_file.c_str());
165     if (!*out_x509) {
166       return false;
167     }
168   }
169   return true;
170 }
171
172 static bool InstallCertificate(SSL *ssl) {
173   bssl::UniquePtr<X509> x509;
174   bssl::UniquePtr<EVP_PKEY> pkey;
175   if (!GetCertificate(ssl, &x509, &pkey)) {
176     return false;
177   }
178
179   if (pkey && !SSL_use_PrivateKey(ssl, pkey.get())) {
180     return false;
181   }
182
183   if (x509 && !SSL_use_certificate(ssl, x509.get())) {
184     return false;
185   }
186
187   return true;
188 }
189
190 static int ClientCertCallback(SSL *ssl, X509 **out_x509, EVP_PKEY **out_pkey) {
191   if (GetTestConfig(ssl)->async && !GetTestState(ssl)->cert_ready) {
192     return -1;
193   }
194
195   bssl::UniquePtr<X509> x509;
196   bssl::UniquePtr<EVP_PKEY> pkey;
197   if (!GetCertificate(ssl, &x509, &pkey)) {
198     return -1;
199   }
200
201   // Return zero for no certificate.
202   if (!x509) {
203     return 0;
204   }
205
206   // Asynchronous private keys are not supported with client_cert_cb.
207   *out_x509 = x509.release();
208   *out_pkey = pkey.release();
209   return 1;
210 }
211
212 static int VerifySucceed(X509_STORE_CTX *store_ctx, void *arg) {
213   return 1;
214 }
215
216 static int VerifyFail(X509_STORE_CTX *store_ctx, void *arg) {
217   X509_STORE_CTX_set_error(store_ctx, X509_V_ERR_APPLICATION_VERIFICATION);
218   return 0;
219 }
220
221 static int NextProtosAdvertisedCallback(SSL *ssl, const uint8_t **out,
222                                         unsigned int *out_len, void *arg) {
223   const TestConfig *config = GetTestConfig(ssl);
224   if (config->advertise_npn.empty()) {
225     return SSL_TLSEXT_ERR_NOACK;
226   }
227
228   *out = (const uint8_t*)config->advertise_npn.data();
229   *out_len = config->advertise_npn.size();
230   return SSL_TLSEXT_ERR_OK;
231 }
232
233 static int NextProtoSelectCallback(SSL* ssl, uint8_t** out, uint8_t* outlen,
234                                    const uint8_t* in, unsigned inlen, void* arg) {
235   const TestConfig *config = GetTestConfig(ssl);
236   if (config->select_next_proto.empty()) {
237     return SSL_TLSEXT_ERR_NOACK;
238   }
239
240   *out = (uint8_t*)config->select_next_proto.data();
241   *outlen = config->select_next_proto.size();
242   return SSL_TLSEXT_ERR_OK;
243 }
244
245 static int AlpnSelectCallback(SSL* ssl, const uint8_t** out, uint8_t* outlen,
246                               const uint8_t* in, unsigned inlen, void* arg) {
247   if (GetTestState(ssl)->alpn_select_done) {
248     fprintf(stderr, "AlpnSelectCallback called after completion.\n");
249     exit(1);
250   }
251
252   GetTestState(ssl)->alpn_select_done = true;
253
254   const TestConfig *config = GetTestConfig(ssl);
255   if (config->decline_alpn) {
256     return SSL_TLSEXT_ERR_NOACK;
257   }
258
259   if (!config->expected_advertised_alpn.empty() &&
260       (config->expected_advertised_alpn.size() != inlen ||
261        memcmp(config->expected_advertised_alpn.data(),
262               in, inlen) != 0)) {
263     fprintf(stderr, "bad ALPN select callback inputs\n");
264     exit(1);
265   }
266
267   *out = (const uint8_t*)config->select_alpn.data();
268   *outlen = config->select_alpn.size();
269   return SSL_TLSEXT_ERR_OK;
270 }
271
272 static unsigned PskClientCallback(SSL *ssl, const char *hint,
273                                   char *out_identity,
274                                   unsigned max_identity_len,
275                                   uint8_t *out_psk, unsigned max_psk_len) {
276   const TestConfig *config = GetTestConfig(ssl);
277
278   if (config->psk_identity.empty()) {
279     if (hint != nullptr) {
280       fprintf(stderr, "Server PSK hint was non-null.\n");
281       return 0;
282     }
283   } else if (hint == nullptr ||
284              strcmp(hint, config->psk_identity.c_str()) != 0) {
285     fprintf(stderr, "Server PSK hint did not match.\n");
286     return 0;
287   }
288
289   // Account for the trailing '\0' for the identity.
290   if (config->psk_identity.size() >= max_identity_len ||
291       config->psk.size() > max_psk_len) {
292     fprintf(stderr, "PSK buffers too small\n");
293     return 0;
294   }
295
296   OPENSSL_strlcpy(out_identity, config->psk_identity.c_str(),
297                   max_identity_len);
298   memcpy(out_psk, config->psk.data(), config->psk.size());
299   return config->psk.size();
300 }
301
302 static unsigned PskServerCallback(SSL *ssl, const char *identity,
303                                   uint8_t *out_psk, unsigned max_psk_len) {
304   const TestConfig *config = GetTestConfig(ssl);
305
306   if (strcmp(identity, config->psk_identity.c_str()) != 0) {
307     fprintf(stderr, "Client PSK identity did not match.\n");
308     return 0;
309   }
310
311   if (config->psk.size() > max_psk_len) {
312     fprintf(stderr, "PSK buffers too small\n");
313     return 0;
314   }
315
316   memcpy(out_psk, config->psk.data(), config->psk.size());
317   return config->psk.size();
318 }
319
320 static int CertCallback(SSL *ssl, void *arg) {
321   const TestConfig *config = GetTestConfig(ssl);
322
323   // Check the CertificateRequest metadata is as expected.
324   //
325   // TODO(davidben): Test |SSL_get_client_CA_list|.
326   if (!SSL_is_server(ssl) &&
327       !config->expected_certificate_types.empty()) {
328     const uint8_t *certificate_types;
329     size_t certificate_types_len =
330         SSL_get0_certificate_types(ssl, &certificate_types);
331     if (certificate_types_len != config->expected_certificate_types.size() ||
332         memcmp(certificate_types,
333                config->expected_certificate_types.data(),
334                certificate_types_len) != 0) {
335       fprintf(stderr, "certificate types mismatch\n");
336       return 0;
337     }
338   }
339
340   // The certificate will be installed via other means.
341   if (!config->async ||
342       config->use_old_client_cert_callback) {
343     return 1;
344   }
345
346   if (!GetTestState(ssl)->cert_ready) {
347     return -1;
348   }
349   if (!InstallCertificate(ssl)) {
350     return 0;
351   }
352   return 1;
353 }
354
355 static void InfoCallback(const SSL *ssl, int type, int val) {
356   if (type == SSL_CB_HANDSHAKE_DONE) {
357     if (GetTestConfig(ssl)->handshake_never_done) {
358       fprintf(stderr, "Handshake unexpectedly completed.\n");
359       // Abort before any expected error code is printed, to ensure the overall
360       // test fails.
361       abort();
362     }
363     GetTestState(ssl)->handshake_done = true;
364
365     // Callbacks may be called again on a new handshake.
366     GetTestState(ssl)->ticket_decrypt_done = false;
367     GetTestState(ssl)->alpn_select_done = false;
368   }
369 }
370
371 static int NewSessionCallback(SSL *ssl, SSL_SESSION *session) {
372   GetTestState(ssl)->got_new_session = true;
373   GetTestState(ssl)->new_session.reset(session);
374   return 1;
375 }
376
377 #ifndef OPENSSL_NO_DEPRECATED_3_0
378 static int TicketKeyCallback(SSL *ssl, uint8_t *key_name, uint8_t *iv,
379                              EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
380                              int encrypt) {
381   if (!encrypt) {
382     if (GetTestState(ssl)->ticket_decrypt_done) {
383       fprintf(stderr, "TicketKeyCallback called after completion.\n");
384       return -1;
385     }
386
387     GetTestState(ssl)->ticket_decrypt_done = true;
388   }
389
390   // This is just test code, so use the all-zeros key.
391   static const uint8_t kZeros[16] = {0};
392
393   if (encrypt) {
394     memcpy(key_name, kZeros, sizeof(kZeros));
395     RAND_bytes(iv, 16);
396   } else if (memcmp(key_name, kZeros, 16) != 0) {
397     return 0;
398   }
399
400   if (!HMAC_Init_ex(hmac_ctx, kZeros, sizeof(kZeros), EVP_sha256(), NULL) ||
401       !EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, kZeros, iv, encrypt)) {
402     return -1;
403   }
404
405   if (!encrypt) {
406     return GetTestConfig(ssl)->renew_ticket ? 2 : 1;
407   }
408   return 1;
409 }
410 #endif
411
412 // kCustomExtensionValue is the extension value that the custom extension
413 // callbacks will add.
414 static const uint16_t kCustomExtensionValue = 1234;
415 static void *const kCustomExtensionAddArg =
416     reinterpret_cast<void *>(kCustomExtensionValue);
417 static void *const kCustomExtensionParseArg =
418     reinterpret_cast<void *>(kCustomExtensionValue + 1);
419 static const char kCustomExtensionContents[] = "custom extension";
420
421 static int CustomExtensionAddCallback(SSL *ssl, unsigned extension_value,
422                                       const uint8_t **out, size_t *out_len,
423                                       int *out_alert_value, void *add_arg) {
424   if (extension_value != kCustomExtensionValue ||
425       add_arg != kCustomExtensionAddArg) {
426     abort();
427   }
428
429   if (GetTestConfig(ssl)->custom_extension_skip) {
430     return 0;
431   }
432   if (GetTestConfig(ssl)->custom_extension_fail_add) {
433     return -1;
434   }
435
436   *out = reinterpret_cast<const uint8_t*>(kCustomExtensionContents);
437   *out_len = sizeof(kCustomExtensionContents) - 1;
438
439   return 1;
440 }
441
442 static void CustomExtensionFreeCallback(SSL *ssl, unsigned extension_value,
443                                         const uint8_t *out, void *add_arg) {
444   if (extension_value != kCustomExtensionValue ||
445       add_arg != kCustomExtensionAddArg ||
446       out != reinterpret_cast<const uint8_t *>(kCustomExtensionContents)) {
447     abort();
448   }
449 }
450
451 static int CustomExtensionParseCallback(SSL *ssl, unsigned extension_value,
452                                         const uint8_t *contents,
453                                         size_t contents_len,
454                                         int *out_alert_value, void *parse_arg) {
455   if (extension_value != kCustomExtensionValue ||
456       parse_arg != kCustomExtensionParseArg) {
457     abort();
458   }
459
460   if (contents_len != sizeof(kCustomExtensionContents) - 1 ||
461       memcmp(contents, kCustomExtensionContents, contents_len) != 0) {
462     *out_alert_value = SSL_AD_DECODE_ERROR;
463     return 0;
464   }
465
466   return 1;
467 }
468
469 static int ServerNameCallback(SSL *ssl, int *out_alert, void *arg) {
470   // SNI must be accessible from the SNI callback.
471   const TestConfig *config = GetTestConfig(ssl);
472   const char *server_name = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
473   if (server_name == nullptr ||
474       std::string(server_name) != config->expected_server_name) {
475     fprintf(stderr, "servername mismatch (got %s; want %s)\n", server_name,
476             config->expected_server_name.c_str());
477     return SSL_TLSEXT_ERR_ALERT_FATAL;
478   }
479
480   return SSL_TLSEXT_ERR_OK;
481 }
482
483 // Connect returns a new socket connected to localhost on |port| or -1 on
484 // error.
485 static int Connect(uint16_t port) {
486   int sock = socket(AF_INET, SOCK_STREAM, 0);
487   if (sock == -1) {
488     PrintSocketError("socket");
489     return -1;
490   }
491   int nodelay = 1;
492   if (setsockopt(sock, IPPROTO_TCP, TCP_NODELAY,
493           reinterpret_cast<const char*>(&nodelay), sizeof(nodelay)) != 0) {
494     PrintSocketError("setsockopt");
495     closesocket(sock);
496     return -1;
497   }
498   sockaddr_in sin;
499   memset(&sin, 0, sizeof(sin));
500   sin.sin_family = AF_INET;
501   sin.sin_port = htons(port);
502   if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
503     PrintSocketError("inet_pton");
504     closesocket(sock);
505     return -1;
506   }
507   if (connect(sock, reinterpret_cast<const sockaddr*>(&sin),
508               sizeof(sin)) != 0) {
509     PrintSocketError("connect");
510     closesocket(sock);
511     return -1;
512   }
513   return sock;
514 }
515
516 class SocketCloser {
517  public:
518   explicit SocketCloser(int sock) : sock_(sock) {}
519   ~SocketCloser() {
520     // Half-close and drain the socket before releasing it. This seems to be
521     // necessary for graceful shutdown on Windows. It will also avoid write
522     // failures in the test runner.
523 #if defined(OPENSSL_SYS_WINDOWS)
524     shutdown(sock_, SD_SEND);
525 #else
526     shutdown(sock_, SHUT_WR);
527 #endif
528     while (true) {
529       char buf[1024];
530       if (recv(sock_, buf, sizeof(buf), 0) <= 0) {
531         break;
532       }
533     }
534     closesocket(sock_);
535   }
536
537  private:
538   const int sock_;
539 };
540
541 static bssl::UniquePtr<SSL_CTX> SetupCtx(const TestConfig *config) {
542   const char sess_id_ctx[] = "ossl_shim";
543   bssl::UniquePtr<SSL_CTX> ssl_ctx(SSL_CTX_new(
544       config->is_dtls ? DTLS_method() : TLS_method()));
545   if (!ssl_ctx) {
546     return nullptr;
547   }
548
549   SSL_CTX_set_security_level(ssl_ctx.get(), 0);
550 #if 0
551   /* Disabled for now until we have some TLS1.3 support */
552   // Enable TLS 1.3 for tests.
553   if (!config->is_dtls &&
554       !SSL_CTX_set_max_proto_version(ssl_ctx.get(), TLS1_3_VERSION)) {
555     return nullptr;
556   }
557 #else
558   /* Ensure we don't negotiate TLSv1.3 until we can handle it */
559   if (!config->is_dtls &&
560       !SSL_CTX_set_max_proto_version(ssl_ctx.get(), TLS1_2_VERSION)) {
561     return nullptr;
562   }
563 #endif
564
565   std::string cipher_list = "ALL";
566   if (!config->cipher.empty()) {
567     cipher_list = config->cipher;
568     SSL_CTX_set_options(ssl_ctx.get(), SSL_OP_CIPHER_SERVER_PREFERENCE);
569   }
570   if (!SSL_CTX_set_cipher_list(ssl_ctx.get(), cipher_list.c_str())) {
571     return nullptr;
572   }
573
574   DH *tmpdh;
575
576   if (config->use_sparse_dh_prime) {
577     BIGNUM *p, *g;
578     p = BN_new();
579     g = BN_new();
580     tmpdh = DH_new();
581     if (p == NULL || g == NULL || tmpdh == NULL) {
582         BN_free(p);
583         BN_free(g);
584         DH_free(tmpdh);
585         return nullptr;
586     }
587     // This prime number is 2^1024 + 643 â€“ a value just above a power of two.
588     // Because of its form, values modulo it are essentially certain to be one
589     // byte shorter. This is used to test padding of these values.
590     if (BN_hex2bn(
591             &p,
592             "1000000000000000000000000000000000000000000000000000000000000000"
593             "0000000000000000000000000000000000000000000000000000000000000000"
594             "0000000000000000000000000000000000000000000000000000000000000000"
595             "0000000000000000000000000000000000000000000000000000000000000028"
596             "3") == 0 ||
597         !BN_set_word(g, 2)) {
598       BN_free(p);
599       BN_free(g);
600       DH_free(tmpdh);
601       return nullptr;
602     }
603     DH_set0_pqg(tmpdh, p, NULL, g);
604   } else {
605       tmpdh = DH_get_2048_256();
606   }
607
608   bssl::UniquePtr<DH> dh(tmpdh);
609
610   if (!dh || !SSL_CTX_set_tmp_dh(ssl_ctx.get(), dh.get())) {
611     return nullptr;
612   }
613
614   SSL_CTX_set_session_cache_mode(ssl_ctx.get(), SSL_SESS_CACHE_BOTH);
615
616   if (config->use_old_client_cert_callback) {
617     SSL_CTX_set_client_cert_cb(ssl_ctx.get(), ClientCertCallback);
618   }
619
620   SSL_CTX_set_npn_advertised_cb(
621       ssl_ctx.get(), NextProtosAdvertisedCallback, NULL);
622   if (!config->select_next_proto.empty()) {
623     SSL_CTX_set_next_proto_select_cb(ssl_ctx.get(), NextProtoSelectCallback,
624                                      NULL);
625   }
626
627   if (!config->select_alpn.empty() || config->decline_alpn) {
628     SSL_CTX_set_alpn_select_cb(ssl_ctx.get(), AlpnSelectCallback, NULL);
629   }
630
631   SSL_CTX_set_info_callback(ssl_ctx.get(), InfoCallback);
632   SSL_CTX_sess_set_new_cb(ssl_ctx.get(), NewSessionCallback);
633
634 #ifndef OPENSSL_NO_DEPRECATED_3_0
635   if (config->use_ticket_callback) {
636     SSL_CTX_set_tlsext_ticket_key_cb(ssl_ctx.get(), TicketKeyCallback);
637   }
638 #endif
639
640   if (config->enable_client_custom_extension &&
641       !SSL_CTX_add_client_custom_ext(
642           ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
643           CustomExtensionFreeCallback, kCustomExtensionAddArg,
644           CustomExtensionParseCallback, kCustomExtensionParseArg)) {
645     return nullptr;
646   }
647
648   if (config->enable_server_custom_extension &&
649       !SSL_CTX_add_server_custom_ext(
650           ssl_ctx.get(), kCustomExtensionValue, CustomExtensionAddCallback,
651           CustomExtensionFreeCallback, kCustomExtensionAddArg,
652           CustomExtensionParseCallback, kCustomExtensionParseArg)) {
653     return nullptr;
654   }
655
656   if (config->verify_fail) {
657     SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifyFail, NULL);
658   } else {
659     SSL_CTX_set_cert_verify_callback(ssl_ctx.get(), VerifySucceed, NULL);
660   }
661
662   if (config->use_null_client_ca_list) {
663     SSL_CTX_set_client_CA_list(ssl_ctx.get(), nullptr);
664   }
665
666   if (!SSL_CTX_set_session_id_context(ssl_ctx.get(),
667                                       (const unsigned char *)sess_id_ctx,
668                                       sizeof(sess_id_ctx) - 1))
669     return nullptr;
670
671   if (!config->expected_server_name.empty()) {
672     SSL_CTX_set_tlsext_servername_callback(ssl_ctx.get(), ServerNameCallback);
673   }
674
675   return ssl_ctx;
676 }
677
678 // RetryAsync is called after a failed operation on |ssl| with return code
679 // |ret|. If the operation should be retried, it simulates one asynchronous
680 // event and returns true. Otherwise it returns false.
681 static bool RetryAsync(SSL *ssl, int ret) {
682   // No error; don't retry.
683   if (ret >= 0) {
684     return false;
685   }
686
687   TestState *test_state = GetTestState(ssl);
688   assert(GetTestConfig(ssl)->async);
689
690   if (test_state->packeted_bio != nullptr &&
691       PacketedBioAdvanceClock(test_state->packeted_bio)) {
692     // The DTLS retransmit logic silently ignores write failures. So the test
693     // may progress, allow writes through synchronously.
694     AsyncBioEnforceWriteQuota(test_state->async_bio, false);
695     int timeout_ret = DTLSv1_handle_timeout(ssl);
696     AsyncBioEnforceWriteQuota(test_state->async_bio, true);
697
698     if (timeout_ret < 0) {
699       fprintf(stderr, "Error retransmitting.\n");
700       return false;
701     }
702     return true;
703   }
704
705   // See if we needed to read or write more. If so, allow one byte through on
706   // the appropriate end to maximally stress the state machine.
707   switch (SSL_get_error(ssl, ret)) {
708     case SSL_ERROR_WANT_READ:
709       AsyncBioAllowRead(test_state->async_bio, 1);
710       return true;
711     case SSL_ERROR_WANT_WRITE:
712       AsyncBioAllowWrite(test_state->async_bio, 1);
713       return true;
714     case SSL_ERROR_WANT_X509_LOOKUP:
715       test_state->cert_ready = true;
716       return true;
717     default:
718       return false;
719   }
720 }
721
722 // DoRead reads from |ssl|, resolving any asynchronous operations. It returns
723 // the result value of the final |SSL_read| call.
724 static int DoRead(SSL *ssl, uint8_t *out, size_t max_out) {
725   const TestConfig *config = GetTestConfig(ssl);
726   TestState *test_state = GetTestState(ssl);
727   int ret;
728   do {
729     if (config->async) {
730       // The DTLS retransmit logic silently ignores write failures. So the test
731       // may progress, allow writes through synchronously. |SSL_read| may
732       // trigger a retransmit, so disconnect the write quota.
733       AsyncBioEnforceWriteQuota(test_state->async_bio, false);
734     }
735     ret = config->peek_then_read ? SSL_peek(ssl, out, max_out)
736                                  : SSL_read(ssl, out, max_out);
737     if (config->async) {
738       AsyncBioEnforceWriteQuota(test_state->async_bio, true);
739     }
740   } while (config->async && RetryAsync(ssl, ret));
741
742   if (config->peek_then_read && ret > 0) {
743     std::unique_ptr<uint8_t[]> buf(new uint8_t[static_cast<size_t>(ret)]);
744
745     // SSL_peek should synchronously return the same data.
746     int ret2 = SSL_peek(ssl, buf.get(), ret);
747     if (ret2 != ret ||
748         memcmp(buf.get(), out, ret) != 0) {
749       fprintf(stderr, "First and second SSL_peek did not match.\n");
750       return -1;
751     }
752
753     // SSL_read should synchronously return the same data and consume it.
754     ret2 = SSL_read(ssl, buf.get(), ret);
755     if (ret2 != ret ||
756         memcmp(buf.get(), out, ret) != 0) {
757       fprintf(stderr, "SSL_peek and SSL_read did not match.\n");
758       return -1;
759     }
760   }
761
762   return ret;
763 }
764
765 // WriteAll writes |in_len| bytes from |in| to |ssl|, resolving any asynchronous
766 // operations. It returns the result of the final |SSL_write| call.
767 static int WriteAll(SSL *ssl, const uint8_t *in, size_t in_len) {
768   const TestConfig *config = GetTestConfig(ssl);
769   int ret;
770   do {
771     ret = SSL_write(ssl, in, in_len);
772     if (ret > 0) {
773       in += ret;
774       in_len -= ret;
775     }
776   } while ((config->async && RetryAsync(ssl, ret)) || (ret > 0 && in_len > 0));
777   return ret;
778 }
779
780 // DoShutdown calls |SSL_shutdown|, resolving any asynchronous operations. It
781 // returns the result of the final |SSL_shutdown| call.
782 static int DoShutdown(SSL *ssl) {
783   const TestConfig *config = GetTestConfig(ssl);
784   int ret;
785   do {
786     ret = SSL_shutdown(ssl);
787   } while (config->async && RetryAsync(ssl, ret));
788   return ret;
789 }
790
791 static uint16_t GetProtocolVersion(const SSL *ssl) {
792   uint16_t version = SSL_version(ssl);
793   if (!SSL_is_dtls(ssl)) {
794     return version;
795   }
796   return 0x0201 + ~version;
797 }
798
799 // CheckHandshakeProperties checks, immediately after |ssl| completes its
800 // initial handshake (or False Starts), whether all the properties are
801 // consistent with the test configuration and invariants.
802 static bool CheckHandshakeProperties(SSL *ssl, bool is_resume) {
803   const TestConfig *config = GetTestConfig(ssl);
804
805   if (SSL_get_current_cipher(ssl) == nullptr) {
806     fprintf(stderr, "null cipher after handshake\n");
807     return false;
808   }
809
810   if (is_resume &&
811       (!!SSL_session_reused(ssl) == config->expect_session_miss)) {
812     fprintf(stderr, "session was%s reused\n",
813             SSL_session_reused(ssl) ? "" : " not");
814     return false;
815   }
816
817   if (!GetTestState(ssl)->handshake_done) {
818     fprintf(stderr, "handshake was not completed\n");
819     return false;
820   }
821
822   if (!config->is_server) {
823     bool expect_new_session =
824         !config->expect_no_session &&
825         (!SSL_session_reused(ssl) || config->expect_ticket_renewal) &&
826         // Session tickets are sent post-handshake in TLS 1.3.
827         GetProtocolVersion(ssl) < TLS1_3_VERSION;
828     if (expect_new_session != GetTestState(ssl)->got_new_session) {
829       fprintf(stderr,
830               "new session was%s cached, but we expected the opposite\n",
831               GetTestState(ssl)->got_new_session ? "" : " not");
832       return false;
833     }
834   }
835
836   if (!config->expected_server_name.empty()) {
837     const char *server_name =
838         SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
839     if (server_name == nullptr ||
840             std::string(server_name) != config->expected_server_name) {
841       fprintf(stderr, "servername mismatch (got %s; want %s)\n",
842               server_name, config->expected_server_name.c_str());
843       return false;
844     }
845   }
846
847   if (!config->expected_next_proto.empty()) {
848     const uint8_t *next_proto;
849     unsigned next_proto_len;
850     SSL_get0_next_proto_negotiated(ssl, &next_proto, &next_proto_len);
851     if (next_proto_len != config->expected_next_proto.size() ||
852         memcmp(next_proto, config->expected_next_proto.data(),
853                next_proto_len) != 0) {
854       fprintf(stderr, "negotiated next proto mismatch\n");
855       return false;
856     }
857   }
858
859   if (!config->expected_alpn.empty()) {
860     const uint8_t *alpn_proto;
861     unsigned alpn_proto_len;
862     SSL_get0_alpn_selected(ssl, &alpn_proto, &alpn_proto_len);
863     if (alpn_proto_len != config->expected_alpn.size() ||
864         memcmp(alpn_proto, config->expected_alpn.data(),
865                alpn_proto_len) != 0) {
866       fprintf(stderr, "negotiated alpn proto mismatch\n");
867       return false;
868     }
869   }
870
871   if (config->expect_extended_master_secret) {
872     if (!SSL_get_extms_support(ssl)) {
873       fprintf(stderr, "No EMS for connection when expected");
874       return false;
875     }
876   }
877
878   if (config->expect_verify_result) {
879     int expected_verify_result = config->verify_fail ?
880       X509_V_ERR_APPLICATION_VERIFICATION :
881       X509_V_OK;
882
883     if (SSL_get_verify_result(ssl) != expected_verify_result) {
884       fprintf(stderr, "Wrong certificate verification result\n");
885       return false;
886     }
887   }
888
889   if (!config->psk.empty()) {
890     if (SSL_get_peer_cert_chain(ssl) != nullptr) {
891       fprintf(stderr, "Received peer certificate on a PSK cipher.\n");
892       return false;
893     }
894   } else if (!config->is_server || config->require_any_client_certificate) {
895     if (SSL_get_peer_certificate(ssl) == nullptr) {
896       fprintf(stderr, "Received no peer certificate but expected one.\n");
897       return false;
898     }
899   }
900
901   return true;
902 }
903
904 // DoExchange runs a test SSL exchange against the peer. On success, it returns
905 // true and sets |*out_session| to the negotiated SSL session. If the test is a
906 // resumption attempt, |is_resume| is true and |session| is the session from the
907 // previous exchange.
908 static bool DoExchange(bssl::UniquePtr<SSL_SESSION> *out_session,
909                        SSL_CTX *ssl_ctx, const TestConfig *config,
910                        bool is_resume, SSL_SESSION *session) {
911   bssl::UniquePtr<SSL> ssl(SSL_new(ssl_ctx));
912   if (!ssl) {
913     return false;
914   }
915
916   if (!SetTestConfig(ssl.get(), config) ||
917       !SetTestState(ssl.get(), std::unique_ptr<TestState>(new TestState))) {
918     return false;
919   }
920
921   if (config->fallback_scsv &&
922       !SSL_set_mode(ssl.get(), SSL_MODE_SEND_FALLBACK_SCSV)) {
923     return false;
924   }
925   // Install the certificate synchronously if nothing else will handle it.
926   if (!config->use_old_client_cert_callback &&
927       !config->async &&
928       !InstallCertificate(ssl.get())) {
929     return false;
930   }
931   SSL_set_cert_cb(ssl.get(), CertCallback, nullptr);
932   if (config->require_any_client_certificate) {
933     SSL_set_verify(ssl.get(), SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
934                    NULL);
935   }
936   if (config->verify_peer) {
937     SSL_set_verify(ssl.get(), SSL_VERIFY_PEER, NULL);
938   }
939   if (config->partial_write) {
940     SSL_set_mode(ssl.get(), SSL_MODE_ENABLE_PARTIAL_WRITE);
941   }
942   if (config->no_tls13) {
943     SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_3);
944   }
945   if (config->no_tls12) {
946     SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_2);
947   }
948   if (config->no_tls11) {
949     SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1_1);
950   }
951   if (config->no_tls1) {
952     SSL_set_options(ssl.get(), SSL_OP_NO_TLSv1);
953   }
954   if (config->no_ssl3) {
955     SSL_set_options(ssl.get(), SSL_OP_NO_SSLv3);
956   }
957   if (!config->host_name.empty() &&
958       !SSL_set_tlsext_host_name(ssl.get(), config->host_name.c_str())) {
959     return false;
960   }
961   if (!config->advertise_alpn.empty() &&
962       SSL_set_alpn_protos(ssl.get(),
963                           (const uint8_t *)config->advertise_alpn.data(),
964                           config->advertise_alpn.size()) != 0) {
965     return false;
966   }
967   if (!config->psk.empty()) {
968     SSL_set_psk_client_callback(ssl.get(), PskClientCallback);
969     SSL_set_psk_server_callback(ssl.get(), PskServerCallback);
970   }
971   if (!config->psk_identity.empty() &&
972       !SSL_use_psk_identity_hint(ssl.get(), config->psk_identity.c_str())) {
973     return false;
974   }
975   if (!config->srtp_profiles.empty() &&
976       SSL_set_tlsext_use_srtp(ssl.get(), config->srtp_profiles.c_str())) {
977     return false;
978   }
979   if (config->min_version != 0 &&
980       !SSL_set_min_proto_version(ssl.get(), (uint16_t)config->min_version)) {
981     return false;
982   }
983   if (config->max_version != 0 &&
984       !SSL_set_max_proto_version(ssl.get(), (uint16_t)config->max_version)) {
985     return false;
986   }
987   if (config->mtu != 0) {
988     SSL_set_options(ssl.get(), SSL_OP_NO_QUERY_MTU);
989     SSL_set_mtu(ssl.get(), config->mtu);
990   }
991   if (config->renegotiate_freely) {
992     // This is always on for OpenSSL.
993   }
994   if (!config->check_close_notify) {
995     SSL_set_quiet_shutdown(ssl.get(), 1);
996   }
997   if (config->p384_only) {
998     int nid = NID_secp384r1;
999     if (!SSL_set1_curves(ssl.get(), &nid, 1)) {
1000       return false;
1001     }
1002   }
1003   if (config->enable_all_curves) {
1004     static const int kAllCurves[] = {
1005       NID_X25519, NID_X9_62_prime256v1, NID_X448, NID_secp521r1, NID_secp384r1
1006     };
1007     if (!SSL_set1_curves(ssl.get(), kAllCurves,
1008                          OPENSSL_ARRAY_SIZE(kAllCurves))) {
1009       return false;
1010     }
1011   }
1012   if (config->max_cert_list > 0) {
1013     SSL_set_max_cert_list(ssl.get(), config->max_cert_list);
1014   }
1015
1016   if (!config->async) {
1017     SSL_set_mode(ssl.get(), SSL_MODE_AUTO_RETRY);
1018   }
1019
1020   int sock = Connect(config->port);
1021   if (sock == -1) {
1022     return false;
1023   }
1024   SocketCloser closer(sock);
1025
1026   bssl::UniquePtr<BIO> bio(BIO_new_socket(sock, BIO_NOCLOSE));
1027   if (!bio) {
1028     return false;
1029   }
1030   if (config->is_dtls) {
1031     bssl::UniquePtr<BIO> packeted = PacketedBioCreate(!config->async);
1032     if (!packeted) {
1033       return false;
1034     }
1035     GetTestState(ssl.get())->packeted_bio = packeted.get();
1036     BIO_push(packeted.get(), bio.release());
1037     bio = std::move(packeted);
1038   }
1039   if (config->async) {
1040     bssl::UniquePtr<BIO> async_scoped =
1041         config->is_dtls ? AsyncBioCreateDatagram() : AsyncBioCreate();
1042     if (!async_scoped) {
1043       return false;
1044     }
1045     BIO_push(async_scoped.get(), bio.release());
1046     GetTestState(ssl.get())->async_bio = async_scoped.get();
1047     bio = std::move(async_scoped);
1048   }
1049   SSL_set_bio(ssl.get(), bio.get(), bio.get());
1050   bio.release();  // SSL_set_bio takes ownership.
1051
1052   if (session != NULL) {
1053     if (!config->is_server) {
1054       if (SSL_set_session(ssl.get(), session) != 1) {
1055         return false;
1056       }
1057     }
1058   }
1059
1060 #if 0
1061   // KNOWN BUG: OpenSSL's SSL_get_current_cipher behaves incorrectly when
1062   // offering resumption.
1063   if (SSL_get_current_cipher(ssl.get()) != nullptr) {
1064     fprintf(stderr, "non-null cipher before handshake\n");
1065     return false;
1066   }
1067 #endif
1068
1069   int ret;
1070   if (config->implicit_handshake) {
1071     if (config->is_server) {
1072       SSL_set_accept_state(ssl.get());
1073     } else {
1074       SSL_set_connect_state(ssl.get());
1075     }
1076   } else {
1077     do {
1078       if (config->is_server) {
1079         ret = SSL_accept(ssl.get());
1080       } else {
1081         ret = SSL_connect(ssl.get());
1082       }
1083     } while (config->async && RetryAsync(ssl.get(), ret));
1084     if (ret != 1 ||
1085         !CheckHandshakeProperties(ssl.get(), is_resume)) {
1086       return false;
1087     }
1088
1089     // Reset the state to assert later that the callback isn't called in
1090     // renegotiations.
1091     GetTestState(ssl.get())->got_new_session = false;
1092   }
1093
1094   if (config->export_keying_material > 0) {
1095     std::vector<uint8_t> result(
1096         static_cast<size_t>(config->export_keying_material));
1097     if (SSL_export_keying_material(
1098             ssl.get(), result.data(), result.size(),
1099             config->export_label.data(), config->export_label.size(),
1100             reinterpret_cast<const uint8_t*>(config->export_context.data()),
1101             config->export_context.size(), config->use_export_context) != 1) {
1102       fprintf(stderr, "failed to export keying material\n");
1103       return false;
1104     }
1105     if (WriteAll(ssl.get(), result.data(), result.size()) < 0) {
1106       return false;
1107     }
1108   }
1109
1110   if (config->write_different_record_sizes) {
1111     if (config->is_dtls) {
1112       fprintf(stderr, "write_different_record_sizes not supported for DTLS\n");
1113       return false;
1114     }
1115     // This mode writes a number of different record sizes in an attempt to
1116     // trip up the CBC record splitting code.
1117     static const size_t kBufLen = 32769;
1118     std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1119     memset(buf.get(), 0x42, kBufLen);
1120     static const size_t kRecordSizes[] = {
1121         0, 1, 255, 256, 257, 16383, 16384, 16385, 32767, 32768, 32769};
1122     for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kRecordSizes); i++) {
1123       const size_t len = kRecordSizes[i];
1124       if (len > kBufLen) {
1125         fprintf(stderr, "Bad kRecordSizes value.\n");
1126         return false;
1127       }
1128       if (WriteAll(ssl.get(), buf.get(), len) < 0) {
1129         return false;
1130       }
1131     }
1132   } else {
1133     if (config->shim_writes_first) {
1134       if (WriteAll(ssl.get(), reinterpret_cast<const uint8_t *>("hello"),
1135                    5) < 0) {
1136         return false;
1137       }
1138     }
1139     if (!config->shim_shuts_down) {
1140       for (;;) {
1141         static const size_t kBufLen = 16384;
1142         std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufLen]);
1143
1144         // Read only 512 bytes at a time in TLS to ensure records may be
1145         // returned in multiple reads.
1146         int n = DoRead(ssl.get(), buf.get(), config->is_dtls ? kBufLen : 512);
1147         int err = SSL_get_error(ssl.get(), n);
1148         if (err == SSL_ERROR_ZERO_RETURN ||
1149             (n == 0 && err == SSL_ERROR_SYSCALL)) {
1150           if (n != 0) {
1151             fprintf(stderr, "Invalid SSL_get_error output\n");
1152             return false;
1153           }
1154           // Stop on either clean or unclean shutdown.
1155           break;
1156         } else if (err != SSL_ERROR_NONE) {
1157           if (n > 0) {
1158             fprintf(stderr, "Invalid SSL_get_error output\n");
1159             return false;
1160           }
1161           return false;
1162         }
1163         // Successfully read data.
1164         if (n <= 0) {
1165           fprintf(stderr, "Invalid SSL_get_error output\n");
1166           return false;
1167         }
1168
1169         // After a successful read, with or without False Start, the handshake
1170         // must be complete.
1171         if (!GetTestState(ssl.get())->handshake_done) {
1172           fprintf(stderr, "handshake was not completed after SSL_read\n");
1173           return false;
1174         }
1175
1176         for (int i = 0; i < n; i++) {
1177           buf[i] ^= 0xff;
1178         }
1179         if (WriteAll(ssl.get(), buf.get(), n) < 0) {
1180           return false;
1181         }
1182       }
1183     }
1184   }
1185
1186   if (!config->is_server &&
1187       !config->implicit_handshake &&
1188       // Session tickets are sent post-handshake in TLS 1.3.
1189       GetProtocolVersion(ssl.get()) < TLS1_3_VERSION &&
1190       GetTestState(ssl.get())->got_new_session) {
1191     fprintf(stderr, "new session was established after the handshake\n");
1192     return false;
1193   }
1194
1195   if (GetProtocolVersion(ssl.get()) >= TLS1_3_VERSION && !config->is_server) {
1196     bool expect_new_session =
1197         !config->expect_no_session && !config->shim_shuts_down;
1198     if (expect_new_session != GetTestState(ssl.get())->got_new_session) {
1199       fprintf(stderr,
1200               "new session was%s cached, but we expected the opposite\n",
1201               GetTestState(ssl.get())->got_new_session ? "" : " not");
1202       return false;
1203     }
1204   }
1205
1206   if (out_session) {
1207     *out_session = std::move(GetTestState(ssl.get())->new_session);
1208   }
1209
1210   ret = DoShutdown(ssl.get());
1211
1212   if (config->shim_shuts_down && config->check_close_notify) {
1213     // We initiate shutdown, so |SSL_shutdown| will return in two stages. First
1214     // it returns zero when our close_notify is sent, then one when the peer's
1215     // is received.
1216     if (ret != 0) {
1217       fprintf(stderr, "Unexpected SSL_shutdown result: %d != 0\n", ret);
1218       return false;
1219     }
1220     ret = DoShutdown(ssl.get());
1221   }
1222
1223   if (ret != 1) {
1224     fprintf(stderr, "Unexpected SSL_shutdown result: %d != 1\n", ret);
1225     return false;
1226   }
1227
1228   if (SSL_total_renegotiations(ssl.get()) !=
1229       config->expect_total_renegotiations) {
1230     fprintf(stderr, "Expected %d renegotiations, got %ld\n",
1231             config->expect_total_renegotiations,
1232             SSL_total_renegotiations(ssl.get()));
1233     return false;
1234   }
1235
1236   return true;
1237 }
1238
1239 class StderrDelimiter {
1240  public:
1241   ~StderrDelimiter() { fprintf(stderr, "--- DONE ---\n"); }
1242 };
1243
1244 static int Main(int argc, char **argv) {
1245   // To distinguish ASan's output from ours, add a trailing message to stderr.
1246   // Anything following this line will be considered an error.
1247   StderrDelimiter delimiter;
1248
1249 #if defined(OPENSSL_SYS_WINDOWS)
1250   /* Initialize Winsock. */
1251   WORD wsa_version = MAKEWORD(2, 2);
1252   WSADATA wsa_data;
1253   int wsa_err = WSAStartup(wsa_version, &wsa_data);
1254   if (wsa_err != 0) {
1255     fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
1256     return 1;
1257   }
1258   if (wsa_data.wVersion != wsa_version) {
1259     fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
1260     return 1;
1261   }
1262 #else
1263   signal(SIGPIPE, SIG_IGN);
1264 #endif
1265
1266   OPENSSL_init_crypto(0, NULL);
1267   OPENSSL_init_ssl(0, NULL);
1268   g_config_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
1269   g_state_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, TestStateExFree);
1270   if (g_config_index < 0 || g_state_index < 0) {
1271     return 1;
1272   }
1273
1274   TestConfig config;
1275   if (!ParseConfig(argc - 1, argv + 1, &config)) {
1276     return Usage(argv[0]);
1277   }
1278
1279   bssl::UniquePtr<SSL_CTX> ssl_ctx = SetupCtx(&config);
1280   if (!ssl_ctx) {
1281     ERR_print_errors_fp(stderr);
1282     return 1;
1283   }
1284
1285   bssl::UniquePtr<SSL_SESSION> session;
1286   for (int i = 0; i < config.resume_count + 1; i++) {
1287     bool is_resume = i > 0;
1288     if (is_resume && !config.is_server && !session) {
1289       fprintf(stderr, "No session to offer.\n");
1290       return 1;
1291     }
1292
1293     bssl::UniquePtr<SSL_SESSION> offer_session = std::move(session);
1294     if (!DoExchange(&session, ssl_ctx.get(), &config, is_resume,
1295                     offer_session.get())) {
1296       fprintf(stderr, "Connection %d failed.\n", i + 1);
1297       ERR_print_errors_fp(stderr);
1298       return 1;
1299     }
1300   }
1301
1302   return 0;
1303 }
1304
1305 }  // namespace bssl
1306
1307 int main(int argc, char **argv) {
1308   return bssl::Main(argc, argv);
1309 }