ameth_lib.c 14 KB

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