~ubuntu-branches/ubuntu/trusty/libesmtp/trusty-proposed

« back to all changes in this revision

Viewing changes to ntlm/ntlmdes.c

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy T. Bouse
  • Date: 2002-03-06 08:37:48 UTC
  • Revision ID: james.westby@ubuntu.com-20020306083748-ihmt32mddsslvg5i
Tags: upstream-0.8.11
ImportĀ upstreamĀ versionĀ 0.8.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Authentication module for the Micr$oft NTLM mechanism.
 
3
 *
 
4
 *  This file is part of libESMTP, a library for submission of RFC 2822
 
5
 *  formatted electronic mail messages using the SMTP protocol described
 
6
 *  in RFC 2821.
 
7
 *
 
8
 *  Copyright (C) 2002  Brian Stafford  <brian@stafford.uklinux.net>
 
9
 *
 
10
 *  This library is free software; you can redistribute it and/or
 
11
 *  modify it under the terms of the GNU Lesser General Public
 
12
 *  License as published by the Free Software Foundation; either
 
13
 *  version 2.1 of the License, or (at your option) any later version.
 
14
 *
 
15
 *  This library is distributed in the hope that it will be useful,
 
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
18
 *  Lesser General Public License for more details.
 
19
 *
 
20
 *  You should have received a copy of the GNU Lesser General Public
 
21
 *  License along with this library; if not, write to the Free Software
 
22
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
23
 */
 
24
#include <stdlib.h>
 
25
#include <string.h>
 
26
#include <ctype.h>
 
27
#include <openssl/des.h>
 
28
#include <openssl/md4.h>
 
29
 
 
30
#include "ntlm.h"
 
31
 
 
32
static void
 
33
lm_deshash (void *result, const_des_cblock *iv, const void *secret)
 
34
{
 
35
  des_cblock key;
 
36
  des_key_schedule ks;
 
37
  unsigned char key_56[8];
 
38
  size_t len;
 
39
 
 
40
  /* copy and pad the secret */
 
41
  len = strlen (secret);
 
42
  if (len > sizeof key_56)
 
43
    len = sizeof key_56;
 
44
  memcpy (key_56, secret, len);
 
45
  if (sizeof key_56 - len > 0)
 
46
    memset (key_56 + len, 0, sizeof key_56 - len);
 
47
 
 
48
  /* convert 56 bit key to the 64 bit */
 
49
  key[0] = key_56[0];
 
50
  key[1] = (key_56[0] << 7) | (key_56[1] >> 1);
 
51
  key[2] = (key_56[1] << 6) | (key_56[2] >> 2);
 
52
  key[3] = (key_56[2] << 5) | (key_56[3] >> 3);
 
53
  key[4] = (key_56[3] << 4) | (key_56[4] >> 4);
 
54
  key[5] = (key_56[4] << 3) | (key_56[5] >> 5);
 
55
  key[6] = (key_56[5] << 2) | (key_56[6] >> 6);
 
56
  key[7] = (key_56[6] << 1);
 
57
 
 
58
  des_set_odd_parity (&key);
 
59
  des_set_key (&key, ks);
 
60
  des_ecb_encrypt (iv, result, ks, DES_ENCRYPT);
 
61
 
 
62
  /* paranoia */
 
63
  memset (key, 0, sizeof key);
 
64
  memset (ks, 0, sizeof ks);
 
65
}
 
66
 
 
67
/* Copy and convert to upper case.  If supplied string is shorter than the
 
68
   destination, zero pad the remainder. */
 
69
char *
 
70
lm_uccpy (char *dst, size_t dstlen, const char *src)
 
71
{
 
72
  char *p;
 
73
  size_t len;
 
74
 
 
75
  if ((len = strlen (src)) > dstlen)
 
76
    len = dstlen;
 
77
  for (p = dst; len > 0; p++, src++, len--)
 
78
    *p = toupper (*src);
 
79
  if (p < dst + dstlen)
 
80
    memset (p, 0, dst + dstlen - p);
 
81
  return dst;
 
82
}
 
83
 
 
84
/* create LanManager hashed password */
 
85
void
 
86
lm_hash_password (unsigned char *hash, const char *pass)
 
87
{
 
88
  static const_des_cblock iv = { 0x4B, 0x47, 0x53, 0x21,
 
89
                                 0x40, 0x23, 0x24, 0x25 };
 
90
  char lmpass[14];
 
91
 
 
92
  lm_uccpy (lmpass, sizeof lmpass, pass);
 
93
  lm_deshash (hash, &iv, lmpass);
 
94
  lm_deshash (hash + 8, &iv, lmpass + 7);
 
95
  memset (lmpass, 0, sizeof lmpass);
 
96
}
 
97
 
 
98
/* convert to unicode */
 
99
unsigned char *
 
100
nt_unicode (const char *string, size_t len)
 
101
{
 
102
  unsigned char *uni, *pp;
 
103
 
 
104
  uni = malloc (len * 2);
 
105
  if ((pp = uni) != NULL)
 
106
    while (len-- > 0)
 
107
      {
 
108
        *pp++ = (unsigned char) *string++;
 
109
        *pp++ = 0;
 
110
      }
 
111
  return uni;
 
112
}
 
113
 
 
114
void
 
115
nt_hash_password (unsigned char *hash, const char *pass)
 
116
{
 
117
  int len;
 
118
  unsigned char *nt_pw;
 
119
  MD4_CTX context;
 
120
 
 
121
  len = strlen (pass);
 
122
  if ((nt_pw = nt_unicode (pass, len)) == NULL)
 
123
    return;
 
124
 
 
125
  MD4_Init (&context);
 
126
  MD4_Update (&context, nt_pw, 2 * len);
 
127
  MD4_Final (hash, &context);
 
128
  memset (&context, 0, sizeof context);
 
129
  memset (nt_pw, 0, 2 * len);
 
130
  free (nt_pw);
 
131
}
 
132
 
 
133
/* Use the server's 8 octet nonce and the secret to create the 24 octet
 
134
   LanManager and NT responses. */
 
135
void
 
136
ntlm_responses (unsigned char *lm_resp, unsigned char *nt_resp,
 
137
                const unsigned char *challenge, const char *secret)
 
138
{
 
139
  unsigned char hash[21];
 
140
  des_cblock nonce;
 
141
 
 
142
  memcpy (&nonce, challenge, sizeof nonce);
 
143
 
 
144
  lm_hash_password (hash, secret);
 
145
  memset (hash + 16, 0, 5);
 
146
  lm_deshash (lm_resp, &nonce, hash);
 
147
  lm_deshash (lm_resp + 8, &nonce, hash + 7);
 
148
  lm_deshash (lm_resp + 16, &nonce, hash + 14);
 
149
 
 
150
  nt_hash_password (hash, secret);
 
151
  memset (hash + 16, 0, 5);
 
152
  lm_deshash (nt_resp, &nonce, hash);
 
153
  lm_deshash (nt_resp + 8, &nonce, hash + 7);
 
154
  lm_deshash (nt_resp + 16, &nonce, hash + 14);
 
155
  memset (hash, 0, sizeof hash);
 
156
}
 
157