PROV BIO: add a BIO_vprintf() upcall, and a provider BIO library
[openssl.git] / providers / common / bio_prov.c
1 /*
2  * Copyright 2019 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 #include <openssl/core_numbers.h>
11 #include "prov/bio.h"
12
13 static OSSL_BIO_new_file_fn *c_bio_new_file = NULL;
14 static OSSL_BIO_new_membuf_fn *c_bio_new_membuf = NULL;
15 static OSSL_BIO_read_ex_fn *c_bio_read_ex = NULL;
16 static OSSL_BIO_free_fn *c_bio_free = NULL;
17 static OSSL_BIO_vprintf_fn *c_bio_vprintf = NULL;
18
19 int ossl_prov_bio_from_dispatch(const OSSL_DISPATCH *fns)
20 {
21     for (; fns->function_id != 0; fns++) {
22         switch (fns->function_id) {
23         case OSSL_FUNC_BIO_NEW_FILE:
24             if (c_bio_new_file == NULL)
25                 c_bio_new_file = OSSL_get_BIO_new_file(fns);
26             break;
27         case OSSL_FUNC_BIO_NEW_MEMBUF:
28             if (c_bio_new_membuf == NULL)
29                 c_bio_new_membuf = OSSL_get_BIO_new_membuf(fns);
30             break;
31         case OSSL_FUNC_BIO_READ_EX:
32             if (c_bio_read_ex == NULL)
33                 c_bio_read_ex = OSSL_get_BIO_read_ex(fns);
34             break;
35         case OSSL_FUNC_BIO_FREE:
36             if (c_bio_free == NULL)
37                 c_bio_free = OSSL_get_BIO_free(fns);
38             break;
39         case OSSL_FUNC_BIO_VPRINTF:
40             if (c_bio_vprintf == NULL)
41                 c_bio_vprintf = OSSL_get_BIO_vprintf(fns);
42             break;
43         }
44     }
45
46     return 1;
47 }
48
49 BIO *ossl_prov_bio_new_file(const char *filename, const char *mode)
50 {
51     if (c_bio_new_file == NULL)
52         return NULL;
53     return c_bio_new_file(filename, mode);
54 }
55
56 BIO *ossl_prov_bio_new_membuf(const char *filename, int len)
57 {
58     if (c_bio_new_membuf == NULL)
59         return NULL;
60     return c_bio_new_membuf(filename, len);
61 }
62
63 int ossl_prov_bio_read_ex(BIO *bio, void *data, size_t data_len,
64                           size_t *bytes_read)
65 {
66     if (c_bio_read_ex == NULL)
67         return 0;
68     return c_bio_read_ex(bio, data, data_len, bytes_read);
69 }
70
71 int ossl_prov_bio_free(BIO *bio)
72 {
73     if (c_bio_free == NULL)
74         return 0;
75     return c_bio_free(bio);
76 }
77
78 int ossl_prov_bio_vprintf(BIO *bio, const char *format, va_list ap)
79 {
80     if (c_bio_vprintf == NULL)
81         return -1;
82     return c_bio_vprintf(bio, format, ap);
83 }
84
85 int ossl_prov_bio_printf(BIO *bio, const char *format, ...)
86 {
87     va_list ap;
88     int ret;
89
90     va_start(ap, format);
91     ret = ossl_prov_bio_vprintf(bio, format, ap);
92     va_end(ap);
93
94     return ret;
95 }
96