17 #include "my_config.h" 
   19 #if defined(HAVE_OPENSSL) 
   20 #include "crypt_genhash_impl.h" 
   21 #include "mysql/client_authentication.h" 
   23 #include "sql_common.h" 
   25 #include "sql_string.h" 
   29 #if !defined(HAVE_YASSL) 
   30 #include <openssl/rsa.h> 
   31 #include <openssl/pem.h> 
   32 #include <openssl/err.h> 
   33 #if defined(_WIN32) && !defined(_OPENSSL_Applink) && defined(HAVE_OPENSSL_APPLINK_C) 
   34 #include <openssl/applink.c> 
   39 #define MAX_CIPHER_LENGTH 1024 
   41 #if !defined(HAVE_YASSL) 
   45 int sha256_password_init(
char *a, 
size_t b, 
int c, va_list d)
 
   47 #if !defined(HAVE_YASSL) 
   53 int sha256_password_deinit(
void)
 
   55 #if !defined(HAVE_YASSL) 
   62 #if !defined(HAVE_YASSL) 
   71 RSA *rsa_init(
MYSQL *mysql)
 
   73   static RSA *g_public_key= NULL;
 
   83   FILE *pub_key_file= NULL;
 
   85   if (mysql->options.extension != NULL &&
 
   86       mysql->options.extension->server_public_key_path != NULL &&
 
   87       mysql->options.extension->server_public_key_path != 
'\0')
 
   89     pub_key_file= fopen(mysql->options.extension->server_public_key_path,
 
   96   if (pub_key_file == NULL)
 
  102     fprintf(stderr,
"Can't locate server public key '%s'\n",
 
  103               mysql->options.extension->server_public_key_path);
 
  109   key= g_public_key= PEM_read_RSA_PUBKEY(pub_key_file, 0, 0, 0);
 
  111   fclose(pub_key_file);
 
  112   if (g_public_key == NULL)
 
  115     fprintf(stderr, 
"Public key is not in PEM format: '%s'\n",
 
  116             mysql->options.extension->server_public_key_path);
 
  122 #endif // !defined(HAVE_YASSL) 
  138   bool uses_password= mysql->passwd[0] != 0;
 
  139 #if !defined(HAVE_YASSL) 
  140   unsigned char encrypted_password[MAX_CIPHER_LENGTH];
 
  141   static char request_public_key= 
'\1';
 
  142   RSA *public_key= NULL;
 
  143   bool got_public_key_from_server= 
false;
 
  145   bool connection_is_secure= 
false;
 
  146   unsigned char scramble_pkt[20];
 
  150   DBUG_ENTER(
"sha256_password_auth_client");
 
  156   if (vio->
read_packet(vio, &pkt) != SCRAMBLE_LENGTH)
 
  158     DBUG_PRINT(
"info",(
"Scramble is not of correct length."));
 
  165   memcpy(scramble_pkt, pkt, SCRAMBLE_LENGTH);
 
  167   if (mysql_get_ssl_cipher(mysql) != NULL)
 
  168     connection_is_secure= 
true;
 
  171   if (!connection_is_secure)
 
  173  #if !defined(HAVE_YASSL) 
  174     public_key= rsa_init(mysql);
 
  181     static const unsigned char zero_byte= 
'\0'; 
 
  182     if (vio->
write_packet(vio, (
const unsigned char *) &zero_byte, 1))
 
  188     unsigned int passwd_len= strlen(mysql->passwd) + 1;
 
  189     if (!connection_is_secure)
 
  191 #if !defined(HAVE_YASSL) 
  195       if (public_key == NULL)
 
  197         if (vio->
write_packet(vio, (
const unsigned char *) &request_public_key,
 
  205         BIO* bio= BIO_new_mem_buf(pkt, pkt_len);
 
  206         public_key= PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
 
  213         got_public_key_from_server= 
true;
 
  217       xor_string(mysql->passwd, strlen(mysql->passwd), (
char *) scramble_pkt,
 
  220       int cipher_length= RSA_size(public_key);
 
  225       if (passwd_len + 41 >= (
unsigned) cipher_length)
 
  230       RSA_public_encrypt(passwd_len, (
unsigned char *) mysql->passwd,
 
  232                          public_key, RSA_PKCS1_OAEP_PADDING);
 
  233       if (got_public_key_from_server)
 
  234         RSA_free(public_key);
 
  236       if (vio->
write_packet(vio, (uchar*) encrypted_password, cipher_length))
 
  239       set_mysql_extended_error(mysql, CR_AUTH_PLUGIN_ERR, unknown_sqlstate,
 
  240                                 ER(CR_AUTH_PLUGIN_ERR), 
"sha256_password",
 
  241                                 "Authentication requires SSL encryption");
 
  248       if (vio->
write_packet(vio, (uchar*) mysql->passwd, passwd_len))
 
  252     memset(mysql->passwd, 0, passwd_len);