Update copyright year
[openssl.git] / crypto / objects / obj_xref.c
1 /*
2  * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (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 "obj_xref.h"
12 #include "internal/nelem.h"
13 #include <openssl/err.h>
14
15 static STACK_OF(nid_triple) *sig_app, *sigx_app;
16
17 static int sig_cmp(const nid_triple *a, const nid_triple *b)
18 {
19     return a->sign_id - b->sign_id;
20 }
21
22 DECLARE_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig);
23 IMPLEMENT_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig);
24
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 }
29
30 DECLARE_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx);
31
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 }
40
41 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx);
42
43 int OBJ_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid)
44 {
45     nid_triple tmp;
46     const nid_triple *rv = NULL;
47     tmp.sign_id = signid;
48
49     if (sig_app) {
50         int idx = sk_nid_triple_find(sig_app, &tmp);
51         if (idx >= 0)
52             rv = sk_nid_triple_value(sig_app, idx);
53     }
54 #ifndef OBJ_XREF_TEST2
55     if (rv == NULL) {
56         rv = OBJ_bsearch_sig(&tmp, sigoid_srt, OSSL_NELEM(sigoid_srt));
57     }
58 #endif
59     if (rv == NULL)
60         return 0;
61     if (pdig_nid)
62         *pdig_nid = rv->hash_id;
63     if (ppkey_nid)
64         *ppkey_nid = rv->pkey_id;
65     return 1;
66 }
67
68 int OBJ_find_sigid_by_algs(int *psignid, int dig_nid, int pkey_nid)
69 {
70     nid_triple tmp;
71     const nid_triple *t = &tmp;
72     const nid_triple **rv = NULL;
73
74     tmp.hash_id = dig_nid;
75     tmp.pkey_id = pkey_nid;
76
77     if (sigx_app) {
78         int idx = sk_nid_triple_find(sigx_app, &tmp);
79         if (idx >= 0) {
80             t = sk_nid_triple_value(sigx_app, idx);
81             rv = &t;
82         }
83     }
84 #ifndef OBJ_XREF_TEST2
85     if (rv == NULL) {
86         rv = OBJ_bsearch_sigx(&t, sigoid_srt_xref, OSSL_NELEM(sigoid_srt_xref));
87     }
88 #endif
89     if (rv == NULL)
90         return 0;
91     if (psignid)
92         *psignid = (*rv)->sign_id;
93     return 1;
94 }
95
96 int OBJ_add_sigid(int signid, int dig_id, int pkey_id)
97 {
98     nid_triple *ntr;
99     if (sig_app == NULL)
100         sig_app = sk_nid_triple_new(sig_sk_cmp);
101     if (sig_app == NULL)
102         return 0;
103     if (sigx_app == NULL)
104         sigx_app = sk_nid_triple_new(sigx_cmp);
105     if (sigx_app == NULL)
106         return 0;
107     if ((ntr = OPENSSL_malloc(sizeof(*ntr))) == NULL) {
108         OBJerr(OBJ_F_OBJ_ADD_SIGID, ERR_R_MALLOC_FAILURE);
109         return 0;
110     }
111     ntr->sign_id = signid;
112     ntr->hash_id = dig_id;
113     ntr->pkey_id = pkey_id;
114
115     if (!sk_nid_triple_push(sig_app, ntr)) {
116         OPENSSL_free(ntr);
117         return 0;
118     }
119
120     if (!sk_nid_triple_push(sigx_app, ntr))
121         return 0;
122
123     sk_nid_triple_sort(sig_app);
124     sk_nid_triple_sort(sigx_app);
125
126     return 1;
127 }
128
129 static void sid_free(nid_triple *tt)
130 {
131     OPENSSL_free(tt);
132 }
133
134 void OBJ_sigid_free(void)
135 {
136     sk_nid_triple_pop_free(sig_app, sid_free);
137     sig_app = NULL;
138     sk_nid_triple_free(sigx_app);
139     sigx_app = NULL;
140 }