Fix copyright year issues
[openssl.git] / test / upcallstest.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/objects.h>
11 #include <openssl/crypto.h>
12 #include <openssl/provider.h>
13 #include "testutil.h"
14
15 static const OSSL_ALGORITHM *obj_query(void *provctx, int operation_id,
16                                        int *no_cache)
17 {
18     *no_cache = 0;
19     return NULL;
20 }
21
22 static const OSSL_DISPATCH obj_dispatch_table[] = {
23     { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))obj_query },
24     { 0, NULL }
25 };
26
27 static OSSL_FUNC_core_obj_add_sigid_fn *c_obj_add_sigid = NULL;
28 static OSSL_FUNC_core_obj_create_fn *c_obj_create = NULL;
29
30 #define SIG_OID "1.3.6.1.4.1.16604.998877.1"
31 #define SIG_SN "my-sig"
32 #define SIG_LN "my-sig-long"
33 #define DIGEST_OID "1.3.6.1.4.1.16604.998877.2"
34 #define DIGEST_SN "my-digest"
35 #define DIGEST_LN "my-digest-long"
36 #define SIGALG_OID "1.3.6.1.4.1.16604.998877.3"
37 #define SIGALG_SN "my-sigalg"
38 #define SIGALG_LN "my-sigalg-long"
39
40 static int obj_provider_init(const OSSL_CORE_HANDLE *handle,
41                              const OSSL_DISPATCH *in,
42                              const OSSL_DISPATCH **out,
43                              void **provctx)
44 {
45     *provctx = (void *)handle;
46     *out = obj_dispatch_table;
47
48    for (; in->function_id != 0; in++) {
49         switch (in->function_id) {
50         case OSSL_FUNC_CORE_OBJ_ADD_SIGID:
51             c_obj_add_sigid = OSSL_FUNC_core_obj_add_sigid(in);
52             break;
53         case OSSL_FUNC_CORE_OBJ_CREATE:
54             c_obj_create = OSSL_FUNC_core_obj_create(in);
55             break;
56             break;
57         default:
58             /* Just ignore anything we don't understand */
59             break;
60         }
61     }
62
63     if (!c_obj_create(handle, DIGEST_OID, DIGEST_SN, DIGEST_LN)
64             || !c_obj_create(handle, SIG_OID, SIG_SN, SIG_LN)
65             || !c_obj_create(handle, SIGALG_OID, SIGALG_SN, SIGALG_LN))
66         return 0;
67
68     if (!c_obj_add_sigid(handle, SIGALG_OID, DIGEST_SN, SIG_LN))
69         return 0;
70
71     /* additional tests checking empty digest algs are accepted, too */
72     if (!c_obj_add_sigid(handle, SIGALG_OID, "", SIG_LN))
73         return 0;
74     /* checking wrong digest alg name is rejected: */
75     if (c_obj_add_sigid(handle, SIGALG_OID, "NonsenseAlg", SIG_LN))
76         return 0;
77     /* Testing actual triplet addition under separate sig alg */
78     if (!c_obj_add_sigid(handle, SIG_OID, NULL, SIG_LN))
79         return 0;
80
81     return 1;
82 }
83
84 static int obj_create_test(void)
85 {
86     OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
87     OSSL_PROVIDER *objprov = NULL;
88     int sigalgnid, digestnid, signid;
89     int testresult = 0;
90
91     if (!TEST_ptr(libctx))
92         goto err;
93
94     if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "obj-prov",
95                                              obj_provider_init))
96             || !TEST_ptr(objprov = OSSL_PROVIDER_load(libctx, "obj-prov")))
97         goto err;
98
99     /* Check that the provider created the OIDs/NIDs we expected */
100     sigalgnid = OBJ_txt2nid(SIGALG_OID);
101     if (!TEST_int_ne(sigalgnid, NID_undef)
102             || !TEST_true(OBJ_find_sigid_algs(sigalgnid, &digestnid, &signid))
103             || !TEST_int_ne(digestnid, NID_undef)
104             || !TEST_int_ne(signid, NID_undef)
105             || !TEST_int_eq(digestnid, OBJ_sn2nid(DIGEST_SN))
106             || !TEST_int_eq(signid, OBJ_ln2nid(SIG_LN)))
107         goto err;
108
109     /* Check empty digest alg storage capability */
110     sigalgnid = OBJ_txt2nid(SIG_OID);
111     if (!TEST_int_ne(sigalgnid, NID_undef)
112             || !TEST_true(OBJ_find_sigid_algs(sigalgnid, &digestnid, &signid))
113             || !TEST_int_eq(digestnid, NID_undef)
114             || !TEST_int_ne(signid, NID_undef))
115         goto err;
116
117     testresult = 1;
118  err:
119     OSSL_PROVIDER_unload(objprov);
120     OSSL_LIB_CTX_free(libctx);
121     return testresult;
122 }
123
124 int setup_tests(void)
125 {
126
127     ADD_TEST(obj_create_test);
128
129     return 1;
130 }