[331] | 1 | /*
|
---|
| 2 | * Copyright 2006-2016 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 "e_os.h"
|
---|
| 13 |
|
---|
| 14 | static STACK_OF(nid_triple) *sig_app, *sigx_app;
|
---|
| 15 |
|
---|
| 16 | static int sig_cmp(const nid_triple *a, const nid_triple *b)
|
---|
| 17 | {
|
---|
| 18 | return a->sign_id - b->sign_id;
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | DECLARE_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig);
|
---|
| 22 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(nid_triple, nid_triple, sig);
|
---|
| 23 |
|
---|
| 24 | static int sig_sk_cmp(const nid_triple *const *a, const nid_triple *const *b)
|
---|
| 25 | {
|
---|
| 26 | return (*a)->sign_id - (*b)->sign_id;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | DECLARE_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx);
|
---|
| 30 |
|
---|
| 31 | static int sigx_cmp(const nid_triple *const *a, const nid_triple *const *b)
|
---|
| 32 | {
|
---|
| 33 | int ret;
|
---|
| 34 | ret = (*a)->hash_id - (*b)->hash_id;
|
---|
| 35 | if (ret)
|
---|
| 36 | return ret;
|
---|
| 37 | return (*a)->pkey_id - (*b)->pkey_id;
|
---|
| 38 | }
|
---|
| 39 |
|
---|
| 40 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const nid_triple *, const nid_triple *, sigx);
|
---|
| 41 |
|
---|
| 42 | int OBJ_find_sigid_algs(int signid, int *pdig_nid, int *ppkey_nid)
|
---|
| 43 | {
|
---|
| 44 | nid_triple tmp;
|
---|
| 45 | const nid_triple *rv = NULL;
|
---|
| 46 | tmp.sign_id = signid;
|
---|
| 47 |
|
---|
| 48 | if (sig_app) {
|
---|
| 49 | int idx = sk_nid_triple_find(sig_app, &tmp);
|
---|
| 50 | if (idx >= 0)
|
---|
| 51 | rv = sk_nid_triple_value(sig_app, idx);
|
---|
| 52 | }
|
---|
| 53 | #ifndef OBJ_XREF_TEST2
|
---|
| 54 | if (rv == NULL) {
|
---|
| 55 | rv = OBJ_bsearch_sig(&tmp, sigoid_srt, OSSL_NELEM(sigoid_srt));
|
---|
| 56 | }
|
---|
| 57 | #endif
|
---|
| 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 | }
|
---|
| 66 |
|
---|
| 67 | int OBJ_find_sigid_by_algs(int *psignid, int dig_nid, int pkey_nid)
|
---|
| 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 | }
|
---|
| 83 | #ifndef OBJ_XREF_TEST2
|
---|
| 84 | if (rv == NULL) {
|
---|
| 85 | rv = OBJ_bsearch_sigx(&t, sigoid_srt_xref, OSSL_NELEM(sigoid_srt_xref));
|
---|
| 86 | }
|
---|
| 87 | #endif
|
---|
| 88 | if (rv == NULL)
|
---|
| 89 | return 0;
|
---|
| 90 | if (psignid)
|
---|
| 91 | *psignid = (*rv)->sign_id;
|
---|
| 92 | return 1;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | int OBJ_add_sigid(int signid, int dig_id, int pkey_id)
|
---|
| 96 | {
|
---|
| 97 | nid_triple *ntr;
|
---|
| 98 | if (sig_app == NULL)
|
---|
| 99 | sig_app = sk_nid_triple_new(sig_sk_cmp);
|
---|
| 100 | if (sig_app == NULL)
|
---|
| 101 | return 0;
|
---|
| 102 | if (sigx_app == NULL)
|
---|
| 103 | sigx_app = sk_nid_triple_new(sigx_cmp);
|
---|
| 104 | if (sigx_app == NULL)
|
---|
| 105 | return 0;
|
---|
| 106 | ntr = OPENSSL_malloc(sizeof(*ntr));
|
---|
| 107 | if (ntr == NULL)
|
---|
| 108 | return 0;
|
---|
| 109 | ntr->sign_id = signid;
|
---|
| 110 | ntr->hash_id = dig_id;
|
---|
| 111 | ntr->pkey_id = pkey_id;
|
---|
| 112 |
|
---|
| 113 | if (!sk_nid_triple_push(sig_app, ntr)) {
|
---|
| 114 | OPENSSL_free(ntr);
|
---|
| 115 | return 0;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | if (!sk_nid_triple_push(sigx_app, ntr))
|
---|
| 119 | return 0;
|
---|
| 120 |
|
---|
| 121 | sk_nid_triple_sort(sig_app);
|
---|
| 122 | sk_nid_triple_sort(sigx_app);
|
---|
| 123 |
|
---|
| 124 | return 1;
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | static void sid_free(nid_triple *tt)
|
---|
| 128 | {
|
---|
| 129 | OPENSSL_free(tt);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | void OBJ_sigid_free(void)
|
---|
| 133 | {
|
---|
| 134 | sk_nid_triple_pop_free(sig_app, sid_free);
|
---|
| 135 | sig_app = NULL;
|
---|
| 136 | sk_nid_triple_free(sigx_app);
|
---|
| 137 | sigx_app = NULL;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | #ifdef OBJ_XREF_TEST
|
---|
| 141 |
|
---|
| 142 | main()
|
---|
| 143 | {
|
---|
| 144 | int n1, n2, n3;
|
---|
| 145 |
|
---|
| 146 | int i, rv;
|
---|
| 147 | # ifdef OBJ_XREF_TEST2
|
---|
| 148 | for (i = 0; i < OSSL_NELEM(sigoid_srt); i++) {
|
---|
| 149 | OBJ_add_sigid(sigoid_srt[i][0], sigoid_srt[i][1], sigoid_srt[i][2]);
|
---|
| 150 | }
|
---|
| 151 | # endif
|
---|
| 152 |
|
---|
| 153 | for (i = 0; i < OSSL_NELEM(sigoid_srt); i++) {
|
---|
| 154 | n1 = sigoid_srt[i][0];
|
---|
| 155 | rv = OBJ_find_sigid_algs(n1, &n2, &n3);
|
---|
| 156 | printf("Forward: %d, %s %s %s\n", rv,
|
---|
| 157 | OBJ_nid2ln(n1), OBJ_nid2ln(n2), OBJ_nid2ln(n3));
|
---|
| 158 | n1 = 0;
|
---|
| 159 | rv = OBJ_find_sigid_by_algs(&n1, n2, n3);
|
---|
| 160 | printf("Reverse: %d, %s %s %s\n", rv,
|
---|
| 161 | OBJ_nid2ln(n1), OBJ_nid2ln(n2), OBJ_nid2ln(n3));
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | #endif
|
---|