Add a test for fetching various non-evp objects
[openssl.git] / test / provfetchtest.c
1 /*
2  * Copyright 2021 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/crypto.h>
11 #include <openssl/provider.h>
12 #include <openssl/decoder.h>
13 #include <openssl/encoder.h>
14 #include <openssl/store.h>
15 #include "testutil.h"
16
17 static int dummy_decoder_decode(void *ctx, OSSL_CORE_BIO *cin, int selection,
18                                 OSSL_CALLBACK *object_cb, void *object_cbarg,
19                                 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
20 {
21     return 0;
22 }
23
24 static const OSSL_DISPATCH dummy_decoder_functions[] = {
25     { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_decoder_decode },
26     { 0, NULL }
27 };
28
29 static const OSSL_ALGORITHM dummy_decoders[] = {
30     { "DUMMY", "provider=dummy,input=pem", dummy_decoder_functions },
31     { NULL, NULL, NULL }
32 };
33
34 static int dummy_encoder_encode(void *ctx, OSSL_CORE_BIO *out,
35                                 const void *obj_raw,
36                                 const OSSL_PARAM obj_abstract[], int selection,
37                                 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
38 {
39     return 0;
40 }
41
42 static const OSSL_DISPATCH dummy_encoder_functions[] = {
43     { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_encoder_encode },
44     { 0, NULL }
45 };
46
47 static const OSSL_ALGORITHM dummy_encoders[] = {
48     { "DUMMY", "provider=dummy,output=pem", dummy_encoder_functions },
49     { NULL, NULL, NULL }
50 };
51
52 static void *dummy_store_open(void *provctx, const char *uri)
53 {
54     return NULL;
55 }
56
57 static int dummy_store_load(void *loaderctx,  OSSL_CALLBACK *object_cb,
58                             void *object_cbarg, OSSL_PASSPHRASE_CALLBACK *pw_cb,
59                             void *pw_cbarg)
60 {
61     return 0;
62 }
63
64 static int dumm_store_eof(void *loaderctx)
65 {
66     return 0;
67 }
68
69 static int dummy_store_close(void *loaderctx)
70 {
71     return 0;
72 }
73
74 static const OSSL_DISPATCH dummy_store_functions[] = {
75     { OSSL_FUNC_STORE_OPEN, (void (*)(void))dummy_store_open },
76     { OSSL_FUNC_STORE_LOAD, (void (*)(void))dummy_store_load },
77     { OSSL_FUNC_STORE_EOF, (void (*)(void))dumm_store_eof },
78     { OSSL_FUNC_STORE_CLOSE, (void (*)(void))dummy_store_close },
79     { 0, NULL }
80 };
81
82 static const OSSL_ALGORITHM dummy_store[] = {
83     { "DUMMY", "provider=dummy", dummy_store_functions },
84     { NULL, NULL, NULL }
85 };
86
87 static const OSSL_ALGORITHM *dummy_query(void *provctx, int operation_id,
88                                          int *no_cache)
89 {
90     *no_cache = 0;
91     switch (operation_id) {
92     case OSSL_OP_DECODER:
93         return dummy_decoders;
94     case OSSL_OP_ENCODER:
95         return dummy_encoders;
96     case OSSL_OP_STORE:
97         return dummy_store;
98     }
99     return NULL;
100 }
101
102 static const OSSL_DISPATCH dummy_dispatch_table[] = {
103     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))dummy_query },
104     { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free },
105     { 0, NULL }
106 };
107
108 static int dummy_provider_init(const OSSL_CORE_HANDLE *handle,
109                                const OSSL_DISPATCH *in,
110                                const OSSL_DISPATCH **out,
111                                void **provctx)
112 {
113     OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_child(handle, in);
114
115     *provctx = (void *)libctx;
116     *out = dummy_dispatch_table;
117
118     return 1;
119 }
120
121 /*
122  * Try fetching and freeing various things.
123  * Test 0: Decoder
124  * Test 1: Encoder
125  * Test 2: Store loader
126  */
127 static int fetch_test(int tst)
128 {
129     OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
130     OSSL_PROVIDER *dummyprov = NULL;
131     OSSL_DECODER *decoder = NULL;
132     OSSL_ENCODER *encoder = NULL;
133     OSSL_STORE_LOADER *loader = NULL;
134     int testresult = 0;
135
136     if (!TEST_ptr(libctx))
137         goto err;
138
139     if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "dummy-prov",
140                                              dummy_provider_init))
141             || !TEST_ptr(dummyprov = OSSL_PROVIDER_load(libctx, "dummy-prov")))
142         goto err;
143
144     switch(tst) {
145     case 0:
146         decoder = OSSL_DECODER_fetch(libctx, "DUMMY", NULL);
147         if (!TEST_ptr(decoder))
148             goto err;
149         break;
150     case 1:
151         encoder = OSSL_ENCODER_fetch(libctx, "DUMMY", NULL);
152         if (!TEST_ptr(encoder))
153             goto err;
154         break;
155     case 2:
156         loader = OSSL_STORE_LOADER_fetch(libctx, "DUMMY", NULL);
157         if (!TEST_ptr(loader))
158             goto err;
159         break;
160     default:
161         goto err;
162     }
163
164     testresult = 1;
165  err:
166     OSSL_DECODER_free(decoder);
167     OSSL_ENCODER_free(encoder);
168     OSSL_STORE_LOADER_free(loader);
169     OSSL_PROVIDER_unload(dummyprov);
170     OSSL_LIB_CTX_free(libctx);
171     return testresult;
172 }
173
174 int setup_tests(void)
175 {
176     ADD_ALL_TESTS(fetch_test, 3);
177
178     return 1;
179 }