Main Page | Class List | Directories | File List | Class Members | File Members

passwd.c File Reference

#include <assert.h>
#include <string.h>
#include "apps.h"
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <openssl/des.h>
#include <openssl/md5.h>

Go to the source code of this file.

Defines

#define PROG   passwd_main

Functions

static int do_passwd (int passed_salt, char **salt_p, char **salt_malloc_p, char *passwd, BIO *out, int quiet, int table, int reverse, size_t pw_maxlen, int usecrypt, int use1, int useapr1)
int MAIN (int, char **)
static char * md5crypt (const char *passwd, const char *magic, const char *salt)

Variables

static unsigned const char cov_2char [64]


Define Documentation

#define PROG   passwd_main
 

Definition at line 27 of file passwd.c.


Function Documentation

static int do_passwd int  passed_salt,
char **  salt_p,
char **  salt_malloc_p,
char *  passwd,
BIO out,
int  quiet,
int  table,
int  reverse,
size_t  pw_maxlen,
int  usecrypt,
int  use1,
int  useapr1
[static]
 

Definition at line 417 of file passwd.c.

References ascii2ebcdic, bio_err, BIO_printf(), cov_2char, DES_crypt(), md5crypt(), OPENSSL_malloc, and RAND_pseudo_bytes().

00420         {
00421         char *hash = NULL;
00422 
00423         assert(salt_p != NULL);
00424         assert(salt_malloc_p != NULL);
00425 
00426         /* first make sure we have a salt */
00427         if (!passed_salt)
00428                 {
00429 #ifndef OPENSSL_NO_DES
00430                 if (usecrypt)
00431                         {
00432                         if (*salt_malloc_p == NULL)
00433                                 {
00434                                 *salt_p = *salt_malloc_p = OPENSSL_malloc(3);
00435                                 if (*salt_malloc_p == NULL)
00436                                         goto err;
00437                                 }
00438                         if (RAND_pseudo_bytes((unsigned char *)*salt_p, 2) < 0)
00439                                 goto err;
00440                         (*salt_p)[0] = cov_2char[(*salt_p)[0] & 0x3f]; /* 6 bits */
00441                         (*salt_p)[1] = cov_2char[(*salt_p)[1] & 0x3f]; /* 6 bits */
00442                         (*salt_p)[2] = 0;
00443 #ifdef CHARSET_EBCDIC
00444                         ascii2ebcdic(*salt_p, *salt_p, 2); /* des_crypt will convert
00445                                                             * back to ASCII */
00446 #endif
00447                         }
00448 #endif /* !OPENSSL_NO_DES */
00449 
00450 #ifndef NO_MD5CRYPT_1
00451                 if (use1 || useapr1)
00452                         {
00453                         int i;
00454                         
00455                         if (*salt_malloc_p == NULL)
00456                                 {
00457                                 *salt_p = *salt_malloc_p = OPENSSL_malloc(9);
00458                                 if (*salt_malloc_p == NULL)
00459                                         goto err;
00460                                 }
00461                         if (RAND_pseudo_bytes((unsigned char *)*salt_p, 8) < 0)
00462                                 goto err;
00463                         
00464                         for (i = 0; i < 8; i++)
00465                                 (*salt_p)[i] = cov_2char[(*salt_p)[i] & 0x3f]; /* 6 bits */
00466                         (*salt_p)[8] = 0;
00467                         }
00468 #endif /* !NO_MD5CRYPT_1 */
00469                 }
00470         
00471         assert(*salt_p != NULL);
00472         
00473         /* truncate password if necessary */
00474         if ((strlen(passwd) > pw_maxlen))
00475                 {
00476                 if (!quiet)
00477                         /* XXX: really we should know how to print a size_t, not cast it */
00478                         BIO_printf(bio_err, "Warning: truncating password to %u characters\n", (unsigned)pw_maxlen);
00479                 passwd[pw_maxlen] = 0;
00480                 }
00481         assert(strlen(passwd) <= pw_maxlen);
00482         
00483         /* now compute password hash */
00484 #ifndef OPENSSL_NO_DES
00485         if (usecrypt)
00486                 hash = DES_crypt(passwd, *salt_p);
00487 #endif
00488 #ifndef NO_MD5CRYPT_1
00489         if (use1 || useapr1)
00490                 hash = md5crypt(passwd, (use1 ? "1" : "apr1"), *salt_p);
00491 #endif
00492         assert(hash != NULL);
00493 
00494         if (table && !reverse)
00495                 BIO_printf(out, "%s\t%s\n", passwd, hash);
00496         else if (table && reverse)
00497                 BIO_printf(out, "%s\t%s\n", hash, passwd);
00498         else
00499                 BIO_printf(out, "%s\n", hash);
00500         return 1;
00501         
00502 err:
00503         return 0;
00504         }

int MAIN int  ,
char ** 
 

static char* md5crypt const char *  passwd,
const char *  magic,
const char *  salt
[static]
 

Definition at line 310 of file passwd.c.

References cov_2char, EVP_DigestFinal_ex(), EVP_DigestInit_ex(), EVP_DigestUpdate(), EVP_md5(), EVP_MD_CTX_cleanup(), EVP_MD_CTX_init(), md, MD5_DIGEST_LENGTH, and output.

Referenced by do_passwd().

