mysqlnd_auth.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 5 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 2006-2016 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Georg Richter <georg@mysql.com> |
  16. | Andrey Hristov <andrey@mysql.com> |
  17. | Ulf Wendel <uwendel@mysql.com> |
  18. +----------------------------------------------------------------------+
  19. */
  20. /* $Id: mysqlnd.c 307377 2011-01-11 13:02:57Z andrey $ */
  21. #include "php.h"
  22. #include "mysqlnd.h"
  23. #include "mysqlnd_structs.h"
  24. #include "mysqlnd_wireprotocol.h"
  25. #include "mysqlnd_priv.h"
  26. #include "mysqlnd_result.h"
  27. #include "mysqlnd_charset.h"
  28. #include "mysqlnd_debug.h"
  29. /* {{{ mysqlnd_auth_handshake */
  30. enum_func_status
  31. mysqlnd_auth_handshake(MYSQLND_CONN_DATA * conn,
  32. const char * const user,
  33. const char * const passwd,
  34. const size_t passwd_len,
  35. const char * const db,
  36. const size_t db_len,
  37. const MYSQLND_OPTIONS * const options,
  38. unsigned long mysql_flags,
  39. unsigned int server_charset_no,
  40. zend_bool use_full_blown_auth_packet,
  41. const char * const auth_protocol,
  42. const zend_uchar * const auth_plugin_data,
  43. const size_t auth_plugin_data_len,
  44. char ** switch_to_auth_protocol,
  45. size_t * switch_to_auth_protocol_len,
  46. zend_uchar ** switch_to_auth_protocol_data,
  47. size_t * switch_to_auth_protocol_data_len
  48. TSRMLS_DC)
  49. {
  50. enum_func_status ret = FAIL;
  51. const MYSQLND_CHARSET * charset = NULL;
  52. MYSQLND_PACKET_CHANGE_AUTH_RESPONSE * change_auth_resp_packet = NULL;
  53. MYSQLND_PACKET_AUTH_RESPONSE * auth_resp_packet = NULL;
  54. MYSQLND_PACKET_AUTH * auth_packet = NULL;
  55. DBG_ENTER("mysqlnd_auth_handshake");
  56. auth_resp_packet = conn->protocol->m.get_auth_response_packet(conn->protocol, FALSE TSRMLS_CC);
  57. if (!auth_resp_packet) {
  58. SET_OOM_ERROR(*conn->error_info);
  59. goto end;
  60. }
  61. if (use_full_blown_auth_packet != TRUE) {
  62. change_auth_resp_packet = conn->protocol->m.get_change_auth_response_packet(conn->protocol, FALSE TSRMLS_CC);
  63. if (!change_auth_resp_packet) {
  64. SET_OOM_ERROR(*conn->error_info);
  65. goto end;
  66. }
  67. change_auth_resp_packet->auth_data = auth_plugin_data;
  68. change_auth_resp_packet->auth_data_len = auth_plugin_data_len;
  69. if (!PACKET_WRITE(change_auth_resp_packet, conn)) {
  70. CONN_SET_STATE(conn, CONN_QUIT_SENT);
  71. SET_CLIENT_ERROR(*conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
  72. goto end;
  73. }
  74. } else {
  75. auth_packet = conn->protocol->m.get_auth_packet(conn->protocol, FALSE TSRMLS_CC);
  76. auth_packet->client_flags = mysql_flags;
  77. auth_packet->max_packet_size = options->max_allowed_packet;
  78. if (options->charset_name && (charset = mysqlnd_find_charset_name(options->charset_name))) {
  79. auth_packet->charset_no = charset->nr;
  80. } else {
  81. auth_packet->charset_no = server_charset_no;
  82. }
  83. auth_packet->send_auth_data = TRUE;
  84. auth_packet->user = user;
  85. auth_packet->db = db;
  86. auth_packet->db_len = db_len;
  87. auth_packet->auth_data = auth_plugin_data;
  88. auth_packet->auth_data_len = auth_plugin_data_len;
  89. auth_packet->auth_plugin_name = auth_protocol;
  90. if (conn->server_capabilities & CLIENT_CONNECT_ATTRS) {
  91. auth_packet->connect_attr = conn->options->connect_attr;
  92. }
  93. if (!PACKET_WRITE(auth_packet, conn)) {
  94. goto end;
  95. }
  96. }
  97. if (use_full_blown_auth_packet == TRUE) {
  98. conn->charset = mysqlnd_find_charset_nr(auth_packet->charset_no);
  99. }
  100. if (FAIL == PACKET_READ(auth_resp_packet, conn) || auth_resp_packet->response_code >= 0xFE) {
  101. if (auth_resp_packet->response_code == 0xFE) {
  102. /* old authentication with new server !*/
  103. if (!auth_resp_packet->new_auth_protocol) {
  104. DBG_ERR(mysqlnd_old_passwd);
  105. SET_CLIENT_ERROR(*conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, mysqlnd_old_passwd);
  106. } else {
  107. *switch_to_auth_protocol = mnd_pestrndup(auth_resp_packet->new_auth_protocol, auth_resp_packet->new_auth_protocol_len, FALSE);
  108. *switch_to_auth_protocol_len = auth_resp_packet->new_auth_protocol_len;
  109. if (auth_resp_packet->new_auth_protocol_data) {
  110. *switch_to_auth_protocol_data_len = auth_resp_packet->new_auth_protocol_data_len;
  111. *switch_to_auth_protocol_data = mnd_emalloc(*switch_to_auth_protocol_data_len);
  112. memcpy(*switch_to_auth_protocol_data, auth_resp_packet->new_auth_protocol_data, *switch_to_auth_protocol_data_len);
  113. } else {
  114. *switch_to_auth_protocol_data = NULL;
  115. *switch_to_auth_protocol_data_len = 0;
  116. }
  117. }
  118. } else if (auth_resp_packet->response_code == 0xFF) {
  119. if (auth_resp_packet->sqlstate[0]) {
  120. strlcpy(conn->error_info->sqlstate, auth_resp_packet->sqlstate, sizeof(conn->error_info->sqlstate));
  121. DBG_ERR_FMT("ERROR:%u [SQLSTATE:%s] %s", auth_resp_packet->error_no, auth_resp_packet->sqlstate, auth_resp_packet->error);
  122. }
  123. SET_CLIENT_ERROR(*conn->error_info, auth_resp_packet->error_no, UNKNOWN_SQLSTATE, auth_resp_packet->error);
  124. }
  125. goto end;
  126. }
  127. SET_NEW_MESSAGE(conn->last_message, conn->last_message_len, auth_resp_packet->message, auth_resp_packet->message_len, conn->persistent);
  128. ret = PASS;
  129. end:
  130. PACKET_FREE(change_auth_resp_packet);
  131. PACKET_FREE(auth_packet);
  132. PACKET_FREE(auth_resp_packet);
  133. DBG_RETURN(ret);
  134. }
  135. /* }}} */
  136. /* {{{ mysqlnd_auth_change_user */
  137. enum_func_status
  138. mysqlnd_auth_change_user(MYSQLND_CONN_DATA * const conn,
  139. const char * const user,
  140. const size_t user_len,
  141. const char * const passwd,
  142. const size_t passwd_len,
  143. const char * const db,
  144. const size_t db_len,
  145. const zend_bool silent,
  146. zend_bool use_full_blown_auth_packet,
  147. const char * const auth_protocol,
  148. zend_uchar * auth_plugin_data,
  149. size_t auth_plugin_data_len,
  150. char ** switch_to_auth_protocol,
  151. size_t * switch_to_auth_protocol_len,
  152. zend_uchar ** switch_to_auth_protocol_data,
  153. size_t * switch_to_auth_protocol_data_len
  154. TSRMLS_DC)
  155. {
  156. enum_func_status ret = FAIL;
  157. const MYSQLND_CHARSET * old_cs = conn->charset;
  158. MYSQLND_PACKET_CHANGE_AUTH_RESPONSE * change_auth_resp_packet = NULL;
  159. MYSQLND_PACKET_CHG_USER_RESPONSE * chg_user_resp = NULL;
  160. MYSQLND_PACKET_AUTH * auth_packet = NULL;
  161. DBG_ENTER("mysqlnd_auth_change_user");
  162. chg_user_resp = conn->protocol->m.get_change_user_response_packet(conn->protocol, FALSE TSRMLS_CC);
  163. if (!chg_user_resp) {
  164. SET_OOM_ERROR(*conn->error_info);
  165. goto end;
  166. }
  167. if (use_full_blown_auth_packet != TRUE) {
  168. change_auth_resp_packet = conn->protocol->m.get_change_auth_response_packet(conn->protocol, FALSE TSRMLS_CC);
  169. if (!change_auth_resp_packet) {
  170. SET_OOM_ERROR(*conn->error_info);
  171. goto end;
  172. }
  173. change_auth_resp_packet->auth_data = auth_plugin_data;
  174. change_auth_resp_packet->auth_data_len = auth_plugin_data_len;
  175. if (!PACKET_WRITE(change_auth_resp_packet, conn)) {
  176. CONN_SET_STATE(conn, CONN_QUIT_SENT);
  177. SET_CLIENT_ERROR(*conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
  178. goto end;
  179. }
  180. } else {
  181. auth_packet = conn->protocol->m.get_auth_packet(conn->protocol, FALSE TSRMLS_CC);
  182. if (!auth_packet) {
  183. SET_OOM_ERROR(*conn->error_info);
  184. goto end;
  185. }
  186. auth_packet->is_change_user_packet = TRUE;
  187. auth_packet->user = user;
  188. auth_packet->db = db;
  189. auth_packet->db_len = db_len;
  190. auth_packet->silent = silent;
  191. auth_packet->auth_data = auth_plugin_data;
  192. auth_packet->auth_data_len = auth_plugin_data_len;
  193. auth_packet->auth_plugin_name = auth_protocol;
  194. if (conn->m->get_server_version(conn TSRMLS_CC) >= 50123) {
  195. auth_packet->charset_no = conn->charset->nr;
  196. }
  197. if (!PACKET_WRITE(auth_packet, conn)) {
  198. CONN_SET_STATE(conn, CONN_QUIT_SENT);
  199. SET_CLIENT_ERROR(*conn->error_info, CR_SERVER_GONE_ERROR, UNKNOWN_SQLSTATE, mysqlnd_server_gone);
  200. goto end;
  201. }
  202. }
  203. ret = PACKET_READ(chg_user_resp, conn);
  204. COPY_CLIENT_ERROR(*conn->error_info, chg_user_resp->error_info);
  205. if (0xFE == chg_user_resp->response_code) {
  206. ret = FAIL;
  207. if (!chg_user_resp->new_auth_protocol) {
  208. DBG_ERR(mysqlnd_old_passwd);
  209. SET_CLIENT_ERROR(*conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, mysqlnd_old_passwd);
  210. } else {
  211. *switch_to_auth_protocol = mnd_pestrndup(chg_user_resp->new_auth_protocol, chg_user_resp->new_auth_protocol_len, FALSE);
  212. *switch_to_auth_protocol_len = chg_user_resp->new_auth_protocol_len;
  213. if (chg_user_resp->new_auth_protocol_data) {
  214. *switch_to_auth_protocol_data_len = chg_user_resp->new_auth_protocol_data_len;
  215. *switch_to_auth_protocol_data = mnd_emalloc(*switch_to_auth_protocol_data_len);
  216. memcpy(*switch_to_auth_protocol_data, chg_user_resp->new_auth_protocol_data, *switch_to_auth_protocol_data_len);
  217. } else {
  218. *switch_to_auth_protocol_data = NULL;
  219. *switch_to_auth_protocol_data_len = 0;
  220. }
  221. }
  222. }
  223. if (conn->error_info->error_no) {
  224. ret = FAIL;
  225. /*
  226. COM_CHANGE_USER is broken in 5.1. At least in 5.1.15 and 5.1.14, 5.1.11 is immune.
  227. bug#25371 mysql_change_user() triggers "packets out of sync"
  228. When it gets fixed, there should be one more check here
  229. */
  230. if (conn->m->get_server_version(conn TSRMLS_CC) > 50113L &&conn->m->get_server_version(conn TSRMLS_CC) < 50118L) {
  231. MYSQLND_PACKET_OK * redundant_error_packet = conn->protocol->m.get_ok_packet(conn->protocol, FALSE TSRMLS_CC);
  232. if (redundant_error_packet) {
  233. PACKET_READ(redundant_error_packet, conn);
  234. PACKET_FREE(redundant_error_packet);
  235. DBG_INF_FMT("Server is %u, buggy, sends two ERR messages", conn->m->get_server_version(conn TSRMLS_CC));
  236. } else {
  237. SET_OOM_ERROR(*conn->error_info);
  238. }
  239. }
  240. }
  241. if (ret == PASS) {
  242. char * tmp = NULL;
  243. /* if we get conn->user as parameter and then we first free it, then estrndup it, we will crash */
  244. tmp = mnd_pestrndup(user, user_len, conn->persistent);
  245. if (conn->user) {
  246. mnd_pefree(conn->user, conn->persistent);
  247. }
  248. conn->user = tmp;
  249. tmp = mnd_pestrdup(passwd, conn->persistent);
  250. if (conn->passwd) {
  251. mnd_pefree(conn->passwd, conn->persistent);
  252. }
  253. conn->passwd = tmp;
  254. if (conn->last_message) {
  255. mnd_pefree(conn->last_message, conn->persistent);
  256. conn->last_message = NULL;
  257. }
  258. memset(conn->upsert_status, 0, sizeof(*conn->upsert_status));
  259. /* set charset for old servers */
  260. if (conn->m->get_server_version(conn TSRMLS_CC) < 50123) {
  261. ret = conn->m->set_charset(conn, old_cs->name TSRMLS_CC);
  262. }
  263. } else if (ret == FAIL && chg_user_resp->server_asked_323_auth == TRUE) {
  264. /* old authentication with new server !*/
  265. DBG_ERR(mysqlnd_old_passwd);
  266. SET_CLIENT_ERROR(*conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, mysqlnd_old_passwd);
  267. }
  268. end:
  269. PACKET_FREE(change_auth_resp_packet);
  270. PACKET_FREE(auth_packet);
  271. PACKET_FREE(chg_user_resp);
  272. DBG_RETURN(ret);
  273. }
  274. /* }}} */
  275. /******************************************* MySQL Native Password ***********************************/
  276. #include "ext/standard/sha1.h"
  277. /* {{{ php_mysqlnd_crypt */
  278. static void
  279. php_mysqlnd_crypt(zend_uchar *buffer, const zend_uchar *s1, const zend_uchar *s2, size_t len)
  280. {
  281. const zend_uchar *s1_end = s1 + len;
  282. while (s1 < s1_end) {
  283. *buffer++= *s1++ ^ *s2++;
  284. }
  285. }
  286. /* }}} */
  287. /* {{{ php_mysqlnd_scramble */
  288. void php_mysqlnd_scramble(zend_uchar * const buffer, const zend_uchar * const scramble, const zend_uchar * const password, size_t password_len)
  289. {
  290. PHP_SHA1_CTX context;
  291. zend_uchar sha1[SHA1_MAX_LENGTH];
  292. zend_uchar sha2[SHA1_MAX_LENGTH];
  293. /* Phase 1: hash password */
  294. PHP_SHA1Init(&context);
  295. PHP_SHA1Update(&context, password, password_len);
  296. PHP_SHA1Final(sha1, &context);
  297. /* Phase 2: hash sha1 */
  298. PHP_SHA1Init(&context);
  299. PHP_SHA1Update(&context, (zend_uchar*)sha1, SHA1_MAX_LENGTH);
  300. PHP_SHA1Final(sha2, &context);
  301. /* Phase 3: hash scramble + sha2 */
  302. PHP_SHA1Init(&context);
  303. PHP_SHA1Update(&context, scramble, SCRAMBLE_LENGTH);
  304. PHP_SHA1Update(&context, (zend_uchar*)sha2, SHA1_MAX_LENGTH);
  305. PHP_SHA1Final(buffer, &context);
  306. /* let's crypt buffer now */
  307. php_mysqlnd_crypt(buffer, (const zend_uchar *)buffer, (const zend_uchar *)sha1, SHA1_MAX_LENGTH);
  308. }
  309. /* }}} */
  310. /* {{{ mysqlnd_native_auth_get_auth_data */
  311. static zend_uchar *
  312. mysqlnd_native_auth_get_auth_data(struct st_mysqlnd_authentication_plugin * self,
  313. size_t * auth_data_len,
  314. MYSQLND_CONN_DATA * conn, const char * const user, const char * const passwd,
  315. const size_t passwd_len, zend_uchar * auth_plugin_data, size_t auth_plugin_data_len,
  316. const MYSQLND_OPTIONS * const options,
  317. const MYSQLND_NET_OPTIONS * const net_options,
  318. unsigned long mysql_flags
  319. TSRMLS_DC)
  320. {
  321. zend_uchar * ret = NULL;
  322. DBG_ENTER("mysqlnd_native_auth_get_auth_data");
  323. *auth_data_len = 0;
  324. /* 5.5.x reports 21 as scramble length because it needs to show the length of the data before the plugin name */
  325. if (auth_plugin_data_len < SCRAMBLE_LENGTH) {
  326. /* mysql_native_password only works with SCRAMBLE_LENGTH scramble */
  327. SET_CLIENT_ERROR(*conn->error_info, CR_MALFORMED_PACKET, UNKNOWN_SQLSTATE, "The server sent wrong length for scramble");
  328. DBG_ERR_FMT("The server sent wrong length for scramble %u. Expected %u", auth_plugin_data_len, SCRAMBLE_LENGTH);
  329. DBG_RETURN(NULL);
  330. }
  331. /* copy scrambled pass*/
  332. if (passwd && passwd_len) {
  333. ret = malloc(SCRAMBLE_LENGTH);
  334. *auth_data_len = SCRAMBLE_LENGTH;
  335. /* In 4.1 we use CLIENT_SECURE_CONNECTION and thus the len of the buf should be passed */
  336. php_mysqlnd_scramble((zend_uchar*)ret, auth_plugin_data, (zend_uchar*)passwd, passwd_len);
  337. }
  338. DBG_RETURN(ret);
  339. }
  340. /* }}} */
  341. static struct st_mysqlnd_authentication_plugin mysqlnd_native_auth_plugin =
  342. {
  343. {
  344. MYSQLND_PLUGIN_API_VERSION,
  345. "auth_plugin_mysql_native_password",
  346. MYSQLND_VERSION_ID,
  347. MYSQLND_VERSION,
  348. "PHP License 3.01",
  349. "Andrey Hristov <andrey@mysql.com>, Ulf Wendel <uwendel@mysql.com>, Georg Richter <georg@mysql.com>",
  350. {
  351. NULL, /* no statistics , will be filled later if there are some */
  352. NULL, /* no statistics */
  353. },
  354. {
  355. NULL /* plugin shutdown */
  356. }
  357. },
  358. {/* methods */
  359. mysqlnd_native_auth_get_auth_data
  360. }
  361. };
  362. /******************************************* PAM Authentication ***********************************/
  363. /* {{{ mysqlnd_pam_auth_get_auth_data */
  364. static zend_uchar *
  365. mysqlnd_pam_auth_get_auth_data(struct st_mysqlnd_authentication_plugin * self,
  366. size_t * auth_data_len,
  367. MYSQLND_CONN_DATA * conn, const char * const user, const char * const passwd,
  368. const size_t passwd_len, zend_uchar * auth_plugin_data, size_t auth_plugin_data_len,
  369. const MYSQLND_OPTIONS * const options,
  370. const MYSQLND_NET_OPTIONS * const net_options,
  371. unsigned long mysql_flags
  372. TSRMLS_DC)
  373. {
  374. zend_uchar * ret = NULL;
  375. /* copy pass*/
  376. if (passwd && passwd_len) {
  377. ret = (zend_uchar*) zend_strndup(passwd, passwd_len);
  378. }
  379. *auth_data_len = passwd_len;
  380. return ret;
  381. }
  382. /* }}} */
  383. static struct st_mysqlnd_authentication_plugin mysqlnd_pam_authentication_plugin =
  384. {
  385. {
  386. MYSQLND_PLUGIN_API_VERSION,
  387. "auth_plugin_mysql_clear_password",
  388. MYSQLND_VERSION_ID,
  389. MYSQLND_VERSION,
  390. "PHP License 3.01",
  391. "Andrey Hristov <andrey@php.net>, Ulf Wendel <uw@php.net>, Georg Richter <georg@php.net>",
  392. {
  393. NULL, /* no statistics , will be filled later if there are some */
  394. NULL, /* no statistics */
  395. },
  396. {
  397. NULL /* plugin shutdown */
  398. }
  399. },
  400. {/* methods */
  401. mysqlnd_pam_auth_get_auth_data
  402. }
  403. };
  404. /******************************************* SHA256 Password ***********************************/
  405. #ifdef MYSQLND_HAVE_SSL
  406. static void
  407. mysqlnd_xor_string(char * dst, const size_t dst_len, const char * xor_str, const size_t xor_str_len)
  408. {
  409. unsigned int i;
  410. for (i = 0; i <= dst_len; ++i) {
  411. dst[i] ^= xor_str[i % xor_str_len];
  412. }
  413. }
  414. #include <openssl/rsa.h>
  415. #include <openssl/pem.h>
  416. #include <openssl/err.h>
  417. /* {{{ mysqlnd_sha256_get_rsa_key */
  418. static RSA *
  419. mysqlnd_sha256_get_rsa_key(MYSQLND_CONN_DATA * conn,
  420. const MYSQLND_OPTIONS * const options,
  421. const MYSQLND_NET_OPTIONS * const net_options
  422. TSRMLS_DC)
  423. {
  424. RSA * ret = NULL;
  425. int len;
  426. const char * fname = (net_options->sha256_server_public_key && net_options->sha256_server_public_key[0] != '\0')?
  427. net_options->sha256_server_public_key:
  428. MYSQLND_G(sha256_server_public_key);
  429. php_stream * stream;
  430. DBG_ENTER("mysqlnd_sha256_get_rsa_key");
  431. DBG_INF_FMT("options_s256_pk=[%s] MYSQLND_G(sha256_server_public_key)=[%s]",
  432. net_options->sha256_server_public_key? net_options->sha256_server_public_key:"n/a",
  433. MYSQLND_G(sha256_server_public_key)? MYSQLND_G(sha256_server_public_key):"n/a");
  434. if (!fname || fname[0] == '\0') {
  435. MYSQLND_PACKET_SHA256_PK_REQUEST * pk_req_packet = NULL;
  436. MYSQLND_PACKET_SHA256_PK_REQUEST_RESPONSE * pk_resp_packet = NULL;
  437. do {
  438. DBG_INF("requesting the public key from the server");
  439. pk_req_packet = conn->protocol->m.get_sha256_pk_request_packet(conn->protocol, FALSE TSRMLS_CC);
  440. if (!pk_req_packet) {
  441. SET_OOM_ERROR(*conn->error_info);
  442. break;
  443. }
  444. pk_resp_packet = conn->protocol->m.get_sha256_pk_request_response_packet(conn->protocol, FALSE TSRMLS_CC);
  445. if (!pk_resp_packet) {
  446. SET_OOM_ERROR(*conn->error_info);
  447. PACKET_FREE(pk_req_packet);
  448. break;
  449. }
  450. if (! PACKET_WRITE(pk_req_packet, conn)) {
  451. DBG_ERR_FMT("Error while sending public key request packet");
  452. php_error(E_WARNING, "Error while sending public key request packet. PID=%d", getpid());
  453. CONN_SET_STATE(conn, CONN_QUIT_SENT);
  454. break;
  455. }
  456. if (FAIL == PACKET_READ(pk_resp_packet, conn) || NULL == pk_resp_packet->public_key) {
  457. DBG_ERR_FMT("Error while receiving public key");
  458. php_error(E_WARNING, "Error while receiving public key. PID=%d", getpid());
  459. CONN_SET_STATE(conn, CONN_QUIT_SENT);
  460. break;
  461. }
  462. DBG_INF_FMT("Public key(%d):\n%s", pk_resp_packet->public_key_len, pk_resp_packet->public_key);
  463. /* now extract the public key */
  464. {
  465. BIO * bio = BIO_new_mem_buf(pk_resp_packet->public_key, pk_resp_packet->public_key_len);
  466. ret = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
  467. BIO_free(bio);
  468. }
  469. } while (0);
  470. PACKET_FREE(pk_req_packet);
  471. PACKET_FREE(pk_resp_packet);
  472. DBG_INF_FMT("ret=%p", ret);
  473. DBG_RETURN(ret);
  474. SET_CLIENT_ERROR(*conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE,
  475. "sha256_server_public_key is not set for the connection or as mysqlnd.sha256_server_public_key");
  476. DBG_ERR("server_public_key is not set");
  477. DBG_RETURN(NULL);
  478. } else {
  479. char * key_str = NULL;
  480. DBG_INF_FMT("Key in a file. [%s]", fname);
  481. stream = php_stream_open_wrapper((char *) fname, "rb", REPORT_ERRORS, NULL);
  482. if (stream) {
  483. if ((len = php_stream_copy_to_mem(stream, &key_str, PHP_STREAM_COPY_ALL, 0)) >= 0 ) {
  484. BIO * bio = BIO_new_mem_buf(key_str, len);
  485. ret = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
  486. BIO_free(bio);
  487. DBG_INF("Successfully loaded");
  488. }
  489. if (key_str) {
  490. DBG_INF_FMT("Public key:%*.s", len, key_str);
  491. efree(key_str);
  492. }
  493. php_stream_free(stream, PHP_STREAM_FREE_CLOSE);
  494. }
  495. }
  496. DBG_RETURN(ret);
  497. }
  498. /* }}} */
  499. /* {{{ mysqlnd_sha256_auth_get_auth_data */
  500. static zend_uchar *
  501. mysqlnd_sha256_auth_get_auth_data(struct st_mysqlnd_authentication_plugin * self,
  502. size_t * auth_data_len,
  503. MYSQLND_CONN_DATA * conn, const char * const user, const char * const passwd,
  504. const size_t passwd_len, zend_uchar * auth_plugin_data, size_t auth_plugin_data_len,
  505. const MYSQLND_OPTIONS * const options,
  506. const MYSQLND_NET_OPTIONS * const net_options,
  507. unsigned long mysql_flags
  508. TSRMLS_DC)
  509. {
  510. RSA * server_public_key;
  511. zend_uchar * ret = NULL;
  512. DBG_ENTER("mysqlnd_sha256_auth_get_auth_data");
  513. DBG_INF_FMT("salt(%d)=[%.*s]", auth_plugin_data_len, auth_plugin_data_len, auth_plugin_data);
  514. if (conn->net->data->ssl) {
  515. DBG_INF("simple clear text under SSL");
  516. /* clear text under SSL */
  517. *auth_data_len = passwd_len;
  518. ret = malloc(passwd_len);
  519. memcpy(ret, passwd, passwd_len);
  520. } else {
  521. *auth_data_len = 0;
  522. server_public_key = mysqlnd_sha256_get_rsa_key(conn, options, net_options TSRMLS_CC);
  523. if (server_public_key) {
  524. int server_public_key_len;
  525. char xor_str[passwd_len + 1];
  526. memcpy(xor_str, passwd, passwd_len);
  527. xor_str[passwd_len] = '\0';
  528. mysqlnd_xor_string(xor_str, passwd_len, (char *) auth_plugin_data, auth_plugin_data_len);
  529. server_public_key_len = RSA_size(server_public_key);
  530. /*
  531. Because RSA_PKCS1_OAEP_PADDING is used there is a restriction on the passwd_len.
  532. RSA_PKCS1_OAEP_PADDING is recommended for new applications. See more here:
  533. http://www.openssl.org/docs/crypto/RSA_public_encrypt.html
  534. */
  535. if ((size_t) server_public_key_len - 41 <= passwd_len) {
  536. /* password message is to long */
  537. SET_CLIENT_ERROR(*conn->error_info, CR_UNKNOWN_ERROR, UNKNOWN_SQLSTATE, "password is too long");
  538. DBG_ERR("password is too long");
  539. DBG_RETURN(NULL);
  540. }
  541. *auth_data_len = server_public_key_len;
  542. ret = malloc(*auth_data_len);
  543. RSA_public_encrypt(passwd_len + 1, (zend_uchar *) xor_str, ret, server_public_key, RSA_PKCS1_OAEP_PADDING);
  544. }
  545. }
  546. DBG_RETURN(ret);
  547. }
  548. /* }}} */
  549. static struct st_mysqlnd_authentication_plugin mysqlnd_sha256_authentication_plugin =
  550. {
  551. {
  552. MYSQLND_PLUGIN_API_VERSION,
  553. "auth_plugin_sha256_password",
  554. MYSQLND_VERSION_ID,
  555. MYSQLND_VERSION,
  556. "PHP License 3.01",
  557. "Andrey Hristov <andrey@mysql.com>, Ulf Wendel <uwendel@mysql.com>",
  558. {
  559. NULL, /* no statistics , will be filled later if there are some */
  560. NULL, /* no statistics */
  561. },
  562. {
  563. NULL /* plugin shutdown */
  564. }
  565. },
  566. {/* methods */
  567. mysqlnd_sha256_auth_get_auth_data
  568. }
  569. };
  570. #endif
  571. /* {{{ mysqlnd_register_builtin_authentication_plugins */
  572. void
  573. mysqlnd_register_builtin_authentication_plugins(TSRMLS_D)
  574. {
  575. mysqlnd_plugin_register_ex((struct st_mysqlnd_plugin_header *) &mysqlnd_native_auth_plugin TSRMLS_CC);
  576. mysqlnd_plugin_register_ex((struct st_mysqlnd_plugin_header *) &mysqlnd_pam_authentication_plugin TSRMLS_CC);
  577. #ifdef MYSQLND_HAVE_SSL
  578. mysqlnd_plugin_register_ex((struct st_mysqlnd_plugin_header *) &mysqlnd_sha256_authentication_plugin TSRMLS_CC);
  579. #endif
  580. }
  581. /* }}} */
  582. /*
  583. * Local variables:
  584. * tab-width: 4
  585. * c-basic-offset: 4
  586. * End:
  587. * vim600: noet sw=4 ts=4 fdm=marker
  588. * vim<600: noet sw=4 ts=4
  589. */