2
0

ameth_lib.c 14 KB

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