-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathssleay32.pas
More file actions
188 lines (154 loc) · 4.64 KB
/
Copy pathssleay32.pas
File metadata and controls
188 lines (154 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
unit ssleay32;
{$mode delphi}{$H+}
interface
uses
windows,sysutils,winsock,
opensslutils,libeay32,
utils;
var
TLSv1_2_method:function():pointer;cdecl=nil;
SSLv23_method:function():pointer;cdecl=nil;
SSL_CTX_new:function(const method:pointer):pointer;cdecl=nil;
SSL_new:function(ssl: pointer):pointer;cdecl=nil;
SSL_set_fd:function(ssl:pointer;fd:integer):integer;cdecl=nil;
SSL_connect:function(ssl: pointer):integer;cdecl=nil;
SSL_get_peer_certificate:function(const ssl:pointer):px509;cdecl=nil;
SSL_get_peer_cert_chain:function(const ssl:pointer):pSTACK_OFX509;cdecl=nil;
SSL_shutdown:function(ssl: pointer):integer;cdecl=nil;
SSL_free:procedure(ssl: pointer);cdecl=nil;
SSL_library_init:function():integer;cdecl=nil;
procedure s_client(host:string);
implementation
function init_socket(host:string;port:dword;var sock_:tsocket):boolean;
var
wsadata:TWSADATA;
err:longint;
hostaddr:u_long;
sin:sockaddr_in;
HostEnt:PHostEnt;
begin
result:=false;
//
err := WSAStartup(MAKEWORD(2, 0), wsadata);
if(err <> 0) then raise exception.Create ('WSAStartup failed with error: '+inttostr(err));
//
hostaddr := inet_addr(pchar(host));
//not an ip? lets try to resolve hostname
if hostaddr = INADDR_NONE then
begin
HostEnt:=gethostbyname(pchar(host));
if HostEnt <> nil then hostaddr:=Integer(Pointer(HostEnt^.h_addr^)^);
end;
//
//
sock_ := socket(AF_INET, SOCK_STREAM, 0);
//
sin.sin_family := AF_INET;
sin.sin_port := htons(port);
sin.sin_addr.s_addr := hostaddr;
if connect(sock_, tsockaddr(sin), sizeof(sockaddr_in)) <> 0
then raise exception.Create ('failed to connect');
//
result:=true;
end;
procedure s_client(host:string);
var
sock:tsocket;
err:cardinal;
ssl:pointer=nil; //PSSL;
ctx:pointer=nil;
server_cert:PX509=nil;
str:pchar;
name:pX509_NAME=nil;
certs:pSTACK_OFX509 =nil;
b:byte;
begin
log('SSL_library_init');
SSL_library_init;
//OpenSSL_add_all_algorithms();
//SSL_load_error_strings();
log('SSL_CTX_new');
ctx:=SSL_CTX_new(TLSv1_2_method );
if ctx=nil then exit;
log('SSL_new');
ssl := SSL_new (ctx);
if ssl =nil then exit;
log('init_socket');
init_socket (host,443,sock);
log('SSL_set_fd');
SSL_set_fd (ssl, sock);
log('SSL_connect');
err := SSL_connect (ssl);
//if (err < 0) ...
//writeln ('SSL connection using ' + SSL_get_cipher (ssl));
log('SSL_get_peer_certificate');
//server_cert := SSL_get_peer_certificate (ssl);
certs:=SSL_get_peer_cert_chain (ssl);
writeln('sk_num:'+inttostr(sk_num(Certs)));
writeln('********************************');
for b:=0 to sk_num(Certs) -1 do
begin
server_cert:=sk_value(Certs, b);
//
log('X509_get_subject_name');
NAME:=X509_get_subject_name(server_cert);
writeln('subject_name:'+getdn(name));
//
//
log('X509_get_issuer_name');
NAME:=X509_get_issuer_name(server_cert);
writeln('issuer_name:'+getdn(name));
//
try
log('X509_get_notBefore');
writeln('notBefore:'+DateTimeToStr (getTime (X509_get_notBefore(server_cert))));
log('X509_get_notAfter');
writeln('notAfter:'+DateTimeToStr (getTime (X509_get_notAfter (server_cert))));
except
on e:exception do writeln(e.message);
end;
writeln('SerialNumber:'+getSerialNumber(server_cert));
writeln('********************************');
end; //for b:=0 to sk_num(Certs) -1 do
SSL_shutdown (ssl);
SSL_free (ssl);
closesocket (sock);
end;
function initAPI:boolean;
var
lib:hmodule=0;
buffer:array[0..10] of byte;
begin
log('initapi');
result:=false;
try
//lib:=0;
if lib>0 then begin {log('lib<>0');} result:=true; exit;end;
{$IFDEF win64}lib:=loadlibrary('ssleay32.dll');{$endif}
{$IFDEF win32}lib:=loadlibrary('ssleay32.dll');{$endif}
if lib<=0 then
begin
log('could not loadlibrary ssleay32.dll');
exit;
end;
//log('GetProcAddress');
TLSv1_2_method:=GetProcAddress(lib,'TLSv1_2_method');
SSLv23_method:=GetProcAddress(lib,'SSLv23_method');
SSL_CTX_new:=GetProcAddress(lib,'SSL_CTX_new');
SSL_new:=GetProcAddress(lib,'SSL_new');
SSL_set_fd:=GetProcAddress(lib,'SSL_set_fd');
SSL_connect:=GetProcAddress(lib,'SSL_connect');
SSL_get_peer_certificate:=GetProcAddress(lib,'SSL_get_peer_certificate');
SSL_get_peer_cert_chain:=GetProcAddress(lib,'SSL_get_peer_cert_chain');
SSL_shutdown:=GetProcAddress(lib,'SSL_shutdown');
SSL_free:=GetProcAddress(lib,'SSL_free');
SSL_library_init:=GetProcAddress(lib,'SSL_library_init');
result:=true;
except
//on e:exception do writeln('init error:'+e.message);
log('init error');
end;
end;
initialization
initapi;
end.