ameth_lib.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project
  3. * 2006.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2006-2018 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * [email protected].
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * ([email protected]). This product includes software written by Tim
  55. * Hudson ([email protected]).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/asn1t.h>
  61. #include <openssl/x509.h>
  62. #ifndef OPENSSL_NO_ENGINE
  63. # include <openssl/engine.h>
  64. #endif
  65. #include "asn1_locl.h"
  66. extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meths[];
  67. extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[];
  68. extern const EVP_PKEY_ASN1_METHOD dh_asn1_meth;
  69. extern const EVP_PKEY_ASN1_METHOD dhx_asn1_meth;
  70. extern const EVP_PKEY_ASN1_METHOD eckey_asn1_meth;
  71. extern const EVP_PKEY_ASN1_METHOD hmac_asn1_meth;
  72. extern const EVP_PKEY_ASN1_METHOD cmac_asn1_meth;
  73. /* Keep this sorted in type order !! */
  74. static const EVP_PKEY_ASN1_METHOD *standard_methods[] = {
  75. #ifndef OPENSSL_NO_RSA
  76. &rsa_asn1_meths[0],
  77. &rsa_asn1_meths[1],
  78. #endif
  79. #ifndef OPENSSL_NO_DH
  80. &dh_asn1_meth,
  81. #endif
  82. #ifndef OPENSSL_NO_DSA
  83. &dsa_asn1_meths[0],
  84. &dsa_asn1_meths[1],
  85. &dsa_asn1_meths[2],
  86. &dsa_asn1_meths[3],
  87. &dsa_asn1_meths[4],
  88. #endif
  89. #ifndef OPENSSL_NO_EC
  90. &eckey_asn1_meth,
  91. #endif
  92. &hmac_asn1_meth,
  93. #ifndef OPENSSL_NO_CMAC
  94. &cmac_asn1_meth,
  95. #endif
  96. #ifndef OPENSSL_NO_DH
  97. &dhx_asn1_meth
  98. #endif
  99. };
  100. typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
  101. DECLARE_STACK_OF(EVP_PKEY_ASN1_METHOD)
  102. static STACK_OF(EVP_PKEY_ASN1_METHOD) *app_methods = NULL;
  103. #ifdef TEST
  104. void main()
  105. {
  106. int i;
  107. for (i = 0;
  108. i < sizeof(standard_methods) / sizeof(EVP_PKEY_ASN1_METHOD *); i++)
  109. fprintf(stderr, "Number %d id=%d (%s)\n", i,
  110. standard_methods[i]->pkey_id,
  111. OBJ_nid2sn(standard_methods[i]->pkey_id));
  112. }
  113. #endif
  114. DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
  115. const EVP_PKEY_ASN1_METHOD *, ameth);
  116. static int ameth_cmp(const EVP_PKEY_ASN1_METHOD *const *a,
  117. const EVP_PKEY_ASN1_METHOD *const *b)
  118. {
  119. return ((*a)->pkey_id - (*b)->pkey_id);
  120. }
  121. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
  122. const EVP_PKEY_ASN1_METHOD *, ameth);
  123. int EVP_PKEY_asn1_get_count(void)
  124. {
  125. int num = sizeof(standard_methods) / sizeof(EVP_PKEY_ASN1_METHOD *);
  126. if (app_methods)
  127. num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods);
  128. return num;
  129. }
  130. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx)
  131. {
  132. int num = sizeof(standard_methods) / sizeof(EVP_PKEY_ASN1_METHOD *);
  133. if (idx < 0)
  134. return NULL;
  135. if (idx < num)
  136. return standard_methods[idx];
  137. idx -= num;
  138. return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
  139. }
  140. static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
  141. {
  142. EVP_PKEY_ASN1_METHOD tmp;
  143. const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
  144. tmp.pkey_id = type;
  145. if (app_methods) {
  146. int idx;
  147. idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
  148. if (idx >= 0)
  149. return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
  150. }
  151. ret = OBJ_bsearch_ameth(&t, standard_methods, sizeof(standard_methods)
  152. / sizeof(EVP_PKEY_ASN1_METHOD *));
  153. if (!ret || !*ret)
  154. return NULL;
  155. return *ret;
  156. }
  157. /*
  158. * Find an implementation of an ASN1 algorithm. If 'pe' is not NULL also
  159. * search through engines and set *pe to a functional reference to the engine
  160. * implementing 'type' or NULL if no engine implements it.
  161. */
  162. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
  163. {
  164. const EVP_PKEY_ASN1_METHOD *t;
  165. for (;;) {
  166. t = pkey_asn1_find(type);
  167. if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
  168. break;
  169. type = t->pkey_base_id;
  170. }
  171. if (pe) {
  172. #ifndef OPENSSL_NO_ENGINE
  173. ENGINE *e;
  174. /* type will contain the final unaliased type */
  175. e = ENGINE_get_pkey_asn1_meth_engine(type);
  176. if (e) {
  177. *pe = e;
  178. return ENGINE_get_pkey_asn1_meth(e, type);
  179. }
  180. #endif
  181. *pe = NULL;
  182. }
  183. return t;
  184. }
  185. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
  186. const char *str, int len)
  187. {
  188. int i;
  189. const EVP_PKEY_ASN1_METHOD *ameth;
  190. if (len == -1)
  191. len = strlen(str);
  192. if (pe) {
  193. #ifndef OPENSSL_NO_ENGINE
  194. ENGINE *e;
  195. ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
  196. if (ameth) {
  197. /*
  198. * Convert structural into functional reference
  199. */
  200. if (!ENGINE_init(e))
  201. ameth = NULL;
  202. ENGINE_free(e);
  203. *pe = e;
  204. return ameth;
  205. }
  206. #endif
  207. *pe = NULL;
  208. }
  209. for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
  210. ameth = EVP_PKEY_asn1_get0(i);
  211. if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
  212. continue;
  213. if (((int)strlen(ameth->pem_str) == len) &&
  214. !strncasecmp(ameth->pem_str, str, len))
  215. return ameth;
  216. }
  217. return NULL;
  218. }
  219. int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
  220. {
  221. /*
  222. * One of the following must be true:
  223. *
  224. * pem_str == NULL AND ASN1_PKEY_ALIAS is set
  225. * pem_str != NULL AND ASN1_PKEY_ALIAS is clear
  226. *
  227. * Anything else is an error and may lead to a corrupt ASN1 method table
  228. */
  229. if (!((ameth->pem_str == NULL
  230. && (ameth->pkey_flags & ASN1_PKEY_ALIAS) != 0)
  231. || (ameth->pem_str != NULL
  232. && (ameth->pkey_flags & ASN1_PKEY_ALIAS) == 0))) {
  233. return 0;
  234. }
  235. if (app_methods == NULL) {
  236. app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
  237. if (!app_methods)
  238. return 0;
  239. }
  240. if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
  241. return 0;
  242. sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
  243. return 1;
  244. }
  245. int EVP_PKEY_asn1_add_alias(int to, int from)
  246. {
  247. EVP_PKEY_ASN1_METHOD *ameth;
  248. ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
  249. if (!ameth)
  250. return 0;
  251. ameth->pkey_base_id = to;
  252. if (!EVP_PKEY_asn1_add0(ameth)) {
  253. EVP_PKEY_asn1_free(ameth);
  254. return 0;
  255. }
  256. return 1;
  257. }
  258. int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
  259. int *ppkey_flags, const char **pinfo,
  260. const char **ppem_str,
  261. const EVP_PKEY_ASN1_METHOD *ameth)
  262. {
  263. if (!ameth)
  264. return 0;
  265. if (ppkey_id)
  266. *ppkey_id = ameth->pkey_id;
  267. if (ppkey_base_id)
  268. *ppkey_base_id = ameth->pkey_base_id;
  269. if (ppkey_flags)
  270. *ppkey_flags = ameth->pkey_flags;
  271. if (pinfo)
  272. *pinfo = ameth->info;
  273. if (ppem_str)
  274. *ppem_str = ameth->pem_str;
  275. return 1;
  276. }
  277. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(EVP_PKEY *pkey)
  278. {
  279. return pkey->ameth;
  280. }
  281. EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
  282. const char *pem_str, const char *info)
  283. {
  284. EVP_PKEY_ASN1_METHOD *ameth;
  285. ameth = OPENSSL_malloc(sizeof(EVP_PKEY_ASN1_METHOD));
  286. if (!ameth)
  287. return NULL;
  288. memset(ameth, 0, sizeof(EVP_PKEY_ASN1_METHOD));
  289. ameth->pkey_id = id;
  290. ameth->pkey_base_id = id;
  291. ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
  292. if (info) {
  293. ameth->info = BUF_strdup(info);
  294. if (!ameth->info)
  295. goto err;
  296. } else
  297. ameth->info = NULL;
  298. if (pem_str) {
  299. ameth->pem_str = BUF_strdup(pem_str);
  300. if (!ameth->pem_str)
  301. goto err;
  302. } else
  303. ameth->pem_str = NULL;
  304. ameth->pub_decode = 0;
  305. ameth->pub_encode = 0;
  306. ameth->pub_cmp = 0;
  307. ameth->pub_print = 0;
  308. ameth->priv_decode = 0;
  309. ameth->priv_encode = 0;
  310. ameth->priv_print = 0;
  311. ameth->old_priv_encode = 0;
  312. ameth->old_priv_decode = 0;
  313. ameth->item_verify = 0;
  314. ameth->item_sign = 0;
  315. ameth->pkey_size = 0;
  316. ameth->pkey_bits = 0;
  317. ameth->param_decode = 0;
  318. ameth->param_encode = 0;
  319. ameth->param_missing = 0;
  320. ameth->param_copy = 0;
  321. ameth->param_cmp = 0;
  322. ameth->param_print = 0;
  323. ameth->pkey_free = 0;
  324. ameth->pkey_ctrl = 0;
  325. return ameth;
  326. err:
  327. EVP_PKEY_asn1_free(ameth);
  328. return NULL;
  329. }
  330. void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
  331. const EVP_PKEY_ASN1_METHOD *src)
  332. {
  333. dst->pub_decode = src->pub_decode;
  334. dst->pub_encode = src->pub_encode;
  335. dst->pub_cmp = src->pub_cmp;
  336. dst->pub_print = src->pub_print;
  337. dst->priv_decode = src->priv_decode;
  338. dst->priv_encode = src->priv_encode;
  339. dst->priv_print = src->priv_print;
  340. dst->old_priv_encode = src->old_priv_encode;
  341. dst->old_priv_decode = src->old_priv_decode;
  342. dst->pkey_size = src->pkey_size;
  343. dst->pkey_bits = src->pkey_bits;
  344. dst->param_decode = src->param_decode;
  345. dst->param_encode = src->param_encode;
  346. dst->param_missing = src->param_missing;
  347. dst->param_copy = src->param_copy;
  348. dst->param_cmp = src->param_cmp;
  349. dst->param_print = src->param_print;
  350. dst->pkey_free = src->pkey_free;
  351. dst->pkey_ctrl = src->pkey_ctrl;
  352. dst->item_sign = src->item_sign;
  353. dst->item_verify = src->item_verify;
  354. }
  355. void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
  356. {
  357. if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
  358. if (ameth->pem_str)
  359. OPENSSL_free(ameth->pem_str);
  360. if (ameth->info)
  361. OPENSSL_free(ameth->info);
  362. OPENSSL_free(ameth);
  363. }
  364. }
  365. void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
  366. int (*pub_decode) (EVP_PKEY *pk,
  367. X509_PUBKEY *pub),
  368. int (*pub_encode) (X509_PUBKEY *pub,
  369. const EVP_PKEY *pk),
  370. int (*pub_cmp) (const EVP_PKEY *a,
  371. const EVP_PKEY *b),
  372. int (*pub_print) (BIO *out,
  373. const EVP_PKEY *pkey,
  374. int indent, ASN1_PCTX *pctx),
  375. int (*pkey_size) (const EVP_PKEY *pk),
  376. int (*pkey_bits) (const EVP_PKEY *pk))
  377. {
  378. ameth->pub_decode = pub_decode;
  379. ameth->pub_encode = pub_encode;
  380. ameth->pub_cmp = pub_cmp;
  381. ameth->pub_print = pub_print;
  382. ameth->pkey_size = pkey_size;
  383. ameth->pkey_bits = pkey_bits;
  384. }
  385. void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
  386. int (*priv_decode) (EVP_PKEY *pk,
  387. PKCS8_PRIV_KEY_INFO
  388. *p8inf),
  389. int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
  390. const EVP_PKEY *pk),
  391. int (*priv_print) (BIO *out,
  392. const EVP_PKEY *pkey,
  393. int indent,
  394. ASN1_PCTX *pctx))
  395. {
  396. ameth->priv_decode = priv_decode;
  397. ameth->priv_encode = priv_encode;
  398. ameth->priv_print = priv_print;
  399. }
  400. void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
  401. int (*param_decode) (EVP_PKEY *pkey,
  402. const unsigned char **pder,
  403. int derlen),
  404. int (*param_encode) (const EVP_PKEY *pkey,
  405. unsigned char **pder),
  406. int (*param_missing) (const EVP_PKEY *pk),
  407. int (*param_copy) (EVP_PKEY *to,
  408. const EVP_PKEY *from),
  409. int (*param_cmp) (const EVP_PKEY *a,
  410. const EVP_PKEY *b),
  411. int (*param_print) (BIO *out,
  412. const EVP_PKEY *pkey,
  413. int indent, ASN1_PCTX *pctx))
  414. {
  415. ameth->param_decode = param_decode;
  416. ameth->param_encode = param_encode;
  417. ameth->param_missing = param_missing;
  418. ameth->param_copy = param_copy;
  419. ameth->param_cmp = param_cmp;
  420. ameth->param_print = param_print;
  421. }
  422. void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
  423. void (*pkey_free) (EVP_PKEY *pkey))
  424. {
  425. ameth->pkey_free = pkey_free;
  426. }
  427. void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
  428. int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
  429. long arg1, void *arg2))
  430. {
  431. ameth->pkey_ctrl = pkey_ctrl;
  432. }
  433. void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
  434. int (*item_verify) (EVP_MD_CTX *ctx,
  435. const ASN1_ITEM *it,
  436. void *asn,
  437. X509_ALGOR *a,
  438. ASN1_BIT_STRING *sig,
  439. EVP_PKEY *pkey),
  440. int (*item_sign) (EVP_MD_CTX *ctx,
  441. const ASN1_ITEM *it,
  442. void *asn,
  443. X509_ALGOR *alg1,
  444. X509_ALGOR *alg2,
  445. ASN1_BIT_STRING *sig))
  446. {
  447. ameth->item_sign = item_sign;
  448. ameth->item_verify = item_verify;
  449. }