Rename <openssl/core_numbers.h> -> <openssl/core_dispatch.h>
[oweals/openssl.git] / providers / common / bio_prov.c
index 7b44004399e431e5d542195d4e8de71eb2e1b08a..2186894b11072092ce388a6dea1ad13ae6afcf83 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
  *
  * 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
@@ -7,12 +7,15 @@
  * https://www.openssl.org/source/license.html
  */
 
-#include <openssl/core_numbers.h>
+#include <assert.h>
+#include <openssl/core_dispatch.h>
+#include "internal/cryptlib.h"
 #include "prov/bio.h"
 
 static OSSL_BIO_new_file_fn *c_bio_new_file = NULL;
 static OSSL_BIO_new_membuf_fn *c_bio_new_membuf = NULL;
 static OSSL_BIO_read_ex_fn *c_bio_read_ex = NULL;
+static OSSL_BIO_write_ex_fn *c_bio_write_ex = NULL;
 static OSSL_BIO_free_fn *c_bio_free = NULL;
 static OSSL_BIO_vprintf_fn *c_bio_vprintf = NULL;
 
@@ -32,6 +35,10 @@ int ossl_prov_bio_from_dispatch(const OSSL_DISPATCH *fns)
             if (c_bio_read_ex == NULL)
                 c_bio_read_ex = OSSL_get_BIO_read_ex(fns);
             break;
+        case OSSL_FUNC_BIO_WRITE_EX:
+            if (c_bio_write_ex == NULL)
+                c_bio_write_ex = OSSL_get_BIO_write_ex(fns);
+            break;
         case OSSL_FUNC_BIO_FREE:
             if (c_bio_free == NULL)
                 c_bio_free = OSSL_get_BIO_free(fns);
@@ -46,21 +53,21 @@ int ossl_prov_bio_from_dispatch(const OSSL_DISPATCH *fns)
     return 1;
 }
 
-BIO *ossl_prov_bio_new_file(const char *filename, const char *mode)
+OSSL_CORE_BIO *ossl_prov_bio_new_file(const char *filename, const char *mode)
 {
     if (c_bio_new_file == NULL)
         return NULL;
     return c_bio_new_file(filename, mode);
 }
 
-BIO *ossl_prov_bio_new_membuf(const char *filename, int len)
+OSSL_CORE_BIO *ossl_prov_bio_new_membuf(const char *filename, int len)
 {
     if (c_bio_new_membuf == NULL)
         return NULL;
     return c_bio_new_membuf(filename, len);
 }
 
-int ossl_prov_bio_read_ex(BIO *bio, void *data, size_t data_len,
+int ossl_prov_bio_read_ex(OSSL_CORE_BIO *bio, void *data, size_t data_len,
                           size_t *bytes_read)
 {
     if (c_bio_read_ex == NULL)
@@ -68,21 +75,29 @@ int ossl_prov_bio_read_ex(BIO *bio, void *data, size_t data_len,
     return c_bio_read_ex(bio, data, data_len, bytes_read);
 }
 
-int ossl_prov_bio_free(BIO *bio)
+int ossl_prov_bio_write_ex(OSSL_CORE_BIO *bio, const void *data, size_t data_len,
+                           size_t *written)
+{
+    if (c_bio_write_ex == NULL)
+        return 0;
+    return c_bio_write_ex(bio, data, data_len, written);
+}
+
+int ossl_prov_bio_free(OSSL_CORE_BIO *bio)
 {
     if (c_bio_free == NULL)
         return 0;
     return c_bio_free(bio);
 }
 
-int ossl_prov_bio_vprintf(BIO *bio, const char *format, va_list ap)
+int ossl_prov_bio_vprintf(OSSL_CORE_BIO *bio, const char *format, va_list ap)
 {
     if (c_bio_vprintf == NULL)
         return -1;
     return c_bio_vprintf(bio, format, ap);
 }
 
-int ossl_prov_bio_printf(BIO *bio, const char *format, ...)
+int ossl_prov_bio_printf(OSSL_CORE_BIO *bio, const char *format, ...)
 {
     va_list ap;
     int ret;
@@ -94,3 +109,90 @@ int ossl_prov_bio_printf(BIO *bio, const char *format, ...)
     return ret;
 }
 
+#ifndef FIPS_MODULE
+
+/* No direct BIO support in the FIPS module */
+
+static int bio_core_read_ex(BIO *bio, char *data, size_t data_len,
+                            size_t *bytes_read)
+{
+    return ossl_prov_bio_read_ex(BIO_get_data(bio), data, data_len, bytes_read);
+}
+
+static int bio_core_write_ex(BIO *bio, const char *data, size_t data_len,
+                             size_t *written)
+{
+    return ossl_prov_bio_write_ex(BIO_get_data(bio), data, data_len, written);
+}
+
+static long bio_core_ctrl(BIO *bio, int cmd, long num, void *ptr)
+{
+    /* We don't support this */
+    assert(0);
+    return 0;
+}
+
+static int bio_core_gets(BIO *bio, char *buf, int size)
+{
+    /* We don't support this */
+    assert(0);
+    return -1;
+}
+
+static int bio_core_puts(BIO *bio, const char *str)
+{
+    /* We don't support this */
+    assert(0);
+    return -1;
+}
+
+static int bio_core_new(BIO *bio)
+{
+    BIO_set_init(bio, 1);
+
+    return 1;
+}
+
+static int bio_core_free(BIO *bio)
+{
+    BIO_set_init(bio, 0);
+
+    return 1;
+}
+
+BIO_METHOD *bio_prov_init_bio_method(void)
+{
+    BIO_METHOD *corebiometh = NULL;
+
+    corebiometh = BIO_meth_new(BIO_TYPE_CORE_TO_PROV, "BIO to Core filter");
+    if (corebiometh == NULL
+            || !BIO_meth_set_write_ex(corebiometh, bio_core_write_ex)
+            || !BIO_meth_set_read_ex(corebiometh, bio_core_read_ex)
+            || !BIO_meth_set_puts(corebiometh, bio_core_puts)
+            || !BIO_meth_set_gets(corebiometh, bio_core_gets)
+            || !BIO_meth_set_ctrl(corebiometh, bio_core_ctrl)
+            || !BIO_meth_set_create(corebiometh, bio_core_new)
+            || !BIO_meth_set_destroy(corebiometh, bio_core_free)) {
+        BIO_meth_free(corebiometh);
+        return NULL;
+    }
+
+    return corebiometh;
+}
+
+BIO *bio_new_from_core_bio(PROV_CTX *provctx, OSSL_CORE_BIO *corebio)
+{
+    BIO *outbio;
+    BIO_METHOD *corebiometh = PROV_CTX_get0_core_bio_method(provctx);
+
+    if (corebiometh == NULL)
+        return NULL;
+
+    outbio = BIO_new(corebiometh);
+    if (outbio != NULL)
+        BIO_set_data(outbio, corebio);
+
+    return outbio;
+}
+
+#endif