summaryrefslogtreecommitdiffstats
path: root/src/tls/tls_rsa.c
blob: 67f2a2eeb5e58c899fcf4c7f28962434f7497aff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*++
/* NAME
/*	tls_rsa
/* SUMMARY
/*	RSA support
/* SYNOPSIS
/*	#define TLS_INTERNAL
/*	#include <tls.h>
/*
/*	RSA	*tls_tmp_rsa_cb(ssl, export, keylength)
/*	SSL	*ssl; /* unused */
/*	int	export;
/*	int	keylength;
/* DESCRIPTION
/*	tls_tmp_rsa_cb() is a call-back routine for the
/*	SSL_CTX_set_tmp_rsa_callback() function.
/*
/*	This implementation will generate only 512-bit ephemeral
/*	RSA keys for export ciphersuites. It will log a warning in
/*	all other usage contexts.
/* LICENSE
/* .ad
/* .fi
/*	This software is free. You can do with it whatever you want.
/*	The original author kindly requests that you acknowledge
/*	the use of his software.
/* AUTHOR(S)
/*	Originally written by:
/*	Lutz Jaenicke
/*	BTU Cottbus
/*	Allgemeine Elektrotechnik
/*	Universitaetsplatz 3-4
/*	D-03044 Cottbus, Germany
/*
/*	Updated by:
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*
/*	Viktor Dukhovni.
/*--*/

/* System library. */

#include <sys_defs.h>
#include <msg.h>

#ifdef USE_TLS

/* TLS library. */

#define TLS_INTERNAL
#include <tls.h>
#include <openssl/rsa.h>

 /*
  * 2015-12-05: Ephemeral RSA removed from OpenSSL 1.1.0-dev
  */
#if OPENSSL_VERSION_NUMBER < 0x10100000L

/* tls_tmp_rsa_cb - call-back to generate ephemeral RSA key */

RSA    *tls_tmp_rsa_cb(SSL *unused_ssl, int export, int keylength)
{
    static RSA *rsa_tmp;

    /*
     * We generate ephemeral RSA keys only for export ciphersuites.  In all
     * other contexts use of ephemeral RSA keys violates the SSL/TLS
     * protocol, and only takes place when applications ask for trouble and
     * set the SSL_OP_EPHEMERAL_RSA option.  Postfix should never do that.
     */
    if (!export || keylength != 512) {
	msg_warn("%sexport %d-bit ephemeral RSA key requested",
		 export ? "" : "non-", keylength);
	return 0;
    }
    if (rsa_tmp == 0) {
	BIGNUM *e = BN_new();

	if (e != 0 && BN_set_word(e, RSA_F4) && (rsa_tmp = RSA_new()) != 0)
	    if (!RSA_generate_key_ex(rsa_tmp, keylength, e, 0)) {
		RSA_free(rsa_tmp);
		rsa_tmp = 0;
	    }
	if (e)
	    BN_free(e);
    }
    return (rsa_tmp);
}

#endif					/* OPENSSL_VERSION_NUMBER */

#ifdef TEST

#include <msg_vstream.h>

int     main(int unused_argc, char *const argv[])
{
    int     ok = 0;

    /*
     * 2015-12-05: Ephemeral RSA removed from OpenSSL 1.1.0-dev
     */
#if OPENSSL_VERSION_NUMBER < 0x10100000L
    RSA    *rsa;

    msg_vstream_init(argv[0], VSTREAM_ERR);

    /* Export at 512-bits should work */
    rsa = tls_tmp_rsa_cb(0, 1, 512);
    ok = rsa != 0 && RSA_size(rsa) == 512 / 8;
    ok = ok && PEM_write_RSAPrivateKey(stdout, rsa, 0, 0, 0, 0, 0);
    tls_print_errors();

    /* Non-export or unexpected bit length should fail */
    ok = ok && tls_tmp_rsa_cb(0, 0, 512) == 0;
    ok = ok && tls_tmp_rsa_cb(0, 1, 1024) == 0;
#endif

    return ok ? 0 : 1;
}

#endif

#endif