md5_pwd.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright (C) 2005 Red Hat, Inc.
  3. * All rights reserved.
  4. *
  5. * License: GPL (version 3 or any later version).
  6. * See LICENSE for details.
  7. * END COPYRIGHT BLOCK **/
  8. #ifdef HAVE_CONFIG_H
  9. # include <config.h>
  10. #endif
  11. /*
  12. * MD5 Password Encryption/Comparison routines by David Irving, Fred Brittain,
  13. * and Aaron Gagnon -- University of Maine Farmington
  14. * Donated to the RedHat Directory Server Project 2005-06-10
  15. */
  16. #include <string.h>
  17. #include <sys/types.h>
  18. #include <stdio.h>
  19. #include <pk11func.h>
  20. #include <nss.h>
  21. #include <nssb64.h>
  22. #include "pwdstorage.h"
  23. #define MD5_HASH_LEN 20
  24. #define MD5_SUBSYSTEM_NAME "MD5 password hash"
  25. int
  26. md5_pw_cmp( const char *userpwd, const char *dbpwd )
  27. {
  28. int rc=-1;
  29. char * bver;
  30. PK11Context *ctx=NULL;
  31. unsigned int outLen;
  32. unsigned char hash_out[MD5_HASH_LEN];
  33. unsigned char b2a_out[MD5_HASH_LEN*2]; /* conservative */
  34. SECItem binary_item;
  35. ctx = PK11_CreateDigestContext(SEC_OID_MD5);
  36. if (ctx == NULL) {
  37. slapi_log_error(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
  38. "Could not create context for digest operation for password compare");
  39. goto loser;
  40. }
  41. /* create the hash */
  42. PK11_DigestBegin(ctx);
  43. PK11_DigestOp(ctx, (const unsigned char *)userpwd, strlen(userpwd));
  44. PK11_DigestFinal(ctx, hash_out, &outLen, sizeof hash_out);
  45. PK11_DestroyContext(ctx, 1);
  46. /* convert the binary hash to base64 */
  47. binary_item.data = hash_out;
  48. binary_item.len = outLen;
  49. bver = NSSBase64_EncodeItem(NULL, (char *)b2a_out, sizeof b2a_out, &binary_item);
  50. /* bver points to b2a_out upon success */
  51. if (bver) {
  52. rc = strcmp(bver,dbpwd);
  53. } else {
  54. slapi_log_error(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
  55. "Could not base64 encode hashed value for password compare");
  56. }
  57. loser:
  58. return rc;
  59. }
  60. char *
  61. md5_pw_enc( const char *pwd )
  62. {
  63. char * bver, *enc=NULL;
  64. PK11Context *ctx=NULL;
  65. unsigned int outLen;
  66. unsigned char hash_out[MD5_HASH_LEN];
  67. unsigned char b2a_out[MD5_HASH_LEN*2]; /* conservative */
  68. SECItem binary_item;
  69. ctx = PK11_CreateDigestContext(SEC_OID_MD5);
  70. if (ctx == NULL) {
  71. slapi_log_error(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
  72. "Could not create context for digest operation for password encoding");
  73. return NULL;
  74. }
  75. /* create the hash */
  76. PK11_DigestBegin(ctx);
  77. PK11_DigestOp(ctx, (const unsigned char *)pwd, strlen(pwd));
  78. PK11_DigestFinal(ctx, hash_out, &outLen, sizeof hash_out);
  79. PK11_DestroyContext(ctx, 1);
  80. /* convert the binary hash to base64 */
  81. binary_item.data = hash_out;
  82. binary_item.len = outLen;
  83. bver = NSSBase64_EncodeItem(NULL, (char *)b2a_out, sizeof b2a_out, &binary_item);
  84. if (bver) {
  85. enc = slapi_ch_smprintf("%c%s%c%s", PWD_HASH_PREFIX_START, MD5_SCHEME_NAME,
  86. PWD_HASH_PREFIX_END, bver );
  87. } else {
  88. slapi_log_error(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
  89. "Could not base64 encode hashed value for password encoding");
  90. }
  91. return( enc );
  92. }