Add testing for updated cipher IV
[openssl.git] / test / bad_dtls_test.c
index 9745ff87209c71d4594f1722da41523dea765455..48cf45bae632b728e7eea241db422a06a7a87ba2 100644 (file)
@@ -1,7 +1,7 @@
 /*
- * Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  *
- * Licensed under the OpenSSL license (the "License").  You may not use
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
  * in the file LICENSE in the source distribution or at
  * https://www.openssl.org/source/license.html
@@ -19,7 +19,7 @@
  * Note that unlike other SSL tests, we don't test against our own SSL
  * server method. Firstly because we don't have one; we *only* support
  * DTLS1_BAD_VER as a client. And secondly because even if that were
- * fixed up it's the wrong thing to test against  because if changes
+ * fixed up it's the wrong thing to test against - because if changes
  * are made in generic DTLS code which don't take DTLS1_BAD_VER into
  * account, there's plenty of scope for making those changes such that
  * they break *both* the client and the server in the same way.
@@ -29,6 +29,8 @@
  */
 #include <string.h>
 
+#include <openssl/core_names.h>
+#include <openssl/params.h>
 #include <openssl/opensslconf.h>
 #include <openssl/bio.h>
 #include <openssl/crypto.h>
 #include <openssl/err.h>
 #include <openssl/rand.h>
 #include <openssl/kdf.h>
-
-#include "../ssl/packet_locl.h"
-#include "../e_os.h" /* for OSSL_NELEM() */
-
+#include "internal/packet.h"
+#include "internal/nelem.h"
 #include "testutil.h"
 
 /* For DTLS1_BAD_VER packets the MAC doesn't include the handshake header */
@@ -120,9 +120,10 @@ static int validate_client_hello(BIO *wbio)
     long len;
     unsigned char *data;
     int cookie_found = 0;
-    unsigned int u;
+    unsigned int u = 0;
 
-    len = BIO_get_mem_data(wbio, (char **)&data);
+    if ((len = BIO_get_mem_data(wbio, (char **)&data)) < 0)
+        return 0;
     if (!PACKET_buf_init(&pkt, data, len))
         return 0;
 
@@ -280,11 +281,14 @@ static int send_record(BIO *rbio, unsigned char type, uint64_t seqnr,
     static unsigned char seq[6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
     static unsigned char ver[2] = { 0x01, 0x00 }; /* DTLS1_BAD_VER */
     unsigned char lenbytes[2];
-    HMAC_CTX *ctx;
-    EVP_CIPHER_CTX *enc_ctx;
+    EVP_MAC *hmac = NULL;
+    EVP_MAC_CTX *ctx = NULL;
+    EVP_CIPHER_CTX *enc_ctx = NULL;
     unsigned char iv[16];
     unsigned char pad;
     unsigned char *enc;
+    OSSL_PARAM params[2];
+    int ret = 0;
 
     seq[0] = (seqnr >> 40) & 0xff;
     seq[1] = (seqnr >> 32) & 0xff;
@@ -302,18 +306,23 @@ static int send_record(BIO *rbio, unsigned char type, uint64_t seqnr,
     memcpy(enc, msg, len);
 
     /* Append HMAC to data */
-    ctx = HMAC_CTX_new();
-    HMAC_Init_ex(ctx, mac_key, 20, EVP_sha1(), NULL);
-    HMAC_Update(ctx, epoch, 2);
-    HMAC_Update(ctx, seq, 6);
-    HMAC_Update(ctx, &type, 1);
-    HMAC_Update(ctx, ver, 2); /* Version */
-    lenbytes[0] = len >> 8;
-    lenbytes[1] = len & 0xff;
-    HMAC_Update(ctx, lenbytes, 2); /* Length */
-    HMAC_Update(ctx, enc, len); /* Finally the data itself */
-    HMAC_Final(ctx, enc + len, NULL);
-    HMAC_CTX_free(ctx);
+    if (!TEST_ptr(hmac = EVP_MAC_fetch(NULL, "HMAC", NULL))
+            || !TEST_ptr(ctx = EVP_MAC_CTX_new(hmac)))
+        goto end;
+    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
+                                                 "SHA1", 0);
+    params[1] = OSSL_PARAM_construct_end();
+    lenbytes[0] = (unsigned char)(len >> 8);
+    lenbytes[1] = (unsigned char)(len);
+    if (!EVP_MAC_init(ctx, mac_key, 20, params)
+            || !EVP_MAC_update(ctx, epoch, 2)
+            || !EVP_MAC_update(ctx, seq, 6)
+            || !EVP_MAC_update(ctx, &type, 1)
+            || !EVP_MAC_update(ctx, ver, 2)      /* Version */
+            || !EVP_MAC_update(ctx, lenbytes, 2) /* Length */
+            || !EVP_MAC_update(ctx, enc, len)    /* Finally the data itself */
+            || !EVP_MAC_final(ctx, enc + len, NULL, SHA_DIGEST_LENGTH))
+        goto end;
 
     /* Append padding bytes */
     len += SHA_DIGEST_LENGTH;
@@ -322,26 +331,31 @@ static int send_record(BIO *rbio, unsigned char type, uint64_t seqnr,
     } while (len % 16);
 
     /* Generate IV, and encrypt */
-    RAND_bytes(iv, sizeof(iv));
-    enc_ctx = EVP_CIPHER_CTX_new();
-    EVP_CipherInit_ex(enc_ctx, EVP_aes_128_cbc(), NULL, enc_key, iv, 1);
-    EVP_Cipher(enc_ctx, enc, enc, len);
-    EVP_CIPHER_CTX_free(enc_ctx);
+    if (!TEST_true(RAND_bytes(iv, sizeof(iv)))
+            || !TEST_ptr(enc_ctx = EVP_CIPHER_CTX_new())
+            || !TEST_true(EVP_CipherInit_ex(enc_ctx, EVP_aes_128_cbc(), NULL,
+                                            enc_key, iv, 1))
+            || !TEST_int_ge(EVP_Cipher(enc_ctx, enc, enc, len), 0))
+        goto end;
 
     /* Finally write header (from fragmented variables), IV and encrypted record */
     BIO_write(rbio, &type, 1);
     BIO_write(rbio, ver, 2);
     BIO_write(rbio, epoch, 2);
     BIO_write(rbio, seq, 6);
-    lenbytes[0] = (len + sizeof(iv)) >> 8;
-    lenbytes[1] = (len + sizeof(iv)) & 0xff;
+    lenbytes[0] = (unsigned char)((len + sizeof(iv)) >> 8);
+    lenbytes[1] = (unsigned char)(len + sizeof(iv));
     BIO_write(rbio, lenbytes, 2);
 
     BIO_write(rbio, iv, sizeof(iv));
     BIO_write(rbio, enc, len);
-
+    ret = 1;
+ end:
+    EVP_MAC_free(hmac);
+    EVP_MAC_CTX_free(ctx);
+    EVP_CIPHER_CTX_free(enc_ctx);
     OPENSSL_free(enc);
-    return 1;
+    return ret;
 }
 
 static int send_finished(SSL *s, BIO *rbio)
@@ -384,6 +398,9 @@ static int validate_ccs(BIO *wbio)
     unsigned int u;
 
     len = BIO_get_mem_data(wbio, (char **)&data);
+    if (len < 0)
+        return 0;
+
     if (!PACKET_buf_init(&pkt, data, len))
         return 0;
 
@@ -581,7 +598,8 @@ static int test_bad_dtls(void)
     return testresult;
 }
 
-void register_tests(void)
+int setup_tests(void)
 {
     ADD_TEST(test_bad_dtls);
+    return 1;
 }