Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

589 Zeilen
14 KiB

  1. /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
  2. * 2006.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com). */
  55. #include <openssl/evp.h>
  56. #include <openssl/asn1.h>
  57. #include <openssl/asn1t.h>
  58. #include <openssl/digest.h>
  59. #include <openssl/dsa.h>
  60. #include <openssl/err.h>
  61. #include <openssl/mem.h>
  62. #include <openssl/obj.h>
  63. #include <openssl/x509.h>
  64. #include "../dsa/internal.h"
  65. #include "internal.h"
  66. static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey) {
  67. const uint8_t *p, *pm;
  68. int pklen, pmlen;
  69. int ptype;
  70. void *pval;
  71. ASN1_STRING *pstr;
  72. X509_ALGOR *palg;
  73. ASN1_INTEGER *public_key = NULL;
  74. DSA *dsa = NULL;
  75. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) {
  76. return 0;
  77. }
  78. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  79. if (ptype == V_ASN1_SEQUENCE) {
  80. pstr = pval;
  81. pm = pstr->data;
  82. pmlen = pstr->length;
  83. dsa = d2i_DSAparams(NULL, &pm, pmlen);
  84. if (dsa == NULL) {
  85. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  86. goto err;
  87. }
  88. } else if (ptype == V_ASN1_NULL || ptype == V_ASN1_UNDEF) {
  89. dsa = DSA_new();
  90. if (dsa == NULL) {
  91. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  92. goto err;
  93. }
  94. } else {
  95. OPENSSL_PUT_ERROR(EVP, EVP_R_PARAMETER_ENCODING_ERROR);
  96. goto err;
  97. }
  98. public_key = d2i_ASN1_INTEGER(NULL, &p, pklen);
  99. if (public_key == NULL) {
  100. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  101. goto err;
  102. }
  103. dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL);
  104. if (dsa->pub_key == NULL) {
  105. OPENSSL_PUT_ERROR(EVP, EVP_R_BN_DECODE_ERROR);
  106. goto err;
  107. }
  108. ASN1_INTEGER_free(public_key);
  109. EVP_PKEY_assign_DSA(pkey, dsa);
  110. return 1;
  111. err:
  112. ASN1_INTEGER_free(public_key);
  113. DSA_free(dsa);
  114. return 0;
  115. }
  116. static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) {
  117. DSA *dsa;
  118. ASN1_STRING *pval = NULL;
  119. uint8_t *penc = NULL;
  120. int penclen;
  121. dsa = pkey->pkey.dsa;
  122. dsa->write_params = 0;
  123. int ptype;
  124. if (dsa->p && dsa->q && dsa->g) {
  125. pval = ASN1_STRING_new();
  126. if (!pval) {
  127. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  128. goto err;
  129. }
  130. pval->length = i2d_DSAparams(dsa, &pval->data);
  131. if (pval->length <= 0) {
  132. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  133. goto err;
  134. }
  135. ptype = V_ASN1_SEQUENCE;
  136. } else {
  137. ptype = V_ASN1_UNDEF;
  138. }
  139. penclen = i2d_DSAPublicKey(dsa, &penc);
  140. if (penclen <= 0) {
  141. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  142. goto err;
  143. }
  144. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DSA), ptype, pval,
  145. penc, penclen)) {
  146. return 1;
  147. }
  148. err:
  149. OPENSSL_free(penc);
  150. ASN1_STRING_free(pval);
  151. return 0;
  152. }
  153. static int dsa_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8) {
  154. const uint8_t *p, *pm;
  155. int pklen, pmlen;
  156. int ptype;
  157. void *pval;
  158. ASN1_STRING *pstr;
  159. X509_ALGOR *palg;
  160. ASN1_INTEGER *privkey = NULL;
  161. BN_CTX *ctx = NULL;
  162. /* In PKCS#8 DSA: you just get a private key integer and parameters in the
  163. * AlgorithmIdentifier the pubkey must be recalculated. */
  164. STACK_OF(ASN1_TYPE) *ndsa = NULL;
  165. DSA *dsa = NULL;
  166. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8)) {
  167. return 0;
  168. }
  169. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  170. /* Check for broken DSA PKCS#8, UGH! */
  171. if (*p == (V_ASN1_SEQUENCE | V_ASN1_CONSTRUCTED)) {
  172. ASN1_TYPE *t1, *t2;
  173. ndsa = d2i_ASN1_SEQUENCE_ANY(NULL, &p, pklen);
  174. if (ndsa == NULL) {
  175. goto decerr;
  176. }
  177. if (sk_ASN1_TYPE_num(ndsa) != 2) {
  178. goto decerr;
  179. }
  180. /* Handle Two broken types:
  181. * SEQUENCE {parameters, priv_key}
  182. * SEQUENCE {pub_key, priv_key}. */
  183. t1 = sk_ASN1_TYPE_value(ndsa, 0);
  184. t2 = sk_ASN1_TYPE_value(ndsa, 1);
  185. if (t1->type == V_ASN1_SEQUENCE) {
  186. p8->broken = PKCS8_EMBEDDED_PARAM;
  187. pval = t1->value.ptr;
  188. } else if (ptype == V_ASN1_SEQUENCE) {
  189. p8->broken = PKCS8_NS_DB;
  190. } else {
  191. goto decerr;
  192. }
  193. if (t2->type != V_ASN1_INTEGER) {
  194. goto decerr;
  195. }
  196. privkey = t2->value.integer;
  197. } else {
  198. const uint8_t *q = p;
  199. privkey = d2i_ASN1_INTEGER(NULL, &p, pklen);
  200. if (privkey == NULL) {
  201. goto decerr;
  202. }
  203. if (privkey->type == V_ASN1_NEG_INTEGER) {
  204. p8->broken = PKCS8_NEG_PRIVKEY;
  205. ASN1_INTEGER_free(privkey);
  206. privkey = d2i_ASN1_UINTEGER(NULL, &q, pklen);
  207. if (privkey == NULL) {
  208. goto decerr;
  209. }
  210. }
  211. if (ptype != V_ASN1_SEQUENCE) {
  212. goto decerr;
  213. }
  214. }
  215. pstr = pval;
  216. pm = pstr->data;
  217. pmlen = pstr->length;
  218. dsa = d2i_DSAparams(NULL, &pm, pmlen);
  219. if (dsa == NULL) {
  220. goto decerr;
  221. }
  222. /* We have parameters. Now set private key */
  223. dsa->priv_key = ASN1_INTEGER_to_BN(privkey, NULL);
  224. if (dsa->priv_key == NULL) {
  225. OPENSSL_PUT_ERROR(EVP, ERR_LIB_BN);
  226. goto dsaerr;
  227. }
  228. /* Calculate public key. */
  229. dsa->pub_key = BN_new();
  230. if (dsa->pub_key == NULL) {
  231. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  232. goto dsaerr;
  233. }
  234. ctx = BN_CTX_new();
  235. if (ctx == NULL) {
  236. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  237. goto dsaerr;
  238. }
  239. if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
  240. OPENSSL_PUT_ERROR(EVP, ERR_LIB_BN);
  241. goto dsaerr;
  242. }
  243. EVP_PKEY_assign_DSA(pkey, dsa);
  244. BN_CTX_free(ctx);
  245. sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
  246. ASN1_INTEGER_free(privkey);
  247. return 1;
  248. decerr:
  249. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  250. dsaerr:
  251. BN_CTX_free(ctx);
  252. ASN1_INTEGER_free(privkey);
  253. sk_ASN1_TYPE_pop_free(ndsa, ASN1_TYPE_free);
  254. DSA_free(dsa);
  255. return 0;
  256. }
  257. static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) {
  258. ASN1_STRING *params = NULL;
  259. ASN1_INTEGER *prkey = NULL;
  260. uint8_t *dp = NULL;
  261. int dplen;
  262. if (!pkey->pkey.dsa || !pkey->pkey.dsa->priv_key) {
  263. OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
  264. goto err;
  265. }
  266. params = ASN1_STRING_new();
  267. if (!params) {
  268. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  269. goto err;
  270. }
  271. params->length = i2d_DSAparams(pkey->pkey.dsa, &params->data);
  272. if (params->length <= 0) {
  273. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  274. goto err;
  275. }
  276. params->type = V_ASN1_SEQUENCE;
  277. /* Get private key into integer. */
  278. prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
  279. if (!prkey) {
  280. OPENSSL_PUT_ERROR(EVP, ERR_LIB_BN);
  281. goto err;
  282. }
  283. dplen = i2d_ASN1_INTEGER(prkey, &dp);
  284. ASN1_INTEGER_free(prkey);
  285. prkey = NULL;
  286. if (!PKCS8_pkey_set0(p8, (ASN1_OBJECT *)OBJ_nid2obj(NID_dsa), 0,
  287. V_ASN1_SEQUENCE, params, dp, dplen)) {
  288. goto err;
  289. }
  290. return 1;
  291. err:
  292. OPENSSL_free(dp);
  293. ASN1_STRING_free(params);
  294. ASN1_INTEGER_free(prkey);
  295. return 0;
  296. }
  297. static int int_dsa_size(const EVP_PKEY *pkey) {
  298. return DSA_size(pkey->pkey.dsa);
  299. }
  300. static int dsa_bits(const EVP_PKEY *pkey) {
  301. return BN_num_bits(pkey->pkey.dsa->p);
  302. }
  303. static int dsa_missing_parameters(const EVP_PKEY *pkey) {
  304. DSA *dsa;
  305. dsa = pkey->pkey.dsa;
  306. if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
  307. return 1;
  308. }
  309. return 0;
  310. }
  311. static int dup_bn_into(BIGNUM **out, BIGNUM *src) {
  312. BIGNUM *a;
  313. a = BN_dup(src);
  314. if (a == NULL) {
  315. return 0;
  316. }
  317. BN_free(*out);
  318. *out = a;
  319. return 1;
  320. }
  321. static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
  322. if (!dup_bn_into(&to->pkey.dsa->p, from->pkey.dsa->p) ||
  323. !dup_bn_into(&to->pkey.dsa->q, from->pkey.dsa->q) ||
  324. !dup_bn_into(&to->pkey.dsa->g, from->pkey.dsa->g)) {
  325. return 0;
  326. }
  327. return 1;
  328. }
  329. static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
  330. return BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) == 0 &&
  331. BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) == 0 &&
  332. BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g) == 0;
  333. }
  334. static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
  335. return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0;
  336. }
  337. static void int_dsa_free(EVP_PKEY *pkey) { DSA_free(pkey->pkey.dsa); }
  338. static void update_buflen(const BIGNUM *b, size_t *pbuflen) {
  339. size_t i;
  340. if (!b) {
  341. return;
  342. }
  343. i = BN_num_bytes(b);
  344. if (*pbuflen < i) {
  345. *pbuflen = i;
  346. }
  347. }
  348. static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
  349. uint8_t *m = NULL;
  350. int ret = 0;
  351. size_t buf_len = 0;
  352. const char *ktype = NULL;
  353. const BIGNUM *priv_key, *pub_key;
  354. priv_key = NULL;
  355. if (ptype == 2) {
  356. priv_key = x->priv_key;
  357. }
  358. pub_key = NULL;
  359. if (ptype > 0) {
  360. pub_key = x->pub_key;
  361. }
  362. ktype = "DSA-Parameters";
  363. if (ptype == 2) {
  364. ktype = "Private-Key";
  365. } else if (ptype == 1) {
  366. ktype = "Public-Key";
  367. }
  368. update_buflen(x->p, &buf_len);
  369. update_buflen(x->q, &buf_len);
  370. update_buflen(x->g, &buf_len);
  371. update_buflen(priv_key, &buf_len);
  372. update_buflen(pub_key, &buf_len);
  373. m = (uint8_t *)OPENSSL_malloc(buf_len + 10);
  374. if (m == NULL) {
  375. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  376. goto err;
  377. }
  378. if (priv_key) {
  379. if (!BIO_indent(bp, off, 128) ||
  380. BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p)) <= 0) {
  381. goto err;
  382. }
  383. }
  384. if (!ASN1_bn_print(bp, "priv:", priv_key, m, off) ||
  385. !ASN1_bn_print(bp, "pub: ", pub_key, m, off) ||
  386. !ASN1_bn_print(bp, "P: ", x->p, m, off) ||
  387. !ASN1_bn_print(bp, "Q: ", x->q, m, off) ||
  388. !ASN1_bn_print(bp, "G: ", x->g, m, off)) {
  389. goto err;
  390. }
  391. ret = 1;
  392. err:
  393. OPENSSL_free(m);
  394. return ret;
  395. }
  396. static int dsa_param_decode(EVP_PKEY *pkey, const uint8_t **pder, int derlen) {
  397. DSA *dsa;
  398. dsa = d2i_DSAparams(NULL, pder, derlen);
  399. if (dsa == NULL) {
  400. OPENSSL_PUT_ERROR(EVP, ERR_R_DSA_LIB);
  401. return 0;
  402. }
  403. EVP_PKEY_assign_DSA(pkey, dsa);
  404. return 1;
  405. }
  406. static int dsa_param_encode(const EVP_PKEY *pkey, uint8_t **pder) {
  407. return i2d_DSAparams(pkey->pkey.dsa, pder);
  408. }
  409. static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  410. ASN1_PCTX *ctx) {
  411. return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
  412. }
  413. static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  414. ASN1_PCTX *ctx) {
  415. return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
  416. }
  417. static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  418. ASN1_PCTX *ctx) {
  419. return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
  420. }
  421. static int old_dsa_priv_decode(EVP_PKEY *pkey, const uint8_t **pder,
  422. int derlen) {
  423. DSA *dsa;
  424. dsa = d2i_DSAPrivateKey(NULL, pder, derlen);
  425. if (dsa == NULL) {
  426. OPENSSL_PUT_ERROR(EVP, ERR_R_DSA_LIB);
  427. return 0;
  428. }
  429. EVP_PKEY_assign_DSA(pkey, dsa);
  430. return 1;
  431. }
  432. static int old_dsa_priv_encode(const EVP_PKEY *pkey, uint8_t **pder) {
  433. return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
  434. }
  435. static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  436. const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx) {
  437. DSA_SIG *dsa_sig;
  438. const uint8_t *p;
  439. if (!sig) {
  440. return BIO_puts(bp, "\n") > 0;
  441. }
  442. p = sig->data;
  443. dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
  444. if (dsa_sig == NULL) {
  445. return X509_signature_dump(bp, sig, indent);
  446. }
  447. int rv = 0;
  448. size_t buf_len = 0;
  449. uint8_t *m = NULL;
  450. update_buflen(dsa_sig->r, &buf_len);
  451. update_buflen(dsa_sig->s, &buf_len);
  452. m = OPENSSL_malloc(buf_len + 10);
  453. if (m == NULL) {
  454. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  455. goto err;
  456. }
  457. if (BIO_write(bp, "\n", 1) != 1 ||
  458. !ASN1_bn_print(bp, "r: ", dsa_sig->r, m, indent) ||
  459. !ASN1_bn_print(bp, "s: ", dsa_sig->s, m, indent)) {
  460. goto err;
  461. }
  462. rv = 1;
  463. err:
  464. OPENSSL_free(m);
  465. DSA_SIG_free(dsa_sig);
  466. return rv;
  467. }
  468. const EVP_PKEY_ASN1_METHOD dsa_asn1_meth = {
  469. EVP_PKEY_DSA,
  470. EVP_PKEY_DSA,
  471. 0,
  472. "DSA",
  473. dsa_pub_decode,
  474. dsa_pub_encode,
  475. dsa_pub_cmp,
  476. dsa_pub_print,
  477. dsa_priv_decode,
  478. dsa_priv_encode,
  479. dsa_priv_print,
  480. NULL /* pkey_opaque */,
  481. NULL /* pkey_supports_digest */,
  482. int_dsa_size,
  483. dsa_bits,
  484. dsa_param_decode,
  485. dsa_param_encode,
  486. dsa_missing_parameters,
  487. dsa_copy_parameters,
  488. dsa_cmp_parameters,
  489. dsa_param_print,
  490. dsa_sig_print,
  491. int_dsa_free,
  492. old_dsa_priv_decode,
  493. old_dsa_priv_encode,
  494. NULL /* digest_verify_init_from_algorithm */,
  495. NULL /* digest_sign_algorithm */,
  496. };