Skip to content

Commit 5158b0b

Browse files
committed
ntlmclient: update to ntlmclient 0.9.1
The ntlmclient dependency can now dynamically load OpenSSL.
1 parent c9b80c2 commit 5158b0b

17 files changed

+623
-346
lines changed

deps/ntlmclient/CMakeLists.txt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,37 @@
1-
FILE(GLOB SRC_NTLMCLIENT "ntlm.c" "unicode_builtin.c" "util.c")
1+
FILE(GLOB SRC_NTLMCLIENT "ntlm.c" "ntlm.h" "util.c" "util.h")
22
LIST(SORT SRC_NTLMCLIENT)
33

44
ADD_DEFINITIONS(-DNTLM_STATIC=1)
55

66
DISABLE_WARNINGS(implicit-fallthrough)
77

8+
IF(USE_ICONV)
9+
ADD_DEFINITIONS(-DUNICODE_ICONV=1)
10+
FILE(GLOB SRC_NTLMCLIENT_UNICODE "unicode_iconv.c" "unicode_iconv.h")
11+
ELSE()
12+
ADD_DEFINITIONS(-DUNICODE_BUILTIN=1)
13+
FILE(GLOB SRC_NTLMCLIENT_UNICODE "unicode_builtin.c" "unicode_builtin.h")
14+
ENDIF()
15+
816
IF(USE_HTTPS STREQUAL "SecureTransport")
917
ADD_DEFINITIONS(-DCRYPT_COMMONCRYPTO)
10-
SET(SRC_NTLMCLIENT_CRYPTO "crypt_commoncrypto.c")
18+
SET(SRC_NTLMCLIENT_CRYPTO "crypt_commoncrypto.c" "crypt_commoncrypto.h")
1119
# CC_MD4 has been deprecated in macOS 10.15.
1220
SET_SOURCE_FILES_PROPERTIES("crypt_commoncrypto.c" COMPILE_FLAGS "-Wno-deprecated")
1321
ELSEIF(USE_HTTPS STREQUAL "OpenSSL")
1422
ADD_DEFINITIONS(-DCRYPT_OPENSSL)
1523
INCLUDE_DIRECTORIES(${OPENSSL_INCLUDE_DIR})
16-
SET(SRC_NTLMCLIENT_CRYPTO "crypt_openssl.c")
24+
SET(SRC_NTLMCLIENT_CRYPTO "crypt_openssl.c" "crypt_openssl.h")
25+
ELSEIF(USE_HTTPS STREQUAL "OpenSSL-Dynamic")
26+
ADD_DEFINITIONS(-DCRYPT_OPENSSL)
27+
ADD_DEFINITIONS(-DCRYPT_OPENSSL_DYNAMIC)
28+
SET(SRC_NTLMCLIENT_CRYPTO "crypt_openssl.c" "crypt_openssl.h")
1729
ELSEIF(USE_HTTPS STREQUAL "mbedTLS")
1830
ADD_DEFINITIONS(-DCRYPT_MBEDTLS)
1931
INCLUDE_DIRECTORIES(${MBEDTLS_INCLUDE_DIR})
20-
SET(SRC_NTLMCLIENT_CRYPTO "crypt_mbedtls.c")
32+
SET(SRC_NTLMCLIENT_CRYPTO "crypt_mbedtls.c" "crypt_mbedtls.h")
2133
ELSE()
2234
MESSAGE(FATAL_ERROR "Unable to use libgit2's HTTPS backend (${USE_HTTPS}) for NTLM crypto")
2335
ENDIF()
2436

25-
ADD_LIBRARY(ntlmclient OBJECT ${SRC_NTLMCLIENT} ${SRC_NTLMCLIENT_CRYPTO})
37+
ADD_LIBRARY(ntlmclient OBJECT ${SRC_NTLMCLIENT} ${SRC_NTLMCLIENT_UNICODE} ${SRC_NTLMCLIENT_CRYPTO})

deps/ntlmclient/crypt.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#ifndef PRIVATE_CRYPT_COMMON_H__
1010
#define PRIVATE_CRYPT_COMMON_H__
1111

