12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457 |
- unit OpenSSL;
- {==============================================================================|
- | Project : Ararat Synapse | 003.004.001 |
- |==============================================================================|
- | Content: SSL support by OpenSSL |
- |==============================================================================|
- | Copyright (c)1999-2005, Lukas Gebauer |
- | All rights reserved. |
- | |
- | Redistribution and use in source and binary forms, with or without |
- | modification, are permitted provided that the following conditions are met: |
- | |
- | Redistributions of source code must retain the above copyright notice, this |
- | list of conditions and the following disclaimer. |
- | |
- | Redistributions in binary form must reproduce the above copyright notice, |
- | this list of conditions and the following disclaimer in the documentation |
- | and/or other materials provided with the distribution. |
- | |
- | Neither the name of Lukas Gebauer nor the names of its contributors may |
- | be used to endorse or promote products derived from this software without |
- | specific prior written permission. |
- | |
- | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
- | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
- | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
- | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
- | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
- | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
- | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
- | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
- | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
- | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
- | DAMAGE. |
- |==============================================================================|
- | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
- | Portions created by Lukas Gebauer are Copyright (c)2002-2005. |
- | All Rights Reserved. |
- |==============================================================================|
- | Contributor(s): |
- |==============================================================================|
- | FreePascal basic cleanup (original worked too): Ales Katona |
- | WARNING: due to reliance on some units, I have removed the ThreadLocks init |
- | if need be, it should be re-added, or handled by the |
- | OS threading init somehow |
- |==============================================================================|
- | History: see HISTORY.HTM from distribution package |
- | (Found at URL: http://www.ararat.cz/synapse/) |
- |==============================================================================}
- {
- Special thanks to Gregor Ibic <[email protected]>
- (Intelicom d.o.o., http://www.intelicom.si)
- for good inspiration about begin with SSL programming.
- }
- {$MODE DELPHI}{$H+}
- {:@abstract(OpenSSL support)
- This unit is Pascal interface to OpenSSL library (used by @link(ssl_openssl) unit).
- OpenSSL is loaded dynamicly on-demand. If this library is not found in system,
- requested OpenSSL function just return errorcode.
- }
- interface
- uses
- DynLibs, cTypes;
- var
- {$IFDEF WINDOWS}
- DLLSSLName: string = 'ssleay32.dll';
- DLLSSLName2: string = 'libssl32.dll';
- DLLUtilName: string = 'libeay32.dll';
- {$ELSE}
- DLLSSLName: string = 'libssl';
- DLLUtilName: string = 'libcrypto';
-
- { ADD NEW ONES WHEN THEY APPEAR!
- Always make .so/dylib first, then versions, in descending order!
- Add "." .before the version, first is always just "" }
- DLLVersions: array[1..10] of string = ('', '.0.9.9'{futureproof :D}, '.0.9.8',
- '.0.9.7', '.0.9.6', '.0.9.5', '.0.9.4',
- '.0.9.3', '.0.9.2', '.0.9.1');
- {$ENDIF}
- type
- SslPtr = Pointer;
- PSslPtr = ^SslPtr;
- PSSL_CTX = SslPtr;
- PSSL = SslPtr;
- PSSL_METHOD = SslPtr;
- PX509 = SslPtr;
- PX509_NAME = SslPtr;
- PEVP_MD = SslPtr;
- PBIO_METHOD = SslPtr;
- PBIO = SslPtr;
- EVP_PKEY = SslPtr;
- PRSA = SslPtr;
- PASN1_UTCTIME = SslPtr;
- PASN1_cInt = SslPtr;
- PPasswdCb = SslPtr;
- PFunction = procedure;
- DES_cblock = array[0..7] of Byte;
- PDES_cblock = ^DES_cblock;
- des_ks_struct = packed record
- ks: DES_cblock;
- weak_key: cInt;
- end;
- des_key_schedule = array[1..16] of des_ks_struct;
- const
- EVP_MAX_MD_SIZE = 16 + 20;
- SSL_ERROR_NONE = 0;
- SSL_ERROR_SSL = 1;
- SSL_ERROR_WANT_READ = 2;
- SSL_ERROR_WANT_WRITE = 3;
- SSL_ERROR_WANT_X509_LOOKUP = 4;
- SSL_ERROR_SYSCALL = 5; //look at error stack/return value/errno
- SSL_ERROR_ZERO_RETURN = 6;
- SSL_ERROR_WANT_CONNECT = 7;
- SSL_ERROR_WANT_ACCEPT = 8;
- SSL_OP_NO_SSLv2 = $01000000;
- SSL_OP_NO_SSLv3 = $02000000;
- SSL_OP_NO_TLSv1 = $04000000;
- SSL_OP_ALL = $000FFFFF;
- SSL_VERIFY_NONE = $00;
- SSL_VERIFY_PEER = $01;
- OPENSSL_DES_DECRYPT = 0;
- OPENSSL_DES_ENCRYPT = 1;
- X509_V_OK = 0;
- X509_V_ILLEGAL = 1;
- X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT = 2;
- X509_V_ERR_UNABLE_TO_GET_CRL = 3;
- X509_V_ERR_UNABLE_TO_DECRYPT_CERT_SIGNATURE = 4;
- X509_V_ERR_UNABLE_TO_DECRYPT_CRL_SIGNATURE = 5;
- X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY = 6;
- X509_V_ERR_CERT_SIGNATURE_FAILURE = 7;
- X509_V_ERR_CRL_SIGNATURE_FAILURE = 8;
- X509_V_ERR_CERT_NOT_YET_VALID = 9;
- X509_V_ERR_CERT_HAS_EXPIRED = 10;
- X509_V_ERR_CRL_NOT_YET_VALID = 11;
- X509_V_ERR_CRL_HAS_EXPIRED = 12;
- X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD = 13;
- X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD = 14;
- X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD = 15;
- X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD = 16;
- X509_V_ERR_OUT_OF_MEM = 17;
- X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT = 18;
- X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN = 19;
- X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY = 20;
- X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE = 21;
- X509_V_ERR_CERT_CHAIN_TOO_LONG = 22;
- X509_V_ERR_CERT_REVOKED = 23;
- X509_V_ERR_INVALID_CA = 24;
- X509_V_ERR_PATH_LENGTH_EXCEEDED = 25;
- X509_V_ERR_INVALID_PURPOSE = 26;
- X509_V_ERR_CERT_UNTRUSTED = 27;
- X509_V_ERR_CERT_REJECTED = 28;
- //These are 'informational' when looking for issuer cert
- X509_V_ERR_SUBJECT_ISSUER_MISMATCH = 29;
- X509_V_ERR_AKID_SKID_MISMATCH = 30;
- X509_V_ERR_AKID_ISSUER_SERIAL_MISMATCH = 31;
- X509_V_ERR_KEYUSAGE_NO_CERTSIGN = 32;
- X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER = 33;
- X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION = 34;
- //The application is not happy
- X509_V_ERR_APPLICATION_VERIFICATION = 50;
- SSL_FILETYPE_ASN1 = 2;
- SSL_FILETYPE_PEM = 1;
- EVP_PKEY_RSA = 6;
- var
- SSLLibHandle: TLibHandle = 0;
- SSLUtilHandle: TLibHandle = 0;
- SSLLibFile: string = '';
- SSLUtilFile: string = '';
- // libssl.dll
- function SslGetError(s: PSSL; ret_code: cInt):cInt;
- function SslLibraryInit:cInt;
- procedure SslLoadErrorStrings;
- // function SslCtxSetCipherList(arg0: PSSL_CTX; str: PChar):cInt;
- function SslCtxSetCipherList(arg0: PSSL_CTX; var str: String):cInt;
- function SslCtxNew(meth: PSSL_METHOD):PSSL_CTX;
- procedure SslCtxFree(arg0: PSSL_CTX);
- function SslSetFd(s: PSSL; fd: cInt):cInt;
- function SslMethodV2:PSSL_METHOD;
- function SslMethodV3:PSSL_METHOD;
- function SslMethodTLSV1:PSSL_METHOD;
- function SslMethodV23:PSSL_METHOD;
- function SslCtxUsePrivateKey(ctx: PSSL_CTX; pkey: SslPtr):cInt;
- function SslCtxUsePrivateKeyASN1(pk: cInt; ctx: PSSL_CTX; d: String; len: cLong):cInt;
- // function SslCtxUsePrivateKeyFile(ctx: PSSL_CTX; const _file: PChar; _type: cInt):cInt;
- function SslCtxUsePrivateKeyFile(ctx: PSSL_CTX; const _file: String; _type: cInt):cInt;
- function SslCtxUseCertificate(ctx: PSSL_CTX; x: SslPtr):cInt;
- function SslCtxUseCertificateASN1(ctx: PSSL_CTX; len: cLong; d: String):cInt;
- function SslCtxUseCertificateFile(ctx: PSSL_CTX; const _file: String; _type: cInt):cInt;
- // function SslCtxUseCertificateChainFile(ctx: PSSL_CTX; const _file: PChar):cInt;
- function SslCtxUseCertificateChainFile(ctx: PSSL_CTX; const _file: String):cInt;
- function SslCtxCheckPrivateKeyFile(ctx: PSSL_CTX):cInt;
- procedure SslCtxSetDefaultPasswdCb(ctx: PSSL_CTX; cb: PPasswdCb);
- procedure SslCtxSetDefaultPasswdCbUserdata(ctx: PSSL_CTX; u: SslPtr);
- // function SslCtxLoadVerifyLocations(ctx: PSSL_CTX; const CAfile: PChar; const CApath: PChar):cInt;
- function SslCtxLoadVerifyLocations(ctx: PSSL_CTX; const CAfile: String; const CApath: String):cInt;
- function SslNew(ctx: PSSL_CTX):PSSL;
- procedure SslFree(ssl: PSSL);
- function SslAccept(ssl: PSSL):cInt;
- function SslConnect(ssl: PSSL):cInt;
- function SslShutdown(ssl: PSSL):cInt;
- function SslRead(ssl: PSSL; buf: SslPtr; num: cInt):cInt;
- function SslPeek(ssl: PSSL; buf: SslPtr; num: cInt):cInt;
- function SslWrite(ssl: PSSL; buf: SslPtr; num: cInt):cInt;
- function SslPending(ssl: PSSL):cInt;
- function SslGetVersion(ssl: PSSL):String;
- function SslGetPeerCertificate(ssl: PSSL):PX509;
- procedure SslCtxSetVerify(ctx: PSSL_CTX; mode: cInt; arg2: PFunction);
- function SSLGetCurrentCipher(s: PSSL):SslPtr;
- function SSLCipherGetName(c: SslPtr): String;
- function SSLCipherGetBits(c: SslPtr; var alg_bits: cInt):cInt;
- function SSLGetVerifyResult(ssl: PSSL):cLong;
- // libeay.dll
- function X509New: PX509;
- procedure X509Free(x: PX509);
- function X509NameOneline(a: PX509_NAME; var buf: String; size: cInt):String;
- function X509GetSubjectName(a: PX509):PX509_NAME;
- function X509GetIssuerName(a: PX509):PX509_NAME;
- function X509NameHash(x: PX509_NAME):cuLong;
- // function SslX509Digest(data: PX509; _type: PEVP_MD; md: PChar; len: PcInt):cInt;
- function X509Digest(data: PX509; _type: PEVP_MD; md: String; var len: cInt):cInt;
- function X509print(b: PBIO; a: PX509): cInt;
- function X509SetVersion(x: PX509; version: cInt): cInt;
- function X509SetPubkey(x: PX509; pkey: EVP_PKEY): cInt;
- function X509SetIssuerName(x: PX509; name: PX509_NAME): cInt;
- function X509NameAddEntryByTxt(name: PX509_NAME; field: string; _type: cInt;
- bytes: string; len, loc, _set: cInt): cInt;
- function X509Sign(x: PX509; pkey: EVP_PKEY; const md: PEVP_MD): cInt;
- function X509GmtimeAdj(s: PASN1_UTCTIME; adj: cInt): PASN1_UTCTIME;
- function X509SetNotBefore(x: PX509; tm: PASN1_UTCTIME): cInt;
- function X509SetNotAfter(x: PX509; tm: PASN1_UTCTIME): cInt;
- function X509GetSerialNumber(x: PX509): PASN1_cInt;
- function EvpPkeyNew: EVP_PKEY;
- procedure EvpPkeyFree(pk: EVP_PKEY);
- function EvpPkeyAssign(pkey: EVP_PKEY; _type: cInt; key: Prsa): cInt;
- function EvpGetDigestByName(Name: String): PEVP_MD;
- procedure EVPcleanup;
- // function ErrErrorString(e: cInt; buf: PChar): PChar;
- function SSLeayversion(t: cInt): string;
- procedure ErrErrorString(e: cInt; var buf: string; len: cInt);
- function ErrGetError: cInt;
- procedure ErrClearError;
- procedure ErrFreeStrings;
- procedure ErrRemoveState(pid: cInt);
- procedure OPENSSLaddallalgorithms;
- procedure CRYPTOcleanupAllExData;
- procedure RandScreen;
- function BioNew(b: PBIO_METHOD): PBIO;
- procedure BioFreeAll(b: PBIO);
- function BioSMem: PBIO_METHOD;
- function BioCtrlPending(b: PBIO): cInt;
- function BioRead(b: PBIO; var Buf: String; Len: cInt): cInt;
- function BioWrite(b: PBIO; Buf: String; Len: cInt): cInt;
- function d2iPKCS12bio(b:PBIO; Pkcs12: SslPtr): SslPtr;
- function PKCS12parse(p12: SslPtr; pass: string; var pkey, cert, ca: SslPtr): cInt;
- procedure PKCS12free(p12: SslPtr);
- function RsaGenerateKey(bits, e: cInt; callback: PFunction; cb_arg: SslPtr): PRSA;
- function Asn1UtctimeNew: PASN1_UTCTIME;
- procedure Asn1UtctimeFree(a: PASN1_UTCTIME);
- function Asn1cIntSet(a: PASN1_cInt; v: cInt): cInt;
- function i2dX509bio(b: PBIO; x: PX509): cInt;
- function i2dPrivateKeyBio(b: PBIO; pkey: EVP_PKEY): cInt;
- // 3DES functions
- procedure DESsetoddparity(Key: des_cblock);
- function DESsetkeychecked(key: des_cblock; schedule: des_key_schedule): cInt;
- procedure DESecbencrypt(Input: des_cblock; output: des_cblock; ks: des_key_schedule; enc: cInt);
- function IsSSLloaded: Boolean;
- function InitSSLInterface: Boolean;
- function DestroySSLInterface: Boolean;
- implementation
- type
- // libssl.dll
- TSslGetError = function(s: PSSL; ret_code: cInt):cInt; cdecl;
- TSslLibraryInit = function:cInt; cdecl;
- TSslLoadErrorStrings = procedure; cdecl;
- TSslCtxSetCipherList = function(arg0: PSSL_CTX; str: PChar):cInt; cdecl;
- TSslCtxNew = function(meth: PSSL_METHOD):PSSL_CTX; cdecl;
- TSslCtxFree = procedure(arg0: PSSL_CTX); cdecl;
- TSslSetFd = function(s: PSSL; fd: cInt):cInt; cdecl;
- TSslMethodV2 = function:PSSL_METHOD; cdecl;
- TSslMethodV3 = function:PSSL_METHOD; cdecl;
- TSslMethodTLSV1 = function:PSSL_METHOD; cdecl;
- TSslMethodV23 = function:PSSL_METHOD; cdecl;
- TSslCtxUsePrivateKey = function(ctx: PSSL_CTX; pkey: sslptr):cInt; cdecl;
- TSslCtxUsePrivateKeyASN1 = function(pk: cInt; ctx: PSSL_CTX; d: sslptr; len: cInt):cInt; cdecl;
- TSslCtxUsePrivateKeyFile = function(ctx: PSSL_CTX; const _file: PChar; _type: cInt):cInt; cdecl;
- TSslCtxUseCertificate = function(ctx: PSSL_CTX; x: SslPtr):cInt; cdecl;
- TSslCtxUseCertificateASN1 = function(ctx: PSSL_CTX; len: cInt; d: SslPtr):cInt; cdecl;
- TSslCtxUseCertificateFile = function(ctx: PSSL_CTX; const _file: PChar; _type: cInt):cInt; cdecl;
- TSslCtxUseCertificateChainFile = function(ctx: PSSL_CTX; const _file: PChar):cInt; cdecl;
- TSslCtxCheckPrivateKeyFile = function(ctx: PSSL_CTX):cInt; cdecl;
- TSslCtxSetDefaultPasswdCb = procedure(ctx: PSSL_CTX; cb: SslPtr); cdecl;
- TSslCtxSetDefaultPasswdCbUserdata = procedure(ctx: PSSL_CTX; u: SslPtr); cdecl;
- TSslCtxLoadVerifyLocations = function(ctx: PSSL_CTX; const CAfile: PChar; const CApath: PChar):cInt; cdecl;
- TSslNew = function(ctx: PSSL_CTX):PSSL; cdecl;
- TSslFree = procedure(ssl: PSSL); cdecl;
- TSslAccept = function(ssl: PSSL):cInt; cdecl;
- TSslConnect = function(ssl: PSSL):cInt; cdecl;
- TSslShutdown = function(ssl: PSSL):cInt; cdecl;
- TSslRead = function(ssl: PSSL; buf: PChar; num: cInt):cInt; cdecl;
- TSslPeek = function(ssl: PSSL; buf: PChar; num: cInt):cInt; cdecl;
- TSslWrite = function(ssl: PSSL; const buf: PChar; num: cInt):cInt; cdecl;
- TSslPending = function(ssl: PSSL):cInt; cdecl;
- TSslGetVersion = function(ssl: PSSL):PChar; cdecl;
- TSslGetPeerCertificate = function(ssl: PSSL):PX509; cdecl;
- TSslCtxSetVerify = procedure(ctx: PSSL_CTX; mode: cInt; arg2: SslPtr); cdecl;
- TSSLGetCurrentCipher = function(s: PSSL):SslPtr; cdecl;
- TSSLCipherGetName = function(c: Sslptr):PChar; cdecl;
- TSSLCipherGetBits = function(c: SslPtr; alg_bits: PcInt):cInt; cdecl;
- TSSLGetVerifyResult = function(ssl: PSSL):cInt; cdecl;
- // libeay.dll
- TX509New = function: PX509; cdecl;
- TX509Free = procedure(x: PX509); cdecl;
- TX509NameOneline = function(a: PX509_NAME; buf: PChar; size: cInt):PChar; cdecl;
- TX509GetSubjectName = function(a: PX509):PX509_NAME; cdecl;
- TX509GetIssuerName = function(a: PX509):PX509_NAME; cdecl;
- TX509NameHash = function(x: PX509_NAME):cuLong; cdecl;
- TX509Digest = function(data: PX509; _type: PEVP_MD; md: PChar; len: PcInt):cInt; cdecl;
- TX509print = function(b: PBIO; a: PX509): cInt; cdecl;
- TX509SetVersion = function(x: PX509; version: cInt): cInt; cdecl;
- TX509SetPubkey = function(x: PX509; pkey: EVP_PKEY): cInt; cdecl;
- TX509SetIssuerName = function(x: PX509; name: PX509_NAME): cInt; cdecl;
- TX509NameAddEntryByTxt = function(name: PX509_NAME; field: PChar; _type: cInt;
- bytes: PChar; len, loc, _set: cInt): cInt; cdecl;
- TX509Sign = function(x: PX509; pkey: EVP_PKEY; const md: PEVP_MD): cInt; cdecl;
- TX509GmtimeAdj = function(s: PASN1_UTCTIME; adj: cInt): PASN1_UTCTIME; cdecl;
- TX509SetNotBefore = function(x: PX509; tm: PASN1_UTCTIME): cInt; cdecl;
- TX509SetNotAfter = function(x: PX509; tm: PASN1_UTCTIME): cInt; cdecl;
- TX509GetSerialNumber = function(x: PX509): PASN1_cInt; cdecl;
- TEvpPkeyNew = function: EVP_PKEY; cdecl;
- TEvpPkeyFree = procedure(pk: EVP_PKEY); cdecl;
- TEvpPkeyAssign = function(pkey: EVP_PKEY; _type: cInt; key: Prsa): cInt; cdecl;
- TEvpGetDigestByName = function(Name: PChar): PEVP_MD; cdecl;
- TEVPcleanup = procedure; cdecl;
- TSSLeayversion = function(t: cInt): PChar; cdecl;
- TErrErrorString = procedure(e: cInt; buf: PChar; len: cInt); cdecl;
- TErrGetError = function: cInt; cdecl;
- TErrClearError = procedure; cdecl;
- TErrFreeStrings = procedure; cdecl;
- TErrRemoveState = procedure(pid: cInt); cdecl;
- TOPENSSLaddallalgorithms = procedure; cdecl;
- TCRYPTOcleanupAllExData = procedure; cdecl;
- TRandScreen = procedure; cdecl;
- TBioNew = function(b: PBIO_METHOD): PBIO; cdecl;
- TBioFreeAll = procedure(b: PBIO); cdecl;
- TBioSMem = function: PBIO_METHOD; cdecl;
- TBioCtrlPending = function(b: PBIO): cInt; cdecl;
- TBioRead = function(b: PBIO; Buf: PChar; Len: cInt): cInt; cdecl;
- TBioWrite = function(b: PBIO; Buf: PChar; Len: cInt): cInt; cdecl;
- Td2iPKCS12bio = function(b:PBIO; Pkcs12: SslPtr): SslPtr; cdecl;
- TPKCS12parse = function(p12: SslPtr; pass: PChar; var pkey, cert, ca: SslPtr): cInt; cdecl;
- TPKCS12free = procedure(p12: SslPtr); cdecl;
- TRsaGenerateKey = function(bits, e: cInt; callback: PFunction; cb_arg: SslPtr): PRSA; cdecl;
- TAsn1UtctimeNew = function: PASN1_UTCTIME; cdecl;
- TAsn1UtctimeFree = procedure(a: PASN1_UTCTIME); cdecl;
- TAsn1cIntSet = function(a: PASN1_cInt; v: cInt): cInt; cdecl;
- Ti2dX509bio = function(b: PBIO; x: PX509): cInt; cdecl;
- Ti2dPrivateKeyBio= function(b: PBIO; pkey: EVP_PKEY): cInt; cdecl;
- // 3DES functions
- TDESsetoddparity = procedure(Key: des_cblock); cdecl;
- TDESsetkeychecked = function(key: des_cblock; schedule: des_key_schedule): cInt; cdecl;
- TDESecbencrypt = procedure(Input: des_cblock; output: des_cblock; ks: des_key_schedule; enc: cInt); cdecl;
- //thread lock functions
- TCRYPTOnumlocks = function: cInt; cdecl;
- TCRYPTOSetLockingCallback = procedure(cb: Sslptr); cdecl;
- var
- // libssl.dll
- _SslGetError: TSslGetError = nil;
- _SslLibraryInit: TSslLibraryInit = nil;
- _SslLoadErrorStrings: TSslLoadErrorStrings = nil;
- _SslCtxSetCipherList: TSslCtxSetCipherList = nil;
- _SslCtxNew: TSslCtxNew = nil;
- _SslCtxFree: TSslCtxFree = nil;
- _SslSetFd: TSslSetFd = nil;
- _SslMethodV2: TSslMethodV2 = nil;
- _SslMethodV3: TSslMethodV3 = nil;
- _SslMethodTLSV1: TSslMethodTLSV1 = nil;
- _SslMethodV23: TSslMethodV23 = nil;
- _SslCtxUsePrivateKey: TSslCtxUsePrivateKey = nil;
- _SslCtxUsePrivateKeyASN1: TSslCtxUsePrivateKeyASN1 = nil;
- _SslCtxUsePrivateKeyFile: TSslCtxUsePrivateKeyFile = nil;
- _SslCtxUseCertificate: TSslCtxUseCertificate = nil;
- _SslCtxUseCertificateASN1: TSslCtxUseCertificateASN1 = nil;
- _SslCtxUseCertificateFile: TSslCtxUseCertificateFile = nil;
- _SslCtxUseCertificateChainFile: TSslCtxUseCertificateChainFile = nil;
- _SslCtxCheckPrivateKeyFile: TSslCtxCheckPrivateKeyFile = nil;
- _SslCtxSetDefaultPasswdCb: TSslCtxSetDefaultPasswdCb = nil;
- _SslCtxSetDefaultPasswdCbUserdata: TSslCtxSetDefaultPasswdCbUserdata = nil;
- _SslCtxLoadVerifyLocations: TSslCtxLoadVerifyLocations = nil;
- _SslNew: TSslNew = nil;
- _SslFree: TSslFree = nil;
- _SslAccept: TSslAccept = nil;
- _SslConnect: TSslConnect = nil;
- _SslShutdown: TSslShutdown = nil;
- _SslRead: TSslRead = nil;
- _SslPeek: TSslPeek = nil;
- _SslWrite: TSslWrite = nil;
- _SslPending: TSslPending = nil;
- _SslGetVersion: TSslGetVersion = nil;
- _SslGetPeerCertificate: TSslGetPeerCertificate = nil;
- _SslCtxSetVerify: TSslCtxSetVerify = nil;
- _SSLGetCurrentCipher: TSSLGetCurrentCipher = nil;
- _SSLCipherGetName: TSSLCipherGetName = nil;
- _SSLCipherGetBits: TSSLCipherGetBits = nil;
- _SSLGetVerifyResult: TSSLGetVerifyResult = nil;
- // libeay.dll
- _X509New: TX509New = nil;
- _X509Free: TX509Free = nil;
- _X509NameOneline: TX509NameOneline = nil;
- _X509GetSubjectName: TX509GetSubjectName = nil;
- _X509GetIssuerName: TX509GetIssuerName = nil;
- _X509NameHash: TX509NameHash = nil;
- _X509Digest: TX509Digest = nil;
- _X509print: TX509print = nil;
- _X509SetVersion: TX509SetVersion = nil;
- _X509SetPubkey: TX509SetPubkey = nil;
- _X509SetIssuerName: TX509SetIssuerName = nil;
- _X509NameAddEntryByTxt: TX509NameAddEntryByTxt = nil;
- _X509Sign: TX509Sign = nil;
- _X509GmtimeAdj: TX509GmtimeAdj = nil;
- _X509SetNotBefore: TX509SetNotBefore = nil;
- _X509SetNotAfter: TX509SetNotAfter = nil;
- _X509GetSerialNumber: TX509GetSerialNumber = nil;
- _EvpPkeyNew: TEvpPkeyNew = nil;
- _EvpPkeyFree: TEvpPkeyFree = nil;
- _EvpPkeyAssign: TEvpPkeyAssign = nil;
- _EvpGetDigestByName: TEvpGetDigestByName = nil;
- _EVPcleanup: TEVPcleanup = nil;
- _SSLeayversion: TSSLeayversion = nil;
- _ErrErrorString: TErrErrorString = nil;
- _ErrGetError: TErrGetError = nil;
- _ErrClearError: TErrClearError = nil;
- _ErrFreeStrings: TErrFreeStrings = nil;
- _ErrRemoveState: TErrRemoveState = nil;
- _OPENSSLaddallalgorithms: TOPENSSLaddallalgorithms = nil;
- _CRYPTOcleanupAllExData: TCRYPTOcleanupAllExData = nil;
- _RandScreen: TRandScreen = nil;
- _BioNew: TBioNew = nil;
- _BioFreeAll: TBioFreeAll = nil;
- _BioSMem: TBioSMem = nil;
- _BioCtrlPending: TBioCtrlPending = nil;
- _BioRead: TBioRead = nil;
- _BioWrite: TBioWrite = nil;
- _d2iPKCS12bio: Td2iPKCS12bio = nil;
- _PKCS12parse: TPKCS12parse = nil;
- _PKCS12free: TPKCS12free = nil;
- _RsaGenerateKey: TRsaGenerateKey = nil;
- _Asn1UtctimeNew: TAsn1UtctimeNew = nil;
- _Asn1UtctimeFree: TAsn1UtctimeFree = nil;
- _Asn1cIntSet: TAsn1cIntSet = nil;
- _i2dX509bio: Ti2dX509bio = nil;
- _i2dPrivateKeyBio: Ti2dPrivateKeyBio = nil;
- // 3DES functions
- _DESsetoddparity: TDESsetoddparity = nil;
- _DESsetkeychecked: TDESsetkeychecked = nil;
- _DESecbencrypt: TDESecbencrypt = nil;
- //thread lock functions
- _CRYPTOnumlocks: TCRYPTOnumlocks = nil;
- _CRYPTOSetLockingCallback: TCRYPTOSetLockingCallback = nil;
- var
- SSLloaded: boolean = false;
- // libssl.dll
- function SslGetError(s: PSSL; ret_code: cInt):cInt;
- begin
- if InitSSLInterface and Assigned(_SslGetError) then
- Result := _SslGetError(s, ret_code)
- else
- Result := SSL_ERROR_SSL;
- end;
- function SslLibraryInit:cInt;
- begin
- if InitSSLInterface and Assigned(_SslLibraryInit) then
- Result := _SslLibraryInit
- else
- Result := 1;
- end;
- procedure SslLoadErrorStrings;
- begin
- if InitSSLInterface and Assigned(_SslLoadErrorStrings) then
- _SslLoadErrorStrings;
- end;
- function SslCtxSetCipherList(arg0: PSSL_CTX; var str: String):cInt;
- begin
- if InitSSLInterface and Assigned(_SslCtxSetCipherList) then
- Result := _SslCtxSetCipherList(arg0, PChar(str))
- else
- Result := 0;
- end;
- function SslCtxNew(meth: PSSL_METHOD):PSSL_CTX;
- begin
- if InitSSLInterface and Assigned(_SslCtxNew) then
- Result := _SslCtxNew(meth)
- else
- Result := nil;
- end;
- procedure SslCtxFree(arg0: PSSL_CTX);
- begin
- if InitSSLInterface and Assigned(_SslCtxFree) then
- _SslCtxFree(arg0);
- end;
- function SslSetFd(s: PSSL; fd: cInt):cInt;
- begin
- if InitSSLInterface and Assigned(_SslSetFd) then
- Result := _SslSetFd(s, fd)
- else
- Result := 0;
- end;
- function SslMethodV2:PSSL_METHOD;
- begin
- if InitSSLInterface and Assigned(_SslMethodV2) then
- Result := _SslMethodV2
- else
- Result := nil;
- end;
- function SslMethodV3:PSSL_METHOD;
- begin
- if InitSSLInterface and Assigned(_SslMethodV3) then
- Result := _SslMethodV3
- else
- Result := nil;
- end;
- function SslMethodTLSV1:PSSL_METHOD;
- begin
- if InitSSLInterface and Assigned(_SslMethodTLSV1) then
- Result := _SslMethodTLSV1
- else
- Result := nil;
- end;
- function SslMethodV23:PSSL_METHOD;
- begin
- if InitSSLInterface and Assigned(_SslMethodV23) then
- Result := _SslMethodV23
- else
- Result := nil;
- end;
- function SslCtxUsePrivateKey(ctx: PSSL_CTX; pkey: SslPtr):cInt;
- begin
- if InitSSLInterface and Assigned(_SslCtxUsePrivateKey) then
- Result := _SslCtxUsePrivateKey(ctx, pkey)
- else
- Result := 0;
- end;
- function SslCtxUsePrivateKeyASN1(pk: cInt; ctx: PSSL_CTX; d: String; len: cLong):cInt;
- begin
- if InitSSLInterface and Assigned(_SslCtxUsePrivateKeyASN1) then
- Result := _SslCtxUsePrivateKeyASN1(pk, ctx, Sslptr(d), len)
- else
- Result := 0;
- end;
- function SslCtxUsePrivateKeyFile(ctx: PSSL_CTX; const _file: String; _type: cInt):cInt;
- begin
- if InitSSLInterface and Assigned(_SslCtxUsePrivateKeyFile) then
- Result := _SslCtxUsePrivateKeyFile(ctx, PChar(_file), _type)
- else
- Result := 0;
- end;
- function SslCtxUseCertificate(ctx: PSSL_CTX; x: SslPtr):cInt;
- begin
- if InitSSLInterface and Assigned(_SslCtxUseCertificate) then
- Result := _SslCtxUseCertificate(ctx, x)
- else
- Result := 0;
- end;
- function SslCtxUseCertificateASN1(ctx: PSSL_CTX; len: cLong; d: String):cInt;
- begin
- if InitSSLInterface and Assigned(_SslCtxUseCertificateASN1) then
- Result := _SslCtxUseCertificateASN1(ctx, len, SslPtr(d))
- else
- Result := 0;
- end;
- function SslCtxUseCertificateFile(ctx: PSSL_CTX; const _file: String; _type: cInt):cInt;
- begin
- if InitSSLInterface and Assigned(_SslCtxUseCertificateFile) then
- Result := _SslCtxUseCertificateFile(ctx, PChar(_file), _type)
- else
- Result := 0;
- end;
- function SslCtxUseCertificateChainFile(ctx: PSSL_CTX; const _file: String):cInt;
- begin
- if InitSSLInterface and Assigned(_SslCtxUseCertificateChainFile) then
- Result := _SslCtxUseCertificateChainFile(ctx, PChar(_file))
- else
- Result := 0;
- end;
- function SslCtxCheckPrivateKeyFile(ctx: PSSL_CTX):cInt;
- begin
- if InitSSLInterface and Assigned(_SslCtxCheckPrivateKeyFile) then
- Result := _SslCtxCheckPrivateKeyFile(ctx)
- else
- Result := 0;
- end;
- procedure SslCtxSetDefaultPasswdCb(ctx: PSSL_CTX; cb: PPasswdCb);
- begin
- if InitSSLInterface and Assigned(_SslCtxSetDefaultPasswdCb) then
- _SslCtxSetDefaultPasswdCb(ctx, cb);
- end;
- procedure SslCtxSetDefaultPasswdCbUserdata(ctx: PSSL_CTX; u: SslPtr);
- begin
- if InitSSLInterface and Assigned(_SslCtxSetDefaultPasswdCbUserdata) then
- _SslCtxSetDefaultPasswdCbUserdata(ctx, u);
- end;
- function SslCtxLoadVerifyLocations(ctx: PSSL_CTX; const CAfile: String; const CApath: String):cInt;
- begin
- if InitSSLInterface and Assigned(_SslCtxLoadVerifyLocations) then
- Result := _SslCtxLoadVerifyLocations(ctx, SslPtr(CAfile), SslPtr(CApath))
- else
- Result := 0;
- end;
- function SslNew(ctx: PSSL_CTX):PSSL;
- begin
- if InitSSLInterface and Assigned(_SslNew) then
- Result := _SslNew(ctx)
- else
- Result := nil;
- end;
- procedure SslFree(ssl: PSSL);
- begin
- if InitSSLInterface and Assigned(_SslFree) then
- _SslFree(ssl);
- end;
- function SslAccept(ssl: PSSL):cInt;
- begin
- if InitSSLInterface and Assigned(_SslAccept) then
- Result := _SslAccept(ssl)
- else
- Result := -1;
- end;
- function SslConnect(ssl: PSSL):cInt;
- begin
- if InitSSLInterface and Assigned(_SslConnect) then
- Result := _SslConnect(ssl)
- else
- Result := -1;
- end;
- function SslShutdown(ssl: PSSL):cInt;
- begin
- if InitSSLInterface and Assigned(_SslShutdown) then
- Result := _SslShutdown(ssl)
- else
- Result := -1;
- end;
- function SslRead(ssl: PSSL; buf: SslPtr; num: cInt):cInt;
- begin
- if InitSSLInterface and Assigned(_SslRead) then
- Result := _SslRead(ssl, PChar(buf), num)
- else
- Result := -1;
- end;
- function SslPeek(ssl: PSSL; buf: SslPtr; num: cInt):cInt;
- begin
- if InitSSLInterface and Assigned(_SslPeek) then
- Result := _SslPeek(ssl, PChar(buf), num)
- else
- Result := -1;
- end;
- function SslWrite(ssl: PSSL; buf: SslPtr; num: cInt):cInt;
- begin
- if InitSSLInterface and Assigned(_SslWrite) then
- Result := _SslWrite(ssl, PChar(buf), num)
- else
- Result := -1;
- end;
- function SslPending(ssl: PSSL):cInt;
- begin
- if InitSSLInterface and Assigned(_SslPending) then
- Result := _SslPending(ssl)
- else
- Result := 0;
- end;
- //function SslGetVersion(ssl: PSSL):PChar;
- function SslGetVersion(ssl: PSSL):String;
- begin
- if InitSSLInterface and Assigned(_SslGetVersion) then
- Result := _SslGetVersion(ssl)
- else
- Result := '';
- end;
- function SslGetPeerCertificate(ssl: PSSL):PX509;
- begin
- if InitSSLInterface and Assigned(_SslGetPeerCertificate) then
- Result := _SslGetPeerCertificate(ssl)
- else
- Result := nil;
- end;
- procedure SslCtxSetVerify(ctx: PSSL_CTX; mode: cInt; arg2: PFunction);
- begin
- if InitSSLInterface and Assigned(_SslCtxSetVerify) then
- _SslCtxSetVerify(ctx, mode, @arg2);
- end;
- function SSLGetCurrentCipher(s: PSSL):SslPtr;
- begin
- if InitSSLInterface and Assigned(_SSLGetCurrentCipher) then
- {$IFDEF CIL}
- {$ELSE}
- Result := _SSLGetCurrentCipher(s)
- {$ENDIF}
- else
- Result := nil;
- end;
- function SSLCipherGetName(c: SslPtr):String;
- begin
- if InitSSLInterface and Assigned(_SSLCipherGetName) then
- Result := _SSLCipherGetName(c)
- else
- Result := '';
- end;
- function SSLCipherGetBits(c: SslPtr; var alg_bits: cInt):cInt;
- begin
- if InitSSLInterface and Assigned(_SSLCipherGetBits) then
- Result := _SSLCipherGetBits(c, @alg_bits)
- else
- Result := 0;
- end;
- function SSLGetVerifyResult(ssl: PSSL):cLong;
- begin
- if InitSSLInterface and Assigned(_SSLGetVerifyResult) then
- Result := _SSLGetVerifyResult(ssl)
- else
- Result := X509_V_ERR_APPLICATION_VERIFICATION;
- end;
- // libeay.dll
- function X509New: PX509;
- begin
- if InitSSLInterface and Assigned(_X509New) then
- Result := _X509New
- else
- Result := nil;
- end;
- procedure X509Free(x: PX509);
- begin
- if InitSSLInterface and Assigned(_X509Free) then
- _X509Free(x);
- end;
- function X509NameOneline(a: PX509_NAME; var buf: String; size: cInt):String;
- begin
- if InitSSLInterface and Assigned(_X509NameOneline) then
- Result := _X509NameOneline(a, PChar(buf),size)
- else
- Result := '';
- end;
- function X509GetSubjectName(a: PX509):PX509_NAME;
- begin
- if InitSSLInterface and Assigned(_X509GetSubjectName) then
- Result := _X509GetSubjectName(a)
- else
- Result := nil;
- end;
- function X509GetIssuerName(a: PX509):PX509_NAME;
- begin
- if InitSSLInterface and Assigned(_X509GetIssuerName) then
- Result := _X509GetIssuerName(a)
- else
- Result := nil;
- end;
- function X509NameHash(x: PX509_NAME):cuLong;
- begin
- if InitSSLInterface and Assigned(_X509NameHash) then
- Result := _X509NameHash(x)
- else
- Result := 0;
- end;
- function X509Digest(data: PX509; _type: PEVP_MD; md: String; var len: cInt):cInt;
- begin
- if InitSSLInterface and Assigned(_X509Digest) then
- Result := _X509Digest(data, _type, PChar(md), @len)
- else
- Result := 0;
- end;
- function EvpPkeyNew: EVP_PKEY;
- begin
- if InitSSLInterface and Assigned(_EvpPkeyNew) then
- Result := _EvpPkeyNew
- else
- Result := nil;
- end;
- procedure EvpPkeyFree(pk: EVP_PKEY);
- begin
- if InitSSLInterface and Assigned(_EvpPkeyFree) then
- _EvpPkeyFree(pk);
- end;
- function SSLeayversion(t: cInt): string;
- begin
- if InitSSLInterface and Assigned(_SSLeayversion) then
- Result := PChar(_SSLeayversion(t))
- else
- Result := '';
- end;
- procedure ErrErrorString(e: cInt; var buf: string; len: cInt);
- begin
- if InitSSLInterface and Assigned(_ErrErrorString) then
- _ErrErrorString(e, Pointer(buf), len);
- buf := PChar(Buf);
- end;
- function ErrGetError: cInt;
- begin
- if InitSSLInterface and Assigned(_ErrGetError) then
- Result := _ErrGetError
- else
- Result := SSL_ERROR_SSL;
- end;
- procedure ErrClearError;
- begin
- if InitSSLInterface and Assigned(_ErrClearError) then
- _ErrClearError;
- end;
- procedure ErrFreeStrings;
- begin
- if InitSSLInterface and Assigned(_ErrFreeStrings) then
- _ErrFreeStrings;
- end;
- procedure ErrRemoveState(pid: cInt);
- begin
- if InitSSLInterface and Assigned(_ErrRemoveState) then
- _ErrRemoveState(pid);
- end;
- procedure OPENSSLaddallalgorithms;
- begin
- if InitSSLInterface and Assigned(_OPENSSLaddallalgorithms) then
- _OPENSSLaddallalgorithms;
- end;
- procedure EVPcleanup;
- begin
- if InitSSLInterface and Assigned(_EVPcleanup) then
- _EVPcleanup;
- end;
- procedure CRYPTOcleanupAllExData;
- begin
- if InitSSLInterface and Assigned(_CRYPTOcleanupAllExData) then
- _CRYPTOcleanupAllExData;
- end;
- procedure RandScreen;
- begin
- if InitSSLInterface and Assigned(_RandScreen) then
- _RandScreen;
- end;
- function BioNew(b: PBIO_METHOD): PBIO;
- begin
- if InitSSLInterface and Assigned(_BioNew) then
- Result := _BioNew(b)
- else
- Result := nil;
- end;
- procedure BioFreeAll(b: PBIO);
- begin
- if InitSSLInterface and Assigned(_BioFreeAll) then
- _BioFreeAll(b);
- end;
- function BioSMem: PBIO_METHOD;
- begin
- if InitSSLInterface and Assigned(_BioSMem) then
- Result := _BioSMem
- else
- Result := nil;
- end;
- function BioCtrlPending(b: PBIO): cInt;
- begin
- if InitSSLInterface and Assigned(_BioCtrlPending) then
- Result := _BioCtrlPending(b)
- else
- Result := 0;
- end;
- function BioRead(b: PBIO; var Buf: String; Len: cInt): cInt;
- begin
- if InitSSLInterface and Assigned(_BioRead) then
- Result := _BioRead(b, PChar(Buf), Len)
- else
- Result := -2;
- end;
- //function BioWrite(b: PBIO; Buf: PChar; Len: cInt): cInt;
- function BioWrite(b: PBIO; Buf: String; Len: cInt): cInt;
- begin
- if InitSSLInterface and Assigned(_BioWrite) then
- Result := _BioWrite(b, PChar(Buf), Len)
- else
- Result := -2;
- end;
- function X509print(b: PBIO; a: PX509): cInt;
- begin
- if InitSSLInterface and Assigned(_X509print) then
- Result := _X509print(b, a)
- else
- Result := 0;
- end;
- function d2iPKCS12bio(b:PBIO; Pkcs12: SslPtr): SslPtr;
- begin
- if InitSSLInterface and Assigned(_d2iPKCS12bio) then
- Result := _d2iPKCS12bio(b, Pkcs12)
- else
- Result := nil;
- end;
- function PKCS12parse(p12: SslPtr; pass: string; var pkey, cert, ca: SslPtr): cInt;
- begin
- if InitSSLInterface and Assigned(_PKCS12parse) then
- Result := _PKCS12parse(p12, SslPtr(pass), pkey, cert, ca)
- else
- Result := 0;
- end;
- procedure PKCS12free(p12: SslPtr);
- begin
- if InitSSLInterface and Assigned(_PKCS12free) then
- _PKCS12free(p12);
- end;
- function RsaGenerateKey(bits, e: cInt; callback: PFunction; cb_arg: SslPtr): PRSA;
- begin
- if InitSSLInterface and Assigned(_RsaGenerateKey) then
- Result := _RsaGenerateKey(bits, e, callback, cb_arg)
- else
- Result := nil;
- end;
- function EvpPkeyAssign(pkey: EVP_PKEY; _type: cInt; key: Prsa): cInt;
- begin
- if InitSSLInterface and Assigned(_EvpPkeyAssign) then
- Result := _EvpPkeyAssign(pkey, _type, key)
- else
- Result := 0;
- end;
- function X509SetVersion(x: PX509; version: cInt): cInt;
- begin
- if InitSSLInterface and Assigned(_X509SetVersion) then
- Result := _X509SetVersion(x, version)
- else
- Result := 0;
- end;
- function X509SetPubkey(x: PX509; pkey: EVP_PKEY): cInt;
- begin
- if InitSSLInterface and Assigned(_X509SetPubkey) then
- Result := _X509SetPubkey(x, pkey)
- else
- Result := 0;
- end;
- function X509SetIssuerName(x: PX509; name: PX509_NAME): cInt;
- begin
- if InitSSLInterface and Assigned(_X509SetIssuerName) then
- Result := _X509SetIssuerName(x, name)
- else
- Result := 0;
- end;
- function X509NameAddEntryByTxt(name: PX509_NAME; field: string; _type: cInt;
- bytes: string; len, loc, _set: cInt): cInt;
- begin
- if InitSSLInterface and Assigned(_X509NameAddEntryByTxt) then
- Result := _X509NameAddEntryByTxt(name, PChar(field), _type, PChar(Bytes), len, loc, _set)
- else
- Result := 0;
- end;
- function X509Sign(x: PX509; pkey: EVP_PKEY; const md: PEVP_MD): cInt;
- begin
- if InitSSLInterface and Assigned(_X509Sign) then
- Result := _X509Sign(x, pkey, md)
- else
- Result := 0;
- end;
- function Asn1UtctimeNew: PASN1_UTCTIME;
- begin
- if InitSSLInterface and Assigned(_Asn1UtctimeNew) then
- Result := _Asn1UtctimeNew
- else
- Result := nil;
- end;
- procedure Asn1UtctimeFree(a: PASN1_UTCTIME);
- begin
- if InitSSLInterface and Assigned(_Asn1UtctimeFree) then
- _Asn1UtctimeFree(a);
- end;
- function X509GmtimeAdj(s: PASN1_UTCTIME; adj: cInt): PASN1_UTCTIME;
- begin
- if InitSSLInterface and Assigned(_X509GmtimeAdj) then
- Result := _X509GmtimeAdj(s, adj)
- else
- Result := nil;
- end;
- function X509SetNotBefore(x: PX509; tm: PASN1_UTCTIME): cInt;
- begin
- if InitSSLInterface and Assigned(_X509SetNotBefore) then
- Result := _X509SetNotBefore(x, tm)
- else
- Result := 0;
- end;
- function X509SetNotAfter(x: PX509; tm: PASN1_UTCTIME): cInt;
- begin
- if InitSSLInterface and Assigned(_X509SetNotAfter) then
- Result := _X509SetNotAfter(x, tm)
- else
- Result := 0;
- end;
- function i2dX509bio(b: PBIO; x: PX509): cInt;
- begin
- if InitSSLInterface and Assigned(_i2dX509bio) then
- Result := _i2dX509bio(b, x)
- else
- Result := 0;
- end;
- function i2dPrivateKeyBio(b: PBIO; pkey: EVP_PKEY): cInt;
- begin
- if InitSSLInterface and Assigned(_i2dPrivateKeyBio) then
- Result := _i2dPrivateKeyBio(b, pkey)
- else
- Result := 0;
- end;
- function EvpGetDigestByName(Name: String): PEVP_MD;
- begin
- if InitSSLInterface and Assigned(_EvpGetDigestByName) then
- Result := _EvpGetDigestByName(PChar(Name))
- else
- Result := nil;
- end;
- function Asn1cIntSet(a: PASN1_cInt; v: cInt): cInt;
- begin
- if InitSSLInterface and Assigned(_Asn1cIntSet) then
- Result := _Asn1cIntSet(a, v)
- else
- Result := 0;
- end;
- function X509GetSerialNumber(x: PX509): PASN1_cInt;
- begin
- if InitSSLInterface and Assigned(_X509GetSerialNumber) then
- Result := _X509GetSerialNumber(x)
- else
- Result := nil;
- end;
- // 3DES functions
- procedure DESsetoddparity(Key: des_cblock);
- begin
- if InitSSLInterface and Assigned(_DESsetoddparity) then
- _DESsetoddparity(Key);
- end;
- function DESsetkeychecked(key: des_cblock; schedule: des_key_schedule): cInt;
- begin
- if InitSSLInterface and Assigned(_DESsetkeychecked) then
- Result := _DESsetkeychecked(key, schedule)
- else
- Result := -1;
- end;
- procedure DESecbencrypt(Input: des_cblock; output: des_cblock; ks: des_key_schedule; enc: cInt);
- begin
- if InitSSLInterface and Assigned(_DESecbencrypt) then
- _DESecbencrypt(Input, output, ks, enc);
- end;
- {$IFNDEF WINDOWS}
- { Try to load all library versions until you find or run out }
- function LoadLibHack(const Value: String): HModule;
- var
- i: cInt;
- begin
- Result := NilHandle;
-
- for i := Low(DLLVersions) to High(DLLVersions) do begin
- {$IFDEF DARWIN}
- Result := LoadLibrary(Value + DLLVersions[i] + '.dylib');
- {$ELSE}
- Result := LoadLibrary(Value + '.so' + DLLVersions[i]);
- {$ENDIF}
-
- if Result <> NilHandle then
- Break;
- end;
- end;
- {$ENDIF}
- function LoadLib(const Value: String): HModule;
- begin
- {$IFDEF WINDOWS}
- Result := LoadLibrary(Value);
- {$ELSE}
- Result := LoadLibHack(Value);
- {$ENDIF}
- end;
- function GetProcAddr(module: HModule; const ProcName: string): SslPtr;
- begin
- Result := GetProcAddress(module, PChar(ProcName));
- end;
- function InitSSLInterface: Boolean;
- begin
- if not IsSSLloaded then
- begin
- SSLLibHandle := LoadLib(DLLSSLName);
- SSLUtilHandle := LoadLib(DLLUtilName);
- {$IFNDEF UNIX}
- if (SSLLibHandle = 0) then
- SSLLibHandle := LoadLib(DLLSSLName2);
- {$ENDIF}
- if (SSLLibHandle <> 0) and (SSLUtilHandle <> 0) then
- begin
- _SslGetError := GetProcAddr(SSLLibHandle, 'SSL_get_error');
- _SslLibraryInit := GetProcAddr(SSLLibHandle, 'SSL_library_init');
- _SslLoadErrorStrings := GetProcAddr(SSLLibHandle, 'SSL_load_error_strings');
- _SslCtxSetCipherList := GetProcAddr(SSLLibHandle, 'SSL_CTX_set_cipher_list');
- _SslCtxNew := GetProcAddr(SSLLibHandle, 'SSL_CTX_new');
- _SslCtxFree := GetProcAddr(SSLLibHandle, 'SSL_CTX_free');
- _SslSetFd := GetProcAddr(SSLLibHandle, 'SSL_set_fd');
- _SslMethodV2 := GetProcAddr(SSLLibHandle, 'SSLv2_method');
- _SslMethodV3 := GetProcAddr(SSLLibHandle, 'SSLv3_method');
- _SslMethodTLSV1 := GetProcAddr(SSLLibHandle, 'TLSv1_method');
- _SslMethodV23 := GetProcAddr(SSLLibHandle, 'SSLv23_method');
- _SslCtxUsePrivateKey := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_PrivateKey');
- _SslCtxUsePrivateKeyASN1 := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_PrivateKey_ASN1');
- //use SSL_CTX_use_RSAPrivateKey_file instead SSL_CTX_use_PrivateKey_file,
- //because SSL_CTX_use_PrivateKey_file not support DER format. :-O
- _SslCtxUsePrivateKeyFile := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_RSAPrivateKey_file');
- _SslCtxUseCertificate := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_certificate');
- _SslCtxUseCertificateASN1 := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_certificate_ASN1');
- _SslCtxUseCertificateFile := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_certificate_file');
- _SslCtxUseCertificateChainFile := GetProcAddr(SSLLibHandle, 'SSL_CTX_use_certificate_chain_file');
- _SslCtxCheckPrivateKeyFile := GetProcAddr(SSLLibHandle, 'SSL_CTX_check_private_key');
- _SslCtxSetDefaultPasswdCb := GetProcAddr(SSLLibHandle, 'SSL_CTX_set_default_passwd_cb');
- _SslCtxSetDefaultPasswdCbUserdata := GetProcAddr(SSLLibHandle, 'SSL_CTX_set_default_passwd_cb_userdata');
- _SslCtxLoadVerifyLocations := GetProcAddr(SSLLibHandle, 'SSL_CTX_load_verify_locations');
- _SslNew := GetProcAddr(SSLLibHandle, 'SSL_new');
- _SslFree := GetProcAddr(SSLLibHandle, 'SSL_free');
- _SslAccept := GetProcAddr(SSLLibHandle, 'SSL_accept');
- _SslConnect := GetProcAddr(SSLLibHandle, 'SSL_connect');
- _SslShutdown := GetProcAddr(SSLLibHandle, 'SSL_shutdown');
- _SslRead := GetProcAddr(SSLLibHandle, 'SSL_read');
- _SslPeek := GetProcAddr(SSLLibHandle, 'SSL_peek');
- _SslWrite := GetProcAddr(SSLLibHandle, 'SSL_write');
- _SslPending := GetProcAddr(SSLLibHandle, 'SSL_pending');
- _SslGetPeerCertificate := GetProcAddr(SSLLibHandle, 'SSL_get_peer_certificate');
- _SslGetVersion := GetProcAddr(SSLLibHandle, 'SSL_get_version');
- _SslCtxSetVerify := GetProcAddr(SSLLibHandle, 'SSL_CTX_set_verify');
- _SslGetCurrentCipher := GetProcAddr(SSLLibHandle, 'SSL_get_current_cipher');
- _SslCipherGetName := GetProcAddr(SSLLibHandle, 'SSL_CIPHER_get_name');
- _SslCipherGetBits := GetProcAddr(SSLLibHandle, 'SSL_CIPHER_get_bits');
- _SslGetVerifyResult := GetProcAddr(SSLLibHandle, 'SSL_get_verify_result');
- _X509New := GetProcAddr(SSLUtilHandle, 'X509_new');
- _X509Free := GetProcAddr(SSLUtilHandle, 'X509_free');
- _X509NameOneline := GetProcAddr(SSLUtilHandle, 'X509_NAME_oneline');
- _X509GetSubjectName := GetProcAddr(SSLUtilHandle, 'X509_get_subject_name');
- _X509GetIssuerName := GetProcAddr(SSLUtilHandle, 'X509_get_issuer_name');
- _X509NameHash := GetProcAddr(SSLUtilHandle, 'X509_NAME_hash');
- _X509Digest := GetProcAddr(SSLUtilHandle, 'X509_digest');
- _X509print := GetProcAddr(SSLUtilHandle, 'X509_print');
- _X509SetVersion := GetProcAddr(SSLUtilHandle, 'X509_set_version');
- _X509SetPubkey := GetProcAddr(SSLUtilHandle, 'X509_set_pubkey');
- _X509SetIssuerName := GetProcAddr(SSLUtilHandle, 'X509_set_issuer_name');
- _X509NameAddEntryByTxt := GetProcAddr(SSLUtilHandle, 'X509_NAME_add_entry_by_txt');
- _X509Sign := GetProcAddr(SSLUtilHandle, 'X509_sign');
- _X509GmtimeAdj := GetProcAddr(SSLUtilHandle, 'X509_gmtime_adj');
- _X509SetNotBefore := GetProcAddr(SSLUtilHandle, 'X509_set_notBefore');
- _X509SetNotAfter := GetProcAddr(SSLUtilHandle, 'X509_set_notAfter');
- _X509GetSerialNumber := GetProcAddr(SSLUtilHandle, 'X509_get_serialNumber');
- _EvpPkeyNew := GetProcAddr(SSLUtilHandle, 'EVP_PKEY_new');
- _EvpPkeyFree := GetProcAddr(SSLUtilHandle, 'EVP_PKEY_free');
- _EvpPkeyAssign := GetProcAddr(SSLUtilHandle, 'EVP_PKEY_assign');
- _EVPCleanup := GetProcAddr(SSLUtilHandle, 'EVP_cleanup');
- _EvpGetDigestByName := GetProcAddr(SSLUtilHandle, 'EVP_get_digestbyname');
- _SSLeayversion := GetProcAddr(SSLUtilHandle, 'SSLeay_version');
- _ErrErrorString := GetProcAddr(SSLUtilHandle, 'ERR_error_string_n');
- _ErrGetError := GetProcAddr(SSLUtilHandle, 'ERR_get_error');
- _ErrClearError := GetProcAddr(SSLUtilHandle, 'ERR_clear_error');
- _ErrFreeStrings := GetProcAddr(SSLUtilHandle, 'ERR_free_strings');
- _ErrRemoveState := GetProcAddr(SSLUtilHandle, 'ERR_remove_state');
- _OPENSSLaddallalgorithms := GetProcAddr(SSLUtilHandle, 'OPENSSL_add_all_algorithms_noconf');
- _CRYPTOcleanupAllExData := GetProcAddr(SSLUtilHandle, 'CRYPTO_cleanup_all_ex_data');
- _RandScreen := GetProcAddr(SSLUtilHandle, 'RAND_screen');
- _BioNew := GetProcAddr(SSLUtilHandle, 'BIO_new');
- _BioFreeAll := GetProcAddr(SSLUtilHandle, 'BIO_free_all');
- _BioSMem := GetProcAddr(SSLUtilHandle, 'BIO_s_mem');
- _BioCtrlPending := GetProcAddr(SSLUtilHandle, 'BIO_ctrl_pending');
- _BioRead := GetProcAddr(SSLUtilHandle, 'BIO_read');
- _BioWrite := GetProcAddr(SSLUtilHandle, 'BIO_write');
- _d2iPKCS12bio := GetProcAddr(SSLUtilHandle, 'd2i_PKCS12_bio');
- _PKCS12parse := GetProcAddr(SSLUtilHandle, 'PKCS12_parse');
- _PKCS12free := GetProcAddr(SSLUtilHandle, 'PKCS12_free');
- _RsaGenerateKey := GetProcAddr(SSLUtilHandle, 'RSA_generate_key');
- _Asn1UtctimeNew := GetProcAddr(SSLUtilHandle, 'ASN1_UTCTIME_new');
- _Asn1UtctimeFree := GetProcAddr(SSLUtilHandle, 'ASN1_UTCTIME_free');
- _Asn1cIntSet := GetProcAddr(SSLUtilHandle, 'ASN1_cInt_set');
- _i2dX509bio := GetProcAddr(SSLUtilHandle, 'i2d_X509_bio');
- _i2dPrivateKeyBio := GetProcAddr(SSLUtilHandle, 'i2d_PrivateKey_bio');
- // 3DES functions
- _DESsetoddparity := GetProcAddr(SSLUtilHandle, 'DES_set_odd_parity');
- _DESsetkeychecked := GetProcAddr(SSLUtilHandle, 'DES_set_key_checked');
- _DESecbencrypt := GetProcAddr(SSLUtilHandle, 'DES_ecb_encrypt');
- //
- _CRYPTOnumlocks := GetProcAddr(SSLUtilHandle, 'CRYPTO_num_locks');
- _CRYPTOsetlockingcallback := GetProcAddr(SSLUtilHandle, 'CRYPTO_set_locking_callback');
- //init library
- if assigned(_SslLibraryInit) then
- _SslLibraryInit;
- if assigned(_SslLoadErrorStrings) then
- _SslLoadErrorStrings;
- if assigned(_OPENSSLaddallalgorithms) then
- _OPENSSLaddallalgorithms;
- if assigned(_RandScreen) then
- _RandScreen;
- Result := True;
- SSLloaded := True;
- end
- else
- begin
- //load failed!
- if SSLLibHandle <> 0 then
- begin
- FreeLibrary(SSLLibHandle);
- SSLLibHandle := 0;
- end;
- if SSLUtilHandle <> 0 then
- begin
- FreeLibrary(SSLUtilHandle);
- SSLLibHandle := 0;
- end;
- Result := False;
- end;
- end
- else
- //loaded before...
- Result := true;
- end;
- function DestroySSLInterface: Boolean;
- begin
- if IsSSLLoaded then
- begin
- //deinit library
- EVPCleanup;
- CRYPTOcleanupAllExData;
- ErrRemoveState(0);
- end;
- SSLloaded := false;
- if SSLLibHandle <> 0 then
- begin
- FreeLibrary(SSLLibHandle);
- SSLLibHandle := 0;
- end;
- if SSLUtilHandle <> 0 then
- begin
- FreeLibrary(SSLUtilHandle);
- SSLLibHandle := 0;
- end;
- _SslGetError := nil;
- _SslLibraryInit := nil;
- _SslLoadErrorStrings := nil;
- _SslCtxSetCipherList := nil;
- _SslCtxNew := nil;
- _SslCtxFree := nil;
- _SslSetFd := nil;
- _SslMethodV2 := nil;
- _SslMethodV3 := nil;
- _SslMethodTLSV1 := nil;
- _SslMethodV23 := nil;
- _SslCtxUsePrivateKey := nil;
- _SslCtxUsePrivateKeyASN1 := nil;
- _SslCtxUsePrivateKeyFile := nil;
- _SslCtxUseCertificate := nil;
- _SslCtxUseCertificateASN1 := nil;
- _SslCtxUseCertificateFile := nil;
- _SslCtxUseCertificateChainFile := nil;
- _SslCtxCheckPrivateKeyFile := nil;
- _SslCtxSetDefaultPasswdCb := nil;
- _SslCtxSetDefaultPasswdCbUserdata := nil;
- _SslCtxLoadVerifyLocations := nil;
- _SslNew := nil;
- _SslFree := nil;
- _SslAccept := nil;
- _SslConnect := nil;
- _SslShutdown := nil;
- _SslRead := nil;
- _SslPeek := nil;
- _SslWrite := nil;
- _SslPending := nil;
- _SslGetPeerCertificate := nil;
- _SslGetVersion := nil;
- _SslCtxSetVerify := nil;
- _SslGetCurrentCipher := nil;
- _SslCipherGetName := nil;
- _SslCipherGetBits := nil;
- _SslGetVerifyResult := nil;
- _X509New := nil;
- _X509Free := nil;
- _X509NameOneline := nil;
- _X509GetSubjectName := nil;
- _X509GetIssuerName := nil;
- _X509NameHash := nil;
- _X509Digest := nil;
- _X509print := nil;
- _X509SetVersion := nil;
- _X509SetPubkey := nil;
- _X509SetIssuerName := nil;
- _X509NameAddEntryByTxt := nil;
- _X509Sign := nil;
- _X509GmtimeAdj := nil;
- _X509SetNotBefore := nil;
- _X509SetNotAfter := nil;
- _X509GetSerialNumber := nil;
- _EvpPkeyNew := nil;
- _EvpPkeyFree := nil;
- _EvpPkeyAssign := nil;
- _EVPCleanup := nil;
- _EvpGetDigestByName := nil;
- _SSLeayversion := nil;
- _ErrErrorString := nil;
- _ErrGetError := nil;
- _ErrClearError := nil;
- _ErrFreeStrings := nil;
- _ErrRemoveState := nil;
- _OPENSSLaddallalgorithms := nil;
- _CRYPTOcleanupAllExData := nil;
- _RandScreen := nil;
- _BioNew := nil;
- _BioFreeAll := nil;
- _BioSMem := nil;
- _BioCtrlPending := nil;
- _BioRead := nil;
- _BioWrite := nil;
- _d2iPKCS12bio := nil;
- _PKCS12parse := nil;
- _PKCS12free := nil;
- _RsaGenerateKey := nil;
- _Asn1UtctimeNew := nil;
- _Asn1UtctimeFree := nil;
- _Asn1cIntSet := nil;
- _i2dX509bio := nil;
- _i2dPrivateKeyBio := nil;
- // 3DES functions
- _DESsetoddparity := nil;
- _DESsetkeychecked := nil;
- _DESecbencrypt := nil;
- //
- _CRYPTOnumlocks := nil;
- _CRYPTOsetlockingcallback := nil;
- Result := True;
- end;
- function IsSSLloaded: Boolean;
- begin
- Result := SSLLoaded;
- end;
- finalization
- DestroySSLInterface;
- end.
|