~ubuntu-branches/ubuntu/vivid/freerdp/vivid

« back to all changes in this revision

Viewing changes to winpr/libwinpr/utils/ntlm.c

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2014-11-11 12:20:50 UTC
  • mfrom: (1.1.9) (9.1.17 sid)
  • Revision ID: package-import@ubuntu.com-20141111122050-wyr8hrnwco9fcmum
Tags: 1.1.0~git20140921.1.440916e+dfsg1-2ubuntu1
* Merge with Debian unstable, remaining changes
  - Disable ffmpeg support
* Disable gstreamer support, this relies on gstreamer 0.10 and we don't want
  to add any more deps on that.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * WinPR: Windows Portable Runtime
 
3
 * NTLM Utils
 
4
 *
 
5
 * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
 
6
 *
 
7
 * Licensed under the Apache License, Version 2.0 (the "License");
 
8
 * you may not use this file except in compliance with the License.
 
9
 * You may obtain a copy of the License at
 
10
 *
 
11
 *     http://www.apache.org/licenses/LICENSE-2.0
 
12
 *
 
13
 * Unless required by applicable law or agreed to in writing, software
 
14
 * distributed under the License is distributed on an "AS IS" BASIS,
 
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
16
 * See the License for the specific language governing permissions and
 
17
 * limitations under the License.
 
18
 */
 
19
 
 
20
#ifdef HAVE_CONFIG_H
 
21
#include "config.h"
 
22
#endif
 
23
 
 
24
#include <winpr/ntlm.h>
 
25
 
 
26
#include <winpr/crt.h>
 
27
#include <winpr/windows.h>
 
28
 
 
29
#include <time.h>
 
30
 
 
31
#include <openssl/ssl.h>
 
32
#include <openssl/des.h>
 
33
#include <openssl/md4.h>
 
34
#include <openssl/md5.h>
 
35
#include <openssl/rc4.h>
 
36
#include <openssl/hmac.h>
 
37
#include <openssl/rand.h>
 
38
#include <openssl/engine.h>
 
39
 
 
40
/**
 
41
 * Define NTOWFv1(Password, User, Domain) as
 
42
 *      MD4(UNICODE(Password))
 
43
 * EndDefine
 
44
 */
 
45
 
 
46
BYTE* NTOWFv1W(LPWSTR Password, UINT32 PasswordLength, BYTE* NtHash)
 
47
{
 
48
        MD4_CTX md4_ctx;
 
49
 
 
50
        if (!Password)
 
51
                return NULL;
 
52
 
 
53
        if (!NtHash)
 
54
                NtHash = malloc(16);
 
55
 
 
56
        MD4_Init(&md4_ctx);
 
57
        MD4_Update(&md4_ctx, Password, PasswordLength);
 
58
        MD4_Final((void*) NtHash, &md4_ctx);
 
59
 
 
60
        return NtHash;
 
61
}
 
62
 
 
63
BYTE* NTOWFv1A(LPSTR Password, UINT32 PasswordLength, BYTE* NtHash)
 
64
{
 
65
        LPWSTR PasswordW = NULL;
 
66
 
 
67
        PasswordW = (LPWSTR) malloc(PasswordLength * 2);
 
68
        MultiByteToWideChar(CP_ACP, 0, Password, PasswordLength, PasswordW, PasswordLength);
 
69
 
 
70
        NtHash = NTOWFv1W(PasswordW, PasswordLength * 2, NtHash);
 
71
 
 
72
        free(PasswordW);
 
73
 
 
74
        return NtHash;
 
75
}
 
76
 
 
77
/**
 
78
 * Define NTOWFv2(Password, User, Domain) as
 
79
 *      HMAC_MD5(MD4(UNICODE(Password)),
 
80
 *              UNICODE(ConcatenationOf(UpperCase(User), Domain)))
 
81
 * EndDefine
 
82
 */
 
83
 
 
84
BYTE* NTOWFv2W(LPWSTR Password, UINT32 PasswordLength, LPWSTR User,
 
85
                UINT32 UserLength, LPWSTR Domain, UINT32 DomainLength, BYTE* NtHash)
 
