Raise an error on syscall failure in tls_retry_write_records
[openssl.git] / providers / implementations / kdfs / x942kdf.c
index a220eca80fa0ae67aed458a5f97266e2e650a707..19b54493efca0d3b9fcdf7e443ea64e01dcaac2c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
  * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
@@ -8,7 +8,7 @@
  * https://www.openssl.org/source/license.html
  */
 
-#include "e_os.h"
+#include "internal/e_os.h"
 #include <openssl/core_names.h>
 #include <openssl/core_dispatch.h>
 #include <openssl/err.h>
@@ -17,6 +17,7 @@
 #include <openssl/proverr.h>
 #include "internal/packet.h"
 #include "internal/der.h"
+#include "internal/nelem.h"
 #include "prov/provider_ctx.h"
 #include "prov/providercommon.h"
 #include "prov/implementations.h"
@@ -26,6 +27,7 @@
 #define X942KDF_MAX_INLEN (1 << 30)
 
 static OSSL_FUNC_kdf_newctx_fn x942kdf_new;
+static OSSL_FUNC_kdf_dupctx_fn x942kdf_dup;
 static OSSL_FUNC_kdf_freectx_fn x942kdf_free;
 static OSSL_FUNC_kdf_reset_fn x942kdf_reset;
 static OSSL_FUNC_kdf_derive_fn x942kdf_derive;
@@ -169,7 +171,7 @@ static int der_encode_sharedinfo(WPACKET *pkt, unsigned char *buf, size_t buflen
  * |cek_oidlen| The length (in bytes) of the key wrapping algorithm oid,
  * |acvp| is the optional blob of DER data representing one or more of the
  *   OtherInfo fields related to |partyu|, |partyv|, |supp_pub| and |supp_priv|.
- *   This field should noramlly be NULL. If |acvp| is non NULL then |partyu|,
+ *   This field should normally be NULL. If |acvp| is non NULL then |partyu|,
  *   |partyv|, |supp_pub| and |supp_priv| should all be NULL.
  * |acvp_len| is the |acvp| length (in bytes).
  * |partyu| is the optional public info contributed by the initiator.
@@ -237,7 +239,7 @@ x942_encode_otherinfo(size_t keylen,
         goto err;
     /*
      * Since we allocated the exact size required, the buffer should point to the
-     * start of the alllocated buffer at this point.
+     * start of the allocated buffer at this point.
      */
     if (WPACKET_get_curr(&pkt) != der_buf)
         goto err;
@@ -281,7 +283,7 @@ static int x942kdf_hash_kdm(const EVP_MD *kdf_md,
         return 0;
     }
 
-    hlen = EVP_MD_size(kdf_md);
+    hlen = EVP_MD_get_size(kdf_md);
     if (hlen <= 0)
         return 0;
     out_len = (size_t)hlen;
@@ -332,10 +334,10 @@ static void *x942kdf_new(void *provctx)
     KDF_X942 *ctx;
 
     if (!ossl_prov_is_running())
-        return 0;
+        return NULL;
 
     if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL)
-        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
+        return NULL;
     ctx->provctx = provctx;
     ctx->use_keybits = 1;
     return ctx;
@@ -368,6 +370,41 @@ static void x942kdf_free(void *vctx)
     }
 }
 
+static void *x942kdf_dup(void *vctx)
+{
+    const KDF_X942 *src = (const KDF_X942 *)vctx;
+    KDF_X942 *dest;
+
+    dest = x942kdf_new(src->provctx);
+    if (dest != NULL) {
+        if (!ossl_prov_memdup(src->secret, src->secret_len,
+                              &dest->secret , &dest->secret_len)
+                || !ossl_prov_memdup(src->acvpinfo, src->acvpinfo_len,
+                                     &dest->acvpinfo , &dest->acvpinfo_len)
+                || !ossl_prov_memdup(src->partyuinfo, src->partyuinfo_len,
+                                     &dest->partyuinfo , &dest->partyuinfo_len)
+                || !ossl_prov_memdup(src->partyvinfo, src->partyvinfo_len,
+                                     &dest->partyvinfo , &dest->partyvinfo_len)
+                || !ossl_prov_memdup(src->supp_pubinfo, src->supp_pubinfo_len,
+                                     &dest->supp_pubinfo,
+                                     &dest->supp_pubinfo_len)
+                || !ossl_prov_memdup(src->supp_privinfo, src->supp_privinfo_len,
+                                     &dest->supp_privinfo,
+                                     &dest->supp_privinfo_len)
+                || !ossl_prov_digest_copy(&dest->digest, &src->digest))
+            goto err;
+        dest->cek_oid = src->cek_oid;
+        dest->cek_oid_len = src->cek_oid_len;
+        dest->dkm_len = src->dkm_len;
+        dest->use_keybits = src->use_keybits;
+    }
+    return dest;
+
+ err:
+    x942kdf_free(dest);
+    return NULL;
+}
+
 static int x942kdf_set_buffer(unsigned char **out, size_t *out_len,
                               const OSSL_PARAM *p)
 {
@@ -388,11 +425,12 @@ static size_t x942kdf_size(KDF_X942 *ctx)
         ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_MESSAGE_DIGEST);
         return 0;
     }
-    len = EVP_MD_size(md);
+    len = EVP_MD_get_size(md);
     return (len <= 0) ? 0 : (size_t)len;
 }
 
-static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen)
+static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen,
+                          const OSSL_PARAM params[])
 {
     KDF_X942 *ctx = (KDF_X942 *)vctx;
     const EVP_MD *md;
@@ -401,7 +439,7 @@ static int x942kdf_derive(void *vctx, unsigned char *key, size_t keylen)
     unsigned char *der = NULL;
     size_t der_len = 0;
 
-    if (!ossl_prov_is_running())
+    if (!ossl_prov_is_running() || !x942kdf_set_ctx_params(ctx, params))
         return 0;
 
     /*
@@ -471,6 +509,8 @@ static int x942kdf_set_ctx_params(void *vctx, const OSSL_PARAM params[])
     const char *propq = NULL;
     size_t id;
 
+    if (params == NULL)
+        return 1;
     if (!ossl_prov_digest_load_from_params(&ctx->digest, params, provctx))
         return 0;
 
@@ -576,6 +616,7 @@ static const OSSL_PARAM *x942kdf_gettable_ctx_params(ossl_unused void *ctx,
 
 const OSSL_DISPATCH ossl_kdf_x942_kdf_functions[] = {
     { OSSL_FUNC_KDF_NEWCTX, (void(*)(void))x942kdf_new },
+    { OSSL_FUNC_KDF_DUPCTX, (void(*)(void))x942kdf_dup },
     { OSSL_FUNC_KDF_FREECTX, (void(*)(void))x942kdf_free },
     { OSSL_FUNC_KDF_RESET, (void(*)(void))x942kdf_reset },
     { OSSL_FUNC_KDF_DERIVE, (void(*)(void))x942kdf_derive },
@@ -585,5 +626,5 @@ const OSSL_DISPATCH ossl_kdf_x942_kdf_functions[] = {
     { OSSL_FUNC_KDF_GETTABLE_CTX_PARAMS,
       (void(*)(void))x942kdf_gettable_ctx_params },
     { OSSL_FUNC_KDF_GET_CTX_PARAMS, (void(*)(void))x942kdf_get_ctx_params },
-    { 0, NULL }
+    OSSL_DISPATCH_END
 };