pbkdf2_check.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (c) 2013 Jan-Piet Mens <jpmens()gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of mosquitto nor the names of its
  14. * contributors may be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <openssl/evp.h>
  33. #include <openssl/rand.h>
  34. #include "base64.h"
  35. #include "erl_nif.h"
  36. #include "emqttd_plugin_mysql_app.h"
  37. #define KEY_LENGTH 24
  38. #define SEPARATOR "$"
  39. #define SEPARATOR1 "_"
  40. #define TRUE (1)
  41. #define FALSE (0)
  42. /*
  43. * Split PBKDF2$... string into their components. The caller must free()
  44. * the strings.
  45. */
  46. static int detoken(char *pbkstr, char **sha, int *iter, char **salt, char **key)
  47. {
  48. char *p, *s, *save;
  49. int rc = 1;
  50. save = s = strdup(pbkstr);
  51. if ((p = strsep(&s, SEPARATOR1)) == NULL)
  52. goto out;
  53. if (strcmp(p, "pbkdf2") != 0)
  54. goto out;
  55. if ((p = strsep(&s, SEPARATOR)) == NULL)
  56. goto out;
  57. *sha = strdup(p);
  58. if ((p = strsep(&s, SEPARATOR)) == NULL)
  59. goto out;
  60. *iter = atoi(p);
  61. if ((p = strsep(&s, SEPARATOR)) == NULL)
  62. goto out;
  63. *salt = strdup(p);
  64. if ((p = strsep(&s, SEPARATOR)) == NULL)
  65. goto out;
  66. *key = strdup(p);
  67. rc = 0;
  68. out:
  69. free(save);
  70. return rc;
  71. }
  72. ERL_NIF_TERM
  73. pbkdf2_check(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
  74. {
  75. ERL_NIF_TERM ret;
  76. ErlNifBinary binps, binhash;
  77. emqttd_plugin_mysql_app_st* st = enif_alloc(sizeof(emqttd_plugin_mysql_app_st));
  78. if(st == NULL) {
  79. return make_atom(env, "alloc_error");
  80. }
  81. st->atom_ok = make_atom(env, "ok");
  82. st->atom_error = make_atom(env, "error");
  83. st->atom_null = make_atom(env, "null");
  84. st->atom_true = make_atom(env, "true");
  85. st->atom_false = make_atom(env, "false");
  86. st->atom_bignum = make_atom(env, "bignum");
  87. st->atom_bignum_e = make_atom(env, "bignum_e");
  88. st->atom_bigdbl = make_atom(env, "bigdbl");
  89. st->atom_partial = make_atom(env, "partial");
  90. st->atom_uescape = make_atom(env, "uescape");
  91. st->atom_pretty = make_atom(env, "pretty");
  92. st->atom_force_utf8 = make_atom(env, "force_utf8");
  93. // Markers used in encoding
  94. st->ref_object = make_atom(env, "$object_ref$");
  95. st->ref_array = make_atom(env, "$array_ref$");
  96. if(argc != 2) {
  97. return make_error(st, env, "Bad args");
  98. } else if(!enif_inspect_binary(env, argv[0], &binps)|!enif_inspect_binary(env, argv[1], &binhash)) {
  99. return make_error(st, env, "Bad args password or username inspect error");
  100. }
  101. char* password = (char*)binps.data;
  102. char* hash = (char*)binhash.data;
  103. static char *sha, *salt, *h_pw;
  104. int iterations, saltlen, blen;
  105. char *b64, *keybuf;
  106. unsigned char *out;
  107. int match = FALSE;
  108. const EVP_MD *evpmd;
  109. int keylen, rc;
  110. if (detoken(hash, &sha, &iterations, &salt, &h_pw) != 0)
  111. return match;
  112. /* Determine key length by decoding base64 */
  113. if ((keybuf = malloc(strlen(h_pw) + 1)) == NULL) {
  114. return make_error(st, env, "internal_error: Out Of memory");
  115. }
  116. keylen = base64_decode(h_pw, keybuf);
  117. if (keylen < 1) {
  118. free(keybuf);
  119. return make_atom(env, "false");
  120. }
  121. free(keybuf);
  122. if ((out = malloc(keylen)) == NULL) {
  123. return make_error(st, env, "Cannot allocate out; out of memory\n");
  124. }
  125. #ifdef PWDEBUG
  126. fprintf(stderr, "sha =[%s]\n", sha);
  127. fprintf(stderr, "iterations =%d\n", iterations);
  128. fprintf(stderr, "salt =[%s]\n", salt);
  129. fprintf(stderr, "h_pw =[%s]\n", h_pw);
  130. fprintf(stderr, "kenlen =[%d]\n", keylen);
  131. #endif
  132. saltlen = strlen((char *)salt);
  133. evpmd = EVP_sha256();
  134. if (strcmp(sha, "sha1") == 0) {
  135. evpmd = EVP_sha1();
  136. } else if (strcmp(sha, "sha512") == 0) {
  137. evpmd = EVP_sha512();
  138. }
  139. rc = PKCS5_PBKDF2_HMAC(password, strlen(password),
  140. (unsigned char *)salt, saltlen,
  141. iterations,
  142. evpmd, keylen, out);
  143. if (rc != 1) {
  144. goto out;
  145. }
  146. blen = base64_encode(out, keylen, &b64);
  147. if (blen > 0) {
  148. int i, diff = 0, hlen = strlen(h_pw);
  149. #ifdef PWDEBUG
  150. fprintf(stderr, "HMAC b64 =[%s]\n", b64);
  151. #endif
  152. /* "manual" strcmp() to ensure constant time */
  153. for (i = 0; (i < blen) && (i < hlen); i++) {
  154. diff |= h_pw[i] ^ b64[i];
  155. }
  156. match = diff == 0;
  157. if (hlen != blen)
  158. match = 0;
  159. free(b64);
  160. }
  161. out:
  162. free(sha);
  163. free(salt);
  164. free(h_pw);
  165. free(out);
  166. if(match == 0){
  167. ret = make_atom(env, "false");
  168. }else{
  169. ret = make_atom(env, "true");
  170. }
  171. return ret;
  172. }
  173. int pbkdf2_check_native(char *password, char *hash)
  174. {
  175. static char *sha, *salt, *h_pw;
  176. int iterations, saltlen, blen;
  177. char *b64;
  178. unsigned char key[128];
  179. int match = FALSE;
  180. const EVP_MD *evpmd;
  181. if (detoken(hash, &sha, &iterations, &salt, &h_pw) != 0)
  182. return match;
  183. #ifdef PWDEBUG
  184. fprintf(stderr, "sha =[%s]\n", sha);
  185. fprintf(stderr, "iterations =%d\n", iterations);
  186. fprintf(stderr, "salt =[%s]\n", salt);
  187. fprintf(stderr, "h_pw =[%s]\n", h_pw);
  188. #endif
  189. saltlen = strlen((char *)salt);
  190. evpmd = EVP_sha256();
  191. if (strcmp(sha, "sha1") == 0) {
  192. evpmd = EVP_sha1();
  193. } else if (strcmp(sha, "sha512") == 0) {
  194. evpmd = EVP_sha512();
  195. }
  196. PKCS5_PBKDF2_HMAC(password, strlen(password),
  197. (unsigned char *)salt, saltlen,
  198. iterations,
  199. evpmd, KEY_LENGTH, key);
  200. blen = base64_encode(key, KEY_LENGTH, &b64);
  201. if (blen > 0) {
  202. int i, diff = 0, hlen = strlen(h_pw);
  203. #ifdef PWDEBUG
  204. fprintf(stderr, "HMAC b64 =[%s]\n", b64);
  205. #endif
  206. /* "manual" strcmp() to ensure constant time */
  207. for (i = 0; (i < blen) && (i < hlen); i++) {
  208. diff |= h_pw[i] ^ b64[i];
  209. }
  210. match = diff == 0;
  211. if (hlen != blen)
  212. match = 0;
  213. free(b64);
  214. }
  215. free(sha);
  216. free(salt);
  217. free(h_pw);
  218. return match;
  219. }
  220. int main()
  221. {
  222. // char password[] = "hello";
  223. // char PB1[] = "PBKDF2$sha256$10000$eytf9sEo8EprP9P3$2eO6tROHiqI3bm+gg+vpmWooWMpz1zji";
  224. char password[] = "supersecret";
  225. //char PB1[] = "PBKDF2$sha256$10000$YEbSTt8FaMRDq/ib$Kt97+sMCYg00mqMOBAYinqZlnxX8HqHk";
  226. char PB1[] = "pbkdf2_sha256$10000$YEbSTt8FaMRDq/ib$Kt97+sMCYg00mqMOBAYinqZlnxX8HqHk";
  227. // char PB1[] = "PBKDF2$sha1$10000$XWfyPLeC9gsD6SbI$HOnjU4Ux7RpeBHdqYxpIGH1R5qCCtNA1";
  228. // char PB1[] = "PBKDF2$sha512$10000$v/aaCgBZ+VZN5L8n$BpgjSTyb4weVxr9cA2mvQ+jaCyaAPeYe";
  229. int match;
  230. printf("Checking password [%s] for %s\n", password, PB1);
  231. match = pbkdf2_check_native(password, PB1);
  232. printf("match == %d\n", match);
  233. return match;
  234. }