86
{
 
87
        BYTE* buffer;
 
88
        BYTE NtHashV1[16];
 
89
 
 
90
        if ((!User) || (!Password))
 
91
                return NULL;
 
92
 
 
93
        if (!NtHash)
 
94
                NtHash = (BYTE*) malloc(16);
 
95
 
 
96
        NTOWFv1W(Password, PasswordLength, NtHashV1);
 
97
 
 
98
        buffer = (BYTE*) malloc(UserLength + DomainLength);
 
99
 
 
100
        /* Concatenate(UpperCase(User), Domain) */
 
101
 
 
102
        CopyMemory(buffer, User, UserLength);
 
103
        CharUpperBuffW((LPWSTR) buffer, UserLength / 2);
 
104
        CopyMemory(&buffer[UserLength], Domain, DomainLength);
 
105
 
 
106
        /* Compute the HMAC-MD5 hash of the above value using the NTLMv1 hash as the key, the result is the NTLMv2 hash */
 
107
        HMAC(EVP_md5(), (void*) NtHashV1, 16, buffer, UserLength + DomainLength, (void*) NtHash, NULL);
 
108
 
 
109
        free(buffer);
 
110
 
 
111
        return NtHash;
 
112
}
 
113
 
 
114
BYTE* NTOWFv2A(LPSTR Password, UINT32 PasswordLength, LPSTR User,
 
115
                UINT32 UserLength, LPSTR Domain, UINT32 DomainLength, BYTE* NtHash)
 
116
{
 
117
        LPWSTR UserW = NULL;
 
118
        LPWSTR DomainW = NULL;
 
119
        LPWSTR PasswordW = NULL;
 
120
 
 
121
        UserW = (LPWSTR) malloc(UserLength * 2);
 
122
        DomainW = (LPWSTR) malloc(DomainLength * 2);
 
123
        PasswordW = (LPWSTR) malloc(PasswordLength * 2);
 
124
 
 
125
        MultiByteToWideChar(CP_ACP, 0, User, UserLength, UserW, UserLength);
 
126
        MultiByteToWideChar(CP_ACP, 0, Domain, DomainLength, DomainW, DomainLength);
 
127
        MultiByteToWideChar(CP_ACP, 0, Password, PasswordLength, PasswordW, PasswordLength);
 
128
 
 
129
        NtHash = NTOWFv2W(PasswordW, PasswordLength * 2, UserW, UserLength * 2, DomainW, DomainLength * 2, NtHash);
 
130
 
 
131
        free(UserW);
 
132
        free(DomainW);
 
133
        free(PasswordW);
 
134
 
 
135
        return NtHash;
 
136
}
 
137
 
 
138
BYTE* NTOWFv2FromHashW(BYTE* NtHashV1, LPWSTR User, UINT32 UserLength, LPWSTR Domain, UINT32 DomainLength, BYTE* NtHash)
 
139
{
 
140
        BYTE* buffer;
 
141
 
 
142
        if (!User)
 
143
                return NULL;
 
144
 
 
145
        if (!NtHash)
 
146
                NtHash = (BYTE*) malloc(16);
 
147
 
 
148
        buffer = (BYTE*) malloc(UserLength + DomainLength);
 
149
 
 
150
        /* Concatenate(UpperCase(User), Domain) */
 
151
 
 
152
        CopyMemory(buffer, User, UserLength);
 
153
        CharUpperBuffW((LPWSTR) buffer, UserLength / 2);
 
154
 
 
155
        if (DomainLength > 0)
 
156
        {
 
157
                CopyMemory(&buffer[UserLength], Domain, DomainLength);
 
158
        }
 
159
 
 
160
        /* Compute the HMAC-MD5 hash of the above value using the NTLMv1 hash as the key, the result is the NTLMv2 hash */
 
161
        HMAC(EVP_md5(), (void*) NtHashV1, 16, buffer, UserLength + DomainLength, (void*) NtHash, NULL);
 
162
 
 
163
        free(buffer);
 
164
 
 
165
        return NtHash;
 
166
}
 
167
 
 
168
BYTE* NTOWFv2FromHashA(BYTE* NtHashV1, LPSTR User, UINT32 UserLength, LPSTR Domain, UINT32 DomainLength, BYTE* NtHash)
 
169
{
 
170
        LPWSTR UserW = NULL;
 
171
        LPWSTR DomainW = NULL;
 
172
        LPWSTR PasswordW = NULL;
 
173
 
 
174
        UserW = (LPWSTR) malloc(UserLength * 2);
 
175
        DomainW = (LPWSTR) malloc(DomainLength * 2);
 
176
 
 
177
        MultiByteToWideChar(CP_ACP, 0, User, UserLength, UserW, UserLength);
 
178
        MultiByteToWideChar(CP_ACP, 0, Domain, DomainLength, DomainW, DomainLength);
 
179
 
 
180
        NtHash = NTOWFv2FromHashW(NtHashV1, UserW, UserLength * 2, DomainW, DomainLength * 2, NtHash);
 
181
 
 
182
        free(UserW);
 
183
        free(DomainW);
 
184
        free(PasswordW);
 
185
 
 
186
        return NtHash;
 
187
}