ameth_lib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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 "crypto/asn1.h"
  16. #include "crypto/evp.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. /*
  128. * One of the following must be true:
  129. *
  130. * pem_str == NULL AND ASN1_PKEY_ALIAS is set
  131. * pem_str != NULL AND ASN1_PKEY_ALIAS is clear
  132. *
  133. * Anything else is an error and may lead to a corrupt ASN1 method table
  134. */
  135. if (!((ameth->pem_str == NULL
  136. && (ameth->pkey_flags & ASN1_PKEY_ALIAS) != 0)
  137. || (ameth->pem_str != NULL
  138. && (ameth->pkey_flags & ASN1_PKEY_ALIAS) == 0))) {
  139. EVPerr(EVP_F_EVP_PKEY_ASN1_ADD0, ERR_R_PASSED_INVALID_ARGUMENT);
  140. return 0;
  141. }
  142. if (app_methods == NULL) {
  143. app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
  144. if (app_methods == NULL)
  145. return 0;
  146. }
  147. tmp.pkey_id = ameth->pkey_id;
  148. if (sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp) >= 0) {
  149. EVPerr(EVP_F_EVP_PKEY_ASN1_ADD0,
  150. EVP_R_PKEY_APPLICATION_ASN1_METHOD_ALREADY_REGISTERED);
  151. return 0;
  152. }
  153. if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
  154. return 0;
  155. sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
  156. return 1;
  157. }
  158. int EVP_PKEY_asn1_add_alias(int to, int from)
  159. {
  160. EVP_PKEY_ASN1_METHOD *ameth;
  161. ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
  162. if (ameth == NULL)
  163. return 0;
  164. ameth->pkey_base_id = to;
  165. if (!EVP_PKEY_asn1_add0(ameth)) {
  166. EVP_PKEY_asn1_free(ameth);
  167. return 0;
  168. }
  169. return 1;
  170. }
  171. int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
  172. int *ppkey_flags, const char **pinfo,
  173. const char **ppem_str,
  174. const EVP_PKEY_ASN1_METHOD *ameth)
  175. {
  176. if (!ameth)
  177. return 0;
  178. if (ppkey_id)
  179. *ppkey_id = ameth->pkey_id;
  180. if (ppkey_base_id)
  181. *ppkey_base_id = ameth->pkey_base_id;
  182. if (ppkey_flags)
  183. *ppkey_flags = ameth->pkey_flags;
  184. if (pinfo)
  185. *pinfo = ameth->info;
  186. if (ppem_str)
  187. *ppem_str = ameth->pem_str;
  188. return 1;
  189. }
  190. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(const EVP_PKEY *pkey)
  191. {
  192. return pkey->ameth;
  193. }
  194. EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
  195. const char *pem_str, const char *info)
  196. {
  197. EVP_PKEY_ASN1_METHOD *ameth = OPENSSL_zalloc(sizeof(*ameth));
  198. if (ameth == NULL)
  199. return NULL;
  200. ameth->pkey_id = id;
  201. ameth->pkey_base_id = id;
  202. ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
  203. if (info) {
  204. ameth->info = OPENSSL_strdup(info);
  205. if (!ameth->info)
  206. goto err;
  207. }
  208. if (pem_str) {
  209. ameth->pem_str = OPENSSL_strdup(pem_str);
  210. if (!ameth->pem_str)
  211. goto err;
  212. }
  213. return ameth;
  214. err:
  215. EVP_PKEY_asn1_free(ameth);
  216. return NULL;
  217. }
  218. void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
  219. const EVP_PKEY_ASN1_METHOD *src)
  220. {
  221. dst->pub_decode = src->pub_decode;
  222. dst->pub_encode = src->pub_encode;
  223. dst->pub_cmp = src->pub_cmp;
  224. dst->pub_print = src->pub_print;
  225. dst->priv_decode = src->priv_decode;
  226. dst->priv_encode = src->priv_encode;
  227. dst->priv_print = src->priv_print;
  228. dst->old_priv_encode = src->old_priv_encode;
  229. dst->old_priv_decode = src->old_priv_decode;
  230. dst->pkey_size = src->pkey_size;
  231. dst->pkey_bits = src->pkey_bits;
  232. dst->param_decode = src->param_decode;
  233. dst->param_encode = src->param_encode;
  234. dst->param_missing = src->param_missing;
  235. dst->param_copy = src->param_copy;
  236. dst->param_cmp = src->param_cmp;
  237. dst->param_print = src->param_print;
  238. dst->pkey_free = src->pkey_free;
  239. dst->pkey_ctrl = src->pkey_ctrl;
  240. dst->item_sign = src->item_sign;
  241. dst->item_verify = src->item_verify;
  242. dst->siginf_set = src->siginf_set;
  243. dst->pkey_check = src->pkey_check;
  244. }
  245. void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
  246. {
  247. if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
  248. OPENSSL_free(ameth->pem_str);
  249. OPENSSL_free(ameth->info);
  250. OPENSSL_free(ameth);
  251. }
  252. }
  253. void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
  254. int (*pub_decode) (EVP_PKEY *pk,
  255. X509_PUBKEY *pub),
  256. int (*pub_encode) (X509_PUBKEY *pub,
  257. const EVP_PKEY *pk),
  258. int (*pub_cmp) (const EVP_PKEY *a,
  259. const EVP_PKEY *b),
  260. int (*pub_print) (BIO *out,
  261. const EVP_PKEY *pkey,
  262. int indent, ASN1_PCTX *pctx),
  263. int (*pkey_size) (const EVP_PKEY *pk),
  264. int (*pkey_bits) (const EVP_PKEY *pk))
  265. {
  266. ameth->pub_decode = pub_decode;
  267. ameth->pub_encode = pub_encode;
  268. ameth->pub_cmp = pub_cmp;
  269. ameth->pub_print = pub_print;
  270. ameth->pkey_size = pkey_size;
  271. ameth->pkey_bits = pkey_bits;
  272. }
  273. void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
  274. int (*priv_decode) (EVP_PKEY *pk,
  275. const PKCS8_PRIV_KEY_INFO
  276. *p8inf),
  277. int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
  278. const EVP_PKEY *pk),
  279. int (*priv_print) (BIO *out,
  280. const EVP_PKEY *pkey,
  281. int indent,
  282. ASN1_PCTX *pctx))
  283. {
  284. ameth->priv_decode = priv_decode;
  285. ameth->priv_encode = priv_encode;
  286. ameth->priv_print = priv_print;
  287. }
  288. void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
  289. int (*param_decode) (EVP_PKEY *pkey,
  290. const unsigned char **pder,
  291. int derlen),
  292. int (*param_encode) (const EVP_PKEY *pkey,
  293. unsigned char **pder),
  294. int (*param_missing) (const EVP_PKEY *pk),
  295. int (*param_copy) (EVP_PKEY *to,
  296. const EVP_PKEY *from),
  297. int (*param_cmp) (const EVP_PKEY *a,
  298. const EVP_PKEY *b),
  299. int (*param_print) (BIO *out,
  300. const EVP_PKEY *pkey,
  301. int indent, ASN1_PCTX *pctx))
  302. {
  303. ameth->param_decode = param_decode;
  304. ameth->param_encode = param_encode;
  305. ameth->param_missing = param_missing;
  306. ameth->param_copy = param_copy;
  307. ameth->param_cmp = param_cmp;
  308. ameth->param_print = param_print;
  309. }
  310. void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
  311. void (*pkey_free) (EVP_PKEY *pkey))
  312. {
  313. ameth->pkey_free = pkey_free;
  314. }
  315. void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
  316. int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
  317. long arg1, void *arg2))
  318. {
  319. ameth->pkey_ctrl = pkey_ctrl;
  320. }
  321. void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
  322. int (*pkey_security_bits) (const EVP_PKEY
  323. *pk))
  324. {
  325. ameth->pkey_security_bits = pkey_security_bits;
  326. }
  327. void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
  328. int (*item_verify) (EVP_MD_CTX *ctx,
  329. const ASN1_ITEM *it,
  330. void *asn,
  331. X509_ALGOR *a,
  332. ASN1_BIT_STRING *sig,
  333. EVP_PKEY *pkey),
  334. int (*item_sign) (EVP_MD_CTX *ctx,
  335. const ASN1_ITEM *it,
  336. void *asn,
  337. X509_ALGOR *alg1,
  338. X509_ALGOR *alg2,
  339. ASN1_BIT_STRING *sig))
  340. {
  341. ameth->item_sign = item_sign;
  342. ameth->item_verify = item_verify;
  343. }
  344. void EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD *ameth,
  345. int (*siginf_set) (X509_SIG_INFO *siginf,
  346. const X509_ALGOR *alg,
  347. const ASN1_STRING *sig))
  348. {
  349. ameth->siginf_set = siginf_set;
  350. }
  351. void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth,
  352. int (*pkey_check) (const EVP_PKEY *pk))
  353. {
  354. ameth->pkey_check = pkey_check;
  355. }
  356. void EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD *ameth,
  357. int (*pkey_pub_check) (const EVP_PKEY *pk))
  358. {
  359. ameth->pkey_public_check = pkey_pub_check;
  360. }
  361. void EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD *ameth,
  362. int (*pkey_param_check) (const EVP_PKEY *pk))
  363. {
  364. ameth->pkey_param_check = pkey_param_check;
  365. }
  366. void EVP_PKEY_asn1_set_set_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
  367. int (*set_priv_key) (EVP_PKEY *pk,
  368. const unsigned char
  369. *priv,
  370. size_t len))
  371. {
  372. ameth->set_priv_key = set_priv_key;
  373. }
  374. void EVP_PKEY_asn1_set_set_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
  375. int (*set_pub_key) (EVP_PKEY *pk,
  376. const unsigned char *pub,
  377. size_t len))
  378. {
  379. ameth->set_pub_key = set_pub_key;
  380. }
  381. void EVP_PKEY_asn1_set_get_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
  382. int (*get_priv_key) (const EVP_PKEY *pk,
  383. unsigned char *priv,
  384. size_t *len))
  385. {
  386. ameth->get_priv_key = get_priv_key;
  387. }
  388. void EVP_PKEY_asn1_set_get_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
  389. int (*get_pub_key) (const EVP_PKEY *pk,
  390. unsigned char *pub,
  391. size_t *len))
  392. {
  393. ameth->get_pub_key = get_pub_key;
  394. }