96 #include <openssl/dh.h>
97 #if OPENSSL_VERSION_NUMBER >= 0x1000200fUL && !defined(OPENSSL_NO_ECDH)
98 #include <openssl/ec.h>
123 static unsigned char dh512_der[] = {
124 0x30, 0x46, 0x02, 0x41, 0x00, 0xd8, 0xbf, 0x11, 0xd6, 0x41, 0x2a, 0x7a,
125 0x9c, 0x78, 0xb2, 0xaa, 0x41, 0x23, 0x0a, 0xdc, 0xcf, 0xb7, 0x19, 0xc5,
126 0x16, 0x4c, 0xcb, 0x4a, 0xd0, 0xd2, 0x1f, 0x1f, 0x70, 0x24, 0x86, 0x6f,
127 0x51, 0x52, 0xc6, 0x5b, 0x28, 0xbb, 0x82, 0xe1, 0x24, 0x91, 0x3d, 0x4d,
128 0x95, 0x56, 0xf8, 0x0b, 0x2c, 0xe0, 0x36, 0x67, 0x88, 0x64, 0x15, 0x1f,
129 0x45, 0xd5, 0xb8, 0x0a, 0x00, 0x03, 0x76, 0x32, 0x0b, 0x02, 0x01, 0x02,
140 static unsigned char dh2048_der[] = {
141 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xbf, 0x28, 0x1b,
142 0x68, 0x69, 0x90, 0x2f, 0x37, 0x9f, 0x5a, 0x50, 0x23, 0x73, 0x2c, 0x11,
143 0xf2, 0xac, 0x7c, 0x3e, 0x58, 0xb9, 0x23, 0x3e, 0x02, 0x07, 0x4d, 0xba,
144 0xd9, 0x2c, 0xc1, 0x9e, 0xf9, 0xc4, 0x2f, 0xbc, 0x8d, 0x86, 0x4b, 0x2a,
145 0x87, 0x86, 0x93, 0x32, 0x0f, 0x72, 0x40, 0xfe, 0x7e, 0xa2, 0xc1, 0x32,
146 0xf0, 0x65, 0x9c, 0xc3, 0x19, 0x25, 0x2d, 0xeb, 0x6a, 0x49, 0x94, 0x79,
147 0x2d, 0xa1, 0xbe, 0x05, 0x26, 0xac, 0x8d, 0x69, 0xdc, 0x2e, 0x7e, 0xb5,
148 0xfd, 0x3c, 0x2b, 0x7d, 0x43, 0x22, 0x53, 0xf6, 0x1e, 0x04, 0x45, 0xd7,
149 0x53, 0x84, 0xfd, 0x6b, 0x12, 0x72, 0x47, 0x04, 0xaf, 0xa4, 0xac, 0x4b,
150 0x55, 0xb6, 0x79, 0x42, 0x40, 0x88, 0x54, 0x48, 0xd5, 0x4d, 0x3a, 0xb2,
151 0xbf, 0x6c, 0x26, 0x95, 0x29, 0xdd, 0x8b, 0x9e, 0xed, 0xb8, 0x60, 0x8e,
152 0xb5, 0x35, 0xb6, 0x22, 0x44, 0x1f, 0xfb, 0x56, 0x74, 0xfe, 0xf0, 0x2c,
153 0xe6, 0x0c, 0x22, 0xc9, 0x35, 0xb3, 0x1b, 0x96, 0xbb, 0x0a, 0x5a, 0xc3,
154 0x09, 0xa0, 0xcc, 0xa5, 0x40, 0x90, 0x0f, 0x59, 0xa2, 0x89, 0x69, 0x2a,
155 0x69, 0x79, 0xe4, 0xd3, 0x24, 0xc6, 0x8c, 0xda, 0xbc, 0x98, 0x3a, 0x5b,
156 0x16, 0xae, 0x63, 0x6c, 0x0b, 0x43, 0x4f, 0xf3, 0x2e, 0xc8, 0xa9, 0x6b,
157 0x58, 0x6a, 0xa9, 0x8e, 0x64, 0x09, 0x3d, 0x88, 0x44, 0x4f, 0x97, 0x2c,
158 0x1d, 0x98, 0xb0, 0xa9, 0xc0, 0xb6, 0x8d, 0x19, 0x37, 0x1f, 0xb7, 0xc9,
159 0x86, 0xa8, 0xdc, 0x37, 0x4d, 0x64, 0x27, 0xf3, 0xf5, 0x2b, 0x7b, 0x6b,
160 0x76, 0x84, 0x3f, 0xc1, 0x23, 0x97, 0x2d, 0x71, 0xf7, 0xb6, 0xc2, 0x35,
161 0x28, 0x10, 0x96, 0xd6, 0x69, 0x0c, 0x2e, 0x1f, 0x9f, 0xdf, 0x82, 0x81,
162 0x57, 0x57, 0x39, 0xa5, 0xf2, 0x81, 0x29, 0x57, 0xf9, 0x2f, 0xd0, 0x03,
163 0xab, 0x02, 0x01, 0x02,
169 static DH *dh_1024 = 0;
170 static DH *dh_512 = 0;
174 void tls_set_dh_from_file(
const char *path,
int bits)
187 msg_panic(
"Invalid DH parameters size %d, file %s", bits, path);
198 if ((paramfile = fopen(path,
"r")) != 0) {
199 if ((*dhPtr = PEM_read_DHparams(paramfile, 0, 0, 0)) == 0) {
200 msg_warn(
"cannot load %d-bit DH parameters from file %s"
201 " -- using compiled-in defaults", bits, path);
204 (void) fclose(paramfile);
206 msg_warn(
"cannot load %d-bit DH parameters from file %s: %m"
207 " -- using compiled-in defaults", bits, path);
213 static DH *tls_get_dh(
const unsigned char *p,
size_t plen)
215 const unsigned char *endp = p;
218 if (d2i_DHparams(&dh, &endp, plen) && plen == endp - p)
221 msg_warn(
"cannot load compiled-in DH parameters");
229 DH *tls_tmp_dh_cb(SSL *unused_ssl,
int export,
int keylength)
233 if (export && keylength == 512) {
235 dh_512 = tls_get_dh(dh512_der,
sizeof(dh512_der));
239 dh_1024 = tls_get_dh(dh2048_der,
sizeof(dh2048_der));
245 void tls_auto_eecdh_curves(SSL_CTX *ctx)
247 #if OPENSSL_VERSION_NUMBER >= 0x1000200fUL && !defined(OPENSSL_NO_ECDH)
257 if ((tmpctx = SSL_CTX_new(TLS_method())) == 0) {
258 msg_warn(
"cannot allocate temp SSL_CTX, using default ECDHE curves");
262 nids =
mymalloc(space *
sizeof(
int));
264 #define RETURN do { \
267 SSL_CTX_free(tmpctx); \
272 int nid = EC_curve_nist2nid(curve);
274 if (nid == NID_undef)
275 nid = OBJ_sn2nid(curve);
276 if (nid == NID_undef)
277 nid = OBJ_ln2nid(curve);
278 if (nid == NID_undef) {
279 msg_warn(
"ignoring unknown \"auto\" ECDHE curve \"%s\"",
291 if (SSL_CTX_set1_curves(tmpctx, &nid, 1) <= 0) {
297 nids =
myrealloc(nids, space *
sizeof(
int));
304 msg_warn(
"none of the \"auto\" ECDHE curves are supported");
307 if (SSL_CTX_set1_curves(ctx, nids, n) <= 0) {
308 msg_warn(
"failed to configure \"auto\" ECDHE curves");
317 #if OPENSSL_VERSION_NUMBER < 0x10100000UL
318 if (SSL_CTX_set_ecdh_auto(ctx, 1) <= 0) {
319 msg_warn(
"failed to enable automatic ECDHE curve selection");
328 void tls_set_eecdh_curve(SSL_CTX *server_ctx,
const char *grade)
330 #if OPENSSL_VERSION_NUMBER >= 0x1000000fUL && !defined(OPENSSL_NO_ECDH)
336 #define TLS_EECDH_INVALID 0
337 #define TLS_EECDH_NONE 1
338 #define TLS_EECDH_STRONG 2
339 #define TLS_EECDH_ULTRA 3
340 #if OPENSSL_VERSION_NUMBER >= 0x1000200fUL
341 #define TLS_EECDH_AUTO 4
344 "none", TLS_EECDH_NONE,
345 "strong", TLS_EECDH_STRONG,
346 "ultra", TLS_EECDH_ULTRA,
347 #if OPENSSL_VERSION_NUMBER >= 0x1000200fUL
348 "auto", TLS_EECDH_AUTO,
350 0, TLS_EECDH_INVALID,
355 msg_panic(
"Invalid eecdh grade code: %d", g);
356 case TLS_EECDH_INVALID:
357 msg_warn(
"Invalid TLS eecdh grade \"%s\": EECDH disabled", grade);
361 case TLS_EECDH_STRONG:
364 case TLS_EECDH_ULTRA:
367 #if OPENSSL_VERSION_NUMBER >= 0x1000200fUL
369 tls_auto_eecdh_curves(server_ctx);
383 if ((nid = OBJ_sn2nid(curve)) == NID_undef) {
384 msg_warn(
"unknown curve \"%s\": disabling EECDH support", curve);
388 if ((ecdh = EC_KEY_new_by_curve_name(nid)) == 0
389 || SSL_CTX_set_tmp_ecdh(server_ctx, ecdh) == 0) {
391 msg_warn(
"unable to use curve \"%s\": disabling EECDH support", curve);
402 int main(
int unused_argc,
char **unused_argv)
404 tls_tmp_dh_cb(0, 1, 512);
405 tls_tmp_dh_cb(0, 1, 1024);
406 tls_tmp_dh_cb(0, 1, 2048);
407 tls_tmp_dh_cb(0, 0, 512);
char * var_tls_eecdh_auto
char * var_tls_eecdh_ultra
char * mystrdup(const char *str)
NORETURN msg_panic(const char *fmt,...)
void * myrealloc(void *ptr, ssize_t len)
int main(int argc, char **argv)
char * mystrtok(char **src, const char *sep)
void msg_warn(const char *fmt,...)
#define NAME_CODE_FLAG_NONE
int name_code(const NAME_CODE *table, int flags, const char *name)
char * var_tls_eecdh_strong
void * mymalloc(ssize_t len)