12+
#include "ntlmclient.h"
13+
#include "ntlm.h"
14+
1215
#if defined(CRYPT_OPENSSL)
1316
# include "crypt_openssl.h"
1417
#elif defined(CRYPT_MBEDTLS)
@@ -25,40 +28,42 @@
2528

2629
typedef unsigned char ntlm_des_block[CRYPT_DES_BLOCKSIZE];
2730

31+
typedef struct ntlm_crypt_ctx ntlm_crypt_ctx;
32+
33+
extern bool ntlm_crypt_init(ntlm_client *ntlm);
34+
2835
extern bool ntlm_random_bytes(
29-
ntlm_client *ntlm,
3036
unsigned char *out,
37+
ntlm_client *ntlm,
3138
size_t len);
3239

3340
extern bool ntlm_des_encrypt(
3441
ntlm_des_block *out,
42+
ntlm_client *ntlm,
3543
ntlm_des_block *plaintext,
3644
ntlm_des_block *key);
3745

3846
extern bool ntlm_md4_digest(
3947
unsigned char out[CRYPT_MD4_DIGESTSIZE],
48+
ntlm_client *ntlm,
4049
const unsigned char *in,
4150
size_t in_len);
4251

43-
extern ntlm_hmac_ctx *ntlm_hmac_ctx_init(void);
44-
45-
extern bool ntlm_hmac_ctx_reset(ntlm_hmac_ctx *ctx);
46-
4752
extern bool ntlm_hmac_md5_init(
48-
ntlm_hmac_ctx *ctx,
53+
ntlm_client *ntlm,
4954
const unsigned char *key,
5055
size_t key_len);
5156

5257
extern bool ntlm_hmac_md5_update(
53-
ntlm_hmac_ctx *ctx,
58+
ntlm_client *ntlm,
5459
const unsigned char *data,
5560
size_t data_len);
5661

5762
extern bool ntlm_hmac_md5_final(
5863
unsigned char *out,
5964
size_t *out_len,
60-
ntlm_hmac_ctx *ctx);
65+
ntlm_client *ntlm);
6166

62-
extern void ntlm_hmac_ctx_free(ntlm_hmac_ctx *ctx);
67+
extern void ntlm_crypt_shutdown(ntlm_client *ntlm);
6368

6469
#endif /* PRIVATE_CRYPT_COMMON_H__ */

deps/ntlmclient/crypt_commoncrypto.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@
1818
#include "ntlm.h"
1919
#include "crypt.h"
2020

