ameth_lib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  3. * 2006.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2006 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. * licensing@OpenSSL.org.
  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. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  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. if (app_methods == NULL) {
  222. app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
  223. if (!app_methods)
  224. return 0;
  225. }
  226. if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
  227. return 0;
  228. sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
  229. return 1;
  230. }
  231. int EVP_PKEY_asn1_add_alias(int to, int from)
  232. {
  233. EVP_PKEY_ASN1_METHOD *ameth;
  234. ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
  235. if (!ameth)
  236. return 0;
  237. ameth->pkey_base_id = to;
  238. if (!EVP_PKEY_asn1_add0(ameth)) {
  239. EVP_PKEY_asn1_free(ameth);
  240. return 0;
  241. }
  242. return 1;
  243. }
  244. int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
  245. int *ppkey_flags, const char **pinfo,
  246. const char **ppem_str,
  247. const EVP_PKEY_ASN1_METHOD *ameth)
  248. {
  249. if (!ameth)
  250. return 0;
  251. if (ppkey_id)
  252. *ppkey_id = ameth->pkey_id;
  253. if (ppkey_base_id)
  254. *ppkey_base_id = ameth->pkey_base_id;
  255. if (ppkey_flags)
  256. *ppkey_flags = ameth->pkey_flags;
  257. if (pinfo)
  258. *pinfo = ameth->info;
  259. if (ppem_str)
  260. *ppem_str = ameth->pem_str;
  261. return 1;
  262. }
  263. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(EVP_PKEY *pkey)
  264. {
  265. return pkey->ameth;
  266. }
  267. EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
  268. const char *pem_str, const char *info)
  269. {
  270. EVP_PKEY_ASN1_METHOD *ameth;
  271. ameth = OPENSSL_malloc(sizeof(EVP_PKEY_ASN1_METHOD));
  272. if (!ameth)
  273. return NULL;
  274. memset(ameth, 0, sizeof(EVP_PKEY_ASN1_METHOD));
  275. ameth->pkey_id = id;
  276. ameth->pkey_base_id = id;
  277. ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
  278. if (info) {
  279. ameth->info = BUF_strdup(info);
  280. if (!ameth->info)
  281. goto err;
  282. } else
  283. ameth->info = NULL;
  284. if (pem_str) {
  285. ameth->pem_str = BUF_strdup(pem_str);
  286. if (!ameth->pem_str)
  287. goto err;
  288. } else
  289. ameth->pem_str = NULL;
  290. ameth->pub_decode = 0;
  291. ameth->pub_encode = 0;
  292. ameth->pub_cmp = 0;
  293. ameth->pub_print = 0;
  294. ameth->priv_decode = 0;
  295. ameth->priv_encode = 0;
  296. ameth->priv_print = 0;
  297. ameth->old_priv_encode = 0;
  298. ameth->old_priv_decode = 0;
  299. ameth->item_verify = 0;
  300. ameth->item_sign = 0;
  301. ameth->pkey_size = 0;
  302. ameth->pkey_bits = 0;
  303. ameth->param_decode = 0;
  304. ameth->param_encode = 0;
  305. ameth->param_missing = 0;
  306. ameth->param_copy = 0;
  307. ameth->param_cmp = 0;
  308. ameth->param_print = 0;
  309. ameth->pkey_free = 0;
  310. ameth->pkey_ctrl = 0;
  311. return ameth;
  312. err:
  313. EVP_PKEY_asn1_free(ameth);
  314. return NULL;
  315. }
  316. void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
  317. const EVP_PKEY_ASN1_METHOD *src)
  318. {
  319. dst->pub_decode = src->pub_decode;
  320. dst->pub_encode = src->pub_encode;
  321. dst->pub_cmp = src->pub_cmp;
  322. dst->pub_print = src->pub_print;
  323. dst->priv_decode = src->priv_decode;
  324. dst->priv_encode = src->priv_encode;
  325. dst->priv_print = src->priv_print;
  326. dst->old_priv_encode = src->old_priv_encode;
  327. dst->old_priv_decode = src->old_priv_decode;
  328. dst->pkey_size = src->pkey_size;
  329. dst->pkey_bits = src->pkey_bits;
  330. dst->param_decode = src->param_decode;
  331. dst->param_encode = src->param_encode;
  332. dst->param_missing = src->param_missing;
  333. dst->param_copy = src->param_copy;
  334. dst->param_cmp = src->param_cmp;
  335. dst->param_print = src->param_print;
  336. dst->pkey_free = src->pkey_free;
  337. dst->pkey_ctrl = src->pkey_ctrl;
  338. dst->item_sign = src->item_sign;
  339. dst->item_verify = src->item_verify;
  340. }
  341. void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
  342. {
  343. if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
  344. if (ameth->pem_str)
  345. OPENSSL_free(ameth->pem_str);
  346. if (ameth->info)
  347. OPENSSL_free(ameth->info);
  348. OPENSSL_free(ameth);
  349. }
  350. }
  351. void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
  352. int (*pub_decode) (EVP_PKEY *pk,
  353. X509_PUBKEY *pub),
  354. int (*pub_encode) (X509_PUBKEY *pub,
  355. const EVP_PKEY *pk),
  356. int (*pub_cmp) (const EVP_PKEY *a,
  357. const EVP_PKEY *b),
  358. int (*pub_print) (BIO *out,
  359. const EVP_PKEY *pkey,
  360. int indent, ASN1_PCTX *pctx),
  361. int (*pkey_size) (const EVP_PKEY *pk),
  362. int (*pkey_bits) (const EVP_PKEY *pk))
  363. {
  364. ameth->pub_decode = pub_decode;
  365. ameth->pub_encode = pub_encode;
  366. ameth->pub_cmp = pub_cmp;
  367. ameth->pub_print = pub_print;
  368. ameth->pkey_size = pkey_size;
  369. ameth->pkey_bits = pkey_bits;
  370. }
  371. void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
  372. int (*priv_decode) (EVP_PKEY *pk,
  373. PKCS8_PRIV_KEY_INFO
  374. *p8inf),
  375. int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
  376. const EVP_PKEY *pk),
  377. int (*priv_print) (BIO *out,
  378. const EVP_PKEY *pkey,
  379. int indent,
  380. ASN1_PCTX *pctx))
  381. {
  382. ameth->priv_decode = priv_decode;
  383. ameth->priv_encode = priv_encode;
  384. ameth->priv_print = priv_print;
  385. }
  386. void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
  387. int (*param_decode) (EVP_PKEY *pkey,
  388. const unsigned char **pder,
  389. int derlen),
  390. int (*param_encode) (const EVP_PKEY *pkey,
  391. unsigned char **pder),
  392. int (*param_missing) (const EVP_PKEY *pk),
  393. int (*param_copy) (EVP_PKEY *to,
  394. const EVP_PKEY *from),
  395. int (*param_cmp) (const EVP_PKEY *a,
  396. const EVP_PKEY *b),
  397. int (*param_print) (BIO *out,
  398. const EVP_PKEY *pkey,
  399. int indent, ASN1_PCTX *pctx))
  400. {
  401. ameth->param_decode = param_decode;
  402. ameth->param_encode = param_encode;
  403. ameth->param_missing = param_missing;
  404. ameth->param_copy = param_copy;
  405. ameth->param_cmp = param_cmp;
  406. ameth->param_print = param_print;
  407. }
  408. void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
  409. void (*pkey_free) (EVP_PKEY *pkey))
  410. {
  411. ameth->pkey_free = pkey_free;
  412. }
  413. void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
  414. int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
  415. long arg1, void *arg2))
  416. {
  417. ameth->pkey_ctrl = pkey_ctrl;
  418. }
  419. void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
  420. int (*item_verify) (EVP_MD_CTX *ctx,
  421. const ASN1_ITEM *it,
  422. void *asn,
  423. X509_ALGOR *a,
  424. ASN1_BIT_STRING *sig,
  425. EVP_PKEY *pkey),
  426. int (*item_sign) (EVP_MD_CTX *ctx,
  427. const ASN1_ITEM *it,
  428. void *asn,
  429. X509_ALGOR *alg1,
  430. X509_ALGOR *alg2,
  431. ASN1_BIT_STRING *sig))
  432. {
  433. ameth->item_sign = item_sign;
  434. ameth->item_verify = item_verify;
  435. }