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 | if (!c_obj_add_sigid(handle, SIGALG_OID, NULL, SIG_LN))
|
---|
75 | return 0;
|
---|
76 | /* checking wrong digest alg name is rejected: */
|
---|
77 | if (c_obj_add_sigid(handle, SIGALG_OID, "NonsenseAlg", SIG_LN))
|
---|
78 | return 0;
|
---|
79 |
|
---|
80 | return 1;
|
---|
81 | }
|
---|
82 |
|
---|
83 | static int obj_create_test(void)
|
---|
84 | {
|
---|
85 | OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
|
---|
86 | OSSL_PROVIDER *objprov = NULL;
|
---|
87 | int sigalgnid, digestnid, signid;
|
---|
88 | int testresult = 0;
|
---|
89 |
|
---|
90 | if (!TEST_ptr(libctx))
|
---|
91 | goto err;
|
---|
92 |
|
---|
93 | if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "obj-prov",
|
---|
94 | obj_provider_init))
|
---|
95 | || !TEST_ptr(objprov = OSSL_PROVIDER_load(libctx, "obj-prov")))
|
---|
96 | goto err;
|
---|
97 |
|
---|
98 | /* Check that the provider created the OIDs/NIDs we expected */
|
---|
99 | sigalgnid = OBJ_txt2nid(SIGALG_OID);
|
---|
100 | if (!TEST_int_ne(sigalgnid, NID_undef)
|
---|
101 | || !TEST_true(OBJ_find_sigid_algs(sigalgnid, &digestnid, &signid))
|
---|
102 | || !TEST_int_ne(digestnid, NID_undef)
|
---|
103 | || !TEST_int_ne(signid, NID_undef)
|
---|
104 | || !TEST_int_eq(digestnid, OBJ_sn2nid(DIGEST_SN))
|
---|
105 | || !TEST_int_eq(signid, OBJ_ln2nid(SIG_LN)))
|
---|
106 | goto err;
|
---|
107 |
|
---|
108 | testresult = 1;
|
---|
109 | err:
|
---|
110 | OSSL_PROVIDER_unload(objprov);
|
---|
111 | OSSL_LIB_CTX_free(libctx);
|
---|
112 | return testresult;
|
---|
113 | }
|
---|
114 |
|
---|
115 | int setup_tests(void)
|
---|
116 | {
|
---|
117 |
|
---|
118 | ADD_TEST(obj_create_test);
|
---|
119 |
|
---|
120 | return 1;
|
---|
121 | }
|
---|