Skip to content

Commit 1c582ac

Browse files
Bug#26521654: AUTO GENERATED CERTIFICATES SHOULD USE X509 V3
Description: Certificates generated by mysql server and mysql_ssl_rsa_setup do not use X509v3 and related extensions. Solution: Added X509v3 extension.
1 parent 4a4aab1 commit 1c582ac

File tree

2 files changed

+142
-20
lines changed

2 files changed

+142
-20
lines changed

client/mysql_ssl_rsa_setup.cc

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
2+
Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
33
44
This program is free software; you can redistribute it and/or modify
55
it under the terms of the GNU General Public License as published by
@@ -71,6 +71,12 @@ enum certs
7171
OPENSSL_RND
7272
};
7373

74+
enum extfiles
75+
{
76+
CAV3_EXT=0,
77+
CERTV3_EXT
78+
};
79+
7480
Sql_string_t cert_files[] =
7581
{
7682
create_string("ca.pem"),
@@ -87,6 +93,12 @@ Sql_string_t cert_files[] =
8793
create_string(".rnd")
8894
};
8995

96+
Sql_string_t ext_files[] =
97+
{
98+
create_string("cav3.ext"),
99+
create_string("certv3.ext")
100+
};
101+
90102
#define MAX_PATH_LEN (FN_REFLEN - strlen(FN_DIRSEP) \
91103
- cert_files[SERVER_CERT].length() - 1)
92104
/*
@@ -314,6 +326,49 @@ class X509_key
314326
stringstream m_subj_prefix;
315327
};
316328

329+
class X509v3_ext_writer
330+
{
331+
public:
332+
X509v3_ext_writer()
333+
{
334+
m_cav3_ext_options << "basicConstraints=CA:TRUE" << std::endl;
335+
336+
m_certv3_ext_options << "basicConstraints=CA:FALSE" << std::endl;
337+
}
338+
~X509v3_ext_writer() {};
339+
340+
bool operator()(const Sql_string_t &cav3_ext_file,
341+
const Sql_string_t &certv3_ext_file)
342+
{
343+
if (!cav3_ext_file.length() ||
344+
!certv3_ext_file.length())
345+
return true;
346+
347+
std::ofstream ext_file;
348+
349+
ext_file.open(cav3_ext_file.c_str(),
350+
std::ios::out|std::ios::trunc);
351+
if (!ext_file.is_open())
352+
return true;
353+
ext_file << m_cav3_ext_options.str();
354+
ext_file.close();
355+
356+
ext_file.open(certv3_ext_file.c_str(),
357+
std::ios::out|std::ios::trunc);
358+
if (!ext_file.is_open())
359+
{
360+
remove_file(cav3_ext_file.c_str(), false);
361+
return true;
362+
}
363+
ext_file << m_certv3_ext_options.str();
364+
ext_file.close();
365+
366+
return false;
367+
}
368+
private:
369+
stringstream m_cav3_ext_options;
370+
stringstream m_certv3_ext_options;
371+
};
317372

318373
class X509_cert
319374
{
@@ -328,15 +383,17 @@ class X509_cert
328383
uint32_t serial,
329384
bool self_signed,
330385
const Sql_string_t &sign_key_file,
331-
const Sql_string_t &sign_cert_file)
386+
const Sql_string_t &sign_cert_file,
387+
const Sql_string_t &ext_file)
332388
{
333389
stringstream command;
334390
command << "openssl x509 -sha256 -days " << m_validity;
335-
command << " -set_serial " << serial << " -req -in " << req_file << " ";
391+
command << " -extfile " << ext_file;
392+
command << " -set_serial " << serial << " -req -in " << req_file;
336393
if (self_signed)
337-
command << "-signkey " << sign_key_file;
394+
command << " -signkey " << sign_key_file;
338395
else
339-
command << "-CA " << sign_cert_file << " -CAkey " << sign_key_file;
396+
command << " -CA " << sign_cert_file << " -CAkey " << sign_key_file;
340397
command << " -out " << cert_file;
341398

342399
return command.str();
@@ -551,6 +608,7 @@ int main(int argc, char *argv[])
551608
Sql_string_t empty_string("");
552609
X509_key x509_key(suffix_string);
553610
X509_cert x509_cert;
611+
X509v3_ext_writer x509v3_ext_writer;
554612

555613
/* Delete existing files if any */
556614
remove_file(cert_files[CA_REQ], false);
@@ -560,14 +618,23 @@ int main(int argc, char *argv[])
560618
remove_file(cert_files[CLIENT_KEY], false);
561619
remove_file(cert_files[OPENSSL_RND], false);
562620

621+
/* Remove existing v3 extension files */
622+
remove_file(ext_files[CAV3_EXT], false);
623+
remove_file(ext_files[CERTV3_EXT], false);
624+
625+
/* Create v3 extension files */
626+
if (x509v3_ext_writer(ext_files[CAV3_EXT], ext_files[CERTV3_EXT]))
627+
goto end;
628+
563629
/* Generate CA Key and Certificate */
564630
if ((ret_val= execute_command(x509_key("_Auto_Generated_CA_Certificate",
565631
cert_files[CA_KEY], cert_files[CA_REQ]),
566632
"Error generating ca_key.pem and ca_req.pem")))
567633
goto end;
568634

569635
if ((ret_val= execute_command(x509_cert(cert_files[CA_REQ], cert_files[CA_CERT], 1,
570-
true, cert_files[CA_KEY], empty_string),
636+
true, cert_files[CA_KEY], empty_string,
637+
ext_files[CAV3_EXT]),
571638
"Error generating ca_cert.pem")))
572639
goto end;
573640

