ameth_lib.c 14 KB

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