00311         {
00312         static char out_buf[6 + 9 + 24 + 2]; /* "$apr1$..salt..$.......md5hash..........\0" */
00313         unsigned char buf[MD5_DIGEST_LENGTH];
00314         char *salt_out;
00315         int n;
00316         unsigned int i;
00317         EVP_MD_CTX md,md2;
00318         size_t passwd_len, salt_len;
00319 
00320         passwd_len = strlen(passwd);
00321         out_buf[0] = '$';
00322         out_buf[1] = 0;
00323         assert(strlen(magic) <= 4); /* "1" or "apr1" */
00324         strncat(out_buf, magic, 4);
00325         strncat(out_buf, "$", 1);
00326         strncat(out_buf, salt, 8);
00327         assert(strlen(out_buf) <= 6 + 8); /* "$apr1$..salt.." */
00328         salt_out = out_buf + 2 + strlen(magic);
00329         salt_len = strlen(salt_out);
00330         assert(salt_len <= 8);
00331         
00332         EVP_MD_CTX_init(&md);
00333         EVP_DigestInit_ex(&md,EVP_md5(), NULL);
00334         EVP_DigestUpdate(&md, passwd, passwd_len);
00335         EVP_DigestUpdate(&md, "$", 1);
00336         EVP_DigestUpdate(&md, magic, strlen(magic));
00337         EVP_DigestUpdate(&md, "$", 1);
00338         EVP_DigestUpdate(&md, salt_out, salt_len);
00339         
00340         EVP_MD_CTX_init(&md2);
00341         EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
00342         EVP_DigestUpdate(&md2, passwd, passwd_len);
00343         EVP_DigestUpdate(&md2, salt_out, salt_len);
00344         EVP_DigestUpdate(&md2, passwd, passwd_len);
00345         EVP_DigestFinal_ex(&md2, buf, NULL);
00346 
00347         for (i = passwd_len; i > sizeof buf; i -= sizeof buf)
00348                 EVP_DigestUpdate(&md, buf, sizeof buf);
00349         EVP_DigestUpdate(&md, buf, i);
00350         
00351         n = passwd_len;
00352         while (n)
00353                 {
00354                 EVP_DigestUpdate(&md, (n & 1) ? "\0" : passwd, 1);
00355                 n >>= 1;
00356                 }
00357         EVP_DigestFinal_ex(&md, buf, NULL);
00358 
00359         for (i = 0; i < 1000; i++)
00360                 {
00361                 EVP_DigestInit_ex(&md2,EVP_md5(), NULL);
00362                 EVP_DigestUpdate(&md2, (i & 1) ? (unsigned const char *) passwd : buf,
00363                                        (i & 1) ? passwd_len : sizeof buf);
00364                 if (i % 3)
00365                         EVP_DigestUpdate(&md2, salt_out, salt_len);
00366                 if (i % 7)
00367                         EVP_DigestUpdate(&md2, passwd, passwd_len);
00368                 EVP_DigestUpdate(&md2, (i & 1) ? buf : (unsigned const char *) passwd,
00369                                        (i & 1) ? sizeof buf : passwd_len);
00370                 EVP_DigestFinal_ex(&md2, buf, NULL);
00371                 }
00372         EVP_MD_CTX_cleanup(&md2);
00373         
00374          {
00375                 /* transform buf into output string */
00376         
00377                 unsigned char buf_perm[sizeof buf];
00378                 int dest, source;
00379                 char *output;
00380 
00381                 /* silly output permutation */
00382                 for (dest = 0, source = 0; dest < 14; dest++, source = (source + 6) % 17)
00383                         buf_perm[dest] = buf[source];
00384                 buf_perm[14] = buf[5];
00385                 buf_perm[15] = buf[11];
00386 #ifndef PEDANTIC /* Unfortunately, this generates a "no effect" warning */
00387                 assert(16 == sizeof buf_perm);
00388 #endif
00389                 
00390                 output = salt_out + salt_len;
00391                 assert(output == out_buf + strlen(out_buf));
00392                 
00393                 *output++ = '$';
00394 
00395                 for (i = 0; i < 15; i += 3)
00396                         {
00397                         *output++ = cov_2char[buf_perm[i+2] & 0x3f];
00398                         *output++ = cov_2char[((buf_perm[i+1] & 0xf) << 2) |
00399                                                   (buf_perm[i+2] >> 6)];
00400                         *output++ = cov_2char[((buf_perm[i] & 3) << 4) |
00401                                                   (buf_perm[i+1] >> 4)];
00402                         *output++ = cov_2char[buf_perm[i] >> 2];
00403                         }
00404                 assert(i == 15);
00405                 *output++ = cov_2char[buf_perm[i] & 0x3f];
00406                 *output++ = cov_2char[buf_perm[i] >> 6];
00407                 *output = 0;
00408                 assert(strlen(out_buf) < sizeof(out_buf));
00409          }
00410         EVP_MD_CTX_cleanup(&md);
00411 
00412         return out_buf;
00413         }


Variable Documentation

unsigned const char cov_2char[64] [static]
 

Initial value:

{
        
        0x2E,0x2F,0x30,0x31,0x32,0x33,0x34,0x35,
        0x36,0x37,0x38,0x39,0x41,0x42,0x43,0x44,
        0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,0x4C,
        0x4D,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,
        0x55,0x56,0x57,0x58,0x59,0x5A,0x61,0x62,
        0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6A,
        0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,
        0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A
}

Definition at line 30 of file passwd.c.

Referenced by DES_fcrypt(), do_passwd(), and md5crypt().


© sourcejam.com 2005-2008