p_dsa_asn1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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/bn.h>
  60. #include <openssl/bytestring.h>
  61. #include <openssl/dsa.h>
  62. #include <openssl/err.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/obj.h>
  65. #include <openssl/x509.h>
  66. #include "internal.h"
  67. static int dsa_pub_decode(EVP_PKEY *out, CBS *params, CBS *key) {
  68. /* See RFC 3279, section 2.3.2. */
  69. /* Parameters may or may not be present. */
  70. DSA *dsa;
  71. if (CBS_len(params) == 0) {
  72. dsa = DSA_new();
  73. if (dsa == NULL) {
  74. return 0;
  75. }
  76. } else {
  77. dsa = DSA_parse_parameters(params);
  78. if (dsa == NULL || CBS_len(params) != 0) {
  79. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  80. goto err;
  81. }
  82. }
  83. dsa->pub_key = BN_new();
  84. if (dsa->pub_key == NULL) {
  85. goto err;
  86. }
  87. if (!BN_parse_asn1_unsigned(key, dsa->pub_key) ||
  88. CBS_len(key) != 0) {
  89. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  90. goto err;
  91. }
  92. EVP_PKEY_assign_DSA(out, dsa);
  93. return 1;
  94. err:
  95. DSA_free(dsa);
  96. return 0;
  97. }
  98. static int dsa_pub_encode(CBB *out, const EVP_PKEY *key) {
  99. const DSA *dsa = key->pkey.dsa;
  100. const int has_params = dsa->p != NULL && dsa->q != NULL && dsa->g != NULL;
  101. /* See RFC 5480, section 2. */
  102. CBB spki, algorithm, key_bitstring;
  103. if (!CBB_add_asn1(out, &spki, CBS_ASN1_SEQUENCE) ||
  104. !CBB_add_asn1(&spki, &algorithm, CBS_ASN1_SEQUENCE) ||
  105. !OBJ_nid2cbb(&algorithm, NID_dsa) ||
  106. (has_params &&
  107. !DSA_marshal_parameters(&algorithm, dsa)) ||
  108. !CBB_add_asn1(&spki, &key_bitstring, CBS_ASN1_BITSTRING) ||
  109. !CBB_add_u8(&key_bitstring, 0 /* padding */) ||
  110. !BN_marshal_asn1(&key_bitstring, dsa->pub_key) ||
  111. !CBB_flush(out)) {
  112. OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
  113. return 0;
  114. }
  115. return 1;
  116. }
  117. static int dsa_priv_decode(EVP_PKEY *out, CBS *params, CBS *key) {
  118. /* See PKCS#11, v2.40, section 2.5. */
  119. /* Decode parameters. */
  120. BN_CTX *ctx = NULL;
  121. DSA *dsa = DSA_parse_parameters(params);
  122. if (dsa == NULL || CBS_len(params) != 0) {
  123. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  124. goto err;
  125. }
  126. dsa->priv_key = BN_new();
  127. dsa->pub_key = BN_new();
  128. if (dsa->priv_key == NULL || dsa->pub_key == NULL) {
  129. goto err;
  130. }
  131. /* Decode the key. */
  132. if (!BN_parse_asn1_unsigned(key, dsa->priv_key) ||
  133. CBS_len(key) != 0) {
  134. OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
  135. goto err;
  136. }
  137. /* Calculate the public key. */
  138. ctx = BN_CTX_new();
  139. if (ctx == NULL ||
  140. !BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
  141. goto err;
  142. }
  143. BN_CTX_free(ctx);
  144. EVP_PKEY_assign_DSA(out, dsa);
  145. return 1;
  146. err:
  147. BN_CTX_free(ctx);
  148. DSA_free(dsa);
  149. return 0;
  150. }
  151. static int dsa_priv_encode(CBB *out, const EVP_PKEY *key) {
  152. const DSA *dsa = key->pkey.dsa;
  153. if (dsa == NULL || dsa->priv_key == NULL) {
  154. OPENSSL_PUT_ERROR(EVP, EVP_R_MISSING_PARAMETERS);
  155. return 0;
  156. }
  157. /* See PKCS#11, v2.40, section 2.5. */
  158. CBB pkcs8, algorithm, private_key;
  159. if (!CBB_add_asn1(out, &pkcs8, CBS_ASN1_SEQUENCE) ||
  160. !CBB_add_asn1_uint64(&pkcs8, 0 /* version */) ||
  161. !CBB_add_asn1(&pkcs8, &algorithm, CBS_ASN1_SEQUENCE) ||
  162. !OBJ_nid2cbb(&algorithm, NID_dsa) ||
  163. !DSA_marshal_parameters(&algorithm, dsa) ||
  164. !CBB_add_asn1(&pkcs8, &private_key, CBS_ASN1_OCTETSTRING) ||
  165. !BN_marshal_asn1(&private_key, dsa->priv_key) ||
  166. !CBB_flush(out)) {
  167. OPENSSL_PUT_ERROR(EVP, EVP_R_ENCODE_ERROR);
  168. return 0;
  169. }
  170. return 1;
  171. }
  172. static int int_dsa_size(const EVP_PKEY *pkey) {
  173. return DSA_size(pkey->pkey.dsa);
  174. }
  175. static int dsa_bits(const EVP_PKEY *pkey) {
  176. return BN_num_bits(pkey->pkey.dsa->p);
  177. }
  178. static int dsa_missing_parameters(const EVP_PKEY *pkey) {
  179. DSA *dsa;
  180. dsa = pkey->pkey.dsa;
  181. if (dsa->p == NULL || dsa->q == NULL || dsa->g == NULL) {
  182. return 1;
  183. }
  184. return 0;
  185. }
  186. static int dup_bn_into(BIGNUM **out, BIGNUM *src) {
  187. BIGNUM *a;
  188. a = BN_dup(src);
  189. if (a == NULL) {
  190. return 0;
  191. }
  192. BN_free(*out);
  193. *out = a;
  194. return 1;
  195. }
  196. static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) {
  197. if (!dup_bn_into(&to->pkey.dsa->p, from->pkey.dsa->p) ||
  198. !dup_bn_into(&to->pkey.dsa->q, from->pkey.dsa->q) ||
  199. !dup_bn_into(&to->pkey.dsa->g, from->pkey.dsa->g)) {
  200. return 0;
  201. }
  202. return 1;
  203. }
  204. static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) {
  205. return BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) == 0 &&
  206. BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) == 0 &&
  207. BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g) == 0;
  208. }
  209. static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) {
  210. return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0;
  211. }
  212. static void int_dsa_free(EVP_PKEY *pkey) { DSA_free(pkey->pkey.dsa); }
  213. static void update_buflen(const BIGNUM *b, size_t *pbuflen) {
  214. size_t i;
  215. if (!b) {
  216. return;
  217. }
  218. i = BN_num_bytes(b);
  219. if (*pbuflen < i) {
  220. *pbuflen = i;
  221. }
  222. }
  223. static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
  224. uint8_t *m = NULL;
  225. int ret = 0;
  226. size_t buf_len = 0;
  227. const char *ktype = NULL;
  228. const BIGNUM *priv_key, *pub_key;
  229. priv_key = NULL;
  230. if (ptype == 2) {
  231. priv_key = x->priv_key;
  232. }
  233. pub_key = NULL;
  234. if (ptype > 0) {
  235. pub_key = x->pub_key;
  236. }
  237. ktype = "DSA-Parameters";
  238. if (ptype == 2) {
  239. ktype = "Private-Key";
  240. } else if (ptype == 1) {
  241. ktype = "Public-Key";
  242. }
  243. update_buflen(x->p, &buf_len);
  244. update_buflen(x->q, &buf_len);
  245. update_buflen(x->g, &buf_len);
  246. update_buflen(priv_key, &buf_len);
  247. update_buflen(pub_key, &buf_len);
  248. m = OPENSSL_malloc(buf_len + 10);
  249. if (m == NULL) {
  250. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  251. goto err;
  252. }
  253. if (priv_key) {
  254. if (!BIO_indent(bp, off, 128) ||
  255. BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p)) <= 0) {
  256. goto err;
  257. }
  258. }
  259. if (!ASN1_bn_print(bp, "priv:", priv_key, m, off) ||
  260. !ASN1_bn_print(bp, "pub: ", pub_key, m, off) ||
  261. !ASN1_bn_print(bp, "P: ", x->p, m, off) ||
  262. !ASN1_bn_print(bp, "Q: ", x->q, m, off) ||
  263. !ASN1_bn_print(bp, "G: ", x->g, m, off)) {
  264. goto err;
  265. }
  266. ret = 1;
  267. err:
  268. OPENSSL_free(m);
  269. return ret;
  270. }
  271. static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  272. ASN1_PCTX *ctx) {
  273. return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
  274. }
  275. static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  276. ASN1_PCTX *ctx) {
  277. return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
  278. }
  279. static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  280. ASN1_PCTX *ctx) {
  281. return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
  282. }
  283. static int old_dsa_priv_decode(EVP_PKEY *pkey, const uint8_t **pder,
  284. int derlen) {
  285. DSA *dsa;
  286. dsa = d2i_DSAPrivateKey(NULL, pder, derlen);
  287. if (dsa == NULL) {
  288. OPENSSL_PUT_ERROR(EVP, ERR_R_DSA_LIB);
  289. return 0;
  290. }
  291. EVP_PKEY_assign_DSA(pkey, dsa);
  292. return 1;
  293. }
  294. static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
  295. const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx) {
  296. DSA_SIG *dsa_sig;
  297. const uint8_t *p;
  298. if (!sig) {
  299. return BIO_puts(bp, "\n") > 0;
  300. }
  301. p = sig->data;
  302. dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
  303. if (dsa_sig == NULL) {
  304. return X509_signature_dump(bp, sig, indent);
  305. }
  306. int rv = 0;
  307. size_t buf_len = 0;
  308. uint8_t *m = NULL;
  309. update_buflen(dsa_sig->r, &buf_len);
  310. update_buflen(dsa_sig->s, &buf_len);
  311. m = OPENSSL_malloc(buf_len + 10);
  312. if (m == NULL) {
  313. OPENSSL_PUT_ERROR(EVP, ERR_R_MALLOC_FAILURE);
  314. goto err;
  315. }
  316. if (BIO_write(bp, "\n", 1) != 1 ||
  317. !ASN1_bn_print(bp, "r: ", dsa_sig->r, m, indent) ||
  318. !ASN1_bn_print(bp, "s: ", dsa_sig->s, m, indent)) {
  319. goto err;
  320. }
  321. rv = 1;
  322. err:
  323. OPENSSL_free(m);
  324. DSA_SIG_free(dsa_sig);
  325. return rv;
  326. }
  327. const EVP_PKEY_ASN1_METHOD dsa_asn1_meth = {
  328. EVP_PKEY_DSA,
  329. 0,
  330. "DSA",
  331. dsa_pub_decode,
  332. dsa_pub_encode,
  333. dsa_pub_cmp,
  334. dsa_pub_print,
  335. dsa_priv_decode,
  336. dsa_priv_encode,
  337. dsa_priv_print,
  338. NULL /* pkey_opaque */,
  339. NULL /* pkey_supports_digest */,
  340. int_dsa_size,
  341. dsa_bits,
  342. dsa_missing_parameters,
  343. dsa_copy_parameters,
  344. dsa_cmp_parameters,
  345. dsa_param_print,
  346. dsa_sig_print,
  347. int_dsa_free,
  348. old_dsa_priv_decode,
  349. NULL /* digest_verify_init_from_algorithm */,
  350. NULL /* digest_sign_algorithm */,
  351. };