Skip to content

Commit fdd72d2

Browse files
committed
Cleanup code, clarfy comments for AD/LDAP + STARTTLS
1 parent fc2a020 commit fdd72d2

1 file changed

Lines changed: 32 additions & 21 deletions

File tree

testssl.sh

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11429,7 +11429,8 @@ starttls_ldap_dialog() {
1142911429
local -i ret=0
1143011430
local msg_lenstr=""
1143111431
local -i msg_len=0
11432-
local result="" buffsize=""
11432+
local buffer="" buffsize=""
11433+
local result_code="00"
1143311434
local starttls_init=",
1143411435
x30, x1d, x02, x01, # LDAP extendedReq
1143511436
x01, # messageID: 1
@@ -11438,47 +11439,57 @@ starttls_ldap_dialog() {
1143811439

1143911440
debugme echo "=== starting LDAP STARTTLS dialog ==="
1144011441
socksend "${starttls_init}" 0 && debugme echo "${debugpad}initiated STARTTLS" &&
11441-
result=$(sockread_fast 256)
11442-
[[ $DEBUG -ge 4 ]] && safe_echo "$debugpad $result\n"
11442+
buffer=$(sockread_fast 256)
11443+
[[ $DEBUG -ge 4 ]] && safe_echo "$debugpad $buffer\n"
1144311444

11444-
# For ~ OpenLDAP: response is typically 30 0c 02 01 01 78 07 0a 01 00 04 00 04 00
11445-
# ^^ buffsize ^^ ret value (0 -> success)
11445+
# We have two different scenarios: AD and OpenLDAP. And maybe more we don't more of yet.
11446+
# OpenLDAP-like reply is
11447+
# 30 0c 02 01 01 78 07 0a 01 00 04 00 04 00
11448+
# ^^ buffsize ^^ LDAP result code (0 -> success)
11449+
#
1144611450
# see https://git.openldap.org/openldap/openldap/-/blob/master/include/ldap.h
1144711451
# return values in https://www.rfc-editor.org/rfc/rfc2251#page-45 and e.g.
1144811452

11449-
# We have two different scenarios though. x0C is the buffsize reply from openldap-like servers
11450-
# whereas AD servers probably have x84 and return also the OID. The following is kind of
11451-
# hackish as ldap_ExtendedResponse_parse() in apps/s_client.c of openssl is kind of hard
11452-
# to understand. It was deducted from a number of hosts.
11453-
# Bottom line: We'll look at the 9th byte or at the 17th when retrieving the result code
11453+
# AD-like is
11454+
# error: 30 84 00 00 00 7d 02 01 01 78 84 00 00 00 74 0a 01 34 04 00 04 55 30 30 [.. LdapErr, string, OID.. ]
11455+
# success 30 84 00 00 00 28 02 01 01 78 84 00 00 00 1F 0A 01 00 04 00 04 00 8A 16 [ .. OID .. ]
11456+
# ^^ buffsize ^^ LDAP result code (0 -> success)
11457+
11458+
# We assume that AD servers probably all have x84. It was deducted from a number of hosts.
11459+
# It maybe needs to be amended for other implementations.
11460+
# Basically using ldap_ExtendedResponse_parse() in apps/s_client.c of openssl would be
11461+
# more robust but it is kind of hard to understand.
11462+
#
11463+
# Bottom line: We'll look at the 9th or the 17th byte when retrieving the result code
11464+
# depending what the buffsize is.
1145411465

11455-
buffsize="${result:2:2}"
11466+
buffsize="${buffer:2:2}"
1145611467

1145711468
case $buffsize in
11458-
0C) result_code="${result:18:2}" ;;
11459-
84) result_code="${result:34:2}" ;;
11469+
0C) result_code="${buffer:18:2}" ;;
11470+
84) result_code="${buffer:34:2}" ;;
1146011471
esac
11461-
[[ $DEBUG -ge 2 ]] && safe_echo "$debugpad buffsize: $buffsize / LDAP result_code: $result_code \n"
11472+
[[ $DEBUG -ge 2 ]] && safe_echo "$debugpad buffsize: $buffsize / LDAP result code: $result_code \n"
1146211473

1146311474
case $result_code in
1146411475
00) ret=0 ;;
1146511476
# success
1146611477
01) ret=1 ;;
11467-
# operationsError
11478+
# OpenLDAP: operationsError
1146811479
02) ret=2
11469-
# protocolError (text msg: "unsupported extended operation") e.g. when STARTTLS not supported
11480+
# OpenLDAP: protocolError (text msg: "unsupported extended operation") e.g. when STARTTLS not supported
1147011481
if [[ $DEBUG -ge 2 ]]; then
11471-
msg_lenstr=$(hex2dec ${result:26:02})
11482+
msg_lenstr=$(hex2dec ${buffer:26:02})
1147211483
msg_len=$((2 * msg_lenstr))
11473-
safe_echo "$debugpad $(hex2binary "${result:28:$msg_len}")"
11484+
safe_echo "$debugpad $(hex2binary "${buffer:28:$msg_len}")"
1147411485
fi ;;
11475-
34) [[ $DEBUG -ge 2 ]] && safe_echo " seems AD server with no STARTTLS\n"
11486+
34) # This (52 in dec) seems to be the error code for AD when there's no STARTTLS
11487+
[[ $DEBUG -ge 2 ]] && safe_echo " seems AD server with no STARTTLS\n"
1147611488
ret=52 ;;
11477-
*) [[ $DEBUG -ge 2 ]] && safe_echo "$debugpad $(hex2dec "${result:28:2}")"
11489+
*) [[ $DEBUG -ge 2 ]] && safe_echo "$debugpad $(hex2dec "${buffer:28:2}")"
1147811490
ret=127 ;;
1147911491
esac
1148011492

11481-
1148211493
debugme echo "=== finished LDAP STARTTLS dialog with ${ret} ==="
1148311494
return $ret
1148411495
}

0 commit comments

Comments
 (0)