xref: /openssl/test/upcallstest.c (revision da1c088f)
1 /*
2  * Copyright 2021-2023 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 
obj_query(void * provctx,int operation_id,int * no_cache)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     OSSL_DISPATCH_END
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 /* test signature ids requiring digest */
31 #define SIG_OID "1.3.6.1.4.1.16604.998877.1"
32 #define SIG_SN "my-sig"
33 #define SIG_LN "my-sig-long"
34 #define DIGEST_OID "1.3.6.1.4.1.16604.998877.2"
35 #define DIGEST_SN "my-digest"
36 #define DIGEST_LN "my-digest-long"
37 #define SIGALG_OID "1.3.6.1.4.1.16604.998877.3"
38 #define SIGALG_SN "my-sigalg"
39 #define SIGALG_LN "my-sigalg-long"
40 
41 /* test signature ids requiring no digest */
42 #define NODIG_SIG_OID "1.3.6.1.4.1.16604.998877.4"
43 #define NODIG_SIG_SN "my-nodig-sig"
44 #define NODIG_SIG_LN "my-nodig-sig-long"
45 #define NODIG_SIGALG_OID "1.3.6.1.4.1.16604.998877.5"
46 #define NODIG_SIGALG_SN "my-nodig-sigalg"
47 #define NODIG_SIGALG_LN "my-nodig-sigalg-long"
48 
obj_provider_init(const OSSL_CORE_HANDLE * handle,const OSSL_DISPATCH * in,const OSSL_DISPATCH ** out,void ** provctx)49 static int obj_provider_init(const OSSL_CORE_HANDLE *handle,
50                              const OSSL_DISPATCH *in,
51                              const OSSL_DISPATCH **out,
52                              void **provctx)
53 {
54     *provctx = (void *)handle;
55     *out = obj_dispatch_table;
56 
57     for (; in->function_id != 0; in++) {
58         switch (in->function_id) {
59         case OSSL_FUNC_CORE_OBJ_ADD_SIGID:
60             c_obj_add_sigid = OSSL_FUNC_core_obj_add_sigid(in);
61             break;
62         case OSSL_FUNC_CORE_OBJ_CREATE:
63             c_obj_create = OSSL_FUNC_core_obj_create(in);
64             break;
65             break;
66         default:
67             /* Just ignore anything we don't understand */
68             break;
69         }
70     }
71 
72     if (!c_obj_create(handle, DIGEST_OID, DIGEST_SN, DIGEST_LN)
73             || !c_obj_create(handle, SIG_OID, SIG_SN, SIG_LN)
74             || !c_obj_create(handle, SIGALG_OID, SIGALG_SN, SIGALG_LN))
75         return 0;
76 
77     if (!c_obj_create(handle, NODIG_SIG_OID, NODIG_SIG_SN, NODIG_SIG_LN)
78             || !c_obj_create(handle, NODIG_SIGALG_OID, NODIG_SIGALG_SN, NODIG_SIGALG_LN))
79         return 0;
80 
81     if (!c_obj_add_sigid(handle, SIGALG_OID, DIGEST_SN, SIG_LN))
82         return 0;
83 
84     /* additional tests checking empty digest algs are accepted, too */
85     if (!c_obj_add_sigid(handle, NODIG_SIGALG_OID, "", NODIG_SIG_LN))
86         return 0;
87     /* checking wrong digest alg name is rejected: */
88     if (c_obj_add_sigid(handle, NODIG_SIGALG_OID, "NonsenseAlg", NODIG_SIG_LN))
89         return 0;
90 
91     return 1;
92 }
93 
obj_create_test(void)94 static int obj_create_test(void)
95 {
96     OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
97     OSSL_PROVIDER *objprov = NULL;
98     int sigalgnid, digestnid, signid, foundsid;
99     int testresult = 0;
100 
101     if (!TEST_ptr(libctx))
102         goto err;
103 
104     if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "obj-prov",
105                                              obj_provider_init))
106             || !TEST_ptr(objprov = OSSL_PROVIDER_load(libctx, "obj-prov")))
107         goto err;
108 
109     /* Check that the provider created the OIDs/NIDs we expected */
110     sigalgnid = OBJ_txt2nid(SIGALG_OID);
111     if (!TEST_int_ne(sigalgnid, NID_undef)
112             || !TEST_true(OBJ_find_sigid_algs(sigalgnid, &digestnid, &signid))
113             || !TEST_int_ne(digestnid, NID_undef)
114             || !TEST_int_ne(signid, NID_undef)
115             || !TEST_int_eq(digestnid, OBJ_sn2nid(DIGEST_SN))
116             || !TEST_int_eq(signid, OBJ_ln2nid(SIG_LN)))
117         goto err;
118 
119     /* Check empty digest alg storage capability */
120     sigalgnid = OBJ_txt2nid(NODIG_SIGALG_OID);
121     if (!TEST_int_ne(sigalgnid, NID_undef)
122             || !TEST_true(OBJ_find_sigid_algs(sigalgnid, &digestnid, &signid))
123             || !TEST_int_eq(digestnid, NID_undef)
124             || !TEST_int_ne(signid, NID_undef))
125         goto err;
126 
127     /* Testing OBJ_find_sigid_by_algs */
128     /* First check exact sig/digest recall: */
129     sigalgnid = OBJ_sn2nid(SIGALG_SN);
130     digestnid = OBJ_sn2nid(DIGEST_SN);
131     signid = OBJ_ln2nid(SIG_LN);
132     if ((!OBJ_find_sigid_by_algs(&foundsid, digestnid, signid)) ||
133         (foundsid != sigalgnid))
134         return 0;
135     /* Check wrong signature/digest combination is rejected */
136     if ((OBJ_find_sigid_by_algs(&foundsid, OBJ_sn2nid("SHA512"), signid)) &&
137         (foundsid == sigalgnid))
138         return 0;
139     /* Now also check signature not needing digest is found */
140     /* a) when some digest is given */
141     sigalgnid = OBJ_sn2nid(NODIG_SIGALG_SN);
142     digestnid = OBJ_sn2nid("SHA512");
143     signid = OBJ_ln2nid(NODIG_SIG_LN);
144     if ((!OBJ_find_sigid_by_algs(&foundsid, digestnid, signid)) ||
145         (foundsid != sigalgnid))
146         return 0;
147     /* b) when NID_undef is passed */
148     digestnid = NID_undef;
149     if ((!OBJ_find_sigid_by_algs(&foundsid, digestnid, signid)) ||
150         (foundsid != sigalgnid))
151         return 0;
152 
153     testresult = 1;
154  err:
155     OSSL_PROVIDER_unload(objprov);
156     OSSL_LIB_CTX_free(libctx);
157     return testresult;
158 }
159 
setup_tests(void)160 int setup_tests(void)
161 {
162 
163     ADD_TEST(obj_create_test);
164 
165     return 1;
166 }
167