rugged 0.28.5 → 0.99.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (379) hide show
  1. checksums.yaml +4 -4
  2. data/ext/rugged/extconf.rb +3 -1
  3. data/ext/rugged/rugged.c +35 -31
  4. data/ext/rugged/rugged.h +13 -0
  5. data/ext/rugged/rugged_blob.c +11 -9
  6. data/ext/rugged/rugged_commit.c +17 -15
  7. data/ext/rugged/rugged_diff.c +4 -26
  8. data/ext/rugged/rugged_index.c +4 -2
  9. data/ext/rugged/rugged_note.c +5 -3
  10. data/ext/rugged/rugged_object.c +57 -10
  11. data/ext/rugged/rugged_rebase.c +3 -1
  12. data/ext/rugged/rugged_remote.c +0 -6
  13. data/ext/rugged/rugged_repo.c +222 -17
  14. data/ext/rugged/rugged_tag.c +8 -6
  15. data/ext/rugged/rugged_tree.c +18 -16
  16. data/lib/rugged/version.rb +1 -1
  17. data/vendor/libgit2/CMakeLists.txt +38 -19
  18. data/vendor/libgit2/COPYING +28 -0
  19. data/vendor/libgit2/cmake/Modules/EnableWarnings.cmake +5 -1
  20. data/vendor/libgit2/cmake/Modules/FindCoreFoundation.cmake +2 -2
  21. data/vendor/libgit2/cmake/Modules/FindGSSAPI.cmake +1 -1
  22. data/vendor/libgit2/cmake/Modules/FindGSSFramework.cmake +28 -0
  23. data/vendor/libgit2/cmake/Modules/FindPCRE.cmake +38 -0
  24. data/vendor/libgit2/cmake/Modules/FindPCRE2.cmake +37 -0
  25. data/vendor/libgit2/cmake/Modules/FindSecurity.cmake +2 -2
  26. data/vendor/libgit2/cmake/Modules/FindStatNsec.cmake +6 -0
  27. data/vendor/libgit2/cmake/Modules/PkgBuildConfig.cmake +110 -0
  28. data/vendor/libgit2/cmake/Modules/SelectGSSAPI.cmake +53 -0
  29. data/vendor/libgit2/cmake/Modules/SelectHTTPSBackend.cmake +124 -0
  30. data/vendor/libgit2/cmake/Modules/SelectHashes.cmake +66 -0
  31. data/vendor/libgit2/deps/http-parser/http_parser.c +11 -6
  32. data/vendor/libgit2/deps/ntlmclient/CMakeLists.txt +21 -0
  33. data/vendor/libgit2/deps/ntlmclient/compat.h +33 -0
  34. data/vendor/libgit2/deps/ntlmclient/crypt.h +64 -0
  35. data/vendor/libgit2/deps/ntlmclient/crypt_commoncrypto.c +120 -0
  36. data/vendor/libgit2/deps/ntlmclient/crypt_commoncrypto.h +18 -0
  37. data/vendor/libgit2/deps/ntlmclient/crypt_mbedtls.c +145 -0
  38. data/vendor/libgit2/deps/ntlmclient/crypt_mbedtls.h +18 -0
  39. data/vendor/libgit2/deps/ntlmclient/crypt_openssl.c +130 -0
  40. data/vendor/libgit2/deps/ntlmclient/crypt_openssl.h +21 -0
  41. data/vendor/libgit2/deps/ntlmclient/ntlm.c +1422 -0
  42. data/vendor/libgit2/deps/ntlmclient/ntlm.h +174 -0
  43. data/vendor/libgit2/deps/ntlmclient/ntlmclient.h +320 -0
  44. data/vendor/libgit2/deps/ntlmclient/unicode.h +36 -0
  45. data/vendor/libgit2/deps/ntlmclient/unicode_builtin.c +445 -0
  46. data/vendor/libgit2/deps/ntlmclient/unicode_iconv.c +201 -0
  47. data/vendor/libgit2/deps/ntlmclient/utf8.h +1257 -0
  48. data/vendor/libgit2/deps/ntlmclient/util.c +21 -0
  49. data/vendor/libgit2/deps/ntlmclient/util.h +14 -0
  50. data/vendor/libgit2/deps/pcre/CMakeLists.txt +140 -0
  51. data/vendor/libgit2/deps/pcre/COPYING +5 -0
  52. data/vendor/libgit2/deps/pcre/cmake/COPYING-CMAKE-SCRIPTS +22 -0
  53. data/vendor/libgit2/deps/pcre/cmake/FindEditline.cmake +17 -0
  54. data/vendor/libgit2/deps/pcre/cmake/FindPackageHandleStandardArgs.cmake +58 -0
  55. data/vendor/libgit2/deps/pcre/cmake/FindReadline.cmake +29 -0
  56. data/vendor/libgit2/deps/pcre/config.h.in +57 -0
  57. data/vendor/libgit2/deps/pcre/pcre.h +641 -0
  58. data/vendor/libgit2/deps/pcre/pcre_byte_order.c +319 -0
  59. data/vendor/libgit2/deps/pcre/pcre_chartables.c +198 -0
  60. data/vendor/libgit2/deps/pcre/pcre_compile.c +9800 -0
  61. data/vendor/libgit2/deps/pcre/pcre_config.c +190 -0
  62. data/vendor/libgit2/deps/pcre/pcre_dfa_exec.c +3676 -0
  63. data/vendor/libgit2/deps/pcre/pcre_exec.c +7173 -0
  64. data/vendor/libgit2/deps/pcre/pcre_fullinfo.c +245 -0
  65. data/vendor/libgit2/deps/pcre/pcre_get.c +669 -0
  66. data/vendor/libgit2/deps/pcre/pcre_globals.c +86 -0
  67. data/vendor/libgit2/deps/pcre/pcre_internal.h +2787 -0
  68. data/vendor/libgit2/deps/pcre/pcre_jit_compile.c +11913 -0
  69. data/vendor/libgit2/deps/pcre/pcre_maketables.c +156 -0
  70. data/vendor/libgit2/deps/pcre/pcre_newline.c +210 -0
  71. data/vendor/libgit2/deps/pcre/pcre_ord2utf8.c +94 -0
  72. data/vendor/libgit2/deps/pcre/pcre_printint.c +834 -0
  73. data/vendor/libgit2/deps/pcre/pcre_refcount.c +92 -0
  74. data/vendor/libgit2/deps/pcre/pcre_string_utils.c +211 -0
  75. data/vendor/libgit2/deps/pcre/pcre_study.c +1686 -0
  76. data/vendor/libgit2/deps/pcre/pcre_tables.c +727 -0
  77. data/vendor/libgit2/deps/pcre/pcre_ucd.c +3644 -0
  78. data/vendor/libgit2/deps/pcre/pcre_valid_utf8.c +301 -0
  79. data/vendor/libgit2/deps/pcre/pcre_version.c +98 -0
  80. data/vendor/libgit2/deps/pcre/pcre_xclass.c +268 -0
  81. data/vendor/libgit2/deps/pcre/pcreposix.c +421 -0
  82. data/vendor/libgit2/deps/pcre/pcreposix.h +117 -0
  83. data/vendor/libgit2/deps/pcre/ucp.h +224 -0
  84. data/vendor/libgit2/deps/zlib/adler32.c +0 -7
  85. data/vendor/libgit2/deps/zlib/crc32.c +0 -7
  86. data/vendor/libgit2/include/git2.h +2 -0
  87. data/vendor/libgit2/include/git2/apply.h +22 -2
  88. data/vendor/libgit2/include/git2/attr.h +23 -13
  89. data/vendor/libgit2/include/git2/blame.h +2 -2
  90. data/vendor/libgit2/include/git2/blob.h +44 -12
  91. data/vendor/libgit2/include/git2/branch.h +74 -57
  92. data/vendor/libgit2/include/git2/buffer.h +20 -14
  93. data/vendor/libgit2/include/git2/cert.h +135 -0
  94. data/vendor/libgit2/include/git2/checkout.h +46 -14
  95. data/vendor/libgit2/include/git2/cherrypick.h +3 -3
  96. data/vendor/libgit2/include/git2/clone.h +2 -2
  97. data/vendor/libgit2/include/git2/commit.h +23 -1
  98. data/vendor/libgit2/include/git2/common.h +15 -6
  99. data/vendor/libgit2/include/git2/config.h +12 -12
  100. data/vendor/libgit2/include/git2/cred_helpers.h +4 -42
  101. data/vendor/libgit2/include/git2/credential.h +314 -0
  102. data/vendor/libgit2/include/git2/credential_helpers.h +52 -0
  103. data/vendor/libgit2/include/git2/deprecated.h +314 -3
  104. data/vendor/libgit2/include/git2/describe.h +4 -4
  105. data/vendor/libgit2/include/git2/diff.h +16 -14
  106. data/vendor/libgit2/include/git2/errors.h +4 -2
  107. data/vendor/libgit2/include/git2/filter.h +8 -0
  108. data/vendor/libgit2/include/git2/index.h +2 -1
  109. data/vendor/libgit2/include/git2/indexer.h +48 -4
  110. data/vendor/libgit2/include/git2/merge.h +6 -10
  111. data/vendor/libgit2/include/git2/net.h +0 -5
  112. data/vendor/libgit2/include/git2/object.h +2 -14
  113. data/vendor/libgit2/include/git2/odb.h +3 -2
  114. data/vendor/libgit2/include/git2/odb_backend.h +5 -4
  115. data/vendor/libgit2/include/git2/oid.h +11 -6
  116. data/vendor/libgit2/include/git2/pack.h +12 -1
  117. data/vendor/libgit2/include/git2/proxy.h +6 -4
  118. data/vendor/libgit2/include/git2/rebase.h +46 -2
  119. data/vendor/libgit2/include/git2/refs.h +19 -0
  120. data/vendor/libgit2/include/git2/remote.h +40 -15
  121. data/vendor/libgit2/include/git2/repository.h +24 -2
  122. data/vendor/libgit2/include/git2/revert.h +1 -1
  123. data/vendor/libgit2/include/git2/revwalk.h +7 -3
  124. data/vendor/libgit2/include/git2/stash.h +4 -4
  125. data/vendor/libgit2/include/git2/status.h +25 -16
  126. data/vendor/libgit2/include/git2/submodule.h +20 -3
  127. data/vendor/libgit2/include/git2/sys/alloc.h +9 -9
  128. data/vendor/libgit2/include/git2/sys/cred.h +15 -0
  129. data/vendor/libgit2/include/git2/sys/credential.h +90 -0
  130. data/vendor/libgit2/include/git2/sys/index.h +4 -2
  131. data/vendor/libgit2/include/git2/sys/mempack.h +2 -1
  132. data/vendor/libgit2/include/git2/sys/merge.h +1 -1
  133. data/vendor/libgit2/include/git2/sys/odb_backend.h +48 -4
  134. data/vendor/libgit2/include/git2/sys/refdb_backend.h +57 -21
  135. data/vendor/libgit2/include/git2/sys/repository.h +17 -6
  136. data/vendor/libgit2/include/git2/sys/transport.h +4 -4
  137. data/vendor/libgit2/include/git2/tag.h +11 -2
  138. data/vendor/libgit2/include/git2/trace.h +2 -2
  139. data/vendor/libgit2/include/git2/transport.h +11 -340
  140. data/vendor/libgit2/include/git2/tree.h +5 -3
  141. data/vendor/libgit2/include/git2/types.h +4 -89
  142. data/vendor/libgit2/include/git2/version.h +4 -4
  143. data/vendor/libgit2/include/git2/worktree.h +5 -5
  144. data/vendor/libgit2/src/CMakeLists.txt +89 -224
  145. data/vendor/libgit2/src/alloc.c +2 -14
  146. data/vendor/libgit2/src/{stdalloc.c → allocators/stdalloc.c} +3 -4
  147. data/vendor/libgit2/src/{stdalloc.h → allocators/stdalloc.h} +4 -4
  148. data/vendor/libgit2/src/allocators/win32_crtdbg.c +118 -0
  149. data/vendor/libgit2/src/{transports/cred.h → allocators/win32_crtdbg.h} +5 -4
  150. data/vendor/libgit2/src/apply.c +31 -15
  151. data/vendor/libgit2/src/attr.c +70 -64
  152. data/vendor/libgit2/src/attr_file.c +189 -96
  153. data/vendor/libgit2/src/attr_file.h +9 -9
  154. data/vendor/libgit2/src/attrcache.c +47 -47
  155. data/vendor/libgit2/src/attrcache.h +2 -1
  156. data/vendor/libgit2/src/blame.c +17 -5
  157. data/vendor/libgit2/src/blame.h +1 -1
  158. data/vendor/libgit2/src/blame_git.c +21 -7
  159. data/vendor/libgit2/src/blob.c +81 -17
  160. data/vendor/libgit2/src/blob.h +2 -2
  161. data/vendor/libgit2/src/branch.c +60 -32
  162. data/vendor/libgit2/src/buffer.c +5 -0
  163. data/vendor/libgit2/src/buffer.h +1 -0
  164. data/vendor/libgit2/src/cache.c +26 -33
  165. data/vendor/libgit2/src/cache.h +1 -1
  166. data/vendor/libgit2/src/cc-compat.h +5 -0
  167. data/vendor/libgit2/src/checkout.c +26 -16
  168. data/vendor/libgit2/src/cherrypick.c +7 -1
  169. data/vendor/libgit2/src/clone.c +29 -7
  170. data/vendor/libgit2/src/clone.h +4 -0
  171. data/vendor/libgit2/src/commit.c +70 -22
  172. data/vendor/libgit2/src/commit.h +6 -0
  173. data/vendor/libgit2/src/commit_list.c +28 -76
  174. data/vendor/libgit2/src/commit_list.h +2 -2
  175. data/vendor/libgit2/src/common.h +3 -75
  176. data/vendor/libgit2/src/config.c +31 -40
  177. data/vendor/libgit2/src/config.h +7 -6
  178. data/vendor/libgit2/src/config_backend.h +12 -0
  179. data/vendor/libgit2/src/config_cache.c +39 -39
  180. data/vendor/libgit2/src/config_entries.c +69 -99
  181. data/vendor/libgit2/src/config_entries.h +1 -0
  182. data/vendor/libgit2/src/config_file.c +337 -380
  183. data/vendor/libgit2/src/config_mem.c +12 -16
  184. data/vendor/libgit2/src/config_parse.c +49 -29
  185. data/vendor/libgit2/src/config_parse.h +13 -12
  186. data/vendor/libgit2/src/config_snapshot.c +206 -0
  187. data/vendor/libgit2/src/crlf.c +14 -14
  188. data/vendor/libgit2/src/describe.c +21 -20
  189. data/vendor/libgit2/src/diff.c +43 -66
  190. data/vendor/libgit2/src/diff.h +4 -3
  191. data/vendor/libgit2/src/diff_driver.c +37 -38
  192. data/vendor/libgit2/src/diff_file.c +12 -10
  193. data/vendor/libgit2/src/diff_file.h +2 -2
  194. data/vendor/libgit2/src/diff_generate.c +26 -26
  195. data/vendor/libgit2/src/diff_generate.h +2 -2
  196. data/vendor/libgit2/src/diff_parse.c +1 -1
  197. data/vendor/libgit2/src/diff_print.c +25 -13
  198. data/vendor/libgit2/src/diff_stats.c +1 -1
  199. data/vendor/libgit2/src/diff_tform.c +11 -11
  200. data/vendor/libgit2/src/errors.c +21 -25
  201. data/vendor/libgit2/src/errors.h +81 -0
  202. data/vendor/libgit2/src/features.h.in +9 -2
  203. data/vendor/libgit2/src/fetch.c +7 -2
  204. data/vendor/libgit2/src/fetchhead.c +9 -9
  205. data/vendor/libgit2/src/filebuf.c +1 -1
  206. data/vendor/libgit2/src/filebuf.h +1 -1
  207. data/vendor/libgit2/src/filter.c +16 -8
  208. data/vendor/libgit2/src/{fileops.c → futils.c} +20 -17
  209. data/vendor/libgit2/src/{fileops.h → futils.h} +5 -5
  210. data/vendor/libgit2/src/hash.c +61 -0
  211. data/vendor/libgit2/src/hash.h +19 -21
  212. data/vendor/libgit2/src/hash/sha1.h +38 -0
  213. data/vendor/libgit2/src/hash/{hash_collisiondetect.h → sha1/collisiondetect.c} +14 -17
  214. data/vendor/libgit2/src/hash/sha1/collisiondetect.h +19 -0
  215. data/vendor/libgit2/src/hash/{hash_common_crypto.h → sha1/common_crypto.c} +15 -19
  216. data/vendor/libgit2/src/hash/sha1/common_crypto.h +19 -0
  217. data/vendor/libgit2/src/hash/{hash_generic.c → sha1/generic.c} +22 -10
  218. data/vendor/libgit2/src/hash/{hash_generic.h → sha1/generic.h} +4 -14
  219. data/vendor/libgit2/src/hash/{hash_mbedtls.c → sha1/mbedtls.c} +15 -7
  220. data/vendor/libgit2/src/hash/{hash_mbedtls.h → sha1/mbedtls.h} +6 -11
  221. data/vendor/libgit2/src/hash/{hash_openssl.h → sha1/openssl.c} +14 -18
  222. data/vendor/libgit2/src/hash/sha1/openssl.h +19 -0
  223. data/vendor/libgit2/src/hash/{sha1dc → sha1/sha1dc}/sha1.c +14 -3
  224. data/vendor/libgit2/src/hash/{sha1dc → sha1/sha1dc}/sha1.h +0 -0
  225. data/vendor/libgit2/src/hash/{sha1dc → sha1/sha1dc}/ubc_check.c +0 -0
  226. data/vendor/libgit2/src/hash/{sha1dc → sha1/sha1dc}/ubc_check.h +0 -0
  227. data/vendor/libgit2/src/hash/{hash_win32.c → sha1/win32.c} +34 -24
  228. data/vendor/libgit2/src/hash/{hash_win32.h → sha1/win32.h} +6 -19
  229. data/vendor/libgit2/src/hashsig.c +1 -1
  230. data/vendor/libgit2/src/idxmap.c +91 -65
  231. data/vendor/libgit2/src/idxmap.h +151 -15
  232. data/vendor/libgit2/src/ignore.c +26 -35
  233. data/vendor/libgit2/src/index.c +103 -81
  234. data/vendor/libgit2/src/index.h +1 -1
  235. data/vendor/libgit2/src/indexer.c +69 -70
  236. data/vendor/libgit2/src/integer.h +11 -4
  237. data/vendor/libgit2/src/iterator.c +32 -28
  238. data/vendor/libgit2/src/iterator.h +8 -8
  239. data/vendor/libgit2/src/map.h +1 -1
  240. data/vendor/libgit2/src/merge.c +55 -41
  241. data/vendor/libgit2/src/merge.h +2 -2
  242. data/vendor/libgit2/src/merge_driver.c +5 -5
  243. data/vendor/libgit2/src/merge_file.c +1 -1
  244. data/vendor/libgit2/src/mwindow.c +18 -23
  245. data/vendor/libgit2/src/mwindow.h +4 -4
  246. data/vendor/libgit2/src/net.c +411 -0
  247. data/vendor/libgit2/src/net.h +57 -0
  248. data/vendor/libgit2/src/netops.c +6 -222
  249. data/vendor/libgit2/src/netops.h +1 -37
  250. data/vendor/libgit2/src/notes.c +2 -2
  251. data/vendor/libgit2/src/object.c +3 -3
  252. data/vendor/libgit2/src/object.h +2 -0
  253. data/vendor/libgit2/src/odb.c +41 -23
  254. data/vendor/libgit2/src/odb.h +3 -2
  255. data/vendor/libgit2/src/odb_loose.c +17 -10
  256. data/vendor/libgit2/src/odb_mempack.c +13 -24
  257. data/vendor/libgit2/src/odb_pack.c +4 -4
  258. data/vendor/libgit2/src/offmap.c +43 -55
  259. data/vendor/libgit2/src/offmap.h +102 -24
  260. data/vendor/libgit2/src/oid.c +19 -8
  261. data/vendor/libgit2/src/oidmap.c +39 -57
  262. data/vendor/libgit2/src/oidmap.h +99 -19
  263. data/vendor/libgit2/src/pack-objects.c +25 -32
  264. data/vendor/libgit2/src/pack-objects.h +1 -1
  265. data/vendor/libgit2/src/pack.c +97 -129
  266. data/vendor/libgit2/src/pack.h +15 -18
  267. data/vendor/libgit2/src/parse.c +10 -0
  268. data/vendor/libgit2/src/parse.h +3 -3
  269. data/vendor/libgit2/src/patch.c +1 -1
  270. data/vendor/libgit2/src/patch_generate.c +1 -1
  271. data/vendor/libgit2/src/patch_parse.c +30 -9
  272. data/vendor/libgit2/src/path.c +43 -6
  273. data/vendor/libgit2/src/path.h +2 -0
  274. data/vendor/libgit2/src/pathspec.c +14 -14
  275. data/vendor/libgit2/src/pool.c +26 -22
  276. data/vendor/libgit2/src/pool.h +7 -7
  277. data/vendor/libgit2/src/posix.c +7 -7
  278. data/vendor/libgit2/src/posix.h +12 -1
  279. data/vendor/libgit2/src/proxy.c +7 -2
  280. data/vendor/libgit2/src/push.c +10 -5
  281. data/vendor/libgit2/src/reader.c +2 -2
  282. data/vendor/libgit2/src/rebase.c +87 -28
  283. data/vendor/libgit2/src/refdb.c +12 -0
  284. data/vendor/libgit2/src/refdb_fs.c +215 -169
  285. data/vendor/libgit2/src/reflog.c +11 -13
  286. data/vendor/libgit2/src/refs.c +34 -23
  287. data/vendor/libgit2/src/refs.h +8 -1
  288. data/vendor/libgit2/src/refspec.c +9 -16
  289. data/vendor/libgit2/src/regexp.c +221 -0
  290. data/vendor/libgit2/src/regexp.h +97 -0
  291. data/vendor/libgit2/src/remote.c +57 -55
  292. data/vendor/libgit2/src/remote.h +2 -2
  293. data/vendor/libgit2/src/repository.c +120 -103
  294. data/vendor/libgit2/src/repository.h +49 -40
  295. data/vendor/libgit2/src/revert.c +6 -1
  296. data/vendor/libgit2/src/revparse.c +18 -19
  297. data/vendor/libgit2/src/revwalk.c +71 -33
  298. data/vendor/libgit2/src/revwalk.h +20 -0
  299. data/vendor/libgit2/src/settings.c +13 -1
  300. data/vendor/libgit2/src/sortedcache.c +12 -26
  301. data/vendor/libgit2/src/sortedcache.h +1 -1
  302. data/vendor/libgit2/src/stash.c +45 -65
  303. data/vendor/libgit2/src/status.c +17 -11
  304. data/vendor/libgit2/src/streams/openssl.c +53 -1
  305. data/vendor/libgit2/src/streams/socket.c +2 -2
  306. data/vendor/libgit2/src/strmap.c +37 -84
  307. data/vendor/libgit2/src/strmap.h +105 -33
  308. data/vendor/libgit2/src/submodule.c +151 -126
  309. data/vendor/libgit2/src/submodule.h +1 -1
  310. data/vendor/libgit2/src/tag.c +10 -2
  311. data/vendor/libgit2/src/trace.c +1 -1
  312. data/vendor/libgit2/src/trace.h +3 -3
  313. data/vendor/libgit2/src/trailer.c +46 -32
  314. data/vendor/libgit2/src/transaction.c +3 -8
  315. data/vendor/libgit2/src/transports/auth.c +16 -15
  316. data/vendor/libgit2/src/transports/auth.h +18 -11
  317. data/vendor/libgit2/src/transports/auth_negotiate.c +64 -33
  318. data/vendor/libgit2/src/transports/auth_negotiate.h +2 -2
  319. data/vendor/libgit2/src/transports/auth_ntlm.c +223 -0
  320. data/vendor/libgit2/src/transports/auth_ntlm.h +38 -0
  321. data/vendor/libgit2/src/transports/credential.c +476 -0
  322. data/vendor/libgit2/src/transports/{cred_helpers.c → credential_helpers.c} +21 -8
  323. data/vendor/libgit2/src/transports/git.c +11 -16
  324. data/vendor/libgit2/src/transports/http.c +488 -1248
  325. data/vendor/libgit2/src/transports/http.h +4 -1
  326. data/vendor/libgit2/src/transports/httpclient.c +1526 -0
  327. data/vendor/libgit2/src/transports/httpclient.h +190 -0
  328. data/vendor/libgit2/src/transports/local.c +10 -10
  329. data/vendor/libgit2/src/transports/smart.c +19 -19
  330. data/vendor/libgit2/src/transports/smart.h +3 -3
  331. data/vendor/libgit2/src/transports/smart_protocol.c +40 -64
  332. data/vendor/libgit2/src/transports/ssh.c +77 -59
  333. data/vendor/libgit2/src/transports/winhttp.c +266 -241
  334. data/vendor/libgit2/src/tree-cache.c +14 -7
  335. data/vendor/libgit2/src/tree.c +16 -26
  336. data/vendor/libgit2/src/unix/map.c +1 -1
  337. data/vendor/libgit2/src/unix/posix.h +2 -12
  338. data/vendor/libgit2/src/userdiff.h +3 -1
  339. data/vendor/libgit2/src/util.c +51 -53
  340. data/vendor/libgit2/src/util.h +16 -21
  341. data/vendor/libgit2/src/wildmatch.c +320 -0
  342. data/vendor/libgit2/src/wildmatch.h +23 -0
  343. data/vendor/libgit2/src/win32/map.c +3 -5
  344. data/vendor/libgit2/src/win32/path_w32.c +12 -2
  345. data/vendor/libgit2/src/win32/path_w32.h +0 -29
  346. data/vendor/libgit2/src/win32/posix.h +1 -4
  347. data/vendor/libgit2/src/win32/posix_w32.c +48 -5
  348. data/vendor/libgit2/src/win32/precompiled.h +0 -2
  349. data/vendor/libgit2/src/win32/thread.c +5 -5
  350. data/vendor/libgit2/src/win32/w32_buffer.c +7 -3
  351. data/vendor/libgit2/src/win32/w32_common.h +39 -0
  352. data/vendor/libgit2/src/win32/w32_crtdbg_stacktrace.c +0 -93
  353. data/vendor/libgit2/src/win32/w32_crtdbg_stacktrace.h +0 -2
  354. data/vendor/libgit2/src/win32/w32_stack.c +4 -9
  355. data/vendor/libgit2/src/win32/w32_stack.h +3 -3
  356. data/vendor/libgit2/src/win32/w32_util.c +31 -0
  357. data/vendor/libgit2/src/win32/w32_util.h +6 -32
  358. data/vendor/libgit2/src/worktree.c +36 -22
  359. data/vendor/libgit2/src/xdiff/xdiffi.c +1 -1
  360. data/vendor/libgit2/src/xdiff/xmerge.c +12 -0
  361. data/vendor/libgit2/src/xdiff/xpatience.c +3 -0
  362. data/vendor/libgit2/src/zstream.c +5 -0
  363. data/vendor/libgit2/src/zstream.h +1 -0
  364. metadata +108 -38
  365. data/vendor/libgit2/deps/regex/CMakeLists.txt +0 -2
  366. data/vendor/libgit2/deps/regex/COPYING +0 -502
  367. data/vendor/libgit2/deps/regex/config.h +0 -7
  368. data/vendor/libgit2/deps/regex/regcomp.c +0 -3857
  369. data/vendor/libgit2/deps/regex/regex.c +0 -92
  370. data/vendor/libgit2/deps/regex/regex.h +0 -582
  371. data/vendor/libgit2/deps/regex/regex_internal.c +0 -1744
  372. data/vendor/libgit2/deps/regex/regex_internal.h +0 -819
  373. data/vendor/libgit2/deps/regex/regexec.c +0 -4369
  374. data/vendor/libgit2/include/git2/inttypes.h +0 -309
  375. data/vendor/libgit2/include/git2/sys/time.h +0 -31
  376. data/vendor/libgit2/libgit2.pc.in +0 -13
  377. data/vendor/libgit2/src/fnmatch.c +0 -248
  378. data/vendor/libgit2/src/fnmatch.h +0 -48
  379. data/vendor/libgit2/src/transports/cred.c +0 -390
