1 /* Copyright (C) 2012,2016 Phil Pennock.
2 * Copyright (c) The Exim Maintainers 2021
3 * This is distributed as part of Exim and licensed under the GPL.
4 * See the file "NOTICE" for more details.
8 * c99 $(pkg-config --cflags openssl) gen_pkcs3.c $(pkg-config --libs openssl)
13 * The Diffie-Hellman primes which are embedded into Exim as named primes for
14 * the tls_dhparam option are in the std-crypto.c file. The source for those
15 * comes from various RFCs, where they are given in hexadecimal form.
17 * This tool provides convenient conversion, to reduce the risk of human
18 * error in transcription.
29 #include <openssl/bio.h>
30 #include <openssl/bn.h>
31 #include <openssl/dh.h>
32 #include <openssl/err.h>
33 #include <openssl/pem.h>
35 extern const char *__progname;
38 void __attribute__((__noreturn__)) __attribute__((__format__(printf, 1, 2)))
39 die(const char *fmt, ...)
44 fprintf(stderr, "%s: ", __progname);
46 vfprintf(stderr, fmt, ap);
48 fprintf(stderr, "\n");
54 void __attribute__((__noreturn__))
55 die_openssl_err(const char *msg)
59 ERR_error_string_n(ERR_get_error(), err_string, sizeof(err_string));
60 die("%s: %s", msg, err_string);
65 bn_from_text(const char *text)
74 spaceless = malloc(len + 1);
76 die("malloc(%zu) failed: %s", len + 1, strerror(errno));
78 for (p = spaceless, q = text, end = text + len;
88 rc = BN_hex2bn(&b, spaceless);
91 die("BN_hex2bn did not convert entire input; took %d of %zu bytes",
101 int rc, errflags = 0;
103 rc = DH_check(dh, &errflags);
104 if (!rc) die_openssl_err("DH_check() could not be performed");;
106 /* We ignore DH_UNABLE_TO_CHECK_GENERATOR because some of the invocations
107 * deliberately provide generators other than 2 or 5. */
109 if (errflags & DH_CHECK_P_NOT_SAFE_PRIME)
110 die("DH_check(): p not a safe prime");
111 if (errflags & DH_NOT_SUITABLE_GENERATOR)
112 die("DH_check(): g not suitable as generator");
117 emit_c_format_dh(FILE *stream, DH *dh)
121 char *data, *end, *p, *nl;
123 bio = BIO_new(BIO_s_mem());
124 PEM_write_bio_DHparams(bio, dh);
125 length = BIO_get_mem_data(bio, &data);
127 die("no data in memory BIO to format for printing");
129 die("grr, negative length memory not supported");
132 for (p = data; p < end; /**/) {
133 nl = strchr(p, '\n');
135 fprintf(stream, "\"%s\\n\"\n/* missing final newline */\n", p);
139 fprintf(stream, "\"%s\\n\"%s\n", p, (nl == end - 1 ? ";" : ""));
145 void __attribute__((__noreturn__))
146 usage(FILE *stream, int exitcode)
148 fprintf(stream, "Usage: %s [-CPcst] <dh_p> <dh_g> [<dh_q>]\n"
149 "Both dh_p and dh_g should be hex strings representing the numbers\n"
150 "The same applies to the optional dh_q (prime-order subgroup).\n"
151 "They may contain whitespace.\n"
152 "Older values, dh_g is often just '2', not a long string.\n"
154 " -C show C string form of PEM result\n"
155 " -P do not show PEM\n"
156 " -c run OpenSSL DH_check() on the DH object\n"
157 " -s show the parsed p and g\n"
158 " -t show text form of certificate\n"
166 main(int argc, char *argv[])
171 bool perform_dh_check = false;
172 bool show_c_form = false;
173 bool show_numbers = false;
174 bool show_pem = true;
175 bool show_text = false;
176 bool given_q = false;
178 while ((ch = getopt(argc, argv, "CPcsth")) != -1) {
187 perform_dh_check = true;
199 die("Unknown option or missing argument -%c", optopt);
201 die("Unhandled option -%c", ch);
209 if ((argc < 3) || (argc > 4)) {
210 fprintf(stderr, "argc: %d\n", argc);
214 // If we use DH_set0_pqg instead of setting dh fields directly; the q value
215 // is optional and may be NULL.
216 // Just blank them all.
219 p = bn_from_text(argv[1]);
220 g = bn_from_text(argv[2]);
222 q = bn_from_text(argv[3]);
228 BN_print_fp(stdout, p);
230 BN_print_fp(stdout, g);
233 BN_print_fp(stdout, q);
239 // The documented method for setting q appeared in OpenSSL 1.1.0.
240 #if OPENSSL_VERSION_NUMBER >= 0x1010000f
241 // NULL okay for q; yes, the optional value is in the middle.
242 if (DH_set0_pqg(dh, p, q, g) != 1) {
243 die_openssl_err("initialising DH pqg values failed");
253 if (perform_dh_check)
257 DHparams_print_fp(stdout, dh);
261 emit_c_format_dh(stdout, dh);
263 PEM_write_DHparams(stdout, dh);
266 DH_free(dh); /* should free p,g (& q if non-NULL) too */