20 static my_bool ssl_algorithms_added = FALSE;
21 static my_bool ssl_error_strings_loaded= FALSE;
23 static unsigned char dh512_p[]=
25 0xDA,0x58,0x3C,0x16,0xD9,0x85,0x22,0x89,0xD0,0xE4,0xAF,0x75,
26 0x6F,0x4C,0xCA,0x92,0xDD,0x4B,0xE5,0x33,0xB8,0x04,0xFB,0x0F,
27 0xED,0x94,0xEF,0x9C,0x8A,0x44,0x03,0xED,0x57,0x46,0x50,0xD3,
28 0x69,0x99,0xDB,0x29,0xD7,0x76,0x27,0x6B,0xA2,0xD3,0xD4,0x12,
29 0xE2,0x18,0xF4,0xDD,0x1E,0x08,0x4C,0xF6,0xD8,0x00,0x3E,0x7C,
33 static unsigned char dh512_g[]={
37 static DH *get_dh512(
void)
42 dh->p=BN_bin2bn(dh512_p,
sizeof(dh512_p),NULL);
43 dh->g=BN_bin2bn(dh512_g,
sizeof(dh512_g),NULL);
44 if (! dh->p || ! dh->g)
62 DBUG_ENTER(
"report_errors");
64 while ((l=ERR_get_error_line_data(&file,&line,&data,&flags)) != 0)
68 DBUG_PRINT(
"error", (
"OpenSSL: %s:%s:%d:%s\n", ERR_error_string(l,buf),
69 file,line,(flags & ERR_TXT_STRING) ? data :
"")) ;
79 "Unable to get certificate",
80 "Unable to get private key",
81 "Private key does not match the certificate public key"
82 "SSL_CTX_set_default_verify_paths failed",
83 "Failed to set ciphers to use",
88 sslGetErrString(
enum enum_ssl_init_error e)
90 DBUG_ASSERT(SSL_INITERR_NOERROR < e && e < SSL_INITERR_LASTERR);
91 return ssl_error_string[e];
95 vio_set_cert_stuff(SSL_CTX *ctx,
const char *cert_file,
const char *key_file,
96 enum enum_ssl_init_error* error)
98 DBUG_ENTER(
"vio_set_cert_stuff");
99 DBUG_PRINT(
"enter", (
"ctx: 0x%lx cert_file: %s key_file: %s",
100 (
long) ctx, cert_file, key_file));
102 if (!cert_file && key_file)
105 if (!key_file && cert_file)
109 SSL_CTX_use_certificate_file(ctx, cert_file, SSL_FILETYPE_PEM) <= 0)
111 *error= SSL_INITERR_CERT;
112 DBUG_PRINT(
"error",(
"%s from file '%s'", sslGetErrString(*error), cert_file));
113 DBUG_EXECUTE(
"error", ERR_print_errors_fp(DBUG_FILE););
114 fprintf(stderr,
"SSL error: %s from '%s'\n", sslGetErrString(*error),
121 SSL_CTX_use_PrivateKey_file(ctx, key_file, SSL_FILETYPE_PEM) <= 0)
123 *error= SSL_INITERR_KEY;
124 DBUG_PRINT(
"error", (
"%s from file '%s'", sslGetErrString(*error), key_file));
125 DBUG_EXECUTE(
"error", ERR_print_errors_fp(DBUG_FILE););
126 fprintf(stderr,
"SSL error: %s from '%s'\n", sslGetErrString(*error),
136 if (cert_file && !SSL_CTX_check_private_key(ctx))
138 *error= SSL_INITERR_NOMATCH;
139 DBUG_PRINT(
"error", (
"%s",sslGetErrString(*error)));
140 DBUG_EXECUTE(
"error", ERR_print_errors_fp(DBUG_FILE););
141 fprintf(stderr,
"SSL error: %s\n", sslGetErrString(*error));
152 if (!ssl_algorithms_added)
154 ssl_algorithms_added= TRUE;
156 OpenSSL_add_all_algorithms();
160 if (!ssl_error_strings_loaded)
162 ssl_error_strings_loaded= TRUE;
163 SSL_load_error_strings();
168 static struct st_VioSSLFd *
169 new_VioSSLFd(
const char *key_file,
const char *cert_file,
170 const char *ca_file,
const char *ca_path,
171 const char *cipher, my_bool is_client,
172 enum enum_ssl_init_error *error,
173 const char *crl_file,
const char *crl_path)
176 struct st_VioSSLFd *ssl_fd;
177 DBUG_ENTER(
"new_VioSSLFd");
179 (
"key_file: '%s' cert_file: '%s' ca_file: '%s' ca_path: '%s' "
180 "cipher: '%s' crl_file: '%s' crl_path: '%s' ",
181 key_file ? key_file :
"NULL",
182 cert_file ? cert_file :
"NULL",
183 ca_file ? ca_file :
"NULL",
184 ca_path ? ca_path :
"NULL",
185 cipher ? cipher :
"NULL",
186 crl_file ? crl_file :
"NULL",
187 crl_path ? crl_path :
"NULL"));
191 if (!(ssl_fd= ((
struct st_VioSSLFd*)
192 my_malloc(
sizeof(
struct st_VioSSLFd),MYF(0)))))
195 if (!(ssl_fd->ssl_context= SSL_CTX_new(is_client ?
196 TLSv1_client_method() :
197 TLSv1_server_method())))
199 *error= SSL_INITERR_MEMFAIL;
200 DBUG_PRINT(
"error", (
"%s", sslGetErrString(*error)));
212 SSL_CTX_set_cipher_list(ssl_fd->ssl_context, cipher) == 0)
214 *error= SSL_INITERR_CIPHERS;
215 DBUG_PRINT(
"error", (
"%s", sslGetErrString(*error)));
217 SSL_CTX_free(ssl_fd->ssl_context);
223 if (SSL_CTX_load_verify_locations(ssl_fd->ssl_context, ca_file, ca_path) == 0)
225 DBUG_PRINT(
"warning", (
"SSL_CTX_load_verify_locations failed"));
226 if (ca_file || ca_path)
230 *error= SSL_INITERR_BAD_PATHS;
231 DBUG_PRINT(
"error", (
"SSL_CTX_load_verify_locations failed : %s",
232 sslGetErrString(*error)));
234 SSL_CTX_free(ssl_fd->ssl_context);
240 if (SSL_CTX_set_default_verify_paths(ssl_fd->ssl_context) == 0)
242 *error= SSL_INITERR_BAD_PATHS;
243 DBUG_PRINT(
"error", (
"%s", sslGetErrString(*error)));
245 SSL_CTX_free(ssl_fd->ssl_context);
251 if (crl_file || crl_path)
254 DBUG_PRINT(
"warning", (
"yaSSL doesn't support CRL"));
257 X509_STORE *store= SSL_CTX_get_cert_store(ssl_fd->ssl_context);
259 if (X509_STORE_load_locations(store, crl_file, crl_path) == 0 ||
260 X509_STORE_set_flags(store,
261 X509_V_FLAG_CRL_CHECK |
262 X509_V_FLAG_CRL_CHECK_ALL) == 0)
264 DBUG_PRINT(
"warning", (
"X509_STORE_load_locations for CRL failed"));
265 *error= SSL_INITERR_BAD_PATHS;
266 DBUG_PRINT(
"error", (
"%s", sslGetErrString(*error)));
268 SSL_CTX_free(ssl_fd->ssl_context);
275 if (vio_set_cert_stuff(ssl_fd->ssl_context, cert_file, key_file, error))
277 DBUG_PRINT(
"error", (
"vio_set_cert_stuff failed"));
279 SSL_CTX_free(ssl_fd->ssl_context);
286 SSL_CTX_set_tmp_dh(ssl_fd->ssl_context, dh);
289 DBUG_PRINT(
"exit", (
"OK 1"));
297 new_VioSSLConnectorFd(
const char *key_file,
const char *cert_file,
298 const char *ca_file,
const char *ca_path,
299 const char *cipher,
enum enum_ssl_init_error* error,
300 const char *crl_file,
const char *crl_path)
302 struct st_VioSSLFd *ssl_fd;
303 int verify= SSL_VERIFY_PEER;
309 if (ca_file == 0 && ca_path == 0)
310 verify= SSL_VERIFY_NONE;
312 if (!(ssl_fd= new_VioSSLFd(key_file, cert_file, ca_file,
313 ca_path, cipher, TRUE, error,
314 crl_file, crl_path)))
321 SSL_CTX_set_verify(ssl_fd->ssl_context, verify, NULL);
329 new_VioSSLAcceptorFd(
const char *key_file,
const char *cert_file,
330 const char *ca_file,
const char *ca_path,
331 const char *cipher,
enum enum_ssl_init_error* error,
332 const char *crl_file,
const char *crl_path)
334 struct st_VioSSLFd *ssl_fd;
335 int verify= SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE;
336 if (!(ssl_fd= new_VioSSLFd(key_file, cert_file, ca_file,
337 ca_path, cipher, FALSE, error,
338 crl_file, crl_path)))
345 SSL_CTX_sess_set_cache_size(ssl_fd->ssl_context, 128);
347 SSL_CTX_set_verify(ssl_fd->ssl_context, verify, NULL);
353 SSL_CTX_set_session_id_context(ssl_fd->ssl_context,
354 (
const unsigned char *)ssl_fd,
360 void free_vio_ssl_acceptor_fd(
struct st_VioSSLFd *fd)
362 SSL_CTX_free(fd->ssl_context);