@@ -0,0 +1,21 @@
1
+ /*
2
+ * Copyright (c) Edward Thomson. All rights reserved.
3
+ *
4
+ * This file is part of ntlmclient, distributed under the MIT license.
5
+ * For full terms and copyright information, and for third-party
6
+ * copyright information, see the included LICENSE.txt file.
7
+ */
8
+
9
+ #ifndef PRIVATE_CRYPT_OPENSSL_H__
10
+ #define PRIVATE_CRYPT_OPENSSL_H__
11
+
12
+ #include <openssl/hmac.h>
13
+
14
+ /* OpenSSL 1.1.0 uses opaque structs, we'll reuse these. */
15
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L
16
+ typedef struct hmac_ctx_st ntlm_hmac_ctx;
17
+ #else
18
+ # define ntlm_hmac_ctx HMAC_CTX
19
+ #endif
20
+
21
+ #endif /* PRIVATE_CRYPT_OPENSSL_H__ */
@@ -0,0 +1,1422 @@
1
+ /*
2
+ * Copyright (c) Edward Thomson. All rights reserved.
3
+ *
4
+ * This file is part of ntlmclient, distributed under the MIT license.
5
+ * For full terms and copyright information, and for third-party
6
+ * copyright information, see the included LICENSE.txt file.
7
+ */
8
+
9
+ #include <stdlib.h>
10
+ #include <stdint.h>
11
+ #include <string.h>
12
+ #include <assert.h>
13
+ #include <errno.h>
14
+ #include <ctype.h>
15
+ #include <unistd.h>
16
+ #include <fcntl.h>
17
+ #include <time.h>
18
+ #include <arpa/inet.h>
19
+
20
+ #include "ntlm.h"
21
+ #include "unicode.h"
22
+ #include "utf8.h"
23
+ #include "crypt.h"
24
+ #include "compat.h"
25
+ #include "util.h"
26
+
27
+ unsigned char ntlm_client_signature[] = NTLM_SIGNATURE;
28
+
29
+ static bool supports_unicode(ntlm_client *ntlm)
30
+ {
31
+ return (ntlm->flags & NTLM_CLIENT_DISABLE_UNICODE) ?
32
+ false : true;
33
+ }
34
+
35
+ static inline bool increment_size(size_t *out, size_t incr)
36
+ {
37
+ if (SIZE_MAX - *out < incr) {
38
+ *out = (size_t)-1;
39
+ return false;
40
+ }
41
+
42
+ *out = *out + incr;
43
+ return true;
44
+ }
45
+
46
+ ntlm_client *ntlm_client_init(ntlm_client_flags flags)
47
+ {
48
+ ntlm_client *ntlm = NULL;
49
+
50
+ if ((ntlm = malloc(sizeof(ntlm_client))) == NULL)
51
+ return NULL;
52
+
53
+ memset(ntlm, 0, sizeof(ntlm_client));
54
+
55
+ ntlm->flags = flags;
56
+
57
+ if ((ntlm->hmac_ctx = ntlm_hmac_ctx_init()) == NULL ||
58
+ (ntlm->unicode_ctx = ntlm_unicode_ctx_init(ntlm)) == NULL) {
59
+ ntlm_hmac_ctx_free(ntlm->hmac_ctx);
60
+ ntlm_unicode_ctx_free(ntlm->unicode_ctx);
61
+ free(ntlm);
62
+ return NULL;
63
+ }
64
+
65
+ return ntlm;
66
+ }
67
+
68
+ void ntlm_client_set_errmsg(ntlm_client *ntlm, const char *errmsg)
69
+ {
70
+ ntlm->state = NTLM_STATE_ERROR;
71
+ ntlm->errmsg = errmsg;
72
+ }
73
+
74
+ const char *ntlm_client_errmsg(ntlm_client *ntlm)
75
+ {
76
+ assert(ntlm);
77
+ return ntlm->errmsg ? ntlm->errmsg : "no error";
78
+ }
79
+
80
+ int ntlm_client_set_version(
81
+ ntlm_client *ntlm,
82
+ uint8_t major,
83
+ uint8_t minor,
84
+ uint16_t build)
85
+ {
86
+ assert(ntlm);
87
+
88
+ ntlm->host_version.major = major;
89
+ ntlm->host_version.minor = minor;
90
+ ntlm->host_version.build = build;
91
+ ntlm->host_version.reserved = 0x0f000000;
92
+
93
+ ntlm->flags |= NTLM_ENABLE_HOSTVERSION;
94
+
95
+ return 0;
96
+ }
97
+
98
+ int ntlm_client_set_hostname(
99
+ ntlm_client *ntlm,
100
+ const char *hostname,
101
+ const char *domain)
102
+ {
103
+ assert(ntlm);
104
+
105
+ free(ntlm->hostname);
106
+ free(ntlm->hostdomain);
107
+ free(ntlm->hostname_utf16);
108
+
109
+ ntlm->hostname = NULL;
110
+ ntlm->hostdomain = NULL;
111
+ ntlm->hostname_utf16 = NULL;
112
+
113
+ if (hostname && (ntlm->hostname = strdup(hostname)) == NULL) {
114
+ ntlm_client_set_errmsg(ntlm, "out of memory");
115
+ return -1;
116
+ }
117
+
118
+ if (domain && (ntlm->hostdomain = strdup(domain)) == NULL) {
119
+ ntlm_client_set_errmsg(ntlm, "out of memory");
120
+ return -1;
121
+ }
122
+
123
+ if (hostname && supports_unicode(ntlm) && !ntlm_unicode_utf8_to_16(
124
+ &ntlm->hostname_utf16,
125
+ &ntlm->hostname_utf16_len,
126
+ ntlm->unicode_ctx,
127
+ hostname,
128
+ strlen(hostname)))
129
+ return -1;
130
+
131
+ return 0;
132
+ }
133
+
134
+ static void free_credentials(ntlm_client *ntlm)
135
+ {
136
+ if (ntlm->password)
137
+ memzero(ntlm->password, strlen(ntlm->password));
138
+
139
+ if (ntlm->password_utf16)
140
+ memzero(ntlm->password_utf16, ntlm->password_utf16_len);
141
+
142
+ free(ntlm->username);
143
+ free(ntlm->username_upper);
144
+ free(ntlm->userdomain);
145
+ free(ntlm->password);
146
+
147
+ free(ntlm->username_utf16);
148
+ free(ntlm->username_upper_utf16);
149
+ free(ntlm->userdomain_utf16);
150
+ free(ntlm->password_utf16);
151
+
152
+ ntlm->username = NULL;
153
+ ntlm->username_upper = NULL;
154
+ ntlm->userdomain = NULL;
155
+ ntlm->password = NULL;
156
+
157
+ ntlm->username_utf16 = NULL;
158
+ ntlm->username_upper_utf16 = NULL;
159
+ ntlm->userdomain_utf16 = NULL;
160
+ ntlm->password_utf16 = NULL;
161
+ }
162
+
163
+ int ntlm_client_set_credentials(
164
+ ntlm_client *ntlm,
165
+ const char *username,
166
+ const char *domain,
167
+ const char *password)
168
+ {
169
+ assert(ntlm);
170
+
171
+ free_credentials(ntlm);
172
+
173
+ if ((username && (ntlm->username = strdup(username)) == NULL) ||
174
+ (domain && (ntlm->userdomain = strdup(domain)) == NULL) ||
175
+ (password && (ntlm->password = strdup(password)) == NULL)) {
176
+ ntlm_client_set_errmsg(ntlm, "out of memory");
177
+ return -1;
178
+ }
179
+
180
+ if (username && supports_unicode(ntlm)) {
181
+ if ((ntlm->username_upper = strdup(username)) == NULL) {
182
+ ntlm_client_set_errmsg(ntlm, "out of memory");
183
+ return -1;
184
+ }
185
+ utf8upr(ntlm->username_upper);
186
+
187
+ if (!ntlm_unicode_utf8_to_16(
188
+ &ntlm->username_utf16,
189
+ &ntlm->username_utf16_len,
190
+ ntlm->unicode_ctx,
191
+ ntlm->username,
192
+ strlen(ntlm->username)))
193
+ return -1;
194
+
195
+ if (!ntlm_unicode_utf8_to_16(
196
+ &ntlm->username_upper_utf16,
197
+ &ntlm->username_upper_utf16_len,
198
+ ntlm->unicode_ctx,
199
+ ntlm->username_upper,
200
+ strlen(ntlm->username_upper)))
201
+ return -1;
202
+ }
203
+
204
+ if (domain && supports_unicode(ntlm) && !ntlm_unicode_utf8_to_16(
205
+ &ntlm->userdomain_utf16,
206
+ &ntlm->userdomain_utf16_len,
207
+ ntlm->unicode_ctx,
208
+ ntlm->userdomain,
209
+ strlen(ntlm->userdomain)))
210
+ return -1;
211
+
212
+ return 0;
213
+ }
214
+
215
+ int ntlm_client_set_target(ntlm_client *ntlm, const char *target)
216
+ {
217
+ assert(ntlm);
218
+
219
+ free(ntlm->target);
220
+ free(ntlm->target_utf16);
221
+
222
+ ntlm->target = NULL;
223
+ ntlm->target_utf16 = NULL;
224
+
225
+ if (target) {
226
+ if ((ntlm->target = strdup(target)) == NULL) {
227
+ ntlm_client_set_errmsg(ntlm, "out of memory");
228
+ return -1;
229
+ }
230
+
231
+ if (supports_unicode(ntlm) && !ntlm_unicode_utf8_to_16(
232
+ &ntlm->target_utf16,
233
+ &ntlm->target_utf16_len,
234
+ ntlm->unicode_ctx,
235
+ ntlm->target,
236
+ strlen(ntlm->target)))
237
+ return -1;
238
+ }
239
+
240
+ return 0;
241
+ }
242
+
243
+ int ntlm_client_set_nonce(ntlm_client *ntlm, uint64_t nonce)
244
+ {
245
+ assert(ntlm);
246
+ ntlm->nonce = nonce;
247
+ return 0;
248
+ }
249
+
250
+ int ntlm_client_set_timestamp(ntlm_client *ntlm, uint64_t timestamp)
251
+ {
252
+ assert(ntlm);
253
+ ntlm->timestamp = timestamp;
254
+ return 0;
255
+ }
256
+
257
+ static inline bool write_buf(
258
+ ntlm_client *ntlm,
259
+ ntlm_buf *out,
260
+ const unsigned char *buf,
261
+ size_t len)
262
+ {
263
+ if (out->len - out->pos < len) {
264
+ ntlm_client_set_errmsg(ntlm, "out of buffer space");
265
+ return false;
266
+ }
267
+
268
+ memcpy(&out->buf[out->pos], buf, len);
269
+ out->pos += len;
270
+ return true;
271
+ }
272
+
273
+ static inline bool write_byte(
274
+ ntlm_client *ntlm,
275
+ ntlm_buf *out,
276
+ uint8_t value)
277
+ {
278
+ if (out->len - out->pos < 1) {
279
+ ntlm_client_set_errmsg(ntlm, "out of buffer space");
280
+ return false;
281
+ }
282
+
283
+ out->buf[out->pos++] = value;
284
+ return true;
285
+ }
286
+
287
+ static inline bool write_int16(
288
+ ntlm_client *ntlm,
289
+ ntlm_buf *out,
290
+ uint16_t value)
291
+ {
292
+ if (out->len - out->pos < 2) {
293
+ ntlm_client_set_errmsg(ntlm, "out of buffer space");
294
+ return false;
295
+ }
296
+
297
+ out->buf[out->pos++] = (value & 0x000000ff);
298
+ out->buf[out->pos++] = (value & 0x0000ff00) >> 8;
299
+ return true;
300
+ }
301
+
302
+ static inline bool write_int32(
303
+ ntlm_client *ntlm,
304
+ ntlm_buf *out,
305
+ uint32_t value)
306
+ {
307
+ if (out->len - out->pos < 2) {
308
+ ntlm_client_set_errmsg(ntlm, "out of buffer space");
309
+ return false;
310
+ }
311
+
312
+ out->buf[out->pos++] = (value & 0x000000ff);
313
+ out->buf[out->pos++] = (value & 0x0000ff00) >> 8;
314
+ out->buf[out->pos++] = (value & 0x00ff0000) >> 16;
315
+ out->buf[out->pos++] = (value & 0xff000000) >> 24;
316
+ return true;
317
+ }
318
+
319
+ static inline bool write_version(
320
+ ntlm_client *ntlm,
321
+ ntlm_buf *out,
322
+ ntlm_version *version)
323
+ {
324
+ return write_byte(ntlm, out, version->major) &&
325
+ write_byte(ntlm, out, version->minor) &&
326
+ write_int16(ntlm, out, version->build) &&
327
+ write_int32(ntlm, out, version->reserved);
328
+ }
329
+
330
+ static inline bool write_bufinfo(
331
+ ntlm_client *ntlm,
332
+ ntlm_buf *out,
333
+ size_t len,
334
+ size_t offset)
335
+ {
336
+ if (len > UINT16_MAX) {
337
+ ntlm_client_set_errmsg(ntlm, "invalid string, too long");
338
+ return false;
339
+ }
340
+
341
+ if (offset > UINT32_MAX) {
342
+ ntlm_client_set_errmsg(ntlm, "invalid string, invalid offset");
343
+ return false;
344
+ }
345
+
346
+ return write_int16(ntlm, out, (uint16_t)len) &&
347
+ write_int16(ntlm, out, (uint16_t)len) &&
348
+ write_int32(ntlm, out, (uint32_t)offset);
349
+ }
350
+
351
+ static inline bool read_buf(
352
+ unsigned char *out,
353
+ ntlm_client *ntlm,
354
+ ntlm_buf *message,
355
+ size_t len)
356
+ {
357
+ if (message->len - message->pos < len) {
358
+ ntlm_client_set_errmsg(ntlm, "truncated message");
359
+ return false;
360
+ }
361
+
362
+ memcpy(out, &message->buf[message->pos], len);
363
+ message->pos += len;
364
+
365
+ return true;
366
+ }
367
+
368
+ static inline bool read_byte(
369
+ uint8_t *out,
370
+ ntlm_client *ntlm,
371
+ ntlm_buf *message)
372
+ {
373
+ if (message->len - message->pos < 1) {
374
+ ntlm_client_set_errmsg(ntlm, "truncated message");
375
+ return false;
376
+ }
377
+
378
+ *out = message->buf[message->pos++];
379
+ return true;
380
+ }
381
+
382
+ static inline bool read_int16(
383
+ uint16_t *out,
384
+ ntlm_client *ntlm,
385
+ ntlm_buf *message)
386
+ {
387
+ if (message->len - message->pos < 2) {
388
+ ntlm_client_set_errmsg(ntlm, "truncated message");
389
+ return false;
390
+ }
391
+
392
+ *out =
393
+ ((message->buf[message->pos] & 0xff)) |
394
+ ((message->buf[message->pos+1] & 0xff) << 8);
395
+
396
+ message->pos += 2;
397
+ return true;
398
+ }
399
+
400
+ static inline bool read_int32(
401
+ uint32_t *out,
402
+ ntlm_client *ntlm,
403
+ ntlm_buf *message)
404
+ {
405
+ if (message->len - message->pos < 4) {
406
+ ntlm_client_set_errmsg(ntlm, "truncated message");
407
+ return false;
408
+ }
409
+
410
+ *out =
411
+ ((message->buf[message->pos] & 0xff)) |
412
+ ((message->buf[message->pos+1] & 0xff) << 8) |
413
+ ((message->buf[message->pos+2] & 0xff) << 16) |
414
+ ((message->buf[message->pos+3] & 0xff) << 24);
415
+
416
+ message->pos += 4;
417
+ return true;
418
+ }
419
+
420
+ static inline bool read_int64(
421
+ uint64_t *out,
422
+ ntlm_client *ntlm,
423
+ ntlm_buf *message)
424
+ {
425
+ if (message->len - message->pos < 8) {
426
+ ntlm_client_set_errmsg(ntlm, "truncated message");
427
+ return false;
428
+ }
429
+
430
+ *out =
431
+ ((uint64_t)(message->buf[message->pos] & 0xff)) |
432
+ ((uint64_t)(message->buf[message->pos+1] & 0xff) << 8) |
433
+ ((uint64_t)(message->buf[message->pos+2] & 0xff) << 16) |
434
+ ((uint64_t)(message->buf[message->pos+3] & 0xff) << 24) |
435
+ ((uint64_t)(message->buf[message->pos+4] & 0xff) << 32) |
436
+ ((uint64_t)(message->buf[message->pos+5] & 0xff) << 40) |
437
+ ((uint64_t)(message->buf[message->pos+6] & 0xff) << 48) |
438
+ ((uint64_t)(message->buf[message->pos+7] & 0xff) << 56);
439
+
440
+ message->pos += 8;
441
+ return true;
442
+ }
443
+
444
+ static inline bool read_version(
445
+ ntlm_version *out,
446
+ ntlm_client *ntlm,
447
+ ntlm_buf *message)
448
+ {
449
+ return read_byte(&out->major, ntlm, message) &&
450
+ read_byte(&out->minor, ntlm, message) &&
451
+ read_int16(&out->build, ntlm, message) &&
452
+ read_int32(&out->reserved, ntlm, message);
453
+ }
454
+
455
+ static inline bool read_bufinfo(
456
+ uint16_t *out_len,
457
+ uint32_t *out_offset,
458
+ ntlm_client *ntlm,
459
+ ntlm_buf *message)
460
+ {
461
+ uint16_t allocated;
462
+
463
+ return read_int16(out_len, ntlm, message) &&
464
+ read_int16(&allocated, ntlm, message) &&
465
+ read_int32(out_offset, ntlm, message);
466
+ }
467
+
468
+ static inline bool read_string_unicode(
469
+ char **out,
470
+ ntlm_client *ntlm,
471
+ ntlm_buf *message,
472
+ uint8_t string_len)
473
+ {
474
+ size_t out_len;
475
+ int ret = ntlm_unicode_utf16_to_8(out,
476
+ &out_len,
477
+ ntlm->unicode_ctx,
478
+ (char *)&message->buf[message->pos],
479
+ string_len);
480
+
481
+ message->pos += string_len;
482
+
483
+ return ret;
484
+ }
485
+
486
+ static inline bool read_string_ascii(
487
+ char **out,
488
+ ntlm_client *ntlm,
489
+ ntlm_buf *message,
490
+ uint8_t string_len)
491
+ {
492
+ char *str;
493
+
494
+ if ((str = malloc(string_len + 1)) == NULL) {
495
+ ntlm_client_set_errmsg(ntlm, "out of memory");
496
+ return false;
497
+ }
498
+
499
+ memcpy(str, &message->buf[message->pos], string_len);
500
+ str[string_len] = '\0';
501
+
502
+ message->pos += string_len;
503
+
504
+ *out = str;
505
+ return true;
506
+ }
507
+
508
+ static inline bool read_string(
509
+ char **out,
510
+ ntlm_client *ntlm,
511
+ ntlm_buf *message,
512
+ uint8_t string_len,
513
+ bool unicode)
514
+ {
515
+ if (unicode)
516
+ return read_string_unicode(out, ntlm, message, string_len);
517
+ else
518
+ return read_string_ascii(out, ntlm, message, string_len);
519
+ }
520
+
521
+ static inline bool read_target_info(
522
+ char **server_out,
523
+ char **domain_out,
524
+ char **server_dns_out,
525
+ char **domain_dns_out,
526
+ ntlm_client *ntlm,
527
+ ntlm_buf *message,
528
+ bool unicode)
529
+ {
530
+ uint16_t block_type, block_len;
531
+ bool done = false;
532
+
533
+ *server_out = NULL;
534
+ *domain_out = NULL;
535
+ *server_dns_out = NULL;
536
+ *domain_dns_out = NULL;
537
+
538
+ while (!done && (message->len - message->pos) >= 4) {
539
+ if (!read_int16(&block_type, ntlm, message) ||
540
+ !read_int16(&block_len, ntlm, message)) {
541
+ ntlm_client_set_errmsg(ntlm, "truncated target info block");
542
+ return false;
543
+ }
544
+
545
+ if (!block_type && block_len) {
546
+ ntlm_client_set_errmsg(ntlm, "invalid target info block");
547
+ return -1;
548
+ }
549
+
550
+ switch (block_type) {
551
+ case NTLM_TARGET_INFO_DOMAIN:
552
+ if (!read_string(domain_out, ntlm, message, block_len, unicode))
553
+ return -1;
554
+ break;
555
+ case NTLM_TARGET_INFO_SERVER:
556
+ if (!read_string(server_out, ntlm, message, block_len, unicode))
557
+ return -1;
558
+ break;
559
+ case NTLM_TARGET_INFO_DOMAIN_DNS:
560
+ if (!read_string(domain_dns_out, ntlm, message, block_len, unicode))
561
+ return -1;
562
+ break;
563
+ case NTLM_TARGET_INFO_SERVER_DNS:
564
+ if (!read_string(server_dns_out, ntlm, message, block_len, unicode))
565
+ return -1;
566
+ break;
567
+ case NTLM_TARGET_INFO_END:
568
+ done = true;
569
+ break;
570
+ default:
571
+ ntlm_client_set_errmsg(ntlm, "unknown target info block type");
572
+ return -1;
573
+ }
574
+ }
575
+
576
+ if (message->len != message->pos) {
577
+ ntlm_client_set_errmsg(ntlm,
578
+ "invalid extra data in target info section");
579
+ return false;
580
+ }
581
+
582
+ return true;
583
+ }
584
+
585
+ int ntlm_client_negotiate(
586
+ const unsigned char **out,
587
+ size_t *out_len,
588
+ ntlm_client *ntlm)
589
+ {
590
+ size_t hostname_len, domain_len;
591
+ size_t domain_offset = 0;
592
+ size_t hostname_offset = 0;
593
+ uint32_t flags = 0;
594
+
595
+ assert(out && out_len && ntlm);
596
+
597
+ *out = NULL;
598
+ *out_len = 0;
599
+
600
+ if (ntlm->state != NTLM_STATE_NEGOTIATE) {
601
+ ntlm_client_set_errmsg(ntlm, "ntlm handle in invalid state");
602
+ return -1;
603
+ }
604
+
605
+ flags |= NTLM_NEGOTIATE_OEM;
606
+
607
+ if (supports_unicode(ntlm))
608
+ flags |= NTLM_NEGOTIATE_UNICODE;
609
+
610
+ if (!(ntlm->flags & NTLM_CLIENT_DISABLE_NTLM2) ||
611
+ (ntlm->flags & NTLM_CLIENT_ENABLE_NTLM))
612
+ flags |= NTLM_NEGOTIATE_NTLM;
613
+
614
+ if (!(ntlm->flags & NTLM_CLIENT_DISABLE_REQUEST_TARGET))
615
+ flags |= NTLM_NEGOTIATE_REQUEST_TARGET;
616
+
617
+ hostname_len = ntlm->hostname ? strlen(ntlm->hostname) : 0;
618
+ domain_len = ntlm->hostdomain ? strlen(ntlm->hostdomain) : 0;
619
+
620
+ /* Minimum header size */
621
+ ntlm->negotiate.len = 16;
622
+
623
+ /* Include space for security buffer descriptors */
624
+ if (domain_len)
625
+ increment_size(&ntlm->negotiate.len, 8);
626
+
627
+ if (hostname_len)
628
+ increment_size(&ntlm->negotiate.len, 8);
629
+
630
+ if (ntlm->flags & NTLM_ENABLE_HOSTVERSION)
631
+ increment_size(&ntlm->negotiate.len, 8);
632
+
633
+ /* Location of security buffers */
634
+ if (hostname_len) {
635
+ flags |= NTLM_NEGOTIATE_WORKSTATION_SUPPLIED;
636
+ hostname_offset = ntlm->negotiate.len;
637
+ increment_size(&ntlm->negotiate.len, hostname_len);
638
+ }
639
+
640
+ if (domain_len) {
641
+ flags |= NTLM_NEGOTIATE_DOMAIN_SUPPLIED;
642
+ domain_offset = ntlm->negotiate.len;
643
+ increment_size(&ntlm->negotiate.len, domain_len);
644
+ }
645
+
646
+ if (ntlm->negotiate.len == (size_t)-1) {
647
+ ntlm_client_set_errmsg(ntlm, "message too large");
648
+ return -1;
649
+ }
650
+
651
+ if ((ntlm->negotiate.buf = malloc(ntlm->negotiate.len)) == NULL) {
652
+ ntlm_client_set_errmsg(ntlm, "out of memory");
653
+ return -1;
654
+ }
655
+
656
+ memset(ntlm->negotiate.buf, 0, ntlm->negotiate.len);
657
+
658
+ if (!write_buf(ntlm, &ntlm->negotiate,
659
+ ntlm_client_signature, sizeof(ntlm_client_signature)) ||
660
+ !write_int32(ntlm, &ntlm->negotiate, 1) ||
661
+ !write_int32(ntlm, &ntlm->negotiate, flags))
662
+ return -1;
663
+
664
+ /* Domain information */
665
+ if (domain_len > 0 &&
666
+ !write_bufinfo(ntlm, &ntlm->negotiate, domain_len, domain_offset))
667
+ return -1;
668
+
669
+ /* Workstation information */
670
+ if (hostname_len > 0 &&
671
+ !write_bufinfo(ntlm, &ntlm->negotiate, hostname_len, hostname_offset))
672
+ return -1;
673
+
674
+ /* Version number */
675
+ if (!!(ntlm->flags & NTLM_ENABLE_HOSTVERSION) &&
676
+ !write_version(ntlm, &ntlm->negotiate, &ntlm->host_version))
677
+ return -1;
678
+
679
+ if (hostname_len > 0) {
680
+ assert(hostname_offset == ntlm->negotiate.pos);
681
+ if (!write_buf(ntlm, &ntlm->negotiate,
682
+ (const unsigned char *)ntlm->hostname, hostname_len))
683
+ return -1;
684
+ }
685
+
686
+ if (domain_len > 0) {
687
+ assert(domain_offset == ntlm->negotiate.pos);
688
+ if (!write_buf(ntlm, &ntlm->negotiate,
689
+ (const unsigned char *)ntlm->hostdomain, domain_len))
690
+ return -1;
691
+ }
692
+
693
+ assert(ntlm->negotiate.pos == ntlm->negotiate.len);
694
+
695
+ ntlm->state = NTLM_STATE_CHALLENGE;
696
+
697
+ *out = ntlm->negotiate.buf;
698
+ *out_len = ntlm->negotiate.len;
699
+
700
+ return 0;
701
+ }
702
+
703
+ int ntlm_client_set_challenge(
704
+ ntlm_client *ntlm,
705
+ const unsigned char *challenge_msg,
706
+ size_t challenge_msg_len)
707
+ {
708
+ unsigned char signature[8];
709
+ ntlm_buf challenge;
710
+ uint32_t type_indicator, header_end;
711
+ uint16_t name_len, info_len = 0;
712
+ uint32_t name_offset, info_offset = 0;
713
+ bool unicode, has_target_info = false;
714
+
715
+ assert(ntlm && (challenge_msg || !challenge_msg_len));
716
+
717
+ if (ntlm->state != NTLM_STATE_NEGOTIATE &&
718
+ ntlm->state != NTLM_STATE_CHALLENGE) {
719
+ ntlm_client_set_errmsg(ntlm, "ntlm handle in invalid state");
720
+ return -1;
721
+ }
722
+
723
+ challenge.buf = (unsigned char *)challenge_msg;
724
+ challenge.len = challenge_msg_len;
725
+ challenge.pos = 0;
726
+
727
+ if (!read_buf(signature, ntlm, &challenge, 8) ||
728
+ !read_int32(&type_indicator, ntlm, &challenge) ||
729
+ !read_bufinfo(&name_len, &name_offset, ntlm, &challenge) ||
730
+ !read_int32(&ntlm->challenge.flags, ntlm, &challenge) ||
731
+ !read_int64(&ntlm->challenge.nonce, ntlm, &challenge))
732
+ return -1;
733
+
734
+ if (memcmp(signature,
735
+ ntlm_client_signature, sizeof(ntlm_client_signature)) != 0) {
736
+ ntlm_client_set_errmsg(ntlm, "invalid message signature");
737
+ return -1;
738
+ }
739
+
740
+ if (type_indicator != 2) {
741
+ ntlm_client_set_errmsg(ntlm, "invalid message indicator");
742
+ return -1;
743
+ }
744
+
745
+ /*
746
+ * If there's additional space before the data section, that's the
747
+ * target information description section.
748
+ */
749
+ header_end = challenge.len;
750
+
751
+ if (name_offset && name_offset < header_end)
752
+ header_end = name_offset;
753
+
754
+ if ((header_end - challenge.pos) >= 16) {
755
+ has_target_info = true;
756
+ }
757
+
758
+ if (!has_target_info &&
759
+ (ntlm->challenge.flags & NTLM_NEGOTIATE_TARGET_INFO)) {
760
+ ntlm_client_set_errmsg(ntlm,
761
+ "truncated message; expected target info");
762
+ return -1;
763
+ }
764
+
765
+ /*
766
+ * If there's a target info section then advanced over the reserved
767
+ * space and read the target information.
768
+ */
769
+ if (has_target_info) {
770
+ uint64_t reserved;
771
+
772
+ if (!read_int64(&reserved, ntlm, &challenge)) {
773
+ ntlm_client_set_errmsg(ntlm,
774
+ "truncated message; expected reserved space");
775
+ return -1;
776
+ }
777
+
778
+ if (reserved != 0) {
779
+ ntlm_client_set_errmsg(ntlm,
780
+ "invalid message; expected reserved space to be empty");
781
+ return -1;
782
+ }
783
+
784
+ if (!read_bufinfo(&info_len, &info_offset, ntlm, &challenge)) {
785
+ ntlm_client_set_errmsg(ntlm,
786
+ "truncated message; expected target info");
787
+ return -1;
788
+ }
789
+ }
790
+
791
+ unicode = !!(ntlm->challenge.flags & NTLM_NEGOTIATE_UNICODE);
792
+
793
+ /*
794
+ * If there's still additional space before the data section,
795
+ * that's the server's version information.
796
+ */
797
+ if (info_offset && info_offset < header_end)
798
+ header_end = info_offset;
799
+
800
+ if (ntlm->challenge.flags & NTLM_NEGOTIATE_VERSION) {
801
+ if ((header_end - challenge.pos) != sizeof(ntlm_version) ||
802
+ !read_version(&ntlm->challenge.target_version,
803
+ ntlm, &challenge)) {
804
+ ntlm_client_set_errmsg(ntlm,
805
+ "truncated message; expected version");
806
+ return -1;
807
+ }
808
+ }
809
+
810
+ /* validate data section */
811
+ if ((name_offset && name_offset < challenge.pos) ||
812
+ challenge.len < name_len ||
813
+ (challenge.len - name_len) < name_offset) {
814
+ ntlm_client_set_errmsg(ntlm,
815
+ "invalid message; invalid target name buffer");
816
+ return -1;
817
+ }
818
+ if ((info_offset && info_offset < challenge.pos) ||
819
+ challenge.len < info_len ||
820
+ (challenge.len - info_len) < info_offset) {
821
+ ntlm_client_set_errmsg(ntlm,
822
+ "invalid message; invalid target info buffer");
823
+ return -1;
824
+ }
825
+
826
+ /* advance to the data section */
827
+ if (name_len && name_offset) {
828
+ challenge.pos = name_offset;
829
+
830
+ if (!read_string(&ntlm->challenge.target,
831
+ ntlm, &challenge, name_len, unicode)) {
832
+ ntlm_client_set_errmsg(ntlm,
833
+ "truncated message; truncated target name");
834
+ return -1;
835
+ }
836
+ }
837
+
838
+ if (info_len && info_offset) {
839
+ ntlm_buf info_buf;
840
+
841
+ challenge.pos = info_offset;
842
+
843
+ /* create a copy of the target info; we need the literal data */
844
+ if ((ntlm->challenge.target_info = malloc(info_len)) == NULL) {
845
+ ntlm_client_set_errmsg(ntlm, "out of memory");
846
+ return -1;
847
+ }
848
+
849
+ if (!read_buf(ntlm->challenge.target_info,
850
+ ntlm, &challenge, info_len)) {
851
+ ntlm_client_set_errmsg(ntlm,
852
+ "truncated message; truncated target info");
853
+ return -1;
854
+ }
855
+
856
+ info_buf.buf = ntlm->challenge.target_info;
857
+ info_buf.pos = 0;
858
+ info_buf.len = info_len;
859
+
860
+ /* then set up the target info and parse it */
861
+ if (!read_target_info(&ntlm->challenge.target_server,
862
+ &ntlm->challenge.target_domain,
863
+ &ntlm->challenge.target_server_dns,
864
+ &ntlm->challenge.target_domain_dns,
865
+ ntlm, &info_buf, unicode))
866
+ return -1;
867
+
868
+ ntlm->challenge.target_info_len = info_len;
869
+ }
870
+
871
+ ntlm->state = NTLM_STATE_RESPONSE;
872
+
873
+ return 0;
874
+ }
875
+
876
+ uint64_t ntlm_client_challenge_nonce(ntlm_client *ntlm)
877
+ {
878
+ return ntlm->challenge.nonce;
879
+ }
880
+
881
+ const char *ntlm_client_target(ntlm_client *ntlm)
882
+ {
883
+ return ntlm->challenge.target;
884
+ }
885
+
886
+ const char *ntlm_client_target_server(ntlm_client *ntlm)
887
+ {
888
+ return ntlm->challenge.target_server;
889
+ }
890
+
891
+ const char *ntlm_client_target_domain(ntlm_client *ntlm)
892
+ {
893
+ return ntlm->challenge.target_domain;
894
+ }
895
+
896
+ const char *ntlm_client_target_server_dns(ntlm_client *ntlm)
897
+ {
898
+ return ntlm->challenge.target_server_dns;
899
+ }
900
+
901
+ const char *ntlm_client_target_domain_dns(ntlm_client *ntlm)
902
+ {
903
+ return ntlm->challenge.target_domain_dns;
904
+ }
905
+
906
+ #define EVEN_PARITY(a) \
907
+ (!!((a) & 0x01ll) ^ !!((a) & 0x02ll) ^ \
908
+ !!((a) & 0x04ll) ^ !!((a) & 0x08ll) ^ \
909
+ !!((a) & 0x10ll) ^ !!((a) & 0x20ll) ^ \
910
+ !!((a) & 0x40ll) ^ !!((a) & 0x80ll))
911
+
912
+ static void generate_odd_parity(ntlm_des_block *block)
913
+ {
914
+ size_t i;
915
+
916
+ for (i = 0; i < sizeof(ntlm_des_block); i++)
917
+ (*block)[i] |= (1 ^ EVEN_PARITY((*block)[i]));
918
+ }
919
+
920
+ static void des_key_from_password(
921
+ ntlm_des_block *out,
922
+ const unsigned char *plaintext,
923
+ size_t plaintext_len)
924
+ {
925
+ size_t i;
926
+
927
+ plaintext_len = MIN(plaintext_len, 7);
928
+
929
+ memset(*out, 0, sizeof(ntlm_des_block));
930
+
931
+ for (i = 0; i < plaintext_len; i++) {
932
+ size_t j = (7 - i);
933
+ uint8_t mask = (0xff >> j);
934
+
935
+ (*out)[i] |= ((plaintext[i] & (0xff - mask)) >> i);
936
+ (*out)[i+1] |= ((plaintext[i] & mask) << j);
937
+ }
938
+
939
+ generate_odd_parity(out);
940
+ }
941
+
942
+ static inline bool generate_lm_hash(
943
+ ntlm_des_block out[2],
944
+ const char *password)
945
+ {
946
+ /* LM encrypts this known plaintext using the password as a key */
947
+ ntlm_des_block plaintext = NTLM_LM_PLAINTEXT;
948
+ ntlm_des_block keystr1, keystr2;
949
+ size_t keystr1_len, keystr2_len;
950
+ ntlm_des_block key1, key2;
951
+ size_t password_len, i;
952
+
953
+ /* Copy the first 14 characters of the password, uppercased */
954
+ memset(&keystr1, 0, sizeof(keystr1));
955
+ memset(&keystr2, 0, sizeof(keystr2));
956
+
957
+ password_len = password ? strlen(password) : 0;
958
+
959
+ /* Split the password into two 7 byte chunks */
960
+ keystr1_len = MIN(7, password_len);
961
+ keystr2_len = (password_len > 7) ? MIN(14, password_len) - 7 : 0;
962
+
963
+ for (i = 0; i < keystr1_len; i++)
964
+ keystr1[i] = (unsigned char)toupper(password[i]);
965
+ for (i = 0; i < keystr2_len; i++)
966
+ keystr2[i] = (unsigned char)toupper(password[i+7]);
967
+
968
+ /* DES encrypt the LM constant using the password as the key */
969
+ des_key_from_password(&key1, keystr1, keystr1_len);
970
+ des_key_from_password(&key2, keystr2, keystr2_len);
971
+
972
+ return ntlm_des_encrypt(&out[0], &plaintext, &key1) &&
973
+ ntlm_des_encrypt(&out[1], &plaintext, &key2);
974
+ }
975
+
976
+ static void des_keys_from_lm_hash(ntlm_des_block out[3], ntlm_des_block lm_hash[2])
977
+ {
978
+ ntlm_des_block split[3];
979
+
980
+ memcpy(&split[0][0], &lm_hash[0][0], 7);
981
+
982
+ memcpy(&split[1][0], &lm_hash[0][7], 1);
983
+ memcpy(&split[1][1], &lm_hash[1][0], 6);
984
+
985
+ memcpy(&split[2][0], &lm_hash[1][6], 2);
986
+
987
+ des_key_from_password(&out[0], split[0], 7);
988
+ des_key_from_password(&out[1], split[1], 7);
989
+ des_key_from_password(&out[2], split[2], 2);
990
+ }
991
+
992
+ static bool generate_lm_response(ntlm_client *ntlm)
993
+ {
994
+ ntlm_des_block lm_hash[2], key[3], lm_response[3];
995
+ ntlm_des_block *challenge = (ntlm_des_block *)&ntlm->challenge.nonce;
996
+
997
+ /* Generate the LM hash from the password */
998
+ if (!generate_lm_hash(lm_hash, ntlm->password))
999
+ return false;
1000
+
1001
+ /* Convert that LM hash to three DES keys */
1002
+ des_keys_from_lm_hash(key, lm_hash);
1003
+
1004
+ /* Finally, encrypt the challenge with each of these keys */
1005
+ if (!ntlm_des_encrypt(&lm_response[0], challenge, &key[0]) ||
1006
+ !ntlm_des_encrypt(&lm_response[1], challenge, &key[1]) ||
1007
+ !ntlm_des_encrypt(&lm_response[2], challenge, &key[2]))
1008
+ return false;
1009
+
1010
+ memcpy(&ntlm->lm_response[0], lm_response[0], 8);
1011
+ memcpy(&ntlm->lm_response[8], lm_response[1], 8);
1012
+ memcpy(&ntlm->lm_response[16], lm_response[2], 8);
1013
+
1014
+ ntlm->lm_response_len = sizeof(ntlm->lm_response);
1015
+
1016
+ return true;
1017
+ }
1018
+
1019
+ static bool generate_ntlm_hash(
1020
+ unsigned char out[NTLM_NTLM_HASH_LEN], ntlm_client *ntlm)
1021
+ {
1022
+ /* Generate the LM hash from the (Unicode) password */
1023
+ if (ntlm->password && !ntlm_unicode_utf8_to_16(
1024
+ &ntlm->password_utf16,
1025
+ &ntlm->password_utf16_len,
1026
+ ntlm->unicode_ctx,
1027
+ ntlm->password,
1028
+ strlen(ntlm->password)))
1029
+ return false;
1030
+
1031
+ return ntlm_md4_digest(out,
1032
+ (const unsigned char *)ntlm->password_utf16,
1033
+ ntlm->password_utf16_len);
1034
+ }
1035
+
1036
+ static bool generate_ntlm_response(ntlm_client *ntlm)
1037
+ {
1038
+ unsigned char ntlm_hash[NTLM_NTLM_HASH_LEN] = {0};
1039
+ ntlm_des_block key[3], ntlm_response[3];
1040
+ ntlm_des_block *challenge =
1041
+ (ntlm_des_block *)&ntlm->challenge.nonce;
1042
+
1043
+ if (!generate_ntlm_hash(ntlm_hash, ntlm))
1044
+ return false;
1045
+
1046
+ /* Convert that LM hash to three DES keys */
1047
+ des_key_from_password(&key[0], &ntlm_hash[0], 7);
1048
+ des_key_from_password(&key[1], &ntlm_hash[7], 7);
1049
+ des_key_from_password(&key[2], &ntlm_hash[14], 2);
1050
+
1051
+ /* Finally, encrypt the challenge with each of these keys */
1052
+ if (!ntlm_des_encrypt(&ntlm_response[0], challenge, &key[0]) ||
1053
+ !ntlm_des_encrypt(&ntlm_response[1], challenge, &key[1]) ||
1054
+ !ntlm_des_encrypt(&ntlm_response[2], challenge, &key[2]))
1055
+ return false;
1056
+
1057
+ memcpy(&ntlm->ntlm_response[0], ntlm_response[0], 8);
1058
+ memcpy(&ntlm->ntlm_response[8], ntlm_response[1], 8);
1059
+ memcpy(&ntlm->ntlm_response[16], ntlm_response[2], 8);
1060
+
1061
+ ntlm->ntlm_response_len = sizeof(ntlm->ntlm_response);
1062
+ return true;
1063
+ }
1064
+
1065
+ static bool generate_ntlm2_hash(
1066
+ unsigned char out[NTLM_NTLM2_HASH_LEN], ntlm_client *ntlm)
1067
+ {
1068
+ unsigned char ntlm_hash[NTLM_NTLM_HASH_LEN] = {0};
1069
+ const unsigned char *username = NULL, *target = NULL;
1070
+ size_t username_len = 0, target_len = 0, out_len = NTLM_NTLM2_HASH_LEN;
1071
+
1072
+ if (!generate_ntlm_hash(ntlm_hash, ntlm))
1073
+ return false;
1074
+
1075
+ if (ntlm->username_upper_utf16) {
1076
+ username = (const unsigned char *)ntlm->username_upper_utf16;
1077
+ username_len = ntlm->username_upper_utf16_len;
1078
+ }
1079
+
1080
+ if (ntlm->target_utf16) {
1081
+ target = (const unsigned char *)ntlm->target_utf16;
1082
+ target_len = ntlm->target_utf16_len;
1083
+ }
1084
+
1085
+ if (!ntlm_hmac_ctx_reset(ntlm->hmac_ctx) ||
1086
+ !ntlm_hmac_md5_init(ntlm->hmac_ctx, ntlm_hash, sizeof(ntlm_hash)) ||
1087
+ !ntlm_hmac_md5_update(ntlm->hmac_ctx, username, username_len) ||
1088
+ !ntlm_hmac_md5_update(ntlm->hmac_ctx, target, target_len) ||
1089
+ !ntlm_hmac_md5_final(out, &out_len, ntlm->hmac_ctx)) {
1090
+ ntlm_client_set_errmsg(ntlm, "failed to create HMAC-MD5");
1091
+ return false;
1092
+ }
1093
+
1094
+ assert(out_len == NTLM_NTLM2_HASH_LEN);
1095
+ return true;
1096
+ }
1097
+
1098
+ static bool generate_ntlm2_challengehash(
1099
+ unsigned char out[16],
1100
+ ntlm_client *ntlm,
1101
+ unsigned char ntlm2_hash[NTLM_NTLM2_HASH_LEN],
1102
+ const unsigned char *blob,
1103
+ size_t blob_len)
1104
+ {
1105
+ size_t out_len = 16;
1106
+
1107
+ if (!ntlm_hmac_ctx_reset(ntlm->hmac_ctx) ||
1108
+ !ntlm_hmac_md5_init(ntlm->hmac_ctx,
1109
+ ntlm2_hash, NTLM_NTLM2_HASH_LEN) ||
1110
+ !ntlm_hmac_md5_update(ntlm->hmac_ctx,
1111
+ (const unsigned char *)&ntlm->challenge.nonce, 8) ||
1112
+ !ntlm_hmac_md5_update(ntlm->hmac_ctx, blob, blob_len) ||
1113
+ !ntlm_hmac_md5_final(out, &out_len, ntlm->hmac_ctx)) {
1114
+ ntlm_client_set_errmsg(ntlm, "failed to create HMAC-MD5");
1115
+ return false;
1116
+ }
1117
+
1118
+ assert(out_len == 16);
1119
+ return true;
1120
+ }
1121
+
1122
+ static bool generate_lm2_response(ntlm_client *ntlm,
1123
+ unsigned char ntlm2_hash[NTLM_NTLM2_HASH_LEN])
1124
+ {
1125
+ unsigned char lm2_challengehash[16];
1126
+ size_t lm2_len = 16;
1127
+ uint64_t local_nonce;
1128
+
1129
+ local_nonce = htonll(ntlm->nonce);
1130
+
1131
+ if (!ntlm_hmac_ctx_reset(ntlm->hmac_ctx) ||
1132
+ !ntlm_hmac_md5_init(ntlm->hmac_ctx,
1133
+ ntlm2_hash, NTLM_NTLM2_HASH_LEN) ||
1134
+ !ntlm_hmac_md5_update(ntlm->hmac_ctx,
1135
+ (const unsigned char *)&ntlm->challenge.nonce, 8) ||
1136
+ !ntlm_hmac_md5_update(ntlm->hmac_ctx,
1137
+ (const unsigned char *)&local_nonce, 8) ||
1138
+ !ntlm_hmac_md5_final(lm2_challengehash, &lm2_len, ntlm->hmac_ctx)) {
1139
+ ntlm_client_set_errmsg(ntlm, "failed to create HMAC-MD5");
1140
+ return false;
1141
+ }
1142
+
1143
+ assert(lm2_len == 16);
1144
+
1145
+ memcpy(&ntlm->lm_response[0], lm2_challengehash, 16);
1146
+ memcpy(&ntlm->lm_response[16], &local_nonce, 8);
1147
+
1148
+ ntlm->lm_response_len = 24;
1149
+ return true;
1150
+ }
1151
+
1152
+ static bool generate_timestamp(ntlm_client *ntlm)
1153
+ {
1154
+ if (!ntlm->timestamp)
1155
+ ntlm->timestamp = (time(NULL) + 11644473600) * 10000000;
1156
+
1157
+ return true;
1158
+ }
1159
+
1160
+ static bool generate_nonce(ntlm_client *ntlm)
1161
+ {
1162
+ unsigned char buf[8];
1163
+
1164
+ if (ntlm->nonce)
1165
+ return true;
1166
+
1167
+ if (!ntlm_random_bytes(ntlm, buf, 8))
1168
+ return false;
1169
+
1170
+ memcpy(&ntlm->nonce, buf, sizeof(uint64_t));
1171
+ return true;
1172
+ }
1173
+
1174
+ static bool generate_ntlm2_response(ntlm_client *ntlm)
1175
+ {
1176
+ size_t blob_len, ntlm2_response_len;
1177
+ uint32_t signature;
1178
+ uint64_t timestamp, nonce;
1179
+ unsigned char ntlm2_hash[NTLM_NTLM2_HASH_LEN];
1180
+ unsigned char challengehash[16];
1181
+ unsigned char *blob;
1182
+
1183
+ if (!generate_timestamp(ntlm) ||
1184
+ !generate_nonce(ntlm) ||
1185
+ !generate_ntlm2_hash(ntlm2_hash, ntlm))
1186
+ return false;
1187
+
1188
+ blob_len = ntlm->challenge.target_info_len + 32;
1189
+ ntlm2_response_len = blob_len + 16;
1190
+
1191
+ if ((ntlm->ntlm2_response = malloc(ntlm2_response_len)) == NULL) {
1192
+ ntlm_client_set_errmsg(ntlm, "out of memory");
1193
+ return false;
1194
+ }
1195
+
1196
+ /* position the blob in the response; we'll use it then return it */
1197
+ blob = ntlm->ntlm2_response + 16;
1198
+
1199
+ /* the blob's integer values are in network byte order */
1200
+ signature = htonl(0x01010000);
1201
+ timestamp = htonll(ntlm->timestamp);
1202
+ nonce = htonll(ntlm->nonce);
1203
+
1204
+ /* construct the blob */
1205
+ memcpy(&blob[0], &signature, 4);
1206
+ memset(&blob[4], 0, 4);
1207
+ memcpy(&blob[8], &timestamp, 8);
1208
+ memcpy(&blob[16], &nonce, 8);
1209
+ memset(&blob[24], 0, 4);
1210
+ memcpy(&blob[28], ntlm->challenge.target_info, ntlm->challenge.target_info_len);
1211
+ memset(&blob[28 + ntlm->challenge.target_info_len], 0, 4);
1212
+
1213
+ if (!generate_ntlm2_challengehash(challengehash, ntlm, ntlm2_hash, blob, blob_len))
1214
+ return false;
1215
+
1216
+ memcpy(ntlm->ntlm2_response, challengehash, 16);
1217
+ ntlm->ntlm2_response_len = ntlm2_response_len;
1218
+
1219
+ if (!generate_lm2_response(ntlm, ntlm2_hash))
1220
+ return false;
1221
+
1222
+ return true;
1223
+ }
1224
+
1225
+ int ntlm_client_response(
1226
+ const unsigned char **out,
1227
+ size_t *out_len,
1228
+ ntlm_client *ntlm)
1229
+ {
1230
+ unsigned char *domain, *username, *hostname, *ntlm_rep, *session;
1231
+ size_t lm_rep_len, lm_rep_offset, ntlm_rep_len, ntlm_rep_offset,
1232
+ domain_len, domain_offset, username_len, username_offset,
1233
+ hostname_len, hostname_offset, session_len, session_offset;
1234
+ uint32_t flags = 0;
1235
+ bool unicode;
1236
+
1237
+ assert(out && out_len && ntlm);
1238
+
1239
+ *out = NULL;
1240
+ *out_len = 0;
1241
+
1242
+ if (ntlm->state != NTLM_STATE_RESPONSE) {
1243
+ ntlm_client_set_errmsg(ntlm, "ntlm handle in invalid state");
1244
+ return -1;
1245
+ }
1246
+
1247
+ /*
1248
+ * Minimum message size is 64 bytes:
1249
+ * 8 byte signature,
1250
+ * 4 byte message indicator,
1251
+ * 6x8 byte security buffers
1252
+ * 4 byte flags
1253
+ */
1254
+ ntlm->response.len = 64;
1255
+
1256
+ unicode = supports_unicode(ntlm) &&
1257
+ (ntlm->challenge.flags & NTLM_NEGOTIATE_UNICODE);
1258
+
1259
+ if (unicode)
1260
+ flags |= NTLM_NEGOTIATE_UNICODE;
1261
+ else
1262
+ flags |= NTLM_NEGOTIATE_OEM;
1263
+
1264
+ if (unicode) {
1265
+ domain = (unsigned char *)ntlm->userdomain_utf16;
1266
+ domain_len = ntlm->userdomain_utf16_len;
1267
+
1268
+ username = (unsigned char *)ntlm->username_utf16;
1269
+ username_len = ntlm->username_utf16_len;
1270
+
1271
+ hostname = (unsigned char *)ntlm->hostname_utf16;
1272
+ hostname_len = ntlm->hostname_utf16_len;
1273
+ } else {
1274
+ domain = (unsigned char *)ntlm->userdomain;
1275
+ domain_len = ntlm->userdomain ? strlen(ntlm->userdomain) : 0;
1276
+
1277
+ username = (unsigned char *)ntlm->username;
1278
+ username_len = ntlm->username ? strlen(ntlm->username) : 0;
1279
+
1280
+ hostname = (unsigned char *)ntlm->hostname;
1281
+ hostname_len = ntlm->hostname ? strlen(ntlm->hostname) : 0;
1282
+ }
1283
+
1284
+ /* Negotiate our requested authentication type with the server's */
1285
+ if (!(ntlm->flags & NTLM_CLIENT_DISABLE_NTLM2) &&
1286
+ (ntlm->challenge.flags & NTLM_NEGOTIATE_NTLM)) {
1287
+ flags |= NTLM_NEGOTIATE_NTLM;
1288
+
1289
+ if (!generate_ntlm2_response(ntlm))
1290
+ return -1;
1291
+ } else if ((ntlm->flags & NTLM_CLIENT_ENABLE_NTLM) &&
1292
+ (ntlm->challenge.flags & NTLM_NEGOTIATE_NTLM)) {
1293
+ flags |= NTLM_NEGOTIATE_NTLM;
1294
+
1295
+ if (!generate_ntlm_response(ntlm) ||
1296
+ !generate_lm_response(ntlm))
1297
+ return -1;
1298
+ } else if (ntlm->flags & NTLM_CLIENT_ENABLE_LM) {
1299
+ if (!generate_lm_response(ntlm))
1300
+ return -1;
1301
+ } else {
1302
+ ntlm_client_set_errmsg(ntlm,
1303
+ "no encryption options could be negotiated");
1304
+ return -1;
1305
+ }
1306
+
1307
+ domain_offset = ntlm->response.len;
1308
+ increment_size(&ntlm->response.len, domain_len);
1309
+
1310
+ username_offset = ntlm->response.len;
1311
+ increment_size(&ntlm->response.len, username_len);
1312
+
1313
+ hostname_offset = ntlm->response.len;
1314
+ increment_size(&ntlm->response.len, hostname_len);
1315
+
1316
+ lm_rep_len = ntlm->lm_response_len;
1317
+ lm_rep_offset = ntlm->response.len;
1318
+ increment_size(&ntlm->response.len, lm_rep_len);
1319
+
1320
+ ntlm_rep = ntlm->ntlm2_response_len ?
1321
+ ntlm->ntlm2_response : ntlm->ntlm_response;
1322
+ ntlm_rep_len = ntlm->ntlm2_response_len ?
1323
+ ntlm->ntlm2_response_len : ntlm->ntlm_response_len;
1324
+ ntlm_rep_offset = ntlm->response.len;
1325
+ increment_size(&ntlm->response.len, ntlm_rep_len);
1326
+
1327
+ session = NULL;
1328
+ session_len = 0;
1329
+ session_offset = ntlm->response.len;
1330
+ increment_size(&ntlm->response.len, session_len);
1331
+
1332
+ if (ntlm->response.len == (size_t)-1) {
1333
+ ntlm_client_set_errmsg(ntlm, "message too large");
1334
+ return -1;
1335
+ }
1336
+
1337
+ if ((ntlm->response.buf = malloc(ntlm->response.len)) == NULL) {
1338
+ ntlm_client_set_errmsg(ntlm, "out of memory");
1339
+ return -1;
1340
+ }
1341
+
1342
+ memset(ntlm->response.buf, 0, ntlm->response.len);
1343
+
1344
+ if (!write_buf(ntlm, &ntlm->response,
1345
+ ntlm_client_signature, sizeof(ntlm_client_signature)) ||
1346
+ !write_int32(ntlm, &ntlm->response, 3) ||
1347
+ !write_bufinfo(ntlm, &ntlm->response, lm_rep_len, lm_rep_offset) ||
1348
+ !write_bufinfo(ntlm, &ntlm->response, ntlm_rep_len, ntlm_rep_offset) ||
1349
+ !write_bufinfo(ntlm, &ntlm->response, domain_len, domain_offset) ||
1350
+ !write_bufinfo(ntlm, &ntlm->response, username_len, username_offset) ||
1351
+ !write_bufinfo(ntlm, &ntlm->response, hostname_len, hostname_offset) ||
1352
+ !write_bufinfo(ntlm, &ntlm->response, session_len, session_offset) ||
1353
+ !write_int32(ntlm, &ntlm->response, flags) ||
1354
+ !write_buf(ntlm, &ntlm->response, domain, domain_len) ||
1355
+ !write_buf(ntlm, &ntlm->response, username, username_len) ||
1356
+ !write_buf(ntlm, &ntlm->response, hostname, hostname_len) ||
1357
+ !write_buf(ntlm, &ntlm->response, ntlm->lm_response, lm_rep_len) ||
1358
+ !write_buf(ntlm, &ntlm->response, ntlm_rep, ntlm_rep_len) ||
1359
+ !write_buf(ntlm, &ntlm->response, session, session_len))
1360
+ return -1;
1361
+
1362
+ assert(ntlm->response.pos == ntlm->response.len);
1363
+
1364
+ ntlm->state = NTLM_STATE_COMPLETE;
1365
+
1366
+ *out = ntlm->response.buf;
1367
+ *out_len = ntlm->response.len;
1368
+
1369
+ return 0;
1370
+ }
1371
+
1372
+ void ntlm_client_reset(ntlm_client *ntlm)
1373
+ {
1374
+ ntlm_client_flags flags;
1375
+ ntlm_hmac_ctx *hmac_ctx;
1376
+ ntlm_unicode_ctx *unicode_ctx;
1377
+
1378
+ assert(ntlm);
1379
+
1380
+ free(ntlm->negotiate.buf);
1381
+ free(ntlm->challenge.target_info);
1382
+ free(ntlm->challenge.target);
1383
+ free(ntlm->challenge.target_domain);
1384
+ free(ntlm->challenge.target_domain_dns);
1385
+ free(ntlm->challenge.target_server);
1386
+ free(ntlm->challenge.target_server_dns);
1387
+ free(ntlm->response.buf);
1388
+
1389
+ free(ntlm->hostname);
1390
+ free(ntlm->hostname_utf16);
1391
+ free(ntlm->hostdomain);
1392
+
1393
+ free(ntlm->target);
1394
+ free(ntlm->target_utf16);
1395
+
1396
+ free(ntlm->ntlm2_response);
1397
+
1398
+ free_credentials(ntlm);
1399
+
1400
+ flags = ntlm->flags;
1401
+ hmac_ctx = ntlm->hmac_ctx;
1402
+ unicode_ctx = ntlm->unicode_ctx;
1403
+
1404
+ memset(ntlm, 0, sizeof(struct ntlm_client));
1405
+
1406
+ ntlm->flags = flags;
1407
+ ntlm->hmac_ctx = hmac_ctx;
1408
+ ntlm->unicode_ctx = unicode_ctx;
1409
+ }
1410
+
1411
+ void ntlm_client_free(ntlm_client *ntlm)
1412
+ {
1413
+ if (!ntlm)
1414
+ return;
1415
+
1416
+ ntlm_client_reset(ntlm);
1417
+
1418
+ ntlm_hmac_ctx_free(ntlm->hmac_ctx);
1419
+ ntlm_unicode_ctx_free(ntlm->unicode_ctx);
1420
+
1421
+ free(ntlm);
1422
+ }