sha_pwd.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  3. * Copyright (C) 2005 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * License: GPL (version 3 or any later version).
  7. * See LICENSE for details.
  8. * END COPYRIGHT BLOCK **/
  9. #ifdef HAVE_CONFIG_H
  10. # include <config.h>
  11. #endif
  12. /*
  13. * slapd hashed password routines
  14. *
  15. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <sys/types.h>
  19. #include "pwdstorage.h"
  20. #include <sechash.h>
  21. #define SHA_SALT_LENGTH 8 /* number of bytes of data in salt */
  22. #define OLD_SALT_LENGTH 8
  23. #define NOT_FIRST_TIME (time_t)1 /* not the first logon */
  24. static char *hasherrmsg = "pw_cmp: %s userPassword \"%s\" is the wrong length or is not properly encoded BASE64\n";
  25. static char *plugin_name = "NSPwdStoragePlugin";
  26. #define DS40B1_SALTED_SHA_LENGTH 18
  27. /* Directory Server 4.0 Beta 1 implemented a scheme that stored
  28. * 8 bytes of salt plus the first 10 bytes of the SHA-1 digest.
  29. * It's obsolescent now, but we still handle such stored values.
  30. */
  31. int
  32. sha_pw_cmp (const char *userpwd, const char *dbpwd, unsigned int shaLen )
  33. {
  34. /*
  35. * SHA passwords are stored in the database as shaLen bytes of
  36. * hash, followed by zero or more bytes of salt, all BASE64 encoded.
  37. */
  38. int result = 1; /* failure */
  39. char userhash[MAX_SHA_HASH_SIZE];
  40. char quick_dbhash[MAX_SHA_HASH_SIZE + SHA_SALT_LENGTH + 3];
  41. char *dbhash = quick_dbhash;
  42. struct berval salt;
  43. PRUint32 hash_len;
  44. unsigned int secOID;
  45. char *schemeName;
  46. char *hashresult = NULL;
  47. PRUint32 dbpwd_len;
  48. /* Determine which algorithm we're using */
  49. switch (shaLen) {
  50. case SHA1_LENGTH:
  51. schemeName = SHA1_SCHEME_NAME;
  52. secOID = SEC_OID_SHA1;
  53. break;
  54. case SHA256_LENGTH:
  55. schemeName = SHA256_SCHEME_NAME;
  56. secOID = SEC_OID_SHA256;
  57. break;
  58. case SHA384_LENGTH:
  59. schemeName = SHA384_SCHEME_NAME;
  60. secOID = SEC_OID_SHA384;
  61. break;
  62. case SHA512_LENGTH:
  63. schemeName = SHA512_SCHEME_NAME;
  64. secOID = SEC_OID_SHA512;
  65. break;
  66. default:
  67. /* An unknown shaLen was passed in. We shouldn't get here. */
  68. goto loser;
  69. }
  70. /* in some cases, the password was stored incorrectly - the base64 dbpwd ends
  71. in a newline - we check for this case and remove the newline, if any -
  72. see bug 552421 */
  73. dbpwd_len = strlen(dbpwd);
  74. if ((dbpwd_len > 0) && (dbpwd[dbpwd_len-1] == '\n')) {
  75. dbpwd_len--;
  76. }
  77. /*
  78. * Decode hash stored in database.
  79. */
  80. hash_len = pwdstorage_base64_decode_len(dbpwd, dbpwd_len);
  81. if ( hash_len > sizeof(quick_dbhash) ) { /* get more space: */
  82. dbhash = (char*) slapi_ch_calloc( hash_len, sizeof(char) );
  83. if ( dbhash == NULL ) goto loser;
  84. } else {
  85. memset( quick_dbhash, 0, sizeof(quick_dbhash) );
  86. }
  87. hashresult = PL_Base64Decode( dbpwd, dbpwd_len, dbhash );
  88. if (NULL == hashresult) {
  89. slapi_log_err(SLAPI_LOG_PLUGIN, plugin_name, hasherrmsg, schemeName, dbpwd );
  90. goto loser;
  91. } else if ( hash_len >= shaLen ) { /* must be salted */
  92. salt.bv_val = (void*)(dbhash + shaLen); /* salt starts after hash value */
  93. salt.bv_len = hash_len - shaLen; /* remaining bytes must be salt */
  94. } else if ( hash_len >= DS40B1_SALTED_SHA_LENGTH ) {
  95. salt.bv_val = (void*)dbhash;
  96. salt.bv_len = OLD_SALT_LENGTH;
  97. } else { /* unsupported, invalid BASE64 (hash_len < 0), or similar */
  98. slapi_log_err(SLAPI_LOG_PLUGIN, plugin_name, hasherrmsg, schemeName, dbpwd );
  99. goto loser;
  100. }
  101. /* hash the user's key */
  102. memset( userhash, 0, sizeof(userhash) );
  103. if ( sha_salted_hash( userhash, userpwd, &salt, secOID ) != SECSuccess ) {
  104. slapi_log_err(SLAPI_LOG_PLUGIN, plugin_name, "sha_pw_cmp: sha_salted_hash() failed\n");
  105. goto loser;
  106. }
  107. /* the proof is in the comparison... */
  108. if ( hash_len >= shaLen ) {
  109. result = slapi_ct_memcmp( userhash, dbhash, shaLen );
  110. } else {
  111. result = slapi_ct_memcmp( userhash, dbhash + OLD_SALT_LENGTH, hash_len - OLD_SALT_LENGTH );
  112. }
  113. loser:
  114. if ( dbhash && dbhash != quick_dbhash ) {
  115. slapi_ch_free_string( &dbhash );
  116. }
  117. return result;
  118. }
  119. char *
  120. sha_pw_enc( const char *pwd, unsigned int shaLen )
  121. {
  122. char hash[MAX_SHA_HASH_SIZE];
  123. char *enc;
  124. char *schemeName;
  125. unsigned int schemeNameLen;
  126. unsigned int secOID;
  127. size_t enclen;
  128. /* Determine which algorithm we're using */
  129. switch (shaLen) {
  130. case SHA1_LENGTH:
  131. schemeName = SHA1_SCHEME_NAME;
  132. schemeNameLen = SHA1_NAME_LEN;
  133. secOID = SEC_OID_SHA1;
  134. break;
  135. case SHA256_LENGTH:
  136. schemeName = SHA256_SCHEME_NAME;
  137. schemeNameLen = SHA256_NAME_LEN;
  138. secOID = SEC_OID_SHA256;
  139. break;
  140. case SHA384_LENGTH:
  141. schemeName = SHA384_SCHEME_NAME;
  142. schemeNameLen = SHA384_NAME_LEN;
  143. secOID = SEC_OID_SHA384;
  144. break;
  145. case SHA512_LENGTH:
  146. schemeName = SHA512_SCHEME_NAME;
  147. schemeNameLen = SHA512_NAME_LEN;
  148. secOID = SEC_OID_SHA512;
  149. break;
  150. default:
  151. /* An unknown shaLen was passed in. We shouldn't get here. */
  152. return( NULL );
  153. }
  154. /* hash the user's key */
  155. memset( hash, 0, sizeof(hash) );
  156. if ( sha_salted_hash( hash, pwd, NULL, secOID ) != SECSuccess ) {
  157. return( NULL );
  158. }
  159. enclen = 3 + schemeNameLen + LDIF_BASE64_LEN( shaLen );
  160. if (( enc = slapi_ch_calloc( enclen, sizeof(char) )) == NULL ) {
  161. return( NULL );
  162. }
  163. sprintf( enc, "%c%s%c", PWD_HASH_PREFIX_START, schemeName,
  164. PWD_HASH_PREFIX_END );
  165. (void)PL_Base64Encode( hash, shaLen, enc + 2 + schemeNameLen );
  166. return( enc );
  167. }
  168. /*
  169. * Wrapper password comparison functions
  170. */
  171. int
  172. sha1_pw_cmp (const char *userpwd, const char *dbpwd )
  173. {
  174. return sha_pw_cmp( userpwd, dbpwd, SHA1_LENGTH );
  175. }
  176. int
  177. sha256_pw_cmp (const char *userpwd, const char *dbpwd )
  178. {
  179. return sha_pw_cmp( userpwd, dbpwd, SHA256_LENGTH );
  180. }
  181. int
  182. sha384_pw_cmp (const char *userpwd, const char *dbpwd )
  183. {
  184. return sha_pw_cmp( userpwd, dbpwd, SHA384_LENGTH );
  185. }
  186. int
  187. sha512_pw_cmp (const char *userpwd, const char *dbpwd )
  188. {
  189. return sha_pw_cmp( userpwd, dbpwd, SHA512_LENGTH );
  190. }
  191. /*
  192. * Wrapper password encryption functions
  193. */
  194. char *
  195. sha1_pw_enc( const char *pwd )
  196. {
  197. return sha_pw_enc( pwd, SHA1_LENGTH );
  198. }
  199. char *
  200. sha256_pw_enc( const char *pwd )
  201. {
  202. return sha_pw_enc( pwd, SHA256_LENGTH );
  203. }
  204. char *
  205. sha384_pw_enc( const char *pwd )
  206. {
  207. return sha_pw_enc( pwd, SHA384_LENGTH );
  208. }
  209. char *
  210. sha512_pw_enc( const char *pwd )
  211. {
  212. return sha_pw_enc( pwd, SHA512_LENGTH );
  213. }