sha_pwd.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. int hash_len; /* must be a signed valued -- see below */
  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_error( 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_error( 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_error( 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. result = ( hash_len >= shaLen ) ?
  109. ( memcmp( userhash, dbhash, shaLen ) ) : /* include salt */
  110. ( memcmp( userhash, dbhash + OLD_SALT_LENGTH,
  111. hash_len - OLD_SALT_LENGTH ) ); /* exclude salt */
  112. loser:
  113. if ( dbhash && dbhash != quick_dbhash ) slapi_ch_free_string( &dbhash );
  114. return result;
  115. }
  116. char *
  117. sha_pw_enc( const char *pwd, unsigned int shaLen )
  118. {
  119. char hash[MAX_SHA_HASH_SIZE];
  120. char *enc;
  121. char *schemeName;
  122. unsigned int schemeNameLen;
  123. unsigned int secOID;
  124. size_t enclen;
  125. /* Determine which algorithm we're using */
  126. switch (shaLen) {
  127. case SHA1_LENGTH:
  128. schemeName = SHA1_SCHEME_NAME;
  129. schemeNameLen = SHA1_NAME_LEN;
  130. secOID = SEC_OID_SHA1;
  131. break;
  132. case SHA256_LENGTH:
  133. schemeName = SHA256_SCHEME_NAME;
  134. schemeNameLen = SHA256_NAME_LEN;
  135. secOID = SEC_OID_SHA256;
  136. break;
  137. case SHA384_LENGTH:
  138. schemeName = SHA384_SCHEME_NAME;
  139. schemeNameLen = SHA384_NAME_LEN;
  140. secOID = SEC_OID_SHA384;
  141. break;
  142. case SHA512_LENGTH:
  143. schemeName = SHA512_SCHEME_NAME;
  144. schemeNameLen = SHA512_NAME_LEN;
  145. secOID = SEC_OID_SHA512;
  146. break;
  147. default:
  148. /* An unknown shaLen was passed in. We shouldn't get here. */
  149. return( NULL );
  150. }
  151. /* hash the user's key */
  152. memset( hash, 0, sizeof(hash) );
  153. if ( sha_salted_hash( hash, pwd, NULL, secOID ) != SECSuccess ) {
  154. return( NULL );
  155. }
  156. enclen = 3 + schemeNameLen + LDIF_BASE64_LEN( shaLen );
  157. if (( enc = slapi_ch_calloc( enclen, sizeof(char) )) == NULL ) {
  158. return( NULL );
  159. }
  160. sprintf( enc, "%c%s%c", PWD_HASH_PREFIX_START, schemeName,
  161. PWD_HASH_PREFIX_END );
  162. (void)PL_Base64Encode( hash, shaLen, enc + 2 + schemeNameLen );
  163. return( enc );
  164. }
  165. /*
  166. * Wrapper password comparison functions
  167. */
  168. int
  169. sha1_pw_cmp (const char *userpwd, const char *dbpwd )
  170. {
  171. return sha_pw_cmp( userpwd, dbpwd, SHA1_LENGTH );
  172. }
  173. int
  174. sha256_pw_cmp (const char *userpwd, const char *dbpwd )
  175. {
  176. return sha_pw_cmp( userpwd, dbpwd, SHA256_LENGTH );
  177. }
  178. int
  179. sha384_pw_cmp (const char *userpwd, const char *dbpwd )
  180. {
  181. return sha_pw_cmp( userpwd, dbpwd, SHA384_LENGTH );
  182. }
  183. int
  184. sha512_pw_cmp (const char *userpwd, const char *dbpwd )
  185. {
  186. return sha_pw_cmp( userpwd, dbpwd, SHA512_LENGTH );
  187. }
  188. /*
  189. * Wrapper password encryption functions
  190. */
  191. char *
  192. sha1_pw_enc( const char *pwd )
  193. {
  194. return sha_pw_enc( pwd, SHA1_LENGTH );
  195. }
  196. char *
  197. sha256_pw_enc( const char *pwd )
  198. {
  199. return sha_pw_enc( pwd, SHA256_LENGTH );
  200. }
  201. char *
  202. sha384_pw_enc( const char *pwd )
  203. {
  204. return sha_pw_enc( pwd, SHA384_LENGTH );
  205. }
  206. char *
  207. sha512_pw_enc( const char *pwd )
  208. {
  209. return sha_pw_enc( pwd, SHA512_LENGTH );
  210. }