md5_pwd.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #ifdef HAVE_CONFIG_H
  38. # include <config.h>
  39. #endif
  40. /*
  41. * MD5 Password Encryption/Comparison routines by David Irving, Fred Brittain,
  42. * and Aaron Gagnon -- University of Maine Farmington
  43. * Donated to the RedHat Directory Server Project 2005-06-10
  44. */
  45. #include <string.h>
  46. #include <sys/types.h>
  47. #include <stdio.h>
  48. #include <pk11func.h>
  49. #include <nss.h>
  50. #include <nssb64.h>
  51. #include "pwdstorage.h"
  52. #define MD5_HASH_LEN 20
  53. #define MD5_SUBSYSTEM_NAME "MD5 password hash"
  54. int
  55. md5_pw_cmp( char *userpwd, char *dbpwd )
  56. {
  57. int rc=-1;
  58. char * bver;
  59. PK11Context *ctx=NULL;
  60. unsigned int outLen;
  61. unsigned char hash_out[MD5_HASH_LEN];
  62. unsigned char b2a_out[MD5_HASH_LEN*2]; /* conservative */
  63. SECItem binary_item;
  64. ctx = PK11_CreateDigestContext(SEC_OID_MD5);
  65. if (ctx == NULL) {
  66. slapi_log_error(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
  67. "Could not create context for digest operation for password compare");
  68. goto loser;
  69. }
  70. /* create the hash */
  71. PK11_DigestBegin(ctx);
  72. PK11_DigestOp(ctx, (const unsigned char *)userpwd, strlen(userpwd));
  73. PK11_DigestFinal(ctx, hash_out, &outLen, sizeof hash_out);
  74. PK11_DestroyContext(ctx, 1);
  75. /* convert the binary hash to base64 */
  76. binary_item.data = hash_out;
  77. binary_item.len = outLen;
  78. bver = NSSBase64_EncodeItem(NULL, (char *)b2a_out, sizeof b2a_out, &binary_item);
  79. /* bver points to b2a_out upon success */
  80. if (bver) {
  81. rc = strcmp(bver,dbpwd);
  82. } else {
  83. slapi_log_error(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
  84. "Could not base64 encode hashed value for password compare");
  85. }
  86. loser:
  87. return rc;
  88. }
  89. char *
  90. md5_pw_enc( char *pwd )
  91. {
  92. char * bver, *enc=NULL;
  93. PK11Context *ctx=NULL;
  94. unsigned int outLen;
  95. unsigned char hash_out[MD5_HASH_LEN];
  96. unsigned char b2a_out[MD5_HASH_LEN*2]; /* conservative */
  97. SECItem binary_item;
  98. ctx = PK11_CreateDigestContext(SEC_OID_MD5);
  99. if (ctx == NULL) {
  100. slapi_log_error(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
  101. "Could not create context for digest operation for password encoding");
  102. return NULL;
  103. }
  104. /* create the hash */
  105. PK11_DigestBegin(ctx);
  106. PK11_DigestOp(ctx, (const unsigned char *)pwd, strlen(pwd));
  107. PK11_DigestFinal(ctx, hash_out, &outLen, sizeof hash_out);
  108. PK11_DestroyContext(ctx, 1);
  109. /* convert the binary hash to base64 */
  110. binary_item.data = hash_out;
  111. binary_item.len = outLen;
  112. bver = NSSBase64_EncodeItem(NULL, (char *)b2a_out, sizeof b2a_out, &binary_item);
  113. if (bver) {
  114. enc = slapi_ch_smprintf("%c%s%c%s", PWD_HASH_PREFIX_START, MD5_SCHEME_NAME,
  115. PWD_HASH_PREFIX_END, bver );
  116. } else {
  117. slapi_log_error(SLAPI_LOG_PLUGIN, MD5_SUBSYSTEM_NAME,
  118. "Could not base64 encode hashed value for password encoding");
  119. }
  120. return( enc );
  121. }