ameth_lib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. &cmac_asn1_meth,
  94. #ifndef OPENSSL_NO_DH
  95. &dhx_asn1_meth
  96. #endif
  97. };
  98. typedef int sk_cmp_fn_type(const char *const *a, const char *const *b);
  99. DECLARE_STACK_OF(EVP_PKEY_ASN1_METHOD)
  100. static STACK_OF(EVP_PKEY_ASN1_METHOD) *app_methods = NULL;
  101. #ifdef TEST
  102. void main()
  103. {
  104. int i;
  105. for (i = 0;
  106. i < sizeof(standard_methods) / sizeof(EVP_PKEY_ASN1_METHOD *); i++)
  107. fprintf(stderr, "Number %d id=%d (%s)\n", i,
  108. standard_methods[i]->pkey_id,
  109. OBJ_nid2sn(standard_methods[i]->pkey_id));
  110. }
  111. #endif
  112. DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
  113. const EVP_PKEY_ASN1_METHOD *, ameth);
  114. static int ameth_cmp(const EVP_PKEY_ASN1_METHOD *const *a,
  115. const EVP_PKEY_ASN1_METHOD *const *b)
  116. {
  117. return ((*a)->pkey_id - (*b)->pkey_id);
  118. }
  119. IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_ASN1_METHOD *,
  120. const EVP_PKEY_ASN1_METHOD *, ameth);
  121. int EVP_PKEY_asn1_get_count(void)
  122. {
  123. int num = sizeof(standard_methods) / sizeof(EVP_PKEY_ASN1_METHOD *);
  124. if (app_methods)
  125. num += sk_EVP_PKEY_ASN1_METHOD_num(app_methods);
  126. return num;
  127. }
  128. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_get0(int idx)
  129. {
  130. int num = sizeof(standard_methods) / sizeof(EVP_PKEY_ASN1_METHOD *);
  131. if (idx < 0)
  132. return NULL;
  133. if (idx < num)
  134. return standard_methods[idx];
  135. idx -= num;
  136. return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
  137. }
  138. static const EVP_PKEY_ASN1_METHOD *pkey_asn1_find(int type)
  139. {
  140. EVP_PKEY_ASN1_METHOD tmp;
  141. const EVP_PKEY_ASN1_METHOD *t = &tmp, **ret;
  142. tmp.pkey_id = type;
  143. if (app_methods) {
  144. int idx;
  145. idx = sk_EVP_PKEY_ASN1_METHOD_find(app_methods, &tmp);
  146. if (idx >= 0)
  147. return sk_EVP_PKEY_ASN1_METHOD_value(app_methods, idx);
  148. }
  149. ret = OBJ_bsearch_ameth(&t, standard_methods, sizeof(standard_methods)
  150. / sizeof(EVP_PKEY_ASN1_METHOD *));
  151. if (!ret || !*ret)
  152. return NULL;
  153. return *ret;
  154. }
  155. /*
  156. * Find an implementation of an ASN1 algorithm. If 'pe' is not NULL also
  157. * search through engines and set *pe to a functional reference to the engine
  158. * implementing 'type' or NULL if no engine implements it.
  159. */
  160. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(ENGINE **pe, int type)
  161. {
  162. const EVP_PKEY_ASN1_METHOD *t;
  163. for (;;) {
  164. t = pkey_asn1_find(type);
  165. if (!t || !(t->pkey_flags & ASN1_PKEY_ALIAS))
  166. break;
  167. type = t->pkey_base_id;
  168. }
  169. if (pe) {
  170. #ifndef OPENSSL_NO_ENGINE
  171. ENGINE *e;
  172. /* type will contain the final unaliased type */
  173. e = ENGINE_get_pkey_asn1_meth_engine(type);
  174. if (e) {
  175. *pe = e;
  176. return ENGINE_get_pkey_asn1_meth(e, type);
  177. }
  178. #endif
  179. *pe = NULL;
  180. }
  181. return t;
  182. }
  183. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find_str(ENGINE **pe,
  184. const char *str, int len)
  185. {
  186. int i;
  187. const EVP_PKEY_ASN1_METHOD *ameth;
  188. if (len == -1)
  189. len = strlen(str);
  190. if (pe) {
  191. #ifndef OPENSSL_NO_ENGINE
  192. ENGINE *e;
  193. ameth = ENGINE_pkey_asn1_find_str(&e, str, len);
  194. if (ameth) {
  195. /*
  196. * Convert structural into functional reference
  197. */
  198. if (!ENGINE_init(e))
  199. ameth = NULL;
  200. ENGINE_free(e);
  201. *pe = e;
  202. return ameth;
  203. }
  204. #endif
  205. *pe = NULL;
  206. }
  207. for (i = 0; i < EVP_PKEY_asn1_get_count(); i++) {
  208. ameth = EVP_PKEY_asn1_get0(i);
  209. if (ameth->pkey_flags & ASN1_PKEY_ALIAS)
  210. continue;
  211. if (((int)strlen(ameth->pem_str) == len) &&
  212. !strncasecmp(ameth->pem_str, str, len))
  213. return ameth;
  214. }
  215. return NULL;
  216. }
  217. int EVP_PKEY_asn1_add0(const EVP_PKEY_ASN1_METHOD *ameth)
  218. {
  219. if (app_methods == NULL) {
  220. app_methods = sk_EVP_PKEY_ASN1_METHOD_new(ameth_cmp);
  221. if (!app_methods)
  222. return 0;
  223. }
  224. if (!sk_EVP_PKEY_ASN1_METHOD_push(app_methods, ameth))
  225. return 0;
  226. sk_EVP_PKEY_ASN1_METHOD_sort(app_methods);
  227. return 1;
  228. }
  229. int EVP_PKEY_asn1_add_alias(int to, int from)
  230. {
  231. EVP_PKEY_ASN1_METHOD *ameth;
  232. ameth = EVP_PKEY_asn1_new(from, ASN1_PKEY_ALIAS, NULL, NULL);
  233. if (!ameth)
  234. return 0;
  235. ameth->pkey_base_id = to;
  236. if (!EVP_PKEY_asn1_add0(ameth)) {
  237. EVP_PKEY_asn1_free(ameth);
  238. return 0;
  239. }
  240. return 1;
  241. }
  242. int EVP_PKEY_asn1_get0_info(int *ppkey_id, int *ppkey_base_id,
  243. int *ppkey_flags, const char **pinfo,
  244. const char **ppem_str,
  245. const EVP_PKEY_ASN1_METHOD *ameth)
  246. {
  247. if (!ameth)
  248. return 0;
  249. if (ppkey_id)
  250. *ppkey_id = ameth->pkey_id;
  251. if (ppkey_base_id)
  252. *ppkey_base_id = ameth->pkey_base_id;
  253. if (ppkey_flags)
  254. *ppkey_flags = ameth->pkey_flags;
  255. if (pinfo)
  256. *pinfo = ameth->info;
  257. if (ppem_str)
  258. *ppem_str = ameth->pem_str;
  259. return 1;
  260. }
  261. const EVP_PKEY_ASN1_METHOD *EVP_PKEY_get0_asn1(EVP_PKEY *pkey)
  262. {
  263. return pkey->ameth;
  264. }
  265. EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_new(int id, int flags,
  266. const char *pem_str, const char *info)
  267. {
  268. EVP_PKEY_ASN1_METHOD *ameth;
  269. ameth = OPENSSL_malloc(sizeof(EVP_PKEY_ASN1_METHOD));
  270. if (!ameth)
  271. return NULL;
  272. memset(ameth, 0, sizeof(EVP_PKEY_ASN1_METHOD));
  273. ameth->pkey_id = id;
  274. ameth->pkey_base_id = id;
  275. ameth->pkey_flags = flags | ASN1_PKEY_DYNAMIC;
  276. if (info) {
  277. ameth->info = BUF_strdup(info);
  278. if (!ameth->info)
  279. goto err;
  280. } else
  281. ameth->info = NULL;
  282. if (pem_str) {
  283. ameth->pem_str = BUF_strdup(pem_str);
  284. if (!ameth->pem_str)
  285. goto err;
  286. } else
  287. ameth->pem_str = NULL;
  288. ameth->pub_decode = 0;
  289. ameth->pub_encode = 0;
  290. ameth->pub_cmp = 0;
  291. ameth->pub_print = 0;
  292. ameth->priv_decode = 0;
  293. ameth->priv_encode = 0;
  294. ameth->priv_print = 0;
  295. ameth->old_priv_encode = 0;
  296. ameth->old_priv_decode = 0;
  297. ameth->item_verify = 0;
  298. ameth->item_sign = 0;
  299. ameth->pkey_size = 0;
  300. ameth->pkey_bits = 0;
  301. ameth->param_decode = 0;
  302. ameth->param_encode = 0;
  303. ameth->param_missing = 0;
  304. ameth->param_copy = 0;
  305. ameth->param_cmp = 0;
  306. ameth->param_print = 0;
  307. ameth->pkey_free = 0;
  308. ameth->pkey_ctrl = 0;
  309. return ameth;
  310. err:
  311. EVP_PKEY_asn1_free(ameth);
  312. return NULL;
  313. }
  314. void EVP_PKEY_asn1_copy(EVP_PKEY_ASN1_METHOD *dst,
  315. const EVP_PKEY_ASN1_METHOD *src)
  316. {
  317. dst->pub_decode = src->pub_decode;
  318. dst->pub_encode = src->pub_encode;
  319. dst->pub_cmp = src->pub_cmp;
  320. dst->pub_print = src->pub_print;
  321. dst->priv_decode = src->priv_decode;
  322. dst->priv_encode = src->priv_encode;
  323. dst->priv_print = src->priv_print;
  324. dst->old_priv_encode = src->old_priv_encode;
  325. dst->old_priv_decode = src->old_priv_decode;
  326. dst->pkey_size = src->pkey_size;
  327. dst->pkey_bits = src->pkey_bits;
  328. dst->param_decode = src->param_decode;
  329. dst->param_encode = src->param_encode;
  330. dst->param_missing = src->param_missing;
  331. dst->param_copy = src->param_copy;
  332. dst->param_cmp = src->param_cmp;
  333. dst->param_print = src->param_print;
  334. dst->pkey_free = src->pkey_free;
  335. dst->pkey_ctrl = src->pkey_ctrl;
  336. dst->item_sign = src->item_sign;
  337. dst->item_verify = src->item_verify;
  338. }
  339. void EVP_PKEY_asn1_free(EVP_PKEY_ASN1_METHOD *ameth)
  340. {
  341. if (ameth && (ameth->pkey_flags & ASN1_PKEY_DYNAMIC)) {
  342. if (ameth->pem_str)
  343. OPENSSL_free(ameth->pem_str);
  344. if (ameth->info)
  345. OPENSSL_free(ameth->info);
  346. OPENSSL_free(ameth);
  347. }
  348. }
  349. void EVP_PKEY_asn1_set_public(EVP_PKEY_ASN1_METHOD *ameth,
  350. int (*pub_decode) (EVP_PKEY *pk,
  351. X509_PUBKEY *pub),
  352. int (*pub_encode) (X509_PUBKEY *pub,
  353. const EVP_PKEY *pk),
  354. int (*pub_cmp) (const EVP_PKEY *a,
  355. const EVP_PKEY *b),
  356. int (*pub_print) (BIO *out,
  357. const EVP_PKEY *pkey,
  358. int indent, ASN1_PCTX *pctx),
  359. int (*pkey_size) (const EVP_PKEY *pk),
  360. int (*pkey_bits) (const EVP_PKEY *pk))
  361. {
  362. ameth->pub_decode = pub_decode;
  363. ameth->pub_encode = pub_encode;
  364. ameth->pub_cmp = pub_cmp;
  365. ameth->pub_print = pub_print;
  366. ameth->pkey_size = pkey_size;
  367. ameth->pkey_bits = pkey_bits;
  368. }
  369. void EVP_PKEY_asn1_set_private(EVP_PKEY_ASN1_METHOD *ameth,
  370. int (*priv_decode) (EVP_PKEY *pk,
  371. PKCS8_PRIV_KEY_INFO
  372. *p8inf),
  373. int (*priv_encode) (PKCS8_PRIV_KEY_INFO *p8,
  374. const EVP_PKEY *pk),
  375. int (*priv_print) (BIO *out,
  376. const EVP_PKEY *pkey,
  377. int indent,
  378. ASN1_PCTX *pctx))
  379. {
  380. ameth->priv_decode = priv_decode;
  381. ameth->priv_encode = priv_encode;
  382. ameth->priv_print = priv_print;
  383. }
  384. void EVP_PKEY_asn1_set_param(EVP_PKEY_ASN1_METHOD *ameth,
  385. int (*param_decode) (EVP_PKEY *pkey,
  386. const unsigned char **pder,
  387. int derlen),
  388. int (*param_encode) (const EVP_PKEY *pkey,
  389. unsigned char **pder),
  390. int (*param_missing) (const EVP_PKEY *pk),
  391. int (*param_copy) (EVP_PKEY *to,
  392. const EVP_PKEY *from),
  393. int (*param_cmp) (const EVP_PKEY *a,
  394. const EVP_PKEY *b),
  395. int (*param_print) (BIO *out,
  396. const EVP_PKEY *pkey,
  397. int indent, ASN1_PCTX *pctx))
  398. {
  399. ameth->param_decode = param_decode;
  400. ameth->param_encode = param_encode;
  401. ameth->param_missing = param_missing;
  402. ameth->param_copy = param_copy;
  403. ameth->param_cmp = param_cmp;
  404. ameth->param_print = param_print;
  405. }
  406. void EVP_PKEY_asn1_set_free(EVP_PKEY_ASN1_METHOD *ameth,
  407. void (*pkey_free) (EVP_PKEY *pkey))
  408. {
  409. ameth->pkey_free = pkey_free;
  410. }
  411. void EVP_PKEY_asn1_set_ctrl(EVP_PKEY_ASN1_METHOD *ameth,
  412. int (*pkey_ctrl) (EVP_PKEY *pkey, int op,
  413. long arg1, void *arg2))
  414. {
  415. ameth->pkey_ctrl = pkey_ctrl;
  416. }
  417. void EVP_PKEY_asn1_set_item(EVP_PKEY_ASN1_METHOD *ameth,
  418. int (*item_verify) (EVP_MD_CTX *ctx,
  419. const ASN1_ITEM *it,
  420. void *asn,
  421. X509_ALGOR *a,
  422. ASN1_BIT_STRING *sig,
  423. EVP_PKEY *pkey),
  424. int (*item_sign) (EVP_MD_CTX *ctx,
  425. const ASN1_ITEM *it,
  426. void *asn,
  427. X509_ALGOR *alg1,
  428. X509_ALGOR *alg2,
  429. ASN1_BIT_STRING *sig))
  430. {
  431. ameth->item_sign = item_sign;
  432. ameth->item_verify = item_verify;
  433. }