ameth_lib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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. #include "e_os.h" /* for strncasecmp */
  10. #include "internal/cryptlib.h"
  11. #include <stdio.h>
  12. #include <openssl/asn1t.h>
  13. #include <openssl/x509.h>
  14. #include <openssl/engine.h>
  15. #include "internal/asn1_int.h"
  16. #include "internal/evp_int.h"
  17. #include "standard_methods.h"
  18. typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
  19. static STACK_OF(EVP_PKEY_ASN1_METHOD) *app_methods = NULL;
  20. DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
  21. const EVP_PKEY_ASN1_METHOD *, ameth);
  22. static int ameth_cmp(const EVP_PKEY_ASN1_METHOD *const *a,
  23. const EVP_PKEY_ASN1_METHOD *const *b)
  24. {
  25. return ((*a)->pkey_id - (*b)->pkey_id);
  26. }
  27. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
  28. const EVP_PKEY_ASN1_METHOD *, ameth);
  29. int EVP_PKEY_asn1_get_count(void)
  30. {
  31. int num = OSSL_NELEM(standard_methods);
  32. if (app_methods)
  33. num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods);
  34. return num;
  35. }
  36. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx)
  37. {
  38. int num = OSSL_NELEM(standard_methods);
  39. if (idx < 0)
  40. return NULL;
  41. if (idx < num)
  42. return standard_methods[idx];
  43. idx -= num;
  44. return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
  45. }
  46. static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
  47. {
  48. EVP_PKEY_ASN1_METHOD tmp;
  49. const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
  50. tmp.pkey_id = type;
  51. if (app_methods) {
  52. int idx;
  53. idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
  54. if (idx >= 0)
  55. return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
  56. }
  57. ret = OBJ_bsearch_ameth(&t, standard_methods, OSSL_NELEM(standard_methods));
  58. if (!ret || !*ret)
  59. return NULL;
  60. return *ret;
  61. }
  62. /*
  63. * Find an implementation of an ASN1 algorithm. If 'pe' is not NULL also
  64. * search through engines and set *pe to a functional reference to the engine
  65. * implementing 'type' or NULL if no engine implements it.
  66. */
  67. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
  68. {
  69. const EVP_PKEY_ASN1_METHOD *t;
  70. for (;;) {
  71. t = pkey_asn1_find(type);
  72. if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
  73. break;
  74. type = t->pkey_base_id;
  75. }
  76. if (pe) {
  77. #ifndef OPENSSL_NO_ENGINE
  78. ENGINE *e;
  79. /* type will contain the final unaliased type */
  80. e = ENGINE_get_pkey_asn1_meth_engine(type);
  81. if (e) {
  82. *pe = e;
  83. return ENGINE_get_pkey_asn1_meth(e, type);
  84. }
  85. #endif
  86. *pe = NULL;
  87. }
  88. return t;
  89. }
  90. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
  91. const char *str, int len)
  92. {
  93. int i;
  94. const EVP_PKEY_ASN1_METHOD *ameth = NULL;
  95. if (len == -1)
  96. len = strlen(str);
  97. if (pe) {
  98. #ifndef OPENSSL_NO_ENGINE
  99. ENGINE *e;
  100. ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
  101. if (ameth) {
  102. /*
  103. * Convert structural into functional reference
  104. */
  105. if (!ENGINE_init(e))
  106. ameth = NULL;
  107. ENGINE_free(e);
  108. *pe = e;
  109. return ameth;
  110. }
  111. #endif
  112. *pe = NULL;
  113. }
  114. for (i = EVP_PKEY_asn1_get_count(); i-- > 0; ) {
  115. ameth = EVP_PKEY_asn1_get0(i);
  116. if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
  117. continue;
  118. if ((int)strlen(ameth->pem_str) == len
  119. && strncasecmp(ameth->pem_str, str, len) == 0)
  120. return ameth;
  121. }
  122. return NULL;
  123. }
  124. int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
  125. {
  126. EVP_PKEY_ASN1_METHOD tmp = { 0, };
  127. if (app_methods == NULL) {
  128. app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
  129. if (app_methods == NULL)
  130. return 0;
  131. }
  132. tmp.pkey_id = ameth->pkey_id;
  133. if (sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp) >= 0) {
  134. EVPerr(EVP_F_EVP_PKEY_ASN1_ADD0,
  135. EVP_R_PKEY_APPLICATION_ASN1_METHOD_ALREADY_REGISTERED);
  136. return 0;
  137. }
  138. if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
  139. return 0;
  140. sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
  141. return 1;
  142. }
  143. int EVP_PKEY_asn1_add_alias(int to, int from)
  144. {
  145. EVP_PKEY_ASN1_METHOD *ameth;
  146. ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
  147. if (ameth == NULL)
  148. return 0;
  149. ameth->pkey_base_id = to;
  150. if (!EVP_PKEY_asn1_add0(ameth)) {
  151. EVP_PKEY_asn1_free(ameth);
  152. return 0;
  153. }
  154. return 1;
  155. }
  156. int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
  157. int *ppkey_flags, const char **pinfo,
  158. const char **ppem_str,
  159. const EVP_PKEY_ASN1_METHOD *ameth)
  160. {
  161. if (!ameth)
  162. return 0;
  163. if (ppkey_id)
  164. *ppkey_id = ameth->pkey_id;
  165. if (ppkey_base_id)
  166. *ppkey_base_id = ameth->pkey_base_id;
  167. if (ppkey_flags)
  168. *ppkey_flags = ameth->pkey_flags;
  169. if (pinfo)
  170. *pinfo = ameth->info;
  171. if (ppem_str)
  172. *ppem_str = ameth->pem_str;
  173. return 1;
  174. }
  175. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey)
  176. {
  177. return pkey->ameth;
  178. }
  179. EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
  180. const char *pem_str, const char *info)
  181. {
  182. EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
  183. if (ameth == NULL)
  184. return NULL;
  185. ameth->pkey_id = id;
  186. ameth->pkey_base_id = id;
  187. ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
  188. if (info) {
  189. ameth->info = OPENSSL_strdup(info);
  190. if (!ameth->info)
  191. goto err;
  192. }
  193. /*
  194. * One of the following must be true:
  195. *
  196. * pem_str == NULL AND ASN1_PKEY_ALIAS is set
  197. * pem_str != NULL AND ASN1_PKEY_ALIAS is clear
  198. *
  199. * Anything else is an error and may lead to a corrupt ASN1 method table
  200. */
  201. if (!((pem_str == NULL && (flags & ASN1_PKEY_ALIAS) != 0)
  202. || (pem_str != NULL && (flags & ASN1_PKEY_ALIAS) == 0)))
  203. goto err;
  204. if (pem_str) {
  205. ameth->pem_str = OPENSSL_strdup(pem_str);
  206. if (!ameth->pem_str)
  207. goto err;
  208. }
  209. return ameth;
  210. err:
  211. EVP_PKEY_asn1_free(ameth);
  212. return NULL;
  213. }
  214. void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
  215. const EVP_PKEY_ASN1_METHOD *src)
  216. {
  217. dst->pub_decode = src->pub_decode;
  218. dst->pub_encode = src->pub_encode;
  219. dst->pub_cmp = src->pub_cmp;
  220. dst->pub_print = src->pub_print;
  221. dst->priv_decode = src->priv_decode;
  222. dst->priv_encode = src->priv_encode;
  223. dst->priv_print = src->priv_print;
  224. dst->old_priv_encode = src->old_priv_encode;
  225. dst->old_priv_decode = src->old_priv_decode;
  226. dst->pkey_size = src->pkey_size;
  227. dst->pkey_bits = src->pkey_bits;
  228. dst->param_decode = src->param_decode;
  229. dst->param_encode = src->param_encode;
  230. dst->param_missing = src->param_missing;
  231. dst->param_copy = src->param_copy;
  232. dst->param_cmp = src->param_cmp;
  233. dst->param_print = src->param_print;
  234. dst->pkey_free = src->pkey_free;
  235. dst->pkey_ctrl = src->pkey_ctrl;
  236. dst->item_sign = src->item_sign;
  237. dst->item_verify = src->item_verify;
  238. dst->siginf_set = src->siginf_set;
  239. dst->pkey_check = src->pkey_check;
  240. }
  241. void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
  242. {
  243. if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
  244. OPENSSL_free(ameth->pem_str);
  245. OPENSSL_free(ameth->info);
  246. OPENSSL_free(ameth);
  247. }
  248. }
  249. void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
  250. int (*pub_decode) (EVP_PKEY *pk,
  251. X509_PUBKEY *pub),
  252. int (*pub_encode) (X509_PUBKEY *pub,
  253. const EVP_PKEY *pk),
  254. int (*pub_cmp) (const EVP_PKEY *a,
  255. const EVP_PKEY *b),
  256. int (*pub_print) (BIO *out,
  257. const EVP_PKEY *pkey,
  258. int indent, ASN1_PCTX *pctx),
  259. int (*pkey_size) (const EVP_PKEY *pk),
  260. int (*pkey_bits) (const EVP_PKEY *pk))
  261. {
  262. ameth->pub_decode = pub_decode;
  263. ameth->pub_encode = pub_encode;
  264. ameth->pub_cmp = pub_cmp;
  265. ameth->pub_print = pub_print;
  266. ameth->pkey_size = pkey_size;
  267. ameth->pkey_bits = pkey_bits;
  268. }
  269. void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
  270. int (*priv_decode) (EVP_PKEY *pk,
  271. const PKCS8_PRIV_KEY_INFO
  272. *p8inf),
  273. int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
  274. const EVP_PKEY *pk),
  275. int (*priv_print) (BIO *out,
  276. const EVP_PKEY *pkey,
  277. int indent,
  278. ASN1_PCTX *pctx))
  279. {
  280. ameth->priv_decode = priv_decode;
  281. ameth->priv_encode = priv_encode;
  282. ameth->priv_print = priv_print;
  283. }
  284. void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
  285. int (*param_decode) (EVP_PKEY *pkey,
  286. const unsigned char **pder,
  287. int derlen),
  288. int (*param_encode) (const EVP_PKEY *pkey,
  289. unsigned char **pder),
  290. int (*param_missing) (const EVP_PKEY *pk),
  291. int (*param_copy) (EVP_PKEY *to,
  292. const EVP_PKEY *from),
  293. int (*param_cmp) (const EVP_PKEY *a,
  294. const EVP_PKEY *b),
  295. int (*param_print) (BIO *out,
  296. const EVP_PKEY *pkey,
  297. int indent, ASN1_PCTX *pctx))
  298. {
  299. ameth->param_decode = param_decode;
  300. ameth->param_encode = param_encode;
  301. ameth->param_missing = param_missing;
  302. ameth->param_copy = param_copy;
  303. ameth->param_cmp = param_cmp;
  304. ameth->param_print = param_print;
  305. }
  306. void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
  307. void (*pkey_free) (EVP_PKEY *pkey))
  308. {
  309. ameth->pkey_free = pkey_free;
  310. }
  311. void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
  312. int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
  313. long arg1, void *arg2))
  314. {
  315. ameth->pkey_ctrl = pkey_ctrl;
  316. }
  317. void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
  318. int (*pkey_security_bits) (const EVP_PKEY
  319. *pk))
  320. {
  321. ameth->pkey_security_bits = pkey_security_bits;
  322. }
  323. void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
  324. int (*item_verify) (EVP_MD_CTX *ctx,
  325. const ASN1_ITEM *it,
  326. void *asn,
  327. X509_ALGOR *a,
  328. ASN1_BIT_STRING *sig,
  329. EVP_PKEY *pkey),
  330. int (*item_sign) (EVP_MD_CTX *ctx,
  331. const ASN1_ITEM *it,
  332. void *asn,
  333. X509_ALGOR *alg1,
  334. X509_ALGOR *alg2,
  335. ASN1_BIT_STRING *sig))
  336. {
  337. ameth->item_sign = item_sign;
  338. ameth->item_verify = item_verify;
  339. }
  340. void EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD *ameth,
  341. int (*siginf_set) (X509_SIG_INFO *siginf,
  342. const X509_ALGOR *alg,
  343. const ASN1_STRING *sig))
  344. {
  345. ameth->siginf_set = siginf_set;
  346. }
  347. void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth,
  348. int (*pkey_check) (const EVP_PKEY *pk))
  349. {
  350. ameth->pkey_check = pkey_check;
  351. }
  352. void EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD *ameth,
  353. int (*pkey_pub_check) (const EVP_PKEY *pk))
  354. {
  355. ameth->pkey_public_check = pkey_pub_check;
  356. }
  357. void EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD *ameth,
  358. int (*pkey_param_check) (const EVP_PKEY *pk))
  359. {
  360. ameth->pkey_param_check = pkey_param_check;
  361. }
  362. void EVP_PKEY_asn1_set_set_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
  363. int (*set_priv_key) (EVP_PKEY *pk,
  364. const unsigned char
  365. *priv,
  366. size_t len))
  367. {
  368. ameth->set_priv_key = set_priv_key;
  369. }
  370. void EVP_PKEY_asn1_set_set_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
  371. int (*set_pub_key) (EVP_PKEY *pk,
  372. const unsigned char *pub,
  373. size_t len))
  374. {
  375. ameth->set_pub_key = set_pub_key;
  376. }
  377. void EVP_PKEY_asn1_set_get_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
  378. int (*get_priv_key) (const EVP_PKEY *pk,
  379. unsigned char *priv,
  380. size_t *len))
  381. {
  382. ameth->get_priv_key = get_priv_key;
  383. }
  384. void EVP_PKEY_asn1_set_get_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
  385. int (*get_pub_key) (const EVP_PKEY *pk,
  386. unsigned char *pub,
  387. size_t *len))
  388. {
  389. ameth->get_pub_key = get_pub_key;
  390. }