ameth_lib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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. if (pem_str) {
  194. ameth->pem_str = OPENSSL_strdup(pem_str);
  195. if (!ameth->pem_str)
  196. goto err;
  197. }
  198. return ameth;
  199. err:
  200. EVP_PKEY_asn1_free(ameth);
  201. return NULL;
  202. }
  203. void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
  204. const EVP_PKEY_ASN1_METHOD *src)
  205. {
  206. dst->pub_decode = src->pub_decode;
  207. dst->pub_encode = src->pub_encode;
  208. dst->pub_cmp = src->pub_cmp;
  209. dst->pub_print = src->pub_print;
  210. dst->priv_decode = src->priv_decode;
  211. dst->priv_encode = src->priv_encode;
  212. dst->priv_print = src->priv_print;
  213. dst->old_priv_encode = src->old_priv_encode;
  214. dst->old_priv_decode = src->old_priv_decode;
  215. dst->pkey_size = src->pkey_size;
  216. dst->pkey_bits = src->pkey_bits;
  217. dst->param_decode = src->param_decode;
  218. dst->param_encode = src->param_encode;
  219. dst->param_missing = src->param_missing;
  220. dst->param_copy = src->param_copy;
  221. dst->param_cmp = src->param_cmp;
  222. dst->param_print = src->param_print;
  223. dst->pkey_free = src->pkey_free;
  224. dst->pkey_ctrl = src->pkey_ctrl;
  225. dst->item_sign = src->item_sign;
  226. dst->item_verify = src->item_verify;
  227. dst->siginf_set = src->siginf_set;
  228. dst->pkey_check = src->pkey_check;
  229. }
  230. void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
  231. {
  232. if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
  233. OPENSSL_free(ameth->pem_str);
  234. OPENSSL_free(ameth->info);
  235. OPENSSL_free(ameth);
  236. }
  237. }
  238. void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
  239. int (*pub_decode) (EVP_PKEY *pk,
  240. X509_PUBKEY *pub),
  241. int (*pub_encode) (X509_PUBKEY *pub,
  242. const EVP_PKEY *pk),
  243. int (*pub_cmp) (const EVP_PKEY *a,
  244. const EVP_PKEY *b),
  245. int (*pub_print) (BIO *out,
  246. const EVP_PKEY *pkey,
  247. int indent, ASN1_PCTX *pctx),
  248. int (*pkey_size) (const EVP_PKEY *pk),
  249. int (*pkey_bits) (const EVP_PKEY *pk))
  250. {
  251. ameth->pub_decode = pub_decode;
  252. ameth->pub_encode = pub_encode;
  253. ameth->pub_cmp = pub_cmp;
  254. ameth->pub_print = pub_print;
  255. ameth->pkey_size = pkey_size;
  256. ameth->pkey_bits = pkey_bits;
  257. }
  258. void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
  259. int (*priv_decode) (EVP_PKEY *pk,
  260. const PKCS8_PRIV_KEY_INFO
  261. *p8inf),
  262. int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
  263. const EVP_PKEY *pk),
  264. int (*priv_print) (BIO *out,
  265. const EVP_PKEY *pkey,
  266. int indent,
  267. ASN1_PCTX *pctx))
  268. {
  269. ameth->priv_decode = priv_decode;
  270. ameth->priv_encode = priv_encode;
  271. ameth->priv_print = priv_print;
  272. }
  273. void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
  274. int (*param_decode) (EVP_PKEY *pkey,
  275. const unsigned char **pder,
  276. int derlen),
  277. int (*param_encode) (const EVP_PKEY *pkey,
  278. unsigned char **pder),
  279. int (*param_missing) (const EVP_PKEY *pk),
  280. int (*param_copy) (EVP_PKEY *to,
  281. const EVP_PKEY *from),
  282. int (*param_cmp) (const EVP_PKEY *a,
  283. const EVP_PKEY *b),
  284. int (*param_print) (BIO *out,
  285. const EVP_PKEY *pkey,
  286. int indent, ASN1_PCTX *pctx))
  287. {
  288. ameth->param_decode = param_decode;
  289. ameth->param_encode = param_encode;
  290. ameth->param_missing = param_missing;
  291. ameth->param_copy = param_copy;
  292. ameth->param_cmp = param_cmp;
  293. ameth->param_print = param_print;
  294. }
  295. void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
  296. void (*pkey_free) (EVP_PKEY *pkey))
  297. {
  298. ameth->pkey_free = pkey_free;
  299. }
  300. void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
  301. int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
  302. long arg1, void *arg2))
  303. {
  304. ameth->pkey_ctrl = pkey_ctrl;
  305. }
  306. void EVP_PKEY_asn1_set_security_bits(EVP_PKEY_ASN1_METHOD *ameth,
  307. int (*pkey_security_bits) (const EVP_PKEY
  308. *pk))
  309. {
  310. ameth->pkey_security_bits = pkey_security_bits;
  311. }
  312. void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
  313. int (*item_verify) (EVP_MD_CTX *ctx,
  314. const ASN1_ITEM *it,
  315. void *asn,
  316. X509_ALGOR *a,
  317. ASN1_BIT_STRING *sig,
  318. EVP_PKEY *pkey),
  319. int (*item_sign) (EVP_MD_CTX *ctx,
  320. const ASN1_ITEM *it,
  321. void *asn,
  322. X509_ALGOR *alg1,
  323. X509_ALGOR *alg2,
  324. ASN1_BIT_STRING *sig))
  325. {
  326. ameth->item_sign = item_sign;
  327. ameth->item_verify = item_verify;
  328. }
  329. void EVP_PKEY_asn1_set_siginf(EVP_PKEY_ASN1_METHOD *ameth,
  330. int (*siginf_set) (X509_SIG_INFO *siginf,
  331. const X509_ALGOR *alg,
  332. const ASN1_STRING *sig))
  333. {
  334. ameth->siginf_set = siginf_set;
  335. }
  336. void EVP_PKEY_asn1_set_check(EVP_PKEY_ASN1_METHOD *ameth,
  337. int (*pkey_check) (const EVP_PKEY *pk))
  338. {
  339. ameth->pkey_check = pkey_check;
  340. }
  341. void EVP_PKEY_asn1_set_public_check(EVP_PKEY_ASN1_METHOD *ameth,
  342. int (*pkey_pub_check) (const EVP_PKEY *pk))
  343. {
  344. ameth->pkey_public_check = pkey_pub_check;
  345. }
  346. void EVP_PKEY_asn1_set_param_check(EVP_PKEY_ASN1_METHOD *ameth,
  347. int (*pkey_param_check) (const EVP_PKEY *pk))
  348. {
  349. ameth->pkey_param_check = pkey_param_check;
  350. }
  351. void EVP_PKEY_asn1_set_set_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
  352. int (*set_priv_key) (EVP_PKEY *pk,
  353. const unsigned char
  354. *priv,
  355. size_t len))
  356. {
  357. ameth->set_priv_key = set_priv_key;
  358. }
  359. void EVP_PKEY_asn1_set_set_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
  360. int (*set_pub_key) (EVP_PKEY *pk,
  361. const unsigned char *pub,
  362. size_t len))
  363. {
  364. ameth->set_pub_key = set_pub_key;
  365. }
  366. void EVP_PKEY_asn1_set_get_priv_key(EVP_PKEY_ASN1_METHOD *ameth,
  367. int (*get_priv_key) (const EVP_PKEY *pk,
  368. unsigned char *priv,
  369. size_t *len))
  370. {
  371. ameth->get_priv_key = get_priv_key;
  372. }
  373. void EVP_PKEY_asn1_set_get_pub_key(EVP_PKEY_ASN1_METHOD *ameth,
  374. int (*get_pub_key) (const EVP_PKEY *pk,
  375. unsigned char *pub,
  376. size_t *len))
  377. {
  378. ameth->get_pub_key = get_pub_key;
  379. }