1 /* $OpenBSD: dsa_lib.c,v 1.29 2018/04/14 07:09:21 tb Exp $ */
2 /* $OpenBSD: rsa_lib.c,v 1.37 2018/04/14 07:09:21 tb Exp $ */
3 /* $OpenBSD: evp_lib.c,v 1.17 2018/09/12 06:35:38 djm Exp $ */
4 /* $OpenBSD: dh_lib.c,v 1.32 2018/05/02 15:48:38 tb Exp $ */
5 /* $OpenBSD: p_lib.c,v 1.24 2018/05/30 15:40:50 tb Exp $ */
6 /* $OpenBSD: digest.c,v 1.30 2018/04/14 07:09:21 tb Exp $ */
7 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
10 * This package is an SSL implementation written
11 * by Eric Young (eay@cryptsoft.com).
12 * The implementation was written so as to conform with Netscapes SSL.
14 * This library is free for commercial and non-commercial use as long as
15 * the following conditions are aheared to. The following conditions
16 * apply to all code found in this distribution, be it the RC4, RSA,
17 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
18 * included with this distribution is covered by the same copyright terms
19 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
21 * Copyright remains Eric Young's, and as such any Copyright notices in
22 * the code are not to be removed.
23 * If this package is used in a product, Eric Young should be given attribution
24 * as the author of the parts of the library used.
25 * This can be in the form of a textual message at program startup or
26 * in documentation (online or textual) provided with the package.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * "This product includes cryptographic software written by
39 * Eric Young (eay@cryptsoft.com)"
40 * The word 'cryptographic' can be left out if the rouines from the library
41 * being used are not cryptographic related :-).
42 * 4. If you include any Windows specific code (or a derivative thereof) from
43 * the apps directory (application code) you must include an acknowledgement:
44 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
46 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * The licence and distribution terms for any publically available version or
59 * derivative of this code cannot be changed. i.e. this code cannot simply be
60 * copied and put under another distribution licence
61 * [including the GNU Public Licence.]
64 /* $OpenBSD: dsa_asn1.c,v 1.22 2018/06/14 17:03:19 jsing Exp $ */
65 /* $OpenBSD: ecs_asn1.c,v 1.9 2018/03/17 15:24:44 tb Exp $ */
66 /* $OpenBSD: digest.c,v 1.30 2018/04/14 07:09:21 tb Exp $ */
67 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
70 /* ====================================================================
71 * Copyright (c) 2000-2005 The OpenSSL Project. All rights reserved.
73 * Redistribution and use in source and binary forms, with or without
74 * modification, are permitted provided that the following conditions
77 * 1. Redistributions of source code must retain the above copyright
78 * notice, this list of conditions and the following disclaimer.
80 * 2. Redistributions in binary form must reproduce the above copyright
81 * notice, this list of conditions and the following disclaimer in
82 * the documentation and/or other materials provided with the
85 * 3. All advertising materials mentioning features or use of this
86 * software must display the following acknowledgment:
87 * "This product includes software developed by the OpenSSL Project
88 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
90 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
91 * endorse or promote products derived from this software without
92 * prior written permission. For written permission, please contact
93 * licensing@OpenSSL.org.
95 * 5. Products derived from this software may not be called "OpenSSL"
96 * nor may "OpenSSL" appear in their names without prior written
97 * permission of the OpenSSL Project.
99 * 6. Redistributions of any form whatsoever must retain the following
101 * "This product includes software developed by the OpenSSL Project
102 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
104 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
105 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
106 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
107 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
108 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
109 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
110 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
111 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
112 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
113 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
114 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
115 * OF THE POSSIBILITY OF SUCH DAMAGE.
116 * ====================================================================
118 * This product includes cryptographic software written by Eric Young
119 * (eay@cryptsoft.com). This product includes software written by Tim
120 * Hudson (tjh@cryptsoft.com).
124 /* $OpenBSD: rsa_meth.c,v 1.2 2018/09/12 06:35:38 djm Exp $ */
126 * Copyright (c) 2018 Theo Buehler <tb@openbsd.org>
128 * Permission to use, copy, modify, and distribute this software for any
129 * purpose with or without fee is hereby granted, provided that the above
130 * copyright notice and this permission notice appear in all copies.
132 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
133 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
134 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
135 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
136 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
137 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
138 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
141 #include "includes.h"
145 #include <sys/types.h>
150 #include <openssl/err.h>
151 #include <openssl/bn.h>
152 #include <openssl/dsa.h>
153 #include <openssl/rsa.h>
154 #include <openssl/evp.h>
155 #ifdef OPENSSL_HAS_ECC
156 #include <openssl/ecdsa.h>
158 #include <openssl/dh.h>
160 #ifndef HAVE_DSA_GET0_PQG
162 DSA_get0_pqg(const DSA *d, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
171 #endif /* HAVE_DSA_GET0_PQG */
173 #ifndef HAVE_DSA_SET0_PQG
175 DSA_set0_pqg(DSA *d, BIGNUM *p, BIGNUM *q, BIGNUM *g)
177 if ((d->p == NULL && p == NULL) || (d->q == NULL && q == NULL) ||
178 (d->g == NULL && g == NULL))
196 #endif /* HAVE_DSA_SET0_PQG */
198 #ifndef HAVE_DSA_GET0_KEY
200 DSA_get0_key(const DSA *d, const BIGNUM **pub_key, const BIGNUM **priv_key)
203 *pub_key = d->pub_key;
204 if (priv_key != NULL)
205 *priv_key = d->priv_key;
207 #endif /* HAVE_DSA_GET0_KEY */
209 #ifndef HAVE_DSA_SET0_KEY
211 DSA_set0_key(DSA *d, BIGNUM *pub_key, BIGNUM *priv_key)
213 if (d->pub_key == NULL && pub_key == NULL)
216 if (pub_key != NULL) {
218 d->pub_key = pub_key;
220 if (priv_key != NULL) {
221 BN_free(d->priv_key);
222 d->priv_key = priv_key;
227 #endif /* HAVE_DSA_SET0_KEY */
229 #ifndef HAVE_RSA_GET0_KEY
231 RSA_get0_key(const RSA *r, const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
240 #endif /* HAVE_RSA_GET0_KEY */
242 #ifndef HAVE_RSA_SET0_KEY
244 RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
246 if ((r->n == NULL && n == NULL) || (r->e == NULL && e == NULL))
264 #endif /* HAVE_RSA_SET0_KEY */
266 #ifndef HAVE_RSA_GET0_CRT_PARAMS
268 RSA_get0_crt_params(const RSA *r, const BIGNUM **dmp1, const BIGNUM **dmq1,
278 #endif /* HAVE_RSA_GET0_CRT_PARAMS */
280 #ifndef HAVE_RSA_SET0_CRT_PARAMS
282 RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
284 if ((r->dmp1 == NULL && dmp1 == NULL) ||
285 (r->dmq1 == NULL && dmq1 == NULL) ||
286 (r->iqmp == NULL && iqmp == NULL))
304 #endif /* HAVE_RSA_SET0_CRT_PARAMS */
306 #ifndef HAVE_RSA_GET0_FACTORS
308 RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
315 #endif /* HAVE_RSA_GET0_FACTORS */
317 #ifndef HAVE_RSA_SET0_FACTORS
319 RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
321 if ((r->p == NULL && p == NULL) || (r->q == NULL && q == NULL))
335 #endif /* HAVE_RSA_SET0_FACTORS */
337 #ifndef HAVE_EVP_CIPHER_CTX_GET_IV
339 EVP_CIPHER_CTX_get_iv(const EVP_CIPHER_CTX *ctx, unsigned char *iv, size_t len)
343 if (EVP_CIPHER_CTX_iv_length(ctx) < 0)
345 if (len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
347 if (len > EVP_MAX_IV_LENGTH)
348 return 0; /* sanity check; shouldn't happen */
350 * Skip the memcpy entirely when the requested IV length is zero,
351 * since the iv pointer may be NULL or invalid.
356 # ifdef HAVE_EVP_CIPHER_CTX_IV
357 memcpy(iv, EVP_CIPHER_CTX_iv(ctx), len);
359 memcpy(iv, ctx->iv, len);
360 # endif /* HAVE_EVP_CIPHER_CTX_IV */
364 #endif /* HAVE_EVP_CIPHER_CTX_GET_IV */
366 #ifndef HAVE_EVP_CIPHER_CTX_SET_IV
368 EVP_CIPHER_CTX_set_iv(EVP_CIPHER_CTX *ctx, const unsigned char *iv, size_t len)
372 if (EVP_CIPHER_CTX_iv_length(ctx) < 0)
374 if (len != (size_t)EVP_CIPHER_CTX_iv_length(ctx))
376 if (len > EVP_MAX_IV_LENGTH)
377 return 0; /* sanity check; shouldn't happen */
379 * Skip the memcpy entirely when the requested IV length is zero,
380 * since the iv pointer may be NULL or invalid.
385 # ifdef HAVE_EVP_CIPHER_CTX_IV_NOCONST
386 memcpy(EVP_CIPHER_CTX_iv_noconst(ctx), iv, len);
388 memcpy(ctx->iv, iv, len);
389 # endif /* HAVE_EVP_CIPHER_CTX_IV_NOCONST */
393 #endif /* HAVE_EVP_CIPHER_CTX_SET_IV */
395 #ifndef HAVE_DSA_SIG_GET0
397 DSA_SIG_get0(const DSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
404 #endif /* HAVE_DSA_SIG_GET0 */
406 #ifndef HAVE_DSA_SIG_SET0
408 DSA_SIG_set0(DSA_SIG *sig, BIGNUM *r, BIGNUM *s)
410 if (r == NULL || s == NULL)
413 BN_clear_free(sig->r);
415 BN_clear_free(sig->s);
420 #endif /* HAVE_DSA_SIG_SET0 */
422 #ifdef OPENSSL_HAS_ECC
423 #ifndef HAVE_ECDSA_SIG_GET0
425 ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps)
432 #endif /* HAVE_ECDSA_SIG_GET0 */
434 #ifndef HAVE_ECDSA_SIG_SET0
436 ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
438 if (r == NULL || s == NULL)
441 BN_clear_free(sig->r);
442 BN_clear_free(sig->s);
447 #endif /* HAVE_ECDSA_SIG_SET0 */
448 #endif /* OPENSSL_HAS_ECC */
450 #ifndef HAVE_DH_GET0_PQG
452 DH_get0_pqg(const DH *dh, const BIGNUM **p, const BIGNUM **q, const BIGNUM **g)
461 #endif /* HAVE_DH_GET0_PQG */
463 #ifndef HAVE_DH_SET0_PQG
465 DH_set0_pqg(DH *dh, BIGNUM *p, BIGNUM *q, BIGNUM *g)
467 if ((dh->p == NULL && p == NULL) || (dh->g == NULL && g == NULL))
485 #endif /* HAVE_DH_SET0_PQG */
487 #ifndef HAVE_DH_GET0_KEY
489 DH_get0_key(const DH *dh, const BIGNUM **pub_key, const BIGNUM **priv_key)
492 *pub_key = dh->pub_key;
493 if (priv_key != NULL)
494 *priv_key = dh->priv_key;
496 #endif /* HAVE_DH_GET0_KEY */
498 #ifndef HAVE_DH_SET0_KEY
500 DH_set0_key(DH *dh, BIGNUM *pub_key, BIGNUM *priv_key)
502 if (pub_key != NULL) {
503 BN_free(dh->pub_key);
504 dh->pub_key = pub_key;
506 if (priv_key != NULL) {
507 BN_free(dh->priv_key);
508 dh->priv_key = priv_key;
513 #endif /* HAVE_DH_SET0_KEY */
515 #ifndef HAVE_DH_SET_LENGTH
517 DH_set_length(DH *dh, long length)
519 if (length < 0 || length > INT_MAX)
525 #endif /* HAVE_DH_SET_LENGTH */
527 #ifndef HAVE_RSA_METH_FREE
529 RSA_meth_free(RSA_METHOD *meth)
532 free((char *)meth->name);
536 #endif /* HAVE_RSA_METH_FREE */
538 #ifndef HAVE_RSA_METH_DUP
540 RSA_meth_dup(const RSA_METHOD *meth)
544 if ((copy = calloc(1, sizeof(*copy))) == NULL)
546 memcpy(copy, meth, sizeof(*copy));
547 if ((copy->name = strdup(meth->name)) == NULL) {
554 #endif /* HAVE_RSA_METH_DUP */
556 #ifndef HAVE_RSA_METH_SET1_NAME
558 RSA_meth_set1_name(RSA_METHOD *meth, const char *name)
562 if ((copy = strdup(name)) == NULL)
564 free((char *)meth->name);
568 #endif /* HAVE_RSA_METH_SET1_NAME */
570 #ifndef HAVE_RSA_METH_GET_FINISH
572 (*RSA_meth_get_finish(const RSA_METHOD *meth))(RSA *rsa)
576 #endif /* HAVE_RSA_METH_GET_FINISH */
578 #ifndef HAVE_RSA_METH_SET_PRIV_ENC
580 RSA_meth_set_priv_enc(RSA_METHOD *meth, int (*priv_enc)(int flen,
581 const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
583 meth->rsa_priv_enc = priv_enc;
586 #endif /* HAVE_RSA_METH_SET_PRIV_ENC */
588 #ifndef HAVE_RSA_METH_SET_PRIV_DEC
590 RSA_meth_set_priv_dec(RSA_METHOD *meth, int (*priv_dec)(int flen,
591 const unsigned char *from, unsigned char *to, RSA *rsa, int padding))
593 meth->rsa_priv_dec = priv_dec;
596 #endif /* HAVE_RSA_METH_SET_PRIV_DEC */
598 #ifndef HAVE_RSA_METH_SET_FINISH
600 RSA_meth_set_finish(RSA_METHOD *meth, int (*finish)(RSA *rsa))
602 meth->finish = finish;
605 #endif /* HAVE_RSA_METH_SET_FINISH */
607 #ifndef HAVE_EVP_PKEY_GET0_RSA
609 EVP_PKEY_get0_RSA(EVP_PKEY *pkey)
611 if (pkey->type != EVP_PKEY_RSA) {
612 /* EVPerror(EVP_R_EXPECTING_AN_RSA_KEY); */
615 return pkey->pkey.rsa;
617 #endif /* HAVE_EVP_PKEY_GET0_RSA */
619 #ifndef HAVE_EVP_MD_CTX_NEW
623 return calloc(1, sizeof(EVP_MD_CTX));
625 #endif /* HAVE_EVP_MD_CTX_NEW */
627 #ifndef HAVE_EVP_MD_CTX_FREE
629 EVP_MD_CTX_free(EVP_MD_CTX *ctx)
634 EVP_MD_CTX_cleanup(ctx);
638 #endif /* HAVE_EVP_MD_CTX_FREE */
640 #endif /* WITH_OPENSSL */