1 /* Copyright (C) 2012,2016 Phil Pennock.
2 * This is distributed as part of Exim and licensed under the GPL.
3 * See the file "NOTICE" for more details.
7 * c99 $(pkg-config --cflags openssl) gen_pkcs3.c $(pkg-config --libs openssl)
12 * The Diffie-Hellman primes which are embedded into Exim as named primes for
13 * the tls_dhparam option are in the std-crypto.c file. The source for those
14 * comes from various RFCs, where they are given in hexadecimal form.
16 * This tool provides convenient conversion, to reduce the risk of human
17 * error in transcription.
28 #include <openssl/bio.h>
29 #include <openssl/bn.h>
30 #include <openssl/dh.h>
31 #include <openssl/err.h>
32 #include <openssl/pem.h>
34 extern const char *__progname;
37 void __attribute__((__noreturn__)) __attribute__((__format__(printf, 1, 2)))
38 die(const char *fmt, ...)
43 fprintf(stderr, "%s: ", __progname);
45 vfprintf(stderr, fmt, ap);
47 fprintf(stderr, "\n");
53 void __attribute__((__noreturn__))
54 die_openssl_err(const char *msg)
58 ERR_error_string_n(ERR_get_error(), err_string, sizeof(err_string));
59 die("%s: %s", msg, err_string);
64 bn_from_text(const char *text)
73 spaceless = malloc(len + 1);
75 die("malloc(%zu) failed: %s", len + 1, strerror(errno));
77 for (p = spaceless, q = text, end = text + len;
87 rc = BN_hex2bn(&b, spaceless);
90 die("BN_hex2bn did not convert entire input; took %d of %zu bytes",
100 int rc, errflags = 0;
102 rc = DH_check(dh, &errflags);
103 if (!rc) die_openssl_err("DH_check() could not be performed");;
105 /* We ignore DH_UNABLE_TO_CHECK_GENERATOR because some of the invocations
106 * deliberately provide generators other than 2 or 5. */
108 if (errflags & DH_CHECK_P_NOT_SAFE_PRIME)
109 die("DH_check(): p not a safe prime");
110 if (errflags & DH_NOT_SUITABLE_GENERATOR)
111 die("DH_check(): g not suitable as generator");
116 emit_c_format_dh(FILE *stream, DH *dh)
120 char *data, *end, *p, *nl;
122 bio = BIO_new(BIO_s_mem());
123 PEM_write_bio_DHparams(bio, dh);
124 length = BIO_get_mem_data(bio, &data);
126 die("no data in memory BIO to format for printing");
128 die("grr, negative length memory not supported");
131 for (p = data; p < end; /**/) {
132 nl = strchr(p, '\n');
134 fprintf(stream, "\"%s\\n\"\n/* missing final newline */\n", p);
138 fprintf(stream, "\"%s\\n\"%s\n", p, (nl == end - 1 ? ";" : ""));
144 void __attribute__((__noreturn__))
145 usage(FILE *stream, int exitcode)
147 fprintf(stream, "Usage: %s [-CPcst] <dh_p> <dh_g> [<dh_q>]\n"
148 "Both dh_p and dh_g should be hex strings representing the numbers\n"
149 "The same applies to the optional dh_q (prime-order subgroup).\n"
150 "They may contain whitespace.\n"
151 "Older values, dh_g is often just '2', not a long string.\n"
153 " -C show C string form of PEM result\n"
154 " -P do not show PEM\n"
155 " -c run OpenSSL DH_check() on the DH object\n"
156 " -s show the parsed p and g\n"
157 " -t show text form of certificate\n"
165 main(int argc, char *argv[])
170 bool perform_dh_check = false;
171 bool show_c_form = false;
172 bool show_numbers = false;
173 bool show_pem = true;
174 bool show_text = false;
175 bool given_q = false;
177 while ((ch = getopt(argc, argv, "CPcsth")) != -1) {
186 perform_dh_check = true;
198 die("Unknown option or missing argument -%c", optopt);
200 die("Unhandled option -%c", ch);
208 if ((argc < 3) || (argc > 4)) {
209 fprintf(stderr, "argc: %d\n", argc);
213 // If we use DH_set0_pqg instead of setting dh fields directly; the q value
214 // is optional and may be NULL.
215 // Just blank them all.
218 p = bn_from_text(argv[1]);
219 g = bn_from_text(argv[2]);
221 q = bn_from_text(argv[3]);
227 BN_print_fp(stdout, p);
229 BN_print_fp(stdout, g);
232 BN_print_fp(stdout, q);
238 // The documented method for setting q appeared in OpenSSL 1.1.0.
239 #if OPENSSL_VERSION_NUMBER >= 0x1010000f
240 // NULL okay for q; yes, the optional value is in the middle.
241 if (DH_set0_pqg(dh, p, q, g) != 1) {
242 die_openssl_err("initialising DH pqg values failed");
252 if (perform_dh_check)
256 DHparams_print_fp(stdout, dh);
260 emit_c_format_dh(stdout, dh);
262 PEM_write_DHparams(stdout, dh);
265 DH_free(dh); /* should free p,g (& q if non-NULL) too */