Move libfuzzer sanitizer options to README
[openssl.git] / fuzz / server.c
index 7b376c1abbe5f082ff67760817a1d08f58c84ea0..4f2c794a4ca513779a7001396f28037b47c8863b 100644 (file)
@@ -8,13 +8,14 @@
  * or in the file LICENSE in the source distribution.
  */
 
-// Shamelessly copied from BoringSSL and converted to C.
+/* Shamelessly copied from BoringSSL and converted to C. */
 
-// Test first part of SSL server handshake.
+/* Test first part of SSL server handshake. */
 
 
 #include <openssl/rand.h>
 #include <openssl/ssl.h>
+#include <openssl/rsa.h>
 #include "fuzzer.h"
 
 static const uint8_t kCertificateDER[] = {
@@ -190,41 +191,63 @@ static const uint8_t kRSAPrivateKeyDER[] = {
 
 static SSL_CTX *ctx;
 
-static void Init() {
-    ctx = SSL_CTX_new(SSLv23_method());
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+extern int rand_predictable;
+#endif
+
+int FuzzerInitialize(int *argc, char ***argv)
+{
     const uint8_t *bufp = kRSAPrivateKeyDER;
-    RSA *privkey = d2i_RSAPrivateKey(NULL, &bufp, sizeof(kRSAPrivateKeyDER));
+    RSA *privkey;
+    EVP_PKEY *pkey;
+    int ret;
+    X509 *cert;
+
+    ctx = SSL_CTX_new(SSLv23_method());
+    privkey = d2i_RSAPrivateKey(NULL, &bufp, sizeof(kRSAPrivateKeyDER));
     OPENSSL_assert(privkey != NULL);
-    EVP_PKEY *pkey = EVP_PKEY_new();
+    pkey = EVP_PKEY_new();
     EVP_PKEY_assign_RSA(pkey, privkey);
-    int ret = SSL_CTX_use_PrivateKey(ctx, pkey);
+    ret = SSL_CTX_use_PrivateKey(ctx, pkey);
     OPENSSL_assert(ret == 1);
     EVP_PKEY_free(pkey);
     bufp = kCertificateDER;
-    X509 *cert = d2i_X509(NULL, &bufp, sizeof(kCertificateDER));
+    cert = d2i_X509(NULL, &bufp, sizeof(kCertificateDER));
     OPENSSL_assert(cert != NULL);
     ret = SSL_CTX_use_certificate(ctx, cert);
     OPENSSL_assert(ret == 1);
     X509_free(cert);
-  }
 
-int FuzzerTestOneInput(const uint8_t *buf, size_t len) {
-    if (ctx == NULL)
-        Init();
-    // TODO: make this work for OpenSSL. There's a PREDICT define that may do
-    // the job.
-    // TODO: use the ossltest engine (optionally?) to disable crypto checks.
-    //RAND_reset_for_fuzzing();
+#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
+    rand_predictable = 1;
+#endif
+
+    return 1;
+}
+
+int FuzzerTestOneInput(const uint8_t *buf, size_t len)
+{
+    SSL *server;
+    BIO *in;
+    BIO *out;
+    if (!len) {
+        return 0;
+    }
+    /* TODO: make this work for OpenSSL. There's a PREDICT define that may do
+     * the job.
+     * TODO: use the ossltest engine (optionally?) to disable crypto checks.
+     * RAND_reset_for_fuzzing();
+     */
 
-    // This only fuzzes the initial flow from the client so far.
-    SSL *server = SSL_new(ctx);
-    BIO *in = BIO_new(BIO_s_mem());
-    BIO *out = BIO_new(BIO_s_mem());
+    /* This only fuzzes the initial flow from the client so far. */
+    server = SSL_new(ctx);
+    in = BIO_new(BIO_s_mem());
+    out = BIO_new(BIO_s_mem());
     SSL_set_bio(server, in, out);
     SSL_set_accept_state(server);
     OPENSSL_assert((size_t)BIO_write(in, buf, len) == len);
     if (SSL_do_handshake(server) == 1) {
-        // Keep reading application data until error or EOF.
+        /* Keep reading application data until error or EOF. */
         uint8_t tmp[1024];
         for (;;) {
             if (SSL_read(server, tmp, sizeof(tmp)) <= 0) {
@@ -235,3 +258,8 @@ int FuzzerTestOneInput(const uint8_t *buf, size_t len) {
     SSL_free(server);
     return 0;
 }
+
+void FuzzerCleanup(void)
+{
+    SSL_CTX_free(ctx);
+}