md5_pwd.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2005 Red Hat, Inc.
  35. * All rights reserved.
  36. * END COPYRIGHT BLOCK **/
  37. /*
  38. * MD5 Password Encryption/Comparison routines by David Irving, Fred Brittain,
  39. * and Aaron Gagnon -- University of Maine Farmington
  40. * Donated to the RedHat Directory Server Project 2005-06-10
  41. */
  42. #include <string.h>
  43. #include <sys/types.h>
  44. #include <stdio.h>
  45. #include <pk11func.h>
  46. #include <nss.h>
  47. #include <nssb64.h>
  48. #include "pwdstorage.h"
  49. #define MD5_HASH_LEN 20
  50. #define MD5_SUBSYSTEM_NAME "MD5 password hash"
  51. int
  52. md5_pw_cmp( char *userpwd, char *dbpwd )
  53. {
  54. int rc=-1;
  55. char * bver;
  56. PK11Context *ctx=NULL;
  57. unsigned int outLen;
  58. unsigned char hash_out[MD5_HASH_LEN];
  59. unsigned char b2a_out[MD5_HASH_LEN*2]; /* conservative */
  60. SECItem binary_item;
  61. ctx = PK11_CreateDigestContext(SEC_OID_MD5);
  62. if (ctx == NULL) {
  63. slapi_log_error(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
  64. "Could not create context for digest operation for password compare");
  65. goto loser;
  66. }
  67. /* create the hash */
  68. PK11_DigestBegin(ctx);
  69. PK11_DigestOp(ctx, userpwd, strlen(userpwd));
  70. PK11_DigestFinal(ctx, hash_out, &outLen, sizeof hash_out);
  71. PK11_DestroyContext(ctx, 1);
  72. /* convert the binary hash to base64 */
  73. binary_item.data = hash_out;
  74. binary_item.len = outLen;
  75. bver = NSSBase64_EncodeItem(NULL, b2a_out, sizeof b2a_out, &binary_item);
  76. /* bver points to b2a_out upon success */
  77. if (bver) {
  78. rc = strcmp(bver,dbpwd);
  79. } else {
  80. slapi_log_error(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
  81. "Could not base64 encode hashed value for password compare");
  82. }
  83. loser:
  84. return rc;
  85. }
  86. char *
  87. md5_pw_enc( char *pwd )
  88. {
  89. char * bver, *enc=NULL;
  90. PK11Context *ctx=NULL;
  91. unsigned int outLen;
  92. unsigned char hash_out[MD5_HASH_LEN];
  93. unsigned char b2a_out[MD5_HASH_LEN*2]; /* conservative */
  94. SECItem binary_item;
  95. ctx = PK11_CreateDigestContext(SEC_OID_MD5);
  96. if (ctx == NULL) {
  97. slapi_log_error(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
  98. "Could not create context for digest operation for password encoding");
  99. return NULL;
  100. }
  101. /* create the hash */
  102. PK11_DigestBegin(ctx);
  103. PK11_DigestOp(ctx, pwd, strlen(pwd));
  104. PK11_DigestFinal(ctx, hash_out, &outLen, sizeof hash_out);
  105. PK11_DestroyContext(ctx, 1);
  106. /* convert the binary hash to base64 */
  107. binary_item.data = hash_out;
  108. binary_item.len = outLen;
  109. bver = NSSBase64_EncodeItem(NULL, b2a_out, sizeof b2a_out, &binary_item);
  110. if (bver) {
  111. enc = slapi_ch_smprintf("%c%s%c%s", PWD_HASH_PREFIX_START, MD5_SCHEME_NAME,
  112. PWD_HASH_PREFIX_END, bver );
  113. } else {
  114. slapi_log_error(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
  115. "Could not base64 encode hashed value for password encoding");
  116. }
  117. return( enc );
  118. }