21+
bool ntlm_crypt_init(ntlm_client *ntlm)
22+
{
23+
memset(&ntlm->crypt_ctx, 0, sizeof(ntlm_crypt_ctx));
24+
return true;
25+
}
26+
2127
bool ntlm_random_bytes(
22-
ntlm_client *ntlm,
2328
unsigned char *out,
29+
ntlm_client *ntlm,
2430
size_t len)
2531
{
2632
int fd, ret;
@@ -49,11 +55,14 @@ bool ntlm_random_bytes(
4955

5056
bool ntlm_des_encrypt(
5157
ntlm_des_block *out,
58+
ntlm_client *ntlm,
5259
ntlm_des_block *plaintext,
5360
ntlm_des_block *key)
5461
{
5562
size_t written;
5663

64+
NTLM_UNUSED(ntlm);
65+
5766
CCCryptorStatus result = CCCrypt(kCCEncrypt,
5867
kCCAlgorithmDES, kCCOptionECBMode,
5968
key, sizeof(ntlm_des_block), NULL,
@@ -65,56 +74,47 @@ bool ntlm_des_encrypt(
6574

6675
bool ntlm_md4_digest(
6776
unsigned char out[CRYPT_MD4_DIGESTSIZE],
77+
ntlm_client *ntlm,
6878
const unsigned char *in,
6979
size_t in_len)
7080
{
81+
NTLM_UNUSED(ntlm);
7182
return !!CC_MD4(in, in_len, out);
7283
}
7384

74-
ntlm_hmac_ctx *ntlm_hmac_ctx_init(void)
75-
{
76-
return calloc(1, sizeof(ntlm_hmac_ctx));
77-
}
78-
79-
bool ntlm_hmac_ctx_reset(ntlm_hmac_ctx *ctx)
80-
{
81-
memset(ctx, 0, sizeof(ntlm_hmac_ctx));
82-
return true;
83-
}
84-
8585
bool ntlm_hmac_md5_init(
86-
ntlm_hmac_ctx *ctx,
86+
ntlm_client *ntlm,
8787
const unsigned char *key,
8888
size_t key_len)
8989
{
90-
CCHmacInit(&ctx->native, kCCHmacAlgMD5, key, key_len);
90+
CCHmacInit(&ntlm->crypt_ctx.hmac, kCCHmacAlgMD5, key, key_len);
9191
return true;
9292
}
9393

9494
bool ntlm_hmac_md5_update(
95-
ntlm_hmac_ctx *ctx,
95+
ntlm_client *ntlm,
9696
const unsigned char *data,
9797
size_t data_len)
9898
{
99-
CCHmacUpdate(&ctx->native, data, data_len);
99+
CCHmacUpdate(&ntlm->crypt_ctx.hmac, data, data_len);
100100
return true;
101101
}
102102

103103
bool ntlm_hmac_md5_final(
104104
unsigned char *out,
105105
size_t *out_len,
106-
ntlm_hmac_ctx *ctx)
106+
ntlm_client *ntlm)
107107
{
108108
if (*out_len < CRYPT_MD5_DIGESTSIZE)
109109
return false;
110110

111-
CCHmacFinal(&ctx->native, out);
111+
CCHmacFinal(&ntlm->crypt_ctx.hmac, out);
112112

113113
*out_len = CRYPT_MD5_DIGESTSIZE;
114114
return true;
115115
}
116116

117-
void ntlm_hmac_ctx_free(ntlm_hmac_ctx *ctx)
117+
void ntlm_crypt_shutdown(ntlm_client *ntlm)
118118
{
119-
free(ctx);
119+
NTLM_UNUSED(ntlm);
120120
}

deps/ntlmclient/crypt_commoncrypto.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
#include <CommonCrypto/CommonCrypto.h>
1313

14-
typedef struct {
15-
CCHmacContext native;
16-
} ntlm_hmac_ctx;
14+
struct ntlm_crypt_ctx {
15+
CCHmacContext hmac;
16+
};
1717

1818
#endif /* PRIVATE_CRYPT_COMMONCRYPTO_H__ */

deps/ntlmclient/crypt_mbedtls.c

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,24 @@
1717
#include "ntlm.h"
1818
#include "crypt.h"
1919

20+
bool ntlm_crypt_init(ntlm_client *ntlm)
21+
{
22+
const mbedtls_md_info_t *info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
23+
24+
mbedtls_md_init(&ntlm->crypt_ctx.hmac);
25+
26+
if (mbedtls_md_setup(&ntlm->crypt_ctx.hmac, info, 1) != 0) {
27+
ntlm_client_set_errmsg(ntlm, "could not setup mbedtls digest");
28+
return false;
29+
}
30+
31+
return true;
32+
}
33+
34+
2035
bool ntlm_random_bytes(
21-
ntlm_client *ntlm,
2236
unsigned char *out,
37+
ntlm_client *ntlm,
2338
size_t len)
2439
{
2540
mbedtls_ctr_drbg_context ctr_drbg;
@@ -51,6 +66,7 @@ bool ntlm_random_bytes(
5166

5267
bool ntlm_des_encrypt(
5368
ntlm_des_block *out,
69+
ntlm_client *ntlm,
5470
ntlm_des_block *plaintext,
5571
ntlm_des_block *key)
5672
{
@@ -60,8 +76,10 @@ bool ntlm_des_encrypt(
6076
mbedtls_des_init(&ctx);
6177

6278
if (mbedtls_des_setkey_enc(&ctx, *key) ||
63-
mbedtls_des_crypt_ecb(&ctx, *plaintext, *out))
79+
mbedtls_des_crypt_ecb(&ctx, *plaintext, *out)) {
80+
ntlm_client_set_errmsg(ntlm, "DES encryption failed");
6481
goto done;
82+
}
6583

6684
success = true;
6785

@@ -72,11 +90,14 @@ bool ntlm_des_encrypt(
7290

7391
bool ntlm_md4_digest(
7492
unsigned char out[CRYPT_MD4_DIGESTSIZE],
93+
ntlm_client *ntlm,
7594
const unsigned char *in,
7695
size_t in_len)
7796
{
7897
mbedtls_md4_context ctx;
7998

99+
NTLM_UNUSED(ntlm);
100+
80101
mbedtls_md4_init(&ctx);
81102
mbedtls_md4_starts(&ctx);
82103
mbedtls_md4_update(&ctx, in, in_len);
@@ -86,60 +107,40 @@ bool ntlm_md4_digest(
86107
return true;
87108
}
88109

89-
ntlm_hmac_ctx *ntlm_hmac_ctx_init(void)
90-
{
91-
ntlm_hmac_ctx *ctx;
92-
const mbedtls_md_info_t *info = mbedtls_md_info_from_type(MBEDTLS_MD_MD5);
93-
94-
if ((ctx = calloc(1, sizeof(ntlm_hmac_ctx))) == NULL)
95-
return NULL;
96-
97-
mbedtls_md_init(&ctx->mbed);
98-
99-
if (mbedtls_md_setup(&ctx->mbed, info, 1) != 0) {
100-
free(ctx);
101-
return false;
102-
}
103-
104-
return ctx;
105-
}
106-
107-
bool ntlm_hmac_ctx_reset(ntlm_hmac_ctx *ctx)
108-
{
109-
return !mbedtls_md_hmac_reset(&ctx->mbed);
110-
}
111-
112110
bool ntlm_hmac_md5_init(
113-
ntlm_hmac_ctx *ctx,
111+
ntlm_client *ntlm,
114112
const unsigned char *key,
115113
size_t key_len)
116114
{
117-
return !mbedtls_md_hmac_starts(&ctx->mbed, key, key_len);
115+
if (ntlm->crypt_ctx.hmac_initialized) {
116+
if (mbedtls_md_hmac_reset(&ntlm->crypt_ctx.hmac))
117+
return false;
118+
}
119+
120+
ntlm->crypt_ctx.hmac_initialized = !mbedtls_md_hmac_starts(&ntlm->crypt_ctx.hmac, key, key_len);
121+
return ntlm->crypt_ctx.hmac_initialized;
118122
}
119123

120124
bool ntlm_hmac_md5_update(
121-
ntlm_hmac_ctx *ctx,
125+
ntlm_client *ntlm,
122126
const unsigned char *in,
123127
size_t in_len)
124128
{
125-
return !mbedtls_md_hmac_update(&ctx->mbed, in, in_len);
129+
return !mbedtls_md_hmac_update(&ntlm->crypt_ctx.hmac, in, in_len);
126130
}
127131

128132
bool ntlm_hmac_md5_final(
129133
unsigned char *out,
130134
size_t *out_len,
131-
ntlm_hmac_ctx *ctx)
135+
ntlm_client *ntlm)
132136
{
133137
if (*out_len < CRYPT_MD5_DIGESTSIZE)
134138
return false;
135139

136-
return !mbedtls_md_hmac_finish(&ctx->mbed, out);
140+
return !mbedtls_md_hmac_finish(&ntlm->crypt_ctx.hmac, out);
137141
}
138142

139-
void ntlm_hmac_ctx_free(ntlm_hmac_ctx *ctx)
143+
void ntlm_crypt_shutdown(ntlm_client *ntlm)
140144
{
141-
if (ctx) {
142-
mbedtls_md_free(&ctx->mbed);
143-
free(ctx);
144-
}
145+
mbedtls_md_free(&ntlm->crypt_ctx.hmac);
145146
}

deps/ntlmclient/crypt_mbedtls.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@
1111

1212
#include "mbedtls/md.h"
1313

14-
typedef struct {
15-
mbedtls_md_context_t mbed;
16-
} ntlm_hmac_ctx;
14+
struct ntlm_crypt_ctx {
15+
mbedtls_md_context_t hmac;
16+
unsigned int hmac_initialized : 1;
17+
};
1718

1819
#endif /* PRIVATE_CRYPT_MBEDTLS_H__ */

0 commit comments

Comments
 (0)