@@ -578,7 +645,8 @@ int main(int argc, char *argv[])
578645
goto end;
579646

580647
if ((ret_val= execute_command(x509_cert(cert_files[SERVER_REQ], cert_files[SERVER_CERT], 2,
581-
false, cert_files[CA_KEY], cert_files[CA_CERT]),
648+
false, cert_files[CA_KEY], cert_files[CA_CERT],
649+
ext_files[CERTV3_EXT]),
582650
"Error generating server_cert.pem")))
583651
goto end;
584652

@@ -589,7 +657,8 @@ int main(int argc, char *argv[])
589657
goto end;
590658

591659
if ((ret_val= execute_command(x509_cert(cert_files[CLIENT_REQ], cert_files[CLIENT_CERT], 3,
592-
false, cert_files[CA_KEY], cert_files[CA_CERT]),
660+
false, cert_files[CA_KEY], cert_files[CA_CERT],
661+
ext_files[CERTV3_EXT]),
593662
"Error generating client_cert.pem")))
594663
goto end;
595664

@@ -622,6 +691,11 @@ int main(int argc, char *argv[])
622691
goto end;
623692

624693
remove_file(cert_files[OPENSSL_RND], false);
694+
695+
/* Remove existing v3 extension files */
696+
remove_file(ext_files[CAV3_EXT], false);
697+
remove_file(ext_files[CERTV3_EXT], false);
698+
625699
}
626700

627701
/*

sql/auth/sql_authentication.cc

Lines changed: 60 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include <openssl/rsa.h>
4141
#include <openssl/pem.h>
4242
#include <openssl/err.h>
43+
#include <openssl/x509v3.h>
4344
#endif /* HAVE OPENSSL && !HAVE_YASSL */
4445

4546
#include "auth_internal.h"
@@ -3451,21 +3452,68 @@ class X509_gen
34513452
EVP_PKEY *ca_pkey= NULL)
34523453
{
34533454
X509 *x509= X509_new();
3455+
X509_EXTENSION *ext= 0;
3456+
X509V3_CTX v3ctx;
3457+
X509_NAME *name= 0;
3458+
34543459
DBUG_ASSERT(cn.length() <= MAX_CN_NAME_LENGTH);
3455-
ASN1_INTEGER_set(X509_get_serialNumber(x509), serial);
3456-
X509_gmtime_adj(X509_get_notBefore(x509), notbefore);
3457-
X509_gmtime_adj(X509_get_notAfter(x509), notafter);
3458-
/* Set public key */
3459-
X509_set_pubkey(x509, pkey);
3460-
X509_NAME *name= X509_get_subject_name(x509);
3461-
X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
3462-
(const unsigned char *)cn.c_str(), -1, -1, 0);
3463-
3464-
X509_set_issuer_name(x509,
3465-
self_sign ? name : X509_get_subject_name(ca_x509));
3466-
X509_sign(x509, self_sign ? pkey : ca_pkey, EVP_sha256());
3460+
DBUG_ASSERT(serial != 0);
3461+
DBUG_ASSERT(self_sign || (ca_x509 != NULL && ca_pkey != NULL));
3462+
if (!x509)
3463+
goto err;
3464+
3465+
/** Set certificate version */
3466+
if (!X509_set_version(x509, 2))
3467+
goto err;
3468+
3469+
/** Set serial number */
3470+
if (!ASN1_INTEGER_set(X509_get_serialNumber(x509), serial))
3471+
goto err;
3472+
3473+
/** Set certificate validity */
3474+
if (!X509_gmtime_adj(X509_get_notBefore(x509), notbefore) ||
3475+
!X509_gmtime_adj(X509_get_notAfter(x509), notafter))
3476+
goto err;
3477+
3478+
/** Set public key */
3479+
if (!X509_set_pubkey(x509, pkey))
3480+
goto err;
3481+
3482+
/** Set CN value in subject */
3483+
name= X509_get_subject_name(x509);
3484+
if (!name)
3485+
goto err;
3486+
3487+
if (!X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
3488+
(const unsigned char *)cn.c_str(),
3489+
-1, -1, 0))
3490+
goto err;
3491+
3492+
/** Set Issuer */
3493+
if (!X509_set_issuer_name(x509, self_sign ? name :
3494+
X509_get_subject_name(ca_x509)))
3495+
goto err;
3496+
3497+
/** Add X509v3 extensions */
3498+
X509V3_set_ctx(&v3ctx, self_sign ? x509 : ca_x509, x509, NULL, NULL, 0);
3499+
3500+
/** Add CA:TRUE / CA:FALSE inforamation */
3501+
if (!(ext= X509V3_EXT_conf_nid(NULL, &v3ctx, NID_basic_constraints,
3502+
self_sign ?(char *)"critical,CA:TRUE" :
3503+
(char *)"critical,CA:FALSE")))
3504+
goto err;
3505+
X509_add_ext(x509, ext, -1);
3506+
X509_EXTENSION_free(ext);
3507+
3508+
/** Sign using SHA256 */
3509+
if (!X509_sign(x509, self_sign ? pkey : ca_pkey, EVP_sha256()))
3510+
goto err;
34673511

34683512
return x509;
3513+
err:
3514+
if (x509)
3515+
X509_free(x509);
3516+
return 0;
34693517
}
34703518
};
34713519

0 commit comments

Comments
 (0)