I'm trying to use SSPI authentication to connect to Sql Server. There is working (I hope) C example with FreeTds sspi.c that using InitializeSecurityContext.
The problem is it calling InitializeSecurityContext twice. At first (tds_sspi_get_auth) function called to make auth to put it into login packet. There Service principal name (SPN) created as (Project JEDI JwaSspi used)
FSPN := WideString(Format('MSSQLSvc/%s:%d', [FHostName, FPort]));
status := InitializeSecurityContext(@FCred, nil, PSecWChar(FSPN),
ISC_REQ_CONFIDENTIALITY or ISC_REQ_REPLAY_DETECT or ISC_REQ_CONNECTION,
0, SECURITY_NETWORK_DREP, nil, 0, @FCredCtx, @desc, attrs, @ts);
where FSPN: WideString;
Second call (tds_sspi_handle_next) to InitializeSecurityContext uses same FSPN and response from server
status := InitializeSecurityContext(@FCred, @FCredCtx, PSecWChar(FSPN),
ISC_REQ_CONFIDENTIALITY or ISC_REQ_REPLAY_DETECT or ISC_REQ_CONNECTION,
0, SECURITY_NETWORK_DREP, @in_desc, 0, @FCredCtx, @out_desc, attrs, @ts);
Now hard part: on C SPN created with asprintf
, after first call to InitializeSecurityContext it changed (was $4D $00 $53 $00 $53 $00 ...
, after $08 $04 $01 $00 $4E ...
) and I guess replaced by Digest or similar. By using like that I have Access Violation somewhere in oleaut32.dll.