diff options
Diffstat (limited to 'src/lib/libcrypto/engine/tb_asnmth.c')
| -rw-r--r-- | src/lib/libcrypto/engine/tb_asnmth.c | 167 |
1 files changed, 86 insertions, 81 deletions
diff --git a/src/lib/libcrypto/engine/tb_asnmth.c b/src/lib/libcrypto/engine/tb_asnmth.c index 75090339f7..bd62f292c9 100644 --- a/src/lib/libcrypto/engine/tb_asnmth.c +++ b/src/lib/libcrypto/engine/tb_asnmth.c | |||
| @@ -6,7 +6,7 @@ | |||
| 6 | * are met: | 6 | * are met: |
| 7 | * | 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright | 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. | 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * | 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright | 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in | 12 | * notice, this list of conditions and the following disclaimer in |
| @@ -64,109 +64,115 @@ | |||
| 64 | 64 | ||
| 65 | static ENGINE_TABLE *pkey_asn1_meth_table = NULL; | 65 | static ENGINE_TABLE *pkey_asn1_meth_table = NULL; |
| 66 | 66 | ||
| 67 | void ENGINE_unregister_pkey_asn1_meths(ENGINE *e) | 67 | void |
| 68 | { | 68 | ENGINE_unregister_pkey_asn1_meths(ENGINE *e) |
| 69 | { | ||
| 69 | engine_table_unregister(&pkey_asn1_meth_table, e); | 70 | engine_table_unregister(&pkey_asn1_meth_table, e); |
| 70 | } | 71 | } |
| 71 | 72 | ||
| 72 | static void engine_unregister_all_pkey_asn1_meths(void) | 73 | static void |
| 73 | { | 74 | engine_unregister_all_pkey_asn1_meths(void) |
| 75 | { | ||
| 74 | engine_table_cleanup(&pkey_asn1_meth_table); | 76 | engine_table_cleanup(&pkey_asn1_meth_table); |
| 75 | } | 77 | } |
| 76 | 78 | ||
| 77 | int ENGINE_register_pkey_asn1_meths(ENGINE *e) | 79 | int |
| 78 | { | 80 | ENGINE_register_pkey_asn1_meths(ENGINE *e) |
| 79 | if(e->pkey_asn1_meths) | 81 | { |
| 80 | { | 82 | if (e->pkey_asn1_meths) { |
| 81 | const int *nids; | 83 | const int *nids; |
| 82 | int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0); | 84 | int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0); |
| 83 | if(num_nids > 0) | 85 | if (num_nids > 0) |
| 84 | return engine_table_register(&pkey_asn1_meth_table, | 86 | return engine_table_register(&pkey_asn1_meth_table, |
| 85 | engine_unregister_all_pkey_asn1_meths, e, nids, | 87 | engine_unregister_all_pkey_asn1_meths, e, nids, |
| 86 | num_nids, 0); | 88 | num_nids, 0); |
| 87 | } | ||
| 88 | return 1; | ||
| 89 | } | 89 | } |
| 90 | return 1; | ||
| 91 | } | ||
| 90 | 92 | ||
| 91 | void ENGINE_register_all_pkey_asn1_meths(void) | 93 | void |
| 92 | { | 94 | ENGINE_register_all_pkey_asn1_meths(void) |
| 95 | { | ||
| 93 | ENGINE *e; | 96 | ENGINE *e; |
| 94 | 97 | ||
| 95 | for(e=ENGINE_get_first() ; e ; e=ENGINE_get_next(e)) | 98 | for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e)) |
| 96 | ENGINE_register_pkey_asn1_meths(e); | 99 | ENGINE_register_pkey_asn1_meths(e); |
| 97 | } | 100 | } |
| 98 | 101 | ||
| 99 | int ENGINE_set_default_pkey_asn1_meths(ENGINE *e) | 102 | int |
| 100 | { | 103 | ENGINE_set_default_pkey_asn1_meths(ENGINE *e) |
| 101 | if(e->pkey_asn1_meths) | 104 | { |
| 102 | { | 105 | if (e->pkey_asn1_meths) { |
| 103 | const int *nids; | 106 | const int *nids; |
| 104 | int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0); | 107 | int num_nids = e->pkey_asn1_meths(e, NULL, &nids, 0); |
| 105 | if(num_nids > 0) | 108 | if (num_nids > 0) |
| 106 | return engine_table_register(&pkey_asn1_meth_table, | 109 | return engine_table_register(&pkey_asn1_meth_table, |
| 107 | engine_unregister_all_pkey_asn1_meths, e, nids, | 110 | engine_unregister_all_pkey_asn1_meths, e, nids, |
| 108 | num_nids, 1); | 111 | num_nids, 1); |
| 109 | } | ||
| 110 | return 1; | ||
| 111 | } | 112 | } |
| 113 | return 1; | ||
| 114 | } | ||
| 112 | 115 | ||
| 113 | /* Exposed API function to get a functional reference from the implementation | 116 | /* Exposed API function to get a functional reference from the implementation |
| 114 | * table (ie. try to get a functional reference from the tabled structural | 117 | * table (ie. try to get a functional reference from the tabled structural |
| 115 | * references) for a given pkey_asn1_meth 'nid' */ | 118 | * references) for a given pkey_asn1_meth 'nid' */ |
| 116 | ENGINE *ENGINE_get_pkey_asn1_meth_engine(int nid) | 119 | ENGINE * |
| 117 | { | 120 | ENGINE_get_pkey_asn1_meth_engine(int nid) |
| 121 | { | ||
| 118 | return engine_table_select(&pkey_asn1_meth_table, nid); | 122 | return engine_table_select(&pkey_asn1_meth_table, nid); |
| 119 | } | 123 | } |
| 120 | 124 | ||
| 121 | /* Obtains a pkey_asn1_meth implementation from an ENGINE functional reference */ | 125 | /* Obtains a pkey_asn1_meth implementation from an ENGINE functional reference */ |
| 122 | const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid) | 126 | const EVP_PKEY_ASN1_METHOD * |
| 123 | { | 127 | ENGINE_get_pkey_asn1_meth(ENGINE *e, int nid) |
| 128 | { | ||
| 124 | EVP_PKEY_ASN1_METHOD *ret; | 129 | EVP_PKEY_ASN1_METHOD *ret; |
| 125 | ENGINE_PKEY_ASN1_METHS_PTR fn = ENGINE_get_pkey_asn1_meths(e); | 130 | ENGINE_PKEY_ASN1_METHS_PTR fn = ENGINE_get_pkey_asn1_meths(e); |
| 126 | if(!fn || !fn(e, &ret, NULL, nid)) | 131 | |
| 127 | { | 132 | if (!fn || !fn(e, &ret, NULL, nid)) { |
| 128 | ENGINEerr(ENGINE_F_ENGINE_GET_PKEY_ASN1_METH, | 133 | ENGINEerr(ENGINE_F_ENGINE_GET_PKEY_ASN1_METH, |
| 129 | ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD); | 134 | ENGINE_R_UNIMPLEMENTED_PUBLIC_KEY_METHOD); |
| 130 | return NULL; | 135 | return NULL; |
| 131 | } | ||
| 132 | return ret; | ||
| 133 | } | 136 | } |
| 137 | return ret; | ||
| 138 | } | ||
| 134 | 139 | ||
| 135 | /* Gets the pkey_asn1_meth callback from an ENGINE structure */ | 140 | /* Gets the pkey_asn1_meth callback from an ENGINE structure */ |
| 136 | ENGINE_PKEY_ASN1_METHS_PTR ENGINE_get_pkey_asn1_meths(const ENGINE *e) | 141 | ENGINE_PKEY_ASN1_METHS_PTR |
| 137 | { | 142 | ENGINE_get_pkey_asn1_meths(const ENGINE *e) |
| 143 | { | ||
| 138 | return e->pkey_asn1_meths; | 144 | return e->pkey_asn1_meths; |
| 139 | } | 145 | } |
| 140 | 146 | ||
| 141 | /* Sets the pkey_asn1_meth callback in an ENGINE structure */ | 147 | /* Sets the pkey_asn1_meth callback in an ENGINE structure */ |
| 142 | int ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f) | 148 | int |
| 143 | { | 149 | ENGINE_set_pkey_asn1_meths(ENGINE *e, ENGINE_PKEY_ASN1_METHS_PTR f) |
| 150 | { | ||
| 144 | e->pkey_asn1_meths = f; | 151 | e->pkey_asn1_meths = f; |
| 145 | return 1; | 152 | return 1; |
| 146 | } | 153 | } |
| 147 | 154 | ||
| 148 | /* Internal function to free up EVP_PKEY_ASN1_METHOD structures before an | 155 | /* Internal function to free up EVP_PKEY_ASN1_METHOD structures before an |
| 149 | * ENGINE is destroyed | 156 | * ENGINE is destroyed |
| 150 | */ | 157 | */ |
| 151 | 158 | ||
| 152 | void engine_pkey_asn1_meths_free(ENGINE *e) | 159 | void |
| 153 | { | 160 | engine_pkey_asn1_meths_free(ENGINE *e) |
| 161 | { | ||
| 154 | int i; | 162 | int i; |
| 155 | EVP_PKEY_ASN1_METHOD *pkm; | 163 | EVP_PKEY_ASN1_METHOD *pkm; |
| 156 | if (e->pkey_asn1_meths) | 164 | |
| 157 | { | 165 | if (e->pkey_asn1_meths) { |
| 158 | const int *pknids; | 166 | const int *pknids; |
| 159 | int npknids; | 167 | int npknids; |
| 160 | npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0); | 168 | npknids = e->pkey_asn1_meths(e, NULL, &pknids, 0); |
| 161 | for (i = 0; i < npknids; i++) | 169 | for (i = 0; i < npknids; i++) { |
| 162 | { | 170 | if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i])) { |
| 163 | if (e->pkey_asn1_meths(e, &pkm, NULL, pknids[i])) | ||
| 164 | { | ||
| 165 | EVP_PKEY_asn1_free(pkm); | 171 | EVP_PKEY_asn1_free(pkm); |
| 166 | } | ||
| 167 | } | 172 | } |
| 168 | } | 173 | } |
| 169 | } | 174 | } |
| 175 | } | ||
| 170 | 176 | ||
| 171 | /* Find a method based on a string. This does a linear search through | 177 | /* Find a method based on a string. This does a linear search through |
| 172 | * all implemented algorithms. This is OK in practice because only | 178 | * all implemented algorithms. This is OK in practice because only |
| @@ -174,60 +180,60 @@ void engine_pkey_asn1_meths_free(ENGINE *e) | |||
| 174 | * and it is not used for speed critical operations. | 180 | * and it is not used for speed critical operations. |
| 175 | */ | 181 | */ |
| 176 | 182 | ||
| 177 | const EVP_PKEY_ASN1_METHOD *ENGINE_get_pkey_asn1_meth_str(ENGINE *e, | 183 | const EVP_PKEY_ASN1_METHOD * |
| 178 | const char *str, int len) | 184 | ENGINE_get_pkey_asn1_meth_str(ENGINE *e, const char *str, int len) |
| 179 | { | 185 | { |
| 180 | int i, nidcount; | 186 | int i, nidcount; |
| 181 | const int *nids; | 187 | const int *nids; |
| 182 | EVP_PKEY_ASN1_METHOD *ameth; | 188 | EVP_PKEY_ASN1_METHOD *ameth; |
| 189 | |||
| 183 | if (!e->pkey_asn1_meths) | 190 | if (!e->pkey_asn1_meths) |
| 184 | return NULL; | 191 | return NULL; |
| 185 | if (len == -1) | 192 | if (len == -1) |
| 186 | len = strlen(str); | 193 | len = strlen(str); |
| 187 | nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0); | 194 | nidcount = e->pkey_asn1_meths(e, NULL, &nids, 0); |
| 188 | for (i = 0; i < nidcount; i++) | 195 | for (i = 0; i < nidcount; i++) { |
| 189 | { | ||
| 190 | e->pkey_asn1_meths(e, &ameth, NULL, nids[i]); | 196 | e->pkey_asn1_meths(e, &ameth, NULL, nids[i]); |
| 191 | if (((int)strlen(ameth->pem_str) == len) && | 197 | if (((int)strlen(ameth->pem_str) == len) && |
| 192 | !strncasecmp(ameth->pem_str, str, len)) | 198 | !strncasecmp(ameth->pem_str, str, len)) |
| 193 | return ameth; | 199 | return ameth; |
| 194 | } | ||
| 195 | return NULL; | ||
| 196 | } | 200 | } |
| 201 | return NULL; | ||
| 202 | } | ||
| 197 | 203 | ||
| 198 | typedef struct | 204 | typedef struct { |
| 199 | { | ||
| 200 | ENGINE *e; | 205 | ENGINE *e; |
| 201 | const EVP_PKEY_ASN1_METHOD *ameth; | 206 | const EVP_PKEY_ASN1_METHOD *ameth; |
| 202 | const char *str; | 207 | const char *str; |
| 203 | int len; | 208 | int len; |
| 204 | } ENGINE_FIND_STR; | 209 | } ENGINE_FIND_STR; |
| 205 | 210 | ||
| 206 | static void look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg) | 211 | static void |
| 207 | { | 212 | look_str_cb(int nid, STACK_OF(ENGINE) *sk, ENGINE *def, void *arg) |
| 213 | { | ||
| 208 | ENGINE_FIND_STR *lk = arg; | 214 | ENGINE_FIND_STR *lk = arg; |
| 209 | int i; | 215 | int i; |
| 216 | |||
| 210 | if (lk->ameth) | 217 | if (lk->ameth) |
| 211 | return; | 218 | return; |
| 212 | for (i = 0; i < sk_ENGINE_num(sk); i++) | 219 | for (i = 0; i < sk_ENGINE_num(sk); i++) { |
| 213 | { | ||
| 214 | ENGINE *e = sk_ENGINE_value(sk, i); | 220 | ENGINE *e = sk_ENGINE_value(sk, i); |
| 215 | EVP_PKEY_ASN1_METHOD *ameth; | 221 | EVP_PKEY_ASN1_METHOD *ameth; |
| 216 | e->pkey_asn1_meths(e, &ameth, NULL, nid); | 222 | e->pkey_asn1_meths(e, &ameth, NULL, nid); |
| 217 | if (((int)strlen(ameth->pem_str) == lk->len) && | 223 | if (((int)strlen(ameth->pem_str) == lk->len) && |
| 218 | !strncasecmp(ameth->pem_str, lk->str, lk->len)) | 224 | !strncasecmp(ameth->pem_str, lk->str, lk->len)) { |
| 219 | { | ||
| 220 | lk->e = e; | 225 | lk->e = e; |
| 221 | lk->ameth = ameth; | 226 | lk->ameth = ameth; |
| 222 | return; | 227 | return; |
| 223 | } | ||
| 224 | } | 228 | } |
| 225 | } | 229 | } |
| 230 | } | ||
| 226 | 231 | ||
| 227 | const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe, | 232 | const EVP_PKEY_ASN1_METHOD * |
| 228 | const char *str, int len) | 233 | ENGINE_pkey_asn1_find_str(ENGINE **pe, const char *str, int len) |
| 229 | { | 234 | { |
| 230 | ENGINE_FIND_STR fstr; | 235 | ENGINE_FIND_STR fstr; |
| 236 | |||
| 231 | fstr.e = NULL; | 237 | fstr.e = NULL; |
| 232 | fstr.ameth = NULL; | 238 | fstr.ameth = NULL; |
| 233 | fstr.str = str; | 239 | fstr.str = str; |
| @@ -235,12 +241,11 @@ const EVP_PKEY_ASN1_METHOD *ENGINE_pkey_asn1_find_str(ENGINE **pe, | |||
| 235 | CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); | 241 | CRYPTO_w_lock(CRYPTO_LOCK_ENGINE); |
| 236 | engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr); | 242 | engine_table_doall(pkey_asn1_meth_table, look_str_cb, &fstr); |
| 237 | /* If found obtain a structural reference to engine */ | 243 | /* If found obtain a structural reference to engine */ |
| 238 | if (fstr.e) | 244 | if (fstr.e) { |
| 239 | { | ||
| 240 | fstr.e->struct_ref++; | 245 | fstr.e->struct_ref++; |
| 241 | engine_ref_debug(fstr.e, 0, 1) | 246 | engine_ref_debug(fstr.e, 0, 1) |
| 242 | } | 247 | } |
| 243 | *pe = fstr.e; | 248 | *pe = fstr.e; |
| 244 | CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); | 249 | CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE); |
| 245 | return fstr.ameth; | 250 | return fstr.ameth; |
| 246 | } | 251 | } |
