php_crypt_r.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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: Pierre Alain Joye <pajoye@php.net |
  16. +----------------------------------------------------------------------+
  17. */
  18. /*
  19. * License for the Unix md5crypt implementation (md5_crypt):
  20. *
  21. * ----------------------------------------------------------------------------
  22. * "THE BEER-WARE LICENSE" (Revision 42):
  23. * <phk@login.dknet.dk> wrote this file. As long as you retain this notice you
  24. * can do whatever you want with this stuff. If we meet some day, and you think
  25. * this stuff is worth it, you can buy me a beer in return. Poul-Henning Kamp
  26. * ----------------------------------------------------------------------------
  27. *
  28. * from FreeBSD: crypt.c,v 1.5 1996/10/14 08:34:02 phk Exp
  29. * via OpenBSD: md5crypt.c,v 1.9 1997/07/23 20:58:27 kstailey Exp
  30. * via NetBSD: md5crypt.c,v 1.4.2.1 2002/01/22 19:31:59 he Exp
  31. *
  32. */
  33. #include "php.h"
  34. #include <string.h>
  35. #ifdef PHP_WIN32
  36. # include <windows.h>
  37. # include <Wincrypt.h>
  38. #endif
  39. #ifdef HAVE_ATOMIC_H /* Solaris 10 defines atomic API within */
  40. # include <atomic.h>
  41. #else
  42. # include <signal.h>
  43. #endif
  44. #include "php_crypt_r.h"
  45. #include "crypt_freesec.h"
  46. #if !PHP_WIN32
  47. #include "ext/standard/md5.h"
  48. #endif
  49. #ifdef ZTS
  50. MUTEX_T php_crypt_extended_init_lock;
  51. #endif
  52. /* TODO: enable it when enabling vista/2k8 mode in tsrm */
  53. #if 0
  54. CONDITION_VARIABLE initialized;
  55. #endif
  56. void php_init_crypt_r()
  57. {
  58. #ifdef ZTS
  59. php_crypt_extended_init_lock = tsrm_mutex_alloc();
  60. #endif
  61. }
  62. void php_shutdown_crypt_r()
  63. {
  64. #ifdef ZTS
  65. tsrm_mutex_free(php_crypt_extended_init_lock);
  66. #endif
  67. }
  68. void _crypt_extended_init_r(void)
  69. {
  70. #ifdef PHP_WIN32
  71. LONG volatile initialized = 0;
  72. #elif defined(HAVE_ATOMIC_H) /* Solaris 10 defines atomic API within */
  73. volatile unsigned int initialized = 0;
  74. #else
  75. static volatile sig_atomic_t initialized = 0;
  76. #endif
  77. #ifdef ZTS
  78. tsrm_mutex_lock(php_crypt_extended_init_lock);
  79. #endif
  80. if (!initialized) {
  81. #ifdef PHP_WIN32
  82. InterlockedIncrement(&initialized);
  83. #elif defined(HAVE_SYNC_FETCH_AND_ADD)
  84. __sync_fetch_and_add(&initialized, 1);
  85. #elif defined(HAVE_ATOMIC_H) /* Solaris 10 defines atomic API within */
  86. membar_producer();
  87. atomic_add_int(&initialized, 1);
  88. #endif
  89. _crypt_extended_init();
  90. }
  91. #ifdef ZTS
  92. tsrm_mutex_unlock(php_crypt_extended_init_lock);
  93. #endif
  94. }
  95. /* MD% crypt implementation using the windows CryptoApi */
  96. #define MD5_MAGIC "$1$"
  97. #define MD5_MAGIC_LEN 3
  98. static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
  99. "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  100. static void
  101. to64(char *s, int32_t v, int n)
  102. {
  103. while (--n >= 0) {
  104. *s++ = itoa64[v & 0x3f];
  105. v >>= 6;
  106. }
  107. }
  108. #ifdef PHP_WIN32
  109. char * php_md5_crypt_r(const char *pw, const char *salt, char *out) {
  110. HCRYPTPROV hCryptProv;
  111. HCRYPTHASH ctx, ctx1;
  112. DWORD i, pwl, sl;
  113. const BYTE magic_md5[4] = "$1$";
  114. const DWORD magic_md5_len = 3;
  115. DWORD dwHashLen;
  116. int pl;
  117. __int32 l;
  118. const char *sp = salt;
  119. const char *ep = salt;
  120. char *p = NULL;
  121. char *passwd = out;
  122. unsigned char final[16];
  123. /* Acquire a cryptographic provider context handle. */
  124. if(!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
  125. return NULL;
  126. }
  127. pwl = (DWORD) strlen(pw);
  128. /* Refine the salt first */
  129. sp = salt;
  130. /* If it starts with the magic string, then skip that */
  131. if (strncmp(sp, MD5_MAGIC, MD5_MAGIC_LEN) == 0) {
  132. sp += MD5_MAGIC_LEN;
  133. }
  134. /* It stops at the first '$', max 8 chars */
  135. for (ep = sp; *ep != '\0' && *ep != '$' && ep < (sp + 8); ep++) {
  136. continue;
  137. }
  138. /* get the length of the true salt */
  139. sl = (DWORD)(ep - sp);
  140. /* Create an empty hash object. */
  141. if(!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &ctx)) {
  142. goto _destroyProv;
  143. }
  144. /* The password first, since that is what is most unknown */
  145. if(!CryptHashData(ctx, (BYTE *)pw, pwl, 0)) {
  146. goto _destroyCtx0;
  147. }
  148. /* Then our magic string */
  149. if(!CryptHashData(ctx, magic_md5, magic_md5_len, 0)) {
  150. goto _destroyCtx0;
  151. }
  152. /* Then the raw salt */
  153. if(!CryptHashData( ctx, (BYTE *)sp, sl, 0)) {
  154. goto _destroyCtx0;
  155. }
  156. /* MD5(pw,salt,pw), valid. */
  157. /* Then just as many characters of the MD5(pw,salt,pw) */
  158. if(!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &ctx1)) {
  159. goto _destroyCtx0;
  160. }
  161. if(!CryptHashData(ctx1, (BYTE *)pw, pwl, 0)) {
  162. goto _destroyCtx1;
  163. }
  164. if(!CryptHashData(ctx1, (BYTE *)sp, sl, 0)) {
  165. goto _destroyCtx1;
  166. }
  167. if(!CryptHashData(ctx1, (BYTE *)pw, pwl, 0)) {
  168. goto _destroyCtx1;
  169. }
  170. dwHashLen = 16;
  171. CryptGetHashParam(ctx1, HP_HASHVAL, final, &dwHashLen, 0);
  172. /* MD5(pw,salt,pw). Valid. */
  173. for (pl = pwl; pl > 0; pl -= 16) {
  174. CryptHashData(ctx, final, (DWORD)(pl > 16 ? 16 : pl), 0);
  175. }
  176. /* Don't leave anything around in vm they could use. */
  177. ZEND_SECURE_ZERO(final, sizeof(final));
  178. /* Then something really weird... */
  179. for (i = pwl; i != 0; i >>= 1) {
  180. if ((i & 1) != 0) {
  181. CryptHashData(ctx, (const BYTE *)final, 1, 0);
  182. } else {
  183. CryptHashData(ctx, (const BYTE *)pw, 1, 0);
  184. }
  185. }
  186. memcpy(passwd, MD5_MAGIC, MD5_MAGIC_LEN);
  187. if (strncpy_s(passwd + MD5_MAGIC_LEN, MD5_HASH_MAX_LEN - MD5_MAGIC_LEN, sp, sl + 1) != 0) {
  188. goto _destroyCtx1;
  189. }
  190. passwd[MD5_MAGIC_LEN + sl] = '\0';
  191. strcat_s(passwd, MD5_HASH_MAX_LEN, "$");
  192. dwHashLen = 16;
  193. /* Fetch the ctx hash value */
  194. CryptGetHashParam(ctx, HP_HASHVAL, final, &dwHashLen, 0);
  195. for (i = 0; i < 1000; i++) {
  196. if(!CryptCreateHash(hCryptProv, CALG_MD5, 0, 0, &ctx1)) {
  197. goto _destroyCtx1;
  198. }
  199. if ((i & 1) != 0) {
  200. if(!CryptHashData(ctx1, (BYTE *)pw, pwl, 0)) {
  201. goto _destroyCtx1;
  202. }
  203. } else {
  204. if(!CryptHashData(ctx1, (BYTE *)final, 16, 0)) {
  205. goto _destroyCtx1;
  206. }
  207. }
  208. if ((i % 3) != 0) {
  209. if(!CryptHashData(ctx1, (BYTE *)sp, sl, 0)) {
  210. goto _destroyCtx1;
  211. }
  212. }
  213. if ((i % 7) != 0) {
  214. if(!CryptHashData(ctx1, (BYTE *)pw, pwl, 0)) {
  215. goto _destroyCtx1;
  216. }
  217. }
  218. if ((i & 1) != 0) {
  219. if(!CryptHashData(ctx1, (BYTE *)final, 16, 0)) {
  220. goto _destroyCtx1;
  221. }
  222. } else {
  223. if(!CryptHashData(ctx1, (BYTE *)pw, pwl, 0)) {
  224. goto _destroyCtx1;
  225. }
  226. }
  227. /* Fetch the ctx hash value */
  228. dwHashLen = 16;
  229. CryptGetHashParam(ctx1, HP_HASHVAL, final, &dwHashLen, 0);
  230. if(!(CryptDestroyHash(ctx1))) {
  231. goto _destroyCtx0;
  232. }
  233. }
  234. ctx1 = (HCRYPTHASH) NULL;
  235. p = passwd + sl + MD5_MAGIC_LEN + 1;
  236. l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4;
  237. l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4;
  238. l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4;
  239. l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4;
  240. l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4;
  241. l = final[11]; to64(p,l,2); p += 2;
  242. *p = '\0';
  243. ZEND_SECURE_ZERO(final, sizeof(final));
  244. _destroyCtx1:
  245. if (ctx1) {
  246. if (!CryptDestroyHash(ctx1)) {
  247. }
  248. }
  249. _destroyCtx0:
  250. CryptDestroyHash(ctx);
  251. _destroyProv:
  252. /* Release the provider handle.*/
  253. if(hCryptProv) {
  254. if(!(CryptReleaseContext(hCryptProv, 0))) {
  255. return NULL;
  256. }
  257. }
  258. return out;
  259. }
  260. #else
  261. /*
  262. * MD5 password encryption.
  263. */
  264. char * php_md5_crypt_r(const char *pw, const char *salt, char *out)
  265. {
  266. ZEND_TLS char passwd[MD5_HASH_MAX_LEN], *p;
  267. const char *sp, *ep;
  268. unsigned char final[16];
  269. unsigned int i, sl, pwl;
  270. PHP_MD5_CTX ctx, ctx1;
  271. uint32_t l;
  272. int pl;
  273. pwl = strlen(pw);
  274. /* Refine the salt first */
  275. sp = salt;
  276. /* If it starts with the magic string, then skip that */
  277. if (strncmp(sp, MD5_MAGIC, MD5_MAGIC_LEN) == 0)
  278. sp += MD5_MAGIC_LEN;
  279. /* It stops at the first '$', max 8 chars */
  280. for (ep = sp; *ep != '\0' && *ep != '$' && ep < (sp + 8); ep++)
  281. continue;
  282. /* get the length of the true salt */
  283. sl = ep - sp;
  284. PHP_MD5Init(&ctx);
  285. /* The password first, since that is what is most unknown */
  286. PHP_MD5Update(&ctx, (const unsigned char *)pw, pwl);
  287. /* Then our magic string */
  288. PHP_MD5Update(&ctx, (const unsigned char *)MD5_MAGIC, MD5_MAGIC_LEN);
  289. /* Then the raw salt */
  290. PHP_MD5Update(&ctx, (const unsigned char *)sp, sl);
  291. /* Then just as many characters of the MD5(pw,salt,pw) */
  292. PHP_MD5Init(&ctx1);
  293. PHP_MD5Update(&ctx1, (const unsigned char *)pw, pwl);
  294. PHP_MD5Update(&ctx1, (const unsigned char *)sp, sl);
  295. PHP_MD5Update(&ctx1, (const unsigned char *)pw, pwl);
  296. PHP_MD5Final(final, &ctx1);
  297. for (pl = pwl; pl > 0; pl -= 16)
  298. PHP_MD5Update(&ctx, final, (unsigned int)(pl > 16 ? 16 : pl));
  299. /* Don't leave anything around in vm they could use. */
  300. ZEND_SECURE_ZERO(final, sizeof(final));
  301. /* Then something really weird... */
  302. for (i = pwl; i != 0; i >>= 1)
  303. if ((i & 1) != 0)
  304. PHP_MD5Update(&ctx, final, 1);
  305. else
  306. PHP_MD5Update(&ctx, (const unsigned char *)pw, 1);
  307. /* Now make the output string */
  308. memcpy(passwd, MD5_MAGIC, MD5_MAGIC_LEN);
  309. strlcpy(passwd + MD5_MAGIC_LEN, sp, sl + 1);
  310. strcat(passwd, "$");
  311. PHP_MD5Final(final, &ctx);
  312. /*
  313. * And now, just to make sure things don't run too fast. On a 60 MHz
  314. * Pentium this takes 34 msec, so you would need 30 seconds to build
  315. * a 1000 entry dictionary...
  316. */
  317. for (i = 0; i < 1000; i++) {
  318. PHP_MD5Init(&ctx1);
  319. if ((i & 1) != 0)
  320. PHP_MD5Update(&ctx1, (const unsigned char *)pw, pwl);
  321. else
  322. PHP_MD5Update(&ctx1, final, 16);
  323. if ((i % 3) != 0)
  324. PHP_MD5Update(&ctx1, (const unsigned char *)sp, sl);
  325. if ((i % 7) != 0)
  326. PHP_MD5Update(&ctx1, (const unsigned char *)pw, pwl);
  327. if ((i & 1) != 0)
  328. PHP_MD5Update(&ctx1, final, 16);
  329. else
  330. PHP_MD5Update(&ctx1, (const unsigned char *)pw, pwl);
  331. PHP_MD5Final(final, &ctx1);
  332. }
  333. p = passwd + sl + MD5_MAGIC_LEN + 1;
  334. l = (final[ 0]<<16) | (final[ 6]<<8) | final[12]; to64(p,l,4); p += 4;
  335. l = (final[ 1]<<16) | (final[ 7]<<8) | final[13]; to64(p,l,4); p += 4;
  336. l = (final[ 2]<<16) | (final[ 8]<<8) | final[14]; to64(p,l,4); p += 4;
  337. l = (final[ 3]<<16) | (final[ 9]<<8) | final[15]; to64(p,l,4); p += 4;
  338. l = (final[ 4]<<16) | (final[10]<<8) | final[ 5]; to64(p,l,4); p += 4;
  339. l = final[11] ; to64(p,l,2); p += 2;
  340. *p = '\0';
  341. /* Don't leave anything around in vm they could use. */
  342. ZEND_SECURE_ZERO(final, sizeof(final));
  343. return (passwd);
  344. }
  345. #undef MD5_MAGIC
  346. #undef MD5_MAGIC_LEN
  347. #endif