diff options
Diffstat (limited to 'src/usr.bin/openssl/gendh.c')
| -rw-r--r-- | src/usr.bin/openssl/gendh.c | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/src/usr.bin/openssl/gendh.c b/src/usr.bin/openssl/gendh.c new file mode 100644 index 0000000000..06d62c1b0d --- /dev/null +++ b/src/usr.bin/openssl/gendh.c | |||
| @@ -0,0 +1,204 @@ | |||
| 1 | /* $OpenBSD: gendh.c,v 1.1 2014/08/26 17:47:24 jsing Exp $ */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | #include <openssl/opensslconf.h> | ||
| 60 | |||
| 61 | /* Until the key-gen callbacks are modified to use newer prototypes, we allow | ||
| 62 | * deprecated functions for openssl-internal code */ | ||
| 63 | #ifdef OPENSSL_NO_DEPRECATED | ||
| 64 | #undef OPENSSL_NO_DEPRECATED | ||
| 65 | #endif | ||
| 66 | |||
| 67 | #ifndef OPENSSL_NO_DH | ||
| 68 | |||
| 69 | #include <sys/types.h> | ||
| 70 | #include <sys/stat.h> | ||
| 71 | |||
| 72 | #include <stdio.h> | ||
| 73 | #include <string.h> | ||
| 74 | |||
| 75 | #include "apps.h" | ||
| 76 | |||
| 77 | #include <openssl/bio.h> | ||
| 78 | #include <openssl/bn.h> | ||
| 79 | #include <openssl/dh.h> | ||
| 80 | #include <openssl/err.h> | ||
| 81 | #include <openssl/pem.h> | ||
| 82 | #include <openssl/rand.h> | ||
| 83 | #include <openssl/x509.h> | ||
| 84 | |||
| 85 | #define DEFBITS 512 | ||
| 86 | |||
| 87 | static int dh_cb(int p, int n, BN_GENCB * cb); | ||
| 88 | |||
| 89 | int gendh_main(int, char **); | ||
| 90 | |||
| 91 | int | ||
| 92 | gendh_main(int argc, char **argv) | ||
| 93 | { | ||
| 94 | BN_GENCB cb; | ||
| 95 | DH *dh = NULL; | ||
| 96 | int ret = 1, num = DEFBITS; | ||
| 97 | int g = 2; | ||
| 98 | char *outfile = NULL; | ||
| 99 | #ifndef OPENSSL_NO_ENGINE | ||
| 100 | char *engine = NULL; | ||
| 101 | #endif | ||
| 102 | BIO *out = NULL; | ||
| 103 | |||
| 104 | BN_GENCB_set(&cb, dh_cb, bio_err); | ||
| 105 | |||
| 106 | argv++; | ||
| 107 | argc--; | ||
| 108 | for (;;) { | ||
| 109 | if (argc <= 0) | ||
| 110 | break; | ||
| 111 | if (strcmp(*argv, "-out") == 0) { | ||
| 112 | if (--argc < 1) | ||
| 113 | goto bad; | ||
| 114 | outfile = *(++argv); | ||
| 115 | } else if (strcmp(*argv, "-2") == 0) | ||
| 116 | g = 2; | ||
| 117 | /* | ||
| 118 | * else if (strcmp(*argv,"-3") == 0) g=3; | ||
| 119 | */ | ||
| 120 | else if (strcmp(*argv, "-5") == 0) | ||
| 121 | g = 5; | ||
| 122 | #ifndef OPENSSL_NO_ENGINE | ||
| 123 | else if (strcmp(*argv, "-engine") == 0) { | ||
| 124 | if (--argc < 1) | ||
| 125 | goto bad; | ||
| 126 | engine = *(++argv); | ||
| 127 | } | ||
| 128 | #endif | ||
| 129 | else | ||
| 130 | break; | ||
| 131 | argv++; | ||
| 132 | argc--; | ||
| 133 | } | ||
| 134 | if ((argc >= 1) && ((sscanf(*argv, "%d", &num) == 0) || (num < 0))) { | ||
| 135 | bad: | ||
| 136 | BIO_printf(bio_err, "usage: gendh [args] [numbits]\n"); | ||
| 137 | BIO_printf(bio_err, " -out file - output the key to 'file\n"); | ||
| 138 | BIO_printf(bio_err, " -2 - use 2 as the generator value\n"); | ||
| 139 | /* | ||
| 140 | * BIO_printf(bio_err," -3 - use 3 as the generator | ||
| 141 | * value\n"); | ||
| 142 | */ | ||
| 143 | BIO_printf(bio_err, " -5 - use 5 as the generator value\n"); | ||
| 144 | #ifndef OPENSSL_NO_ENGINE | ||
| 145 | BIO_printf(bio_err, " -engine e - use engine e, possibly a hardware device.\n"); | ||
| 146 | #endif | ||
| 147 | goto end; | ||
| 148 | } | ||
| 149 | #ifndef OPENSSL_NO_ENGINE | ||
| 150 | setup_engine(bio_err, engine, 0); | ||
| 151 | #endif | ||
| 152 | |||
| 153 | out = BIO_new(BIO_s_file()); | ||
| 154 | if (out == NULL) { | ||
| 155 | ERR_print_errors(bio_err); | ||
| 156 | goto end; | ||
| 157 | } | ||
| 158 | if (outfile == NULL) { | ||
| 159 | BIO_set_fp(out, stdout, BIO_NOCLOSE); | ||
| 160 | } else { | ||
| 161 | if (BIO_write_filename(out, outfile) <= 0) { | ||
| 162 | perror(outfile); | ||
| 163 | goto end; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | |||
| 167 | BIO_printf(bio_err, "Generating DH parameters, %d bit long safe prime, generator %d\n", num, g); | ||
| 168 | BIO_printf(bio_err, "This is going to take a long time\n"); | ||
| 169 | |||
| 170 | if (((dh = DH_new()) == NULL) || !DH_generate_parameters_ex(dh, num, g, &cb)) | ||
| 171 | goto end; | ||
| 172 | |||
| 173 | if (!PEM_write_bio_DHparams(out, dh)) | ||
| 174 | goto end; | ||
| 175 | ret = 0; | ||
| 176 | end: | ||
| 177 | if (ret != 0) | ||
| 178 | ERR_print_errors(bio_err); | ||
| 179 | if (out != NULL) | ||
| 180 | BIO_free_all(out); | ||
| 181 | if (dh != NULL) | ||
| 182 | DH_free(dh); | ||
| 183 | |||
| 184 | return (ret); | ||
| 185 | } | ||
| 186 | |||
| 187 | static int | ||
| 188 | dh_cb(int p, int n, BN_GENCB * cb) | ||
| 189 | { | ||
| 190 | char c = '*'; | ||
| 191 | |||
| 192 | if (p == 0) | ||
| 193 | c = '.'; | ||
| 194 | if (p == 1) | ||
| 195 | c = '+'; | ||
| 196 | if (p == 2) | ||
| 197 | c = '*'; | ||
| 198 | if (p == 3) | ||
| 199 | c = '\n'; | ||
| 200 | BIO_write(cb->arg, &c, 1); | ||
| 201 | (void) BIO_flush(cb->arg); | ||
| 202 | return 1; | ||
| 203 | } | ||
| 204 | #endif | ||
