]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
605856d7 | 2 | * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved. |
d2027098 | 3 | * |
3f870de7 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
62867571 RS |
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 | |
d2027098 DSH |
8 | */ |
9 | ||
10 | #include <openssl/objects.h> | |
11 | #include "obj_xref.h" | |
677963e5 | 12 | #include "internal/nelem.h" |
cdb10bae | 13 | #include <openssl/err.h> |
d2027098 | 14 | |
45ebd731 | 15 | static STACK_OF(nid_triple) *sig_app, *sigx_app; |
0ee2166c | 16 | |
e19106f5 | 17 | static int sig_cmp(const nid_triple *a, const nid_triple *b) |
0f113f3e MC |
18 | { |
19 | return a->sign_id - b->sign_id; | |
20 | } | |
d2027098 | 21 | |
e19106f5 DSH |
22 | DECLARE_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig); |
23 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig); | |
babb3798 | 24 | |
0f113f3e MC |
25 | static int sig_sk_cmp(const nid_triple *const *a, const nid_triple *const *b) |
26 | { | |
27 | return (*a)->sign_id - (*b)->sign_id; | |
28 | } | |
0ee2166c | 29 | |
e19106f5 | 30 | DECLARE_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx); |
babb3798 | 31 | |
0f113f3e MC |
32 | static int sigx_cmp(const nid_triple *const *a, const nid_triple *const *b) |
33 | { | |
34 | int ret; | |
35 | ret = (*a)->hash_id - (*b)->hash_id; | |
36 | if (ret) | |
37 | return ret; | |
38 | return (*a)->pkey_id - (*b)->pkey_id; | |
39 | } | |
d2027098 | 40 | |
e19106f5 | 41 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx); |
d2027098 DSH |
42 | |
43 | int OBJ_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid) | |
0f113f3e MC |
44 | { |
45 | nid_triple tmp; | |
46 | const nid_triple *rv = NULL; | |
47 | tmp.sign_id = signid; | |
48 | ||
5b37fef0 | 49 | if (sig_app != NULL) { |
0f113f3e | 50 | int idx = sk_nid_triple_find(sig_app, &tmp); |
5b37fef0 | 51 | rv = sk_nid_triple_value(sig_app, idx); |
0f113f3e | 52 | } |
0ee2166c | 53 | #ifndef OBJ_XREF_TEST2 |
0f113f3e | 54 | if (rv == NULL) { |
b6eb9827 | 55 | rv = OBJ_bsearch_sig(&tmp, sigoid_srt, OSSL_NELEM(sigoid_srt)); |
0f113f3e | 56 | } |
0ee2166c | 57 | #endif |
0f113f3e MC |
58 | if (rv == NULL) |
59 | return 0; | |
60 | if (pdig_nid) | |
61 | *pdig_nid = rv->hash_id; | |
62 | if (ppkey_nid) | |
63 | *ppkey_nid = rv->pkey_id; | |
64 | return 1; | |
65 | } | |
d2027098 DSH |
66 | |
67 | int OBJ_find_sigid_by_algs(int *psignid, int dig_nid, int pkey_nid) | |
0f113f3e MC |
68 | { |
69 | nid_triple tmp; | |
70 | const nid_triple *t = &tmp; | |
71 | const nid_triple **rv = NULL; | |
72 | ||
73 | tmp.hash_id = dig_nid; | |
74 | tmp.pkey_id = pkey_nid; | |
75 | ||
76 | if (sigx_app) { | |
77 | int idx = sk_nid_triple_find(sigx_app, &tmp); | |
78 | if (idx >= 0) { | |
79 | t = sk_nid_triple_value(sigx_app, idx); | |
80 | rv = &t; | |
81 | } | |
82 | } | |
0ee2166c | 83 | #ifndef OBJ_XREF_TEST2 |
0f113f3e | 84 | if (rv == NULL) { |
b6eb9827 | 85 | rv = OBJ_bsearch_sigx(&t, sigoid_srt_xref, OSSL_NELEM(sigoid_srt_xref)); |
0f113f3e | 86 | } |
0ee2166c | 87 | #endif |
0f113f3e MC |
88 | if (rv == NULL) |
89 | return 0; | |
90 | if (psignid) | |
91 | *psignid = (*rv)->sign_id; | |
92 | return 1; | |
93 | } | |
d2027098 | 94 | |
0ee2166c | 95 | int OBJ_add_sigid(int signid, int dig_id, int pkey_id) |
0f113f3e MC |
96 | { |
97 | nid_triple *ntr; | |
90945fa3 | 98 | if (sig_app == NULL) |
0f113f3e | 99 | sig_app = sk_nid_triple_new(sig_sk_cmp); |
90945fa3 | 100 | if (sig_app == NULL) |
0f113f3e | 101 | return 0; |
90945fa3 | 102 | if (sigx_app == NULL) |
0f113f3e | 103 | sigx_app = sk_nid_triple_new(sigx_cmp); |
90945fa3 | 104 | if (sigx_app == NULL) |
0f113f3e | 105 | return 0; |
cdb10bae | 106 | if ((ntr = OPENSSL_malloc(sizeof(*ntr))) == NULL) { |
9311d0c4 | 107 | ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE); |
0f113f3e | 108 | return 0; |
cdb10bae | 109 | } |
0f113f3e MC |
110 | ntr->sign_id = signid; |
111 | ntr->hash_id = dig_id; | |
112 | ntr->pkey_id = pkey_id; | |
113 | ||
114 | if (!sk_nid_triple_push(sig_app, ntr)) { | |
115 | OPENSSL_free(ntr); | |
116 | return 0; | |
117 | } | |
118 | ||
119 | if (!sk_nid_triple_push(sigx_app, ntr)) | |
120 | return 0; | |
121 | ||
122 | sk_nid_triple_sort(sig_app); | |
123 | sk_nid_triple_sort(sigx_app); | |
124 | ||
125 | return 1; | |
126 | } | |
0ee2166c | 127 | |
5ce278a7 | 128 | static void sid_free(nid_triple *tt) |
0f113f3e MC |
129 | { |
130 | OPENSSL_free(tt); | |
131 | } | |
0ee2166c DSH |
132 | |
133 | void OBJ_sigid_free(void) | |
0f113f3e | 134 | { |
efa7dd64 RS |
135 | sk_nid_triple_pop_free(sig_app, sid_free); |
136 | sig_app = NULL; | |
137 | sk_nid_triple_free(sigx_app); | |
138 | sigx_app = NULL; | |
0f113f3e | 139 | } |