├── .gitattributes ├── .gitignore ├── .gitmodules ├── LICENSE ├── README.md ├── contrib ├── cHttpDownload.cls ├── cHttpRequest.cls ├── cRateLimiter.cls ├── cTlsRemaster.cls ├── ctxWinsock.ctl ├── dll │ ├── HttpRequest.cmp │ ├── HttpRequest.vbp │ ├── WinsockTls.cmp │ ├── WinsockTls.vbp │ ├── sign_HttpRequest.bat │ └── sign_WinsockTls.bat └── obsolete │ ├── cTlsClient.cls │ └── cWinSockRequest.cls ├── lib ├── ISRG_Root_X1.cer ├── libsodium.dll └── thunks │ ├── aes.c │ ├── blockwise.c │ ├── cf_inlines.h │ ├── chacha20.c │ ├── chacha20poly1305.c │ ├── codegen │ ├── Form1.frm │ ├── Module1.bas │ └── Project1.vbp │ ├── curve25519.c │ ├── ecc.c │ ├── ecc.h │ ├── ecc384.c │ ├── ecc384.h │ ├── gcm.c │ ├── gf128.c │ ├── mini-gmp.c │ ├── mini-gmp.h │ ├── modes.c │ ├── poly1305.c │ ├── rsa.c │ ├── sha256.c │ ├── sha512.c │ ├── sshaes.c │ ├── sshbn.c │ ├── sshbn.h │ ├── thunks.cpp │ ├── thunks.sln │ ├── thunks.vcxproj │ ├── thunks.vcxproj.filters │ ├── tinflate.c │ ├── win32_crt.cpp │ └── win32_crt_float.cpp ├── samples ├── README.md ├── TlsSocketTest │ ├── TlsSocket.frm │ ├── TlsSocket.vbp │ └── cClientRequest.cls ├── Winsock-simple │ ├── Client │ │ ├── Form1.frm │ │ ├── Form1.frx │ │ └── Project1.vbp │ └── Server │ │ ├── Form1.frm │ │ └── Project1.vbp └── vbscript examples │ ├── GetGoogle.vbs │ └── TwitchVIP.vbs ├── src ├── cAsyncSocket.cls ├── cTlsSocket.cls ├── mdTlsNative.bas ├── mdTlsSodium.bas └── mdTlsThunks.bas └── test ├── BareboneTls ├── Form1.frm ├── Form2.frm ├── Module1.bas ├── Module2.bas ├── Project1.vbp ├── Project2.vbp ├── Project3.vbp └── Project4.vbp ├── Basic ├── Form1.frm ├── Module1.bas ├── Project1.vbp ├── Project2.vbp └── ca-bundle.pem ├── Chat ├── Project1.vbp ├── cClientInfo.cls ├── frmClient.frm ├── frmClient.frx ├── frmMain.frm ├── frmServer.frm ├── frmServer.frx └── mdGlobals.bas ├── Crypto ├── Form1.frm ├── Form1.frx ├── Project1.vbp ├── mdGlobals.bas ├── mdJson.bas └── top-sites.json ├── Secure ├── ClientCerts │ ├── ca.key │ ├── ca.pem │ ├── client1.csr │ ├── client1.key │ ├── client1.pem │ ├── client1.pfx │ ├── client2.csr │ ├── client2.key │ ├── client2.pem │ ├── gen_ca.bat │ ├── gen_client.bat │ └── rsa │ │ ├── ca_rsa.key │ │ ├── ca_rsa.pem │ │ ├── client2.csr │ │ ├── client2.full.pfx │ │ ├── client2.key │ │ ├── client2.pem │ │ ├── gen_ca_rsa.bat │ │ └── gen_client_rsa.bat ├── Form2.frm ├── Project1.vbp ├── Project2.vbp ├── Project3.vbp ├── cRequestHandler.cls ├── ca-bundle.pem ├── ca-bundle.pfx ├── client1.full.pfx ├── client2.full.pfx ├── drag_to_view_pfx.bat ├── eccert.pem ├── eccert.pfx ├── ecprivkey.pem ├── mdGlobals.bas └── test_conn_p521.bat └── Winsock ├── Form1.frm ├── Form2.frm ├── Form3.frm ├── Group3.vbg ├── Project1.vbp ├── Project2.vbp ├── Project3.vbp ├── cClientCallback.cls └── frmRemaster.frm /.gitattributes: -------------------------------------------------------------------------------- 1 | # VB6 source files (show diff + keep CRLF in zip download) 2 | 3 | *.bas working-tree-encoding=CP1251 text eol=crlf linguist-language=vb6 4 | *.cls working-tree-encoding=CP1251 text eol=crlf linguist-language=vb6 5 | *.ctl working-tree-encoding=CP1251 text eol=crlf linguist-language=vb6 6 | *.dob working-tree-encoding=CP1251 text eol=crlf linguist-language=vb6 7 | *.dsr working-tree-encoding=CP1251 text eol=crlf linguist-language=vb6 8 | *.frm working-tree-encoding=CP1251 text eol=crlf linguist-language=vb6 9 | *.pag working-tree-encoding=CP1251 text eol=crlf linguist-language=vb6 10 | *.vbg working-tree-encoding=CP1251 text eol=crlf 11 | *.vbl working-tree-encoding=CP1251 text eol=crlf 12 | *.vbp working-tree-encoding=CP1251 text eol=crlf 13 | *.vbr working-tree-encoding=CP1251 text eol=crlf 14 | *.vbw working-tree-encoding=CP1251 text eol=crlf 15 | *.vbs working-tree-encoding=CP1251 text eol=crlf 16 | 17 | # Other source files (show diff + LF only in zip download) 18 | 19 | *.asm text 20 | *.asp text 21 | *.bat text 22 | *.c text 23 | *.cpp text 24 | *.dsp text 25 | *.dsw text 26 | *.h text 27 | *.idl text 28 | *.java text 29 | *.js text 30 | *.manifest text 31 | *.odl text 32 | *.php text 33 | *.php3 text 34 | *.rc text 35 | *.sln text 36 | *.sql text 37 | *.vb text 38 | *.vbs text 39 | 40 | # Binary 41 | 42 | *.res binary 43 | *.frx binary 44 | *.ctx binary 45 | *.dsx binary 46 | *.exe binary 47 | *.dll binary 48 | *.ocx binary 49 | *.cmp binary 50 | *.pdb binary 51 | *.tlb binary 52 | *.xls binary 53 | *.doc binary 54 | *.ppt binary 55 | *.xlsx binary 56 | *.docx binary 57 | *.pptx binary 58 | *.chm binary 59 | *.hlp binary 60 | *.jpg binary 61 | *.png binary 62 | *.bmp binary 63 | *.gif binary 64 | *.ico binary 65 | *.zip binary 66 | *.cab binary 67 | *.7z binary 68 | *.gz binary 69 | *.cobj binary 70 | *.pfx binary 71 | *.cer binary 72 | 73 | # Text files but keep as binary (no diff) 74 | 75 | # *.cfg text 76 | # *.conf text 77 | # *.csi text 78 | # *.css text 79 | # *.csv text 80 | # *.def text 81 | # *.htm text 82 | # *.html text 83 | # *.inf text 84 | # *.ini text 85 | # *.log text 86 | # *.reg text 87 | # *.rtf text 88 | # *.txt text 89 | # *.url text 90 | # *.xml text 91 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## VB6 workspace 2 | 3 | *.vbw 4 | *.bak 5 | *.log 6 | *.scc 7 | *.zip 8 | *.7z 9 | *.exp 10 | *.lib 11 | *.obj 12 | *.dll 13 | *.exe 14 | *.pdb 15 | *.ocx 16 | *.oca 17 | *.tmp 18 | *.twinproj 19 | **/_del/* 20 | **/SodiumTest - Copy/* 21 | ~* 22 | *.xlsb 23 | 24 | # Build results 25 | [Dd]ebug/ 26 | [Dd]ebugPublic/ 27 | [Rr]elease/ 28 | [Rr]eleases/ 29 | x64/ 30 | x86/ 31 | [Ww][Ii][Nn]32/ 32 | [Aa][Rr][Mm]/ 33 | [Aa][Rr][Mm]64/ 34 | bld/ 35 | [Bb]in/ 36 | [Oo]bj/ 37 | [Ll]og/ 38 | [Ll]ogs/ 39 | 40 | # Visual C++ cache files 41 | ipch/ 42 | *.aps 43 | *.ncb 44 | *.opendb 45 | *.opensdf 46 | *.sdf 47 | *.cachefile 48 | *.VC.db 49 | *.VC.VC.opendb 50 | *.suo 51 | *.user 52 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "test/Crypto/wycheproof"] 2 | path = test/Crypto/wycheproof 3 | url = https://github.com/google/wycheproof 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018-2019 Vladimir Vissoultchev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## VbAsyncSocket 2 | 3 | Simple and thin WinSock API wrappers for VB6 loosly based on the original [`CAsyncSocket`](https://docs.microsoft.com/en-us/cpp/mfc/reference/casyncsocket-class?view=vs-2017) wrapper in [`MFC`](https://docs.microsoft.com/en-us/cpp/mfc/mfc-and-atl?view=vs-2017). 4 | 5 | ### Description 6 | 7 | Base class `cAsyncSocket` wraps OS non-blocking sockets that can be used to implement various network components in VB6 -- clients and servers -- and supports both async and blocking network communications. 8 | 9 | Additionally there is a source-compatible `cTlsSocket` class for transparent TLS transport layer encryption with several crypto backend implementations: 10 | 11 | 1. `mdTlsThunks` is a pure VB6 with ASM thunks implementation for TLS 1.3 and (legacy) TLS 1.2 client-side and server-side support with no dependency on external libraries (like openssl) 12 | 13 | 2. `mdTlsNative` is a native client-side and server-side TLS support using OS provided SSPI/Schannel library for all available protocol versions. 14 | 15 | 3. `mdTlsSodium` is a stripped down compact backend with dependency on libsodium for crypto primitives (no ASM thunking used) with a total compiled size of 64KB. 16 | 17 | The VB6 with thunks backend implementation auto-detects AES-NI and PCLMULQDQ instruction set availability on client machine and switches to [performance optimized implementation of AES](https://github.com/wqweto/VbAsyncSocket/blob/4b7f4d8bc650688e2b6ad5460c997ed1df26d2e0/lib/thunks/sshaes.c#L100-L240)[-GCM](https://github.com/wqweto/VbAsyncSocket/blob/4b7f4d8bc650688e2b6ad5460c997ed1df26d2e0/lib/thunks/gf128.c#L116-L165) which is even faster that OS native SSPI/Schannel implementation of this cipher suit. The VB6 with thunks backend and native backend support legacy OSes up to NT 4.0 and Windows 98 while libsodium DLL is compiled with XP support only. 18 | 19 | ### Usage 20 | 21 | Start by including `src\cAsyncSocket.cls` in your project to have a convenient wrapper of most WinSock API functions. 22 | 23 | Optionally you can add `src\cTlsSocket.cls` and `src\mdTlsThunks.bas` pair of source files to your project for TLS secured connections using VB6 with thunks backend or add `src\cTlsSocket.cls` and `src\mdTlsNative.bas` pair of source files for an alternative backend using native OS provided SSPI/Schannel library. 24 | 25 | #### WinHttpRequest Replacement Class 26 | 27 | Start by including `src\cAsyncSocket.cls`, `src\cTlsSocket.cls` and `src\mdTlsThunks.bas` backend for TLS support (or any other backend) and finally add `contrib\cHttpRequest.cls` for the TLS 1.3 capable source-compatible replacement class. 28 | 29 | Notice that the original `Open` method and `Option` property of the `WinHttpRequest` object have been suffixed with an underscore (`_`) in the replacement implementation (a limitation of the VB6 IDE) so some source-code fixes will be required to integrate the replacement `cHttpRequest` class. 30 | 31 | #### Sample SMTP with STARTTLS 32 | 33 | Here is a working sample with error checking omitted for brevity for accessing smtp.gmail.com over port 587. 34 | 35 | At first the communication goes over unencrypted plain-text socket, then later it is switched to TLS secured one before issuing the final `QUIT` command. 36 | 37 | With New cTlsSocket 38 | .SyncConnect "smtp.gmail.com", 587, UseTls:=False 39 | Debug.Print .SyncReceiveText(); 40 | .SyncSendText "HELO 127.0.0.1" & vbCrLf 41 | Debug.Print .SyncReceiveText(); 42 | .SyncSendText "STARTTLS" & vbCrLf 43 | Debug.Print .SyncReceiveText(); 44 | .SyncStartTls "smtp.gmail.com" 45 | Debug.Print "TLS handshake complete: " & .RemoteHostName 46 | .SyncSendText "QUIT" & vbCrLf 47 | Debug.Print .SyncReceiveText(); 48 | End With 49 | 50 | Which produces debug output in `Immediate Window` similar to this: 51 | 52 | 220 smtp.gmail.com ESMTP c69sm2955334lfg.23 - gsmtp 53 | 250 smtp.gmail.com at your service 54 | 220 2.0.0 Ready to start TLS 55 | 1428790.043 [INFO] Using TLS_AES_128_GCM_SHA256 from smtp.gmail.com [mdTlsThunks.pvTlsParseHandshakeServerHello] 56 | 1428790.057 [INFO] Valid ECDSA_SECP256R1_SHA256 signature [mdTlsThunks.pvTlsSignatureVerify] 57 | TLS handshake complete: smtp.gmail.com 58 | 221 2.0.0 closing connection c69sm2955334lfg.23 - gsmtp 59 | 60 | ### Is it any good? 61 | 62 | [Yes](https://news.ycombinator.com/item?id=3067434). 63 | 64 | ### Implemented Cipher Suites 65 | 66 | This list includes cipher suites as implemented in the ASM thunks backend while the native backend list depends on the OS version and SSPI/Schannel settings. 67 | 68 | Cipher Suite | First In | Selection String | Notes 69 | --|--|--|-- 70 | TLS_AES_128_GCM_SHA256 |TLS 1.3|EECDH+AESGCM|AEAD 71 | TLS_AES_256_GCM_SHA384 |TLS 1.3|EECDH+AESGCM|AEAD 72 | TLS_CHACHA20_POLY1305_SHA256 |TLS 1.3|EECDH+AESGCM|AEAD 73 | TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 |TLS 1.2|EECDH+AESGCM|AEAD 74 | TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 |TLS 1.2|EECDH+AESGCM|AEAD 75 | TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 |TLS 1.2|EECDH+AESGCM|AEAD 76 | TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 |TLS 1.2|EECDH+AESGCM|AEAD 77 | TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 |TLS 1.2|EECDH+CHACHA20|AEAD 78 | TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 |TLS 1.2|EECDH+CHACHA20|AEAD 79 | TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 |TLS 1.2|EECDH+AES+SHA256|Weak, Exotic 80 | TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 |TLS 1.2|EECDH+AES+SHA256|Weak, Exotic 81 | TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 |TLS 1.2|EECDH+AES+SHA384|Weak, Exotic 82 | TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 |TLS 1.2|EECDH+AES+SHA384|Weak, Exotic 83 | TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA |TLSv1|EECDH+AES+SHA1|Weak, HMAC-SHA1 84 | TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA |TLSv1|EECDH+AES+SHA1|Weak, HMAC-SHA1 85 | TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA |TLSv1|EECDH+AES+SHA1|Weak, HMAC-SHA1 86 | TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA |TLSv1|EECDH+AES+SHA1|Weak, HMAC-SHA1 87 | TLS_RSA_WITH_AES_128_GCM_SHA256 |TLS 1.2|RSA+AESGCM|Weak, No FS 88 | TLS_RSA_WITH_AES_256_GCM_SHA384 |TLS 1.2|RSA+AESGCM|Weak, No FS 89 | TLS_RSA_WITH_AES_128_CBC_SHA256 |TLS 1.2|RSA+AES+SHA256|Weak, No FS, Exotic 90 | TLS_RSA_WITH_AES_256_CBC_SHA256 |TLS 1.2|RSA+AES+SHA256|Weak, No FS, Exotic 91 | TLS_RSA_WITH_AES_128_CBC_SHA |SSLv3|RSA+AES+SHA1|Weak, No FS, HMAC-SHA1 92 | TLS_RSA_WITH_AES_256_CBC_SHA |SSLv3|RSA+AES+SHA1|Weak, No FS, HMAC-SHA1 93 | 94 | Note that "exotic" cipher suites are included behind a conditional compilation flag only (off by default). 95 | 96 | ### Legacy Support 97 | 98 | Screenshot from Windows 98 Second Edition running on Pentium II 99 | 100 | [](https://dl.unicontsoft.com/upload/pix/ss_vbasyncsocket_win9x.png) 101 | 102 | ### ToDo 103 | 104 | - [x] Allow client to assign client certificate for connection 105 | - [x] Provide UI for end-user to choose suitable certificates from Personal certificate store 106 | - [x] Add wrapper for http protocol 107 | - [ ] Add wrapper for ftp protocol 108 | - [x] Add WinSock control replacement 109 | - [ ] Add more samples (incl. `vbcurl.exe` utility) 110 | - [x] Refactor subclassing thunk to use msg queue not to re-enter IDE in debug mode 111 | -------------------------------------------------------------------------------- /contrib/dll/HttpRequest.cmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wqweto/VbAsyncSocket/f5e922585800e05d42be166fa8301007a24fee94/contrib/dll/HttpRequest.cmp -------------------------------------------------------------------------------- /contrib/dll/HttpRequest.vbp: -------------------------------------------------------------------------------- 1 | Type=OleDll 2 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 3 | Class=cAsyncSocket; ..\..\src\cAsyncSocket.cls 4 | Class=cTlsSocket; ..\..\src\cTlsSocket.cls 5 | Module=mdTlsThunks; ..\..\src\mdTlsThunks.bas 6 | Class=cHttpRequest; ..\cHttpRequest.cls 7 | Startup="(None)" 8 | HelpFile="" 9 | Title="HttpRequest" 10 | ExeName32="HttpRequest.dll" 11 | Command32="" 12 | Name="HttpRequest" 13 | HelpContextID="0" 14 | Description="Http Request Replacement (1.0.7)" 15 | CompatibleMode="2" 16 | CompatibleEXE32="HttpRequest.cmp" 17 | VersionCompatible32="1" 18 | MajorVer=1 19 | MinorVer=0 20 | RevisionVer=7 21 | AutoIncrementVer=0 22 | ServerSupportFiles=0 23 | DllBaseAddress=&H26e20000 24 | VersionCompanyName="wqweto@gmail.com" 25 | VersionFileDescription="Http Request Replacement (1.0.7)" 26 | VersionProductName="HttpRequest" 27 | CompilationType=0 28 | OptimizationType=0 29 | FavorPentiumPro(tm)=0 30 | CodeViewDebugInfo=0 31 | NoAliasing=-1 32 | BoundsCheck=-1 33 | OverflowCheck=-1 34 | FlPointCheck=-1 35 | FDIVCheck=-1 36 | UnroundedFP=-1 37 | StartMode=1 38 | Unattended=0 39 | Retained=0 40 | ThreadPerObject=0 41 | MaxNumberOfThreads=1 42 | ThreadingModel=1 43 | DebugStartupOption=0 44 | -------------------------------------------------------------------------------- /contrib/dll/WinsockTls.cmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wqweto/VbAsyncSocket/f5e922585800e05d42be166fa8301007a24fee94/contrib/dll/WinsockTls.cmp -------------------------------------------------------------------------------- /contrib/dll/WinsockTls.vbp: -------------------------------------------------------------------------------- 1 | Type=Control 2 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 3 | Class=cAsyncSocket; ..\..\src\cAsyncSocket.cls 4 | Class=cTlsSocket; ..\..\src\cTlsSocket.cls 5 | Module=mdTlsNative; ..\..\src\mdTlsNative.bas 6 | UserControl=..\ctxWinsock.ctl 7 | Startup="(None)" 8 | HelpFile="" 9 | Title="WinsockTls" 10 | ExeName32="WinsockTls.ocx" 11 | Command32="" 12 | Name="WinsockTls" 13 | HelpContextID="0" 14 | Description="Winsock Control Replacement (1.0.7)" 15 | CompatibleMode="2" 16 | CompatibleEXE32="WinsockTls.cmp" 17 | VersionCompatible32="1" 18 | MajorVer=1 19 | MinorVer=0 20 | RevisionVer=7 21 | AutoIncrementVer=0 22 | ServerSupportFiles=0 23 | DllBaseAddress=&H27e20000 24 | VersionCompanyName="wqweto@gmail.com" 25 | VersionFileDescription="Winsock Control Replacement (1.0.7)" 26 | VersionProductName="WinsockTls" 27 | CompilationType=0 28 | OptimizationType=0 29 | FavorPentiumPro(tm)=0 30 | CodeViewDebugInfo=0 31 | NoAliasing=-1 32 | BoundsCheck=-1 33 | OverflowCheck=-1 34 | FlPointCheck=-1 35 | FDIVCheck=-1 36 | UnroundedFP=-1 37 | StartMode=1 38 | Unattended=0 39 | Retained=0 40 | ThreadPerObject=0 41 | MaxNumberOfThreads=1 42 | ThreadingModel=1 43 | DebugStartupOption=1 44 | DebugStartupComponent=ctxWinsock 45 | -------------------------------------------------------------------------------- /contrib/dll/sign_HttpRequest.bat: -------------------------------------------------------------------------------- 1 | call C:\Work\BuildTools\Certificates\codesign.bat /d "Http Request Replacement" %~dp0HttpRequest.dll 2 | -------------------------------------------------------------------------------- /contrib/dll/sign_WinsockTls.bat: -------------------------------------------------------------------------------- 1 | call C:\Work\BuildTools\Certificates\codesign.bat /d "Winsock Control Replacement" %~dp0WinsockTls.ocx 2 | -------------------------------------------------------------------------------- /lib/ISRG_Root_X1.cer: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wqweto/VbAsyncSocket/f5e922585800e05d42be166fa8301007a24fee94/lib/ISRG_Root_X1.cer -------------------------------------------------------------------------------- /lib/libsodium.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wqweto/VbAsyncSocket/f5e922585800e05d42be166fa8301007a24fee94/lib/libsodium.dll -------------------------------------------------------------------------------- /lib/thunks/blockwise.c: -------------------------------------------------------------------------------- 1 | /* 2 | * cifra - embedded cryptography library 3 | * Written in 2014 by Joseph Birr-Pixton 4 | * 5 | * To the extent possible under law, the author(s) have dedicated all 6 | * copyright and related and neighboring rights to this software to the 7 | * public domain worldwide. This software is distributed without any 8 | * warranty. 9 | * 10 | * You should have received a copy of the CC0 Public Domain Dedication 11 | * along with this software. If not, see 12 | * . 13 | */ 14 | 15 | /* Processing function for cf_blockwise_accumulate. */ 16 | typedef void (*cf_blockwise_in_fn)(void *ctx, const uint8_t *data); 17 | 18 | /* Processing function for cf_blockwise_xor. */ 19 | typedef void (*cf_blockwise_out_fn)(void *ctx, uint8_t *data); 20 | 21 | void cf_blockwise_accumulate_final(uint8_t *partial, size_t *npartial, size_t nblock, 22 | const void *inp, size_t nbytes, 23 | cf_blockwise_in_fn process, 24 | cf_blockwise_in_fn process_final, 25 | void *ctx); 26 | 27 | /** out = x ^ y. 28 | * out, x and y may alias. */ 29 | static inline void xor_bb(uint8_t *out, const uint8_t *x, const uint8_t *y, size_t len) 30 | { 31 | for (size_t i = 0; i < len; i++) 32 | out[i] = x[i] ^ y[i]; 33 | } 34 | 35 | static 36 | void cf_blockwise_accumulate(uint8_t *partial, size_t *npartial, size_t nblock, 37 | const void *inp, size_t nbytes, 38 | cf_blockwise_in_fn process, 39 | void *ctx) 40 | { 41 | cf_blockwise_accumulate_final(partial, npartial, nblock, 42 | inp, nbytes, 43 | process, process, ctx); 44 | } 45 | 46 | static 47 | void cf_blockwise_accumulate_final(uint8_t *partial, size_t *npartial, size_t nblock, 48 | const void *inp, size_t nbytes, 49 | cf_blockwise_in_fn process, 50 | cf_blockwise_in_fn process_final, 51 | void *ctx) 52 | { 53 | const uint8_t *bufin = (const uint8_t *)inp; 54 | assert(partial && *npartial < nblock); 55 | assert(inp || !nbytes); 56 | assert(process && ctx); 57 | 58 | /* If we have partial data, copy in to buffer. */ 59 | if (*npartial && nbytes) 60 | { 61 | size_t space = nblock - *npartial; 62 | size_t taken = MIN(space, nbytes); 63 | 64 | memcpy(partial + *npartial, bufin, taken); 65 | 66 | bufin += taken; 67 | nbytes -= taken; 68 | *npartial += taken; 69 | 70 | /* If that gives us a full block, process it. */ 71 | if (*npartial == nblock) 72 | { 73 | if (nbytes == 0) 74 | process_final(ctx, partial); 75 | else 76 | process(ctx, partial); 77 | *npartial = 0; 78 | } 79 | } 80 | 81 | /* now nbytes < nblock or *npartial == 0. */ 82 | 83 | /* If we have a full block of data, process it directly. */ 84 | while (nbytes >= nblock) 85 | { 86 | /* Partial buffer must be empty, or we're ignoring extant data */ 87 | assert(*npartial == 0); 88 | 89 | if (nbytes == nblock) 90 | process_final(ctx, bufin); 91 | else 92 | process(ctx, bufin); 93 | bufin += nblock; 94 | nbytes -= nblock; 95 | } 96 | 97 | /* Finally, if we have remaining data, buffer it. */ 98 | while (nbytes) 99 | { 100 | size_t space = nblock - *npartial; 101 | size_t taken = MIN(space, nbytes); 102 | 103 | memcpy(partial + *npartial, bufin, taken); 104 | 105 | bufin += taken; 106 | nbytes -= taken; 107 | *npartial += taken; 108 | 109 | /* If we started with *npartial, we must have copied it 110 | * in first. */ 111 | assert(*npartial < nblock); 112 | } 113 | } 114 | 115 | static 116 | void cf_blockwise_xor(uint8_t *partial, size_t *npartial, size_t nblock, 117 | const void *inp, void *outp, size_t nbytes, 118 | cf_blockwise_out_fn process, void *ctx) 119 | { 120 | const uint8_t *inb = (const uint8_t *)inp; 121 | uint8_t *outb = (uint8_t *)outp; 122 | 123 | assert(partial && *npartial < nblock); 124 | assert(inp || !nbytes); 125 | assert(process && ctx); 126 | 127 | while (nbytes) 128 | { 129 | /* If we're out of material, and need more, produce a block. */ 130 | if (*npartial == 0) 131 | { 132 | process(ctx, partial); 133 | *npartial = nblock; 134 | } 135 | 136 | size_t offset = nblock - *npartial; 137 | size_t taken = MIN(*npartial, nbytes); 138 | xor_bb(outb, inb, partial + offset, taken); 139 | *npartial -= taken; 140 | nbytes -= taken; 141 | outb += taken; 142 | inb += taken; 143 | } 144 | } 145 | 146 | static 147 | void cf_blockwise_acc_byte(uint8_t *partial, size_t *npartial, 148 | size_t nblock, 149 | uint8_t byte, size_t nbytes, 150 | cf_blockwise_in_fn process, 151 | void *ctx) 152 | { 153 | /* only memset the whole of the block once */ 154 | int filled = 0; 155 | 156 | while (nbytes) 157 | { 158 | size_t start = *npartial; 159 | size_t count = MIN(nbytes, nblock - start); 160 | 161 | if (!filled) 162 | memset(partial + start, byte, count); 163 | 164 | if (start == 0 && count == nblock) 165 | filled = 1; 166 | 167 | if (start + count == nblock) 168 | { 169 | process(ctx, partial); 170 | *npartial = 0; 171 | } else { 172 | *npartial += count; 173 | } 174 | 175 | nbytes -= count; 176 | } 177 | } 178 | 179 | static 180 | void cf_blockwise_acc_pad(uint8_t *partial, size_t *npartial, 181 | size_t nblock, 182 | uint8_t fbyte, uint8_t mbyte, uint8_t lbyte, 183 | size_t nbytes, 184 | cf_blockwise_in_fn process, 185 | void *ctx) 186 | { 187 | 188 | switch (nbytes) 189 | { 190 | case 0: break; 191 | case 1: fbyte ^= lbyte; 192 | cf_blockwise_accumulate(partial, npartial, nblock, &fbyte, 1, process, ctx); 193 | break; 194 | case 2: 195 | cf_blockwise_accumulate(partial, npartial, nblock, &fbyte, 1, process, ctx); 196 | cf_blockwise_accumulate(partial, npartial, nblock, &lbyte, 1, process, ctx); 197 | break; 198 | default: 199 | cf_blockwise_accumulate(partial, npartial, nblock, &fbyte, 1, process, ctx); 200 | 201 | /* If the middle and last bytes differ, then process the last byte separately. 202 | * Otherwise, just extend the middle block size. */ 203 | if (lbyte != mbyte) 204 | { 205 | cf_blockwise_acc_byte(partial, npartial, nblock, mbyte, nbytes - 2, process, ctx); 206 | cf_blockwise_accumulate(partial, npartial, nblock, &lbyte, 1, process, ctx); 207 | } else { 208 | cf_blockwise_acc_byte(partial, npartial, nblock, mbyte, nbytes - 1, process, ctx); 209 | } 210 | 211 | break; 212 | } 213 | } 214 | -------------------------------------------------------------------------------- /lib/thunks/cf_inlines.h: -------------------------------------------------------------------------------- 1 | /** Encode v as a 64-bit big endian quantity into buf. */ 2 | static inline void write64_be(uint64_t v, uint8_t buf[8]) 3 | { 4 | *buf++ = (v >> 56) & 0xff; 5 | *buf++ = (v >> 48) & 0xff; 6 | *buf++ = (v >> 40) & 0xff; 7 | *buf++ = (v >> 32) & 0xff; 8 | *buf++ = (v >> 24) & 0xff; 9 | *buf++ = (v >> 16) & 0xff; 10 | *buf++ = (v >> 8) & 0xff; 11 | *buf = v & 0xff; 12 | } 13 | 14 | /** Encode v as a 32-bit big endian quantity into buf. */ 15 | static inline void write32_be(uint32_t v, uint8_t buf[4]) 16 | { 17 | *buf++ = (v >> 24) & 0xff; 18 | *buf++ = (v >> 16) & 0xff; 19 | *buf++ = (v >> 8) & 0xff; 20 | *buf = v & 0xff; 21 | } 22 | 23 | /** Read 4 bytes from buf, as a 32-bit big endian quantity. */ 24 | static inline uint32_t read32_be(const uint8_t buf[4]) 25 | { 26 | return (buf[0] << 24) | 27 | (buf[1] << 16) | 28 | (buf[2] << 8) | 29 | (buf[3]); 30 | } 31 | 32 | /** Circularly rotate right x by n bits. 33 | * 0 > n > 32. */ 34 | static inline uint32_t rotr32(uint32_t x, unsigned n) 35 | { 36 | return (x >> n) | (x << (32 - n)); 37 | } 38 | 39 | /** Read 4 bytes from buf, as a 32-bit little endian quantity. */ 40 | static inline uint32_t read32_le(const uint8_t buf[4]) 41 | { 42 | return (buf[3] << 24) | 43 | (buf[2] << 16) | 44 | (buf[1] << 8) | 45 | (buf[0]); 46 | } 47 | 48 | /** Read 8 bytes from buf, as a 64-bit big endian quantity. */ 49 | static inline uint64_t read64_be(const uint8_t buf[8]) 50 | { 51 | uint32_t hi = read32_be(buf), 52 | lo = read32_be(buf + 4); 53 | return ((uint64_t)hi) << 32 | 54 | lo; 55 | } 56 | 57 | /** Circularly rotate right x by n bits. 58 | * 0 > n > 64. */ 59 | static inline uint64_t rotr64(uint64_t x, unsigned n) 60 | { 61 | return (x >> n) | (x << (64 - n)); 62 | } 63 | 64 | /** Increments the integer stored at v (of non-zero length len) 65 | * with the least significant byte first. */ 66 | static inline void incr_le(uint8_t *v, size_t len) 67 | { 68 | size_t i = 0; 69 | while (1) 70 | { 71 | if (++v[i] != 0) 72 | return; 73 | i++; 74 | if (i == len) 75 | return; 76 | } 77 | } 78 | 79 | /** Circularly rotate left x by n bits. 80 | * 0 > n > 32. */ 81 | static inline uint32_t rotl32(uint32_t x, unsigned n) 82 | { 83 | return (x << n) | (x >> (32 - n)); 84 | } 85 | 86 | /** Encode v as a 32-bit little endian quantity into buf. */ 87 | static inline void write32_le(uint32_t v, uint8_t buf[4]) 88 | { 89 | *buf++ = v & 0xff; 90 | *buf++ = (v >> 8) & 0xff; 91 | *buf++ = (v >> 16) & 0xff; 92 | *buf = (v >> 24) & 0xff; 93 | } 94 | 95 | /** Produce 0xffffffff if x == y, zero otherwise, without branching. */ 96 | static inline uint32_t mask_u32(uint32_t x, uint32_t y) 97 | { 98 | uint32_t diff = x ^ y; 99 | uint32_t diff_is_zero = ~diff & (diff - 1); 100 | return ~(diff_is_zero >> 31) + 1; 101 | } 102 | 103 | /** Like memset(ptr, 0, len), but not allowed to be removed by 104 | * compilers. */ 105 | static inline void mem_clean(volatile void *v, size_t len) 106 | { 107 | if (len) 108 | { 109 | memset((void *) v, 0, len); 110 | (void) *((volatile uint8_t *) v); 111 | } 112 | } 113 | 114 | /** Returns 1 if len bytes at va equal len bytes at vb, 0 if they do not. 115 | * Does not leak length of common prefix through timing. */ 116 | static inline unsigned mem_eq(const void *va, const void *vb, size_t len) 117 | { 118 | const volatile uint8_t *a = (const volatile uint8_t *)va; 119 | const volatile uint8_t *b = (const volatile uint8_t *)vb; 120 | uint8_t diff = 0; 121 | 122 | while (len--) 123 | { 124 | diff |= *a++ ^ *b++; 125 | } 126 | 127 | return !diff; 128 | } 129 | 130 | /** Encode v as a 64-bit little endian quantity into buf. */ 131 | static inline void write64_le(uint64_t v, uint8_t buf[8]) 132 | { 133 | *buf++ = v & 0xff; 134 | *buf++ = (v >> 8) & 0xff; 135 | *buf++ = (v >> 16) & 0xff; 136 | *buf++ = (v >> 24) & 0xff; 137 | *buf++ = (v >> 32) & 0xff; 138 | *buf++ = (v >> 40) & 0xff; 139 | *buf++ = (v >> 48) & 0xff; 140 | *buf = (v >> 56) & 0xff; 141 | } 142 | 143 | /* out ^= x 144 | * out and x may alias. */ 145 | static inline void xor_words(uint32_t *out, const uint32_t *x, size_t nwords) 146 | { 147 | for (size_t i = 0; i < nwords; i++) 148 | out[i] ^= x[i]; 149 | } 150 | 151 | /** Increments the integer stored at v (of non-zero length len) 152 | * with the most significant byte last. */ 153 | static inline void incr_be(uint8_t *v, size_t len) 154 | { 155 | len--; 156 | while (1) 157 | { 158 | if (++v[len] != 0) 159 | return; 160 | if (len == 0) 161 | return; 162 | len--; 163 | } 164 | } 165 | -------------------------------------------------------------------------------- /lib/thunks/chacha20.c: -------------------------------------------------------------------------------- 1 | /* 2 | * cifra - embedded cryptography library 3 | * Written in 2014 by Joseph Birr-Pixton 4 | * 5 | * To the extent possible under law, the author(s) have dedicated all 6 | * copyright and related and neighboring rights to this software to the 7 | * public domain worldwide. This software is distributed without any 8 | * warranty. 9 | * 10 | * You should have received a copy of the CC0 Public Domain Dedication 11 | * along with this software. If not, see 12 | * . 13 | */ 14 | 15 | typedef struct 16 | { 17 | uint8_t key0[16], key1[16]; 18 | uint8_t nonce[16]; 19 | const uint8_t *constant; 20 | uint8_t block[64]; 21 | size_t nblock; 22 | size_t ncounter; 23 | } cf_salsa20_ctx, cf_chacha20_ctx; 24 | 25 | static 26 | void cf_chacha20_core(const uint8_t key0[16], 27 | const uint8_t key1[16], 28 | const uint8_t nonce[16], 29 | const uint8_t constant[16], 30 | uint8_t out[64]) 31 | { 32 | uint32_t z0, z1, z2, z3, z4, z5, z6, z7, 33 | z8, z9, za, zb, zc, zd, ze, zf; 34 | 35 | uint32_t x0 = z0 = read32_le(constant + 0), 36 | x1 = z1 = read32_le(constant + 4), 37 | x2 = z2 = read32_le(constant + 8), 38 | x3 = z3 = read32_le(constant + 12), 39 | x4 = z4 = read32_le(key0 + 0), 40 | x5 = z5 = read32_le(key0 + 4), 41 | x6 = z6 = read32_le(key0 + 8), 42 | x7 = z7 = read32_le(key0 + 12), 43 | x8 = z8 = read32_le(key1 + 0), 44 | x9 = z9 = read32_le(key1 + 4), 45 | xa = za = read32_le(key1 + 8), 46 | xb = zb = read32_le(key1 + 12), 47 | xc = zc = read32_le(nonce + 0), 48 | xd = zd = read32_le(nonce + 4), 49 | xe = ze = read32_le(nonce + 8), 50 | xf = zf = read32_le(nonce + 12); 51 | 52 | #define QUARTER(a, b, c, d) \ 53 | a += b; d = rotl32(d ^ a, 16); \ 54 | c += d; b = rotl32(b ^ c, 12); \ 55 | a += b; d = rotl32(d ^ a, 8); \ 56 | c += d; b = rotl32(b ^ c, 7); 57 | 58 | for (int i = 0; i < 10; i++) 59 | { 60 | QUARTER(z0, z4, z8, zc); 61 | QUARTER(z1, z5, z9, zd); 62 | QUARTER(z2, z6, za, ze); 63 | QUARTER(z3, z7, zb, zf); 64 | QUARTER(z0, z5, za, zf); 65 | QUARTER(z1, z6, zb, zc); 66 | QUARTER(z2, z7, z8, zd); 67 | QUARTER(z3, z4, z9, ze); 68 | } 69 | 70 | x0 += z0; 71 | x1 += z1; 72 | x2 += z2; 73 | x3 += z3; 74 | x4 += z4; 75 | x5 += z5; 76 | x6 += z6; 77 | x7 += z7; 78 | x8 += z8; 79 | x9 += z9; 80 | xa += za; 81 | xb += zb; 82 | xc += zc; 83 | xd += zd; 84 | xe += ze; 85 | xf += zf; 86 | 87 | write32_le(x0, out + 0); 88 | write32_le(x1, out + 4); 89 | write32_le(x2, out + 8); 90 | write32_le(x3, out + 12); 91 | write32_le(x4, out + 16); 92 | write32_le(x5, out + 20); 93 | write32_le(x6, out + 24); 94 | write32_le(x7, out + 28); 95 | write32_le(x8, out + 32); 96 | write32_le(x9, out + 36); 97 | write32_le(xa, out + 40); 98 | write32_le(xb, out + 44); 99 | write32_le(xc, out + 48); 100 | write32_le(xd, out + 52); 101 | write32_le(xe, out + 56); 102 | write32_le(xf, out + 60); 103 | } 104 | 105 | static const uint8_t g_chacha20_tau[] = "expand 16-byte k"; 106 | static const uint8_t g_chacha20_sigma[] = "expand 32-byte k"; 107 | 108 | static void set_key(cf_chacha20_ctx *ctx, const uint8_t *key, size_t nkey) 109 | { 110 | switch (nkey) 111 | { 112 | case 16: 113 | memcpy(ctx->key0, key, 16); 114 | memcpy(ctx->key1, key, 16); 115 | ctx->constant = chacha20_tau; 116 | break; 117 | case 32: 118 | memcpy(ctx->key0, key, 16); 119 | memcpy(ctx->key1, key + 16, 16); 120 | ctx->constant = chacha20_sigma; 121 | break; 122 | default: 123 | abort(); 124 | } 125 | } 126 | 127 | static 128 | void cf_chacha20_init(cf_chacha20_ctx *ctx, const uint8_t *key, size_t nkey, const uint8_t nonce[8]) 129 | { 130 | set_key(ctx, key, nkey); 131 | memset(ctx->nonce, 0, sizeof ctx->nonce); 132 | memcpy(ctx->nonce + 8, nonce, 8); 133 | ctx->nblock = 0; 134 | ctx->ncounter = 8; 135 | } 136 | 137 | static 138 | void cf_chacha20_init_custom(cf_chacha20_ctx *ctx, const uint8_t *key, size_t nkey, 139 | const uint8_t nonce[16], size_t ncounter) 140 | { 141 | assert(ncounter > 0); 142 | set_key(ctx, key, nkey); 143 | memcpy(ctx->nonce, nonce, sizeof ctx->nonce); 144 | ctx->nblock = 0; 145 | ctx->ncounter = ncounter; 146 | } 147 | 148 | static void cf_chacha20_next_block(void *vctx, uint8_t *out) 149 | { 150 | cf_chacha20_ctx *ctx = (cf_chacha20_ctx *)vctx; 151 | cf_chacha20_core(ctx->key0, 152 | ctx->key1, 153 | ctx->nonce, 154 | ctx->constant, 155 | out); 156 | incr_le(ctx->nonce, ctx->ncounter); 157 | } 158 | 159 | static 160 | void cf_chacha20_cipher(cf_chacha20_ctx *ctx, const uint8_t *input, uint8_t *output, size_t bytes) 161 | { 162 | DECLARE_PFN(cf_blockwise_out_fn, cf_chacha20_next_block); 163 | cf_blockwise_xor(ctx->block, &ctx->nblock, 64, 164 | input, output, bytes, 165 | pfn_cf_chacha20_next_block, 166 | ctx); 167 | } 168 | 169 | #undef QUARTER 170 | -------------------------------------------------------------------------------- /lib/thunks/chacha20poly1305.c: -------------------------------------------------------------------------------- 1 | /* 2 | * cifra - embedded cryptography library 3 | * Written in 2014 by Joseph Birr-Pixton 4 | * 5 | * To the extent possible under law, the author(s) have dedicated all 6 | * copyright and related and neighboring rights to this software to the 7 | * public domain worldwide. This software is distributed without any 8 | * warranty. 9 | * 10 | * You should have received a copy of the CC0 Public Domain Dedication 11 | * along with this software. If not, see 12 | * . 13 | */ 14 | 15 | 16 | 17 | #define ENCRYPT 1 18 | #define DECRYPT 0 19 | 20 | #define SUCCESS 0 21 | #define FAILURE 1 22 | 23 | static int process(const uint8_t key[32], 24 | const uint8_t nonce[12], 25 | const uint8_t *header, size_t nheader, 26 | const uint8_t *input, size_t nbytes, 27 | uint8_t *output, 28 | int mode, 29 | uint8_t tag[16]) 30 | { 31 | /* First, generate the Poly1305 key by running ChaCha20 with the 32 | * given key and a zero counter. The first half of the 33 | * 64-byte output is the key. */ 34 | uint8_t fullnonce[16] = { 0 }; 35 | memcpy(fullnonce + 4, nonce, 12); 36 | 37 | uint8_t polykey[32] = { 0 }; 38 | cf_chacha20_ctx chacha; 39 | cf_chacha20_init_custom(&chacha, key, 32, fullnonce, 4); 40 | cf_chacha20_cipher(&chacha, polykey, polykey, sizeof polykey); 41 | 42 | /* Now initialise Poly1305. */ 43 | cf_poly1305 poly; 44 | cf_poly1305_init(&poly, polykey, polykey + 16); 45 | mem_clean(polykey, sizeof polykey); 46 | 47 | /* Discard next 32 bytes of chacha20 key stream. */ 48 | cf_chacha20_cipher(&chacha, polykey, polykey, sizeof polykey); 49 | mem_clean(polykey, sizeof polykey); 50 | 51 | /* The input to Poly1305 is: 52 | * AAD || pad(AAD) || cipher || pad(cipher) || len_64(aad) || len_64(cipher) */ 53 | uint8_t padbuf[16] = { 0 }; 54 | 55 | #define PADLEN(x) ((16 - ((x) & 0xf)) & 0xf) 56 | 57 | /* AAD || pad(AAD) */ 58 | cf_poly1305_update(&poly, header, nheader); 59 | cf_poly1305_update(&poly, padbuf, PADLEN(nheader)); 60 | 61 | /* || cipher */ 62 | if (mode == ENCRYPT) 63 | { 64 | /* If we're encrypting, we compute the ciphertext 65 | * before inputting it into the MAC. */ 66 | cf_chacha20_cipher(&chacha, input, output, nbytes); 67 | cf_poly1305_update(&poly, output, nbytes); 68 | } else { 69 | /* Otherwise: decryption -- input the ciphertext. 70 | * Delay actual decryption until we checked the MAC. */ 71 | cf_poly1305_update(&poly, input, nbytes); 72 | } 73 | 74 | /* || pad(cipher) */ 75 | cf_poly1305_update(&poly, padbuf, PADLEN(nbytes)); 76 | 77 | /* || len_64(aad) || len_64(cipher) */ 78 | write64_le(nheader, padbuf); 79 | write64_le(nbytes, padbuf + 8); 80 | cf_poly1305_update(&poly, padbuf, sizeof padbuf); 81 | 82 | /* MAC computation is now complete. */ 83 | 84 | if (mode == ENCRYPT) 85 | { 86 | cf_poly1305_finish(&poly, tag); 87 | mem_clean(&chacha, sizeof chacha); 88 | return SUCCESS; 89 | } 90 | 91 | /* Decrypt mode: calculate tag, and check it. 92 | * If it's correct, proceed with decryption. */ 93 | uint8_t checktag[16]; 94 | cf_poly1305_finish(&poly, checktag); 95 | 96 | if (mem_eq(checktag, tag, sizeof checktag)) 97 | { 98 | cf_chacha20_cipher(&chacha, input, output, nbytes); 99 | mem_clean(&chacha, sizeof chacha); 100 | mem_clean(checktag, sizeof checktag); 101 | return SUCCESS; 102 | } else { 103 | mem_clean(output, nbytes); 104 | mem_clean(&chacha, sizeof chacha); 105 | mem_clean(checktag, sizeof checktag); 106 | return FAILURE; 107 | } 108 | } 109 | 110 | static 111 | void cf_chacha20poly1305_encrypt(const uint8_t key[32], 112 | const uint8_t nonce[12], 113 | const uint8_t *header, size_t nheader, 114 | const uint8_t *plaintext, size_t nbytes, 115 | uint8_t *ciphertext, 116 | uint8_t tag[16]) 117 | { 118 | process(key, 119 | nonce, 120 | header, nheader, 121 | plaintext, nbytes, 122 | ciphertext, 123 | ENCRYPT, 124 | tag); 125 | } 126 | 127 | static 128 | int cf_chacha20poly1305_decrypt(const uint8_t key[32], 129 | const uint8_t nonce[12], 130 | const uint8_t *header, size_t nheader, 131 | const uint8_t *ciphertext, size_t nbytes, 132 | const uint8_t tag[16], 133 | uint8_t *plaintext) 134 | { 135 | uint8_t ourtag[16]; 136 | memcpy(ourtag, tag, sizeof ourtag); 137 | 138 | return process(key, 139 | nonce, 140 | header, nheader, 141 | ciphertext, nbytes, 142 | plaintext, 143 | DECRYPT, 144 | ourtag); 145 | } 146 | 147 | #undef PADLEN 148 | -------------------------------------------------------------------------------- /lib/thunks/codegen/Module1.bas: -------------------------------------------------------------------------------- 1 | Attribute VB_Name = "Module1" 2 | Option Explicit 3 | 4 | Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) 5 | 6 | Private m_baBuffer() As Byte 7 | Private m_lBuffIdx As Long 8 | 9 | Private Sub pvAppendBuffer(ByVal a01 As Long, ByVal a02 As Long, ByVal a03 As Long, ByVal a04 As Long, ByVal a05 As Long, ByVal a06 As Long, ByVal a07 As Long, ByVal a08 As Long, _ 10 | ByVal a09 As Long, ByVal a10 As Long, ByVal a11 As Long, ByVal a12 As Long, ByVal a13 As Long, ByVal a14 As Long, ByVal a15 As Long, ByVal a16 As Long) 11 | #If a02 And a03 And a04 And a05 And a06 And a07 And a08 And a09 And a10 And a11 And a12 And a13 And a14 And a15 And a16 Then '--- touch args 12 | #End If 13 | Call CopyMemory(m_baBuffer(m_lBuffIdx), a01, 4 * 16) 14 | m_lBuffIdx = m_lBuffIdx + 4 * 16 15 | End Sub 16 | 17 | Public Sub GetModelData(baBuffer() As Byte) 18 | ReDim m_baBuffer(0 To 1024) As Byte 19 | m_lBuffIdx = 0 20 | '--- begin thunk data 21 | pvAppendBuffer 1&, 2&, 3&, 4&, 5&, 6&, 7&, 8&, 12345691, 12345692, 12345693, 12345694, 12345695, 12345696, 12345697, 12345698 22 | '--- end thunk data 23 | ReDim baBuffer(0 To 300) As Byte 24 | Call CopyMemory(baBuffer(0), m_baBuffer(0), UBound(baBuffer) + 1) 25 | Erase m_baBuffer 26 | End Sub 27 | 28 | Public Function GenerModuleDeclares() As String 29 | Const NUM_PARAMS As Long = 32 30 | Dim cOutput As Collection 31 | Dim lIdx As Long 32 | Dim aStr(0 To NUM_PARAMS - 1) As String 33 | 34 | For lIdx = 1 To NUM_PARAMS 35 | aStr(lIdx - 1) = "a" & Right$("0" & lIdx, 2) 36 | Next 37 | Set cOutput = New Collection 38 | cOutput.Add "Option Explicit" 39 | cOutput.Add "" 40 | cOutput.Add "Private Declare Sub CopyMemory Lib ""kernel32"" Alias ""RtlMoveMemory"" (Destination As Any, Source As Any, ByVal Length As Long)" 41 | cOutput.Add "" 42 | cOutput.Add "Private m_baBuffer() As Byte" 43 | cOutput.Add "Private m_lBuffIdx As Long" 44 | cOutput.Add "" 45 | cOutput.Add "Private Sub pvAppendBuffer(ByVal " & Join(aStr, " As Long, ByVal ") & " As Long)" 46 | cOutput.Add " #If " & Join(aStr, " And ") & " Then '--- touch args" 47 | cOutput.Add " #End If" 48 | cOutput.Add " Call CopyMemory(m_baBuffer(m_lBuffIdx), a01, 4 * " & NUM_PARAMS & ")" 49 | cOutput.Add " m_lBuffIdx = m_lBuffIdx + 4 * " & NUM_PARAMS 50 | cOutput.Add "End Sub" 51 | cOutput.Add "" 52 | GenerModuleDeclares = ConcatCollection(cOutput, vbCrLf) 53 | End Function 54 | 55 | Public Function GenerThunkData(sThunkStr As String, sName As String) As String 56 | Const NUM_PARAMS As Long = 32 57 | Const LNG_ALIGN As Long = 4 * NUM_PARAMS 58 | Dim baThunk() As Byte 59 | Dim lSize As Long 60 | Dim lAlignedSize As Long 61 | Dim lIdx As Long 62 | Dim lJdx As Long 63 | Dim cOutput As Collection 64 | Dim lPtr As Long 65 | Dim aParams(0 To NUM_PARAMS - 1) As Long 66 | Dim aStr(0 To NUM_PARAMS - 1) As String 67 | 68 | baThunk = FromBase64Array(sThunkStr) 69 | lSize = UBound(baThunk) + 1 70 | lAlignedSize = (lSize + LNG_ALIGN - 1) And -LNG_ALIGN 71 | ReDim Preserve baThunk(0 To lAlignedSize - 1) As Byte 72 | Set cOutput = New Collection 73 | cOutput.Add "Private Sub pvGet" & sName & "Data(baBuffer() As Byte)" 74 | cOutput.Add " ReDim m_baBuffer(0 To " & lAlignedSize & " - 1) As Byte" 75 | cOutput.Add " m_lBuffIdx = 0" 76 | cOutput.Add " '--- begin thunk data" 77 | For lIdx = 0 To lSize - 1 Step LNG_ALIGN 78 | Call CopyMemory(aParams(0), baThunk(lIdx), LNG_ALIGN) 79 | For lJdx = 0 To UBound(aParams) 80 | aStr(lJdx) = "&H" & Hex$(aParams(lJdx)) & "&" 81 | Next 82 | cOutput.Add " pvAppendBuffer " & Join(aStr, ", ") 83 | Next 84 | cOutput.Add " '--- end thunk data" 85 | cOutput.Add " ReDim baBuffer(0 To " & lSize & " - 1) As Byte" 86 | cOutput.Add " Call CopyMemory(baBuffer(0), m_baBuffer(0), UBound(baBuffer) + 1)" 87 | cOutput.Add " Erase m_baBuffer" 88 | cOutput.Add "End Sub" 89 | cOutput.Add "" 90 | GenerThunkData = ConcatCollection(cOutput, vbCrLf) 91 | End Function 92 | 93 | Public Function FromBase64Array(sText As String) As Byte() 94 | With VBA.CreateObject("MSXML2.DOMDocument").createElement("dummy") 95 | .DataType = "bin.base64" 96 | .Text = sText 97 | FromBase64Array = .NodeTypedValue 98 | End With 99 | End Function 100 | 101 | Public Function ConcatCollection(oCol As Collection, Optional Separator As String = "") As String 102 | Dim lSize As Long 103 | Dim vElem As Variant 104 | 105 | For Each vElem In oCol 106 | lSize = lSize + Len(vElem) + Len(Separator) 107 | Next 108 | If lSize > 0 Then 109 | ConcatCollection = String$(lSize - Len(Separator), 0) 110 | lSize = 1 111 | For Each vElem In oCol 112 | If lSize <= Len(ConcatCollection) Then 113 | Mid$(ConcatCollection, lSize, Len(vElem) + Len(Separator)) = vElem & Separator 114 | End If 115 | lSize = lSize + Len(vElem) + Len(Separator) 116 | Next 117 | End If 118 | End Function 119 | 120 | -------------------------------------------------------------------------------- /lib/thunks/codegen/Project1.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Form=Form1.frm 3 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 4 | Module=Module1; Module1.bas 5 | IconForm="Form1" 6 | Startup="Form1" 7 | HelpFile="" 8 | Command32="" 9 | Name="codegen" 10 | HelpContextID="0" 11 | CompatibleMode="0" 12 | MajorVer=1 13 | MinorVer=0 14 | RevisionVer=0 15 | AutoIncrementVer=0 16 | ServerSupportFiles=0 17 | VersionCompanyName="Unicontsoft" 18 | CompilationType=0 19 | OptimizationType=0 20 | FavorPentiumPro(tm)=0 21 | CodeViewDebugInfo=0 22 | NoAliasing=0 23 | BoundsCheck=0 24 | OverflowCheck=0 25 | FlPointCheck=0 26 | FDIVCheck=0 27 | UnroundedFP=0 28 | StartMode=0 29 | Unattended=0 30 | Retained=0 31 | ThreadPerObject=0 32 | MaxNumberOfThreads=1 33 | -------------------------------------------------------------------------------- /lib/thunks/curve25519.c: -------------------------------------------------------------------------------- 1 | /* This is based on tweetnacl. Some typedefs have been 2 | * replaced with their stdint equivalents. 3 | * 4 | * Original code was public domain. */ 5 | 6 | typedef int64_t gf[16]; 7 | 8 | static void set25519(gf r, const gf a) 9 | { 10 | for (size_t i = 0; i < 16; i++) 11 | r[i] = a[i]; 12 | } 13 | 14 | static void car25519(gf o) 15 | { 16 | int64_t c; 17 | 18 | for (size_t i = 0; i < 16; i++) 19 | { 20 | o[i] += (1LL << 16); 21 | c = o[i] >> 16; 22 | o[(i + 1) * (i < 15)] += c - 1 + 37 * (c - 1) * (i == 15); 23 | o[i] -= c << 16; 24 | } 25 | } 26 | 27 | static void sel25519(gf p, gf q, int b) 28 | { 29 | int64_t tmp, mask = ~(b-1); 30 | for (size_t i = 0; i < 16; i++) 31 | { 32 | tmp = mask & (p[i] ^ q[i]); 33 | p[i] ^= tmp; 34 | q[i] ^= tmp; 35 | } 36 | } 37 | 38 | static void pack25519(uint8_t out[32], const gf n) 39 | { 40 | int b; 41 | gf m, t; 42 | set25519(t, n); 43 | car25519(t); 44 | car25519(t); 45 | car25519(t); 46 | 47 | for(size_t j = 0; j < 2; j++) 48 | { 49 | m[0] = t[0] - 0xffed; 50 | for (size_t i = 1; i < 15; i++) 51 | { 52 | m[i] = t[i] - 0xffff - ((m[i - 1] >> 16) & 1); 53 | m[i - 1] &= 0xffff; 54 | } 55 | m[15] = t[15] - 0x7fff - ((m[14] >> 16) & 1); 56 | b = (m[15] >> 16) & 1; 57 | m[14] &= 0xffff; 58 | sel25519(t, m, 1 - b); 59 | } 60 | 61 | for (size_t i = 0; i < 16; i++) 62 | { 63 | out[2 * i] = t[i] & 0xff; 64 | out[2 * i + 1] = (uint8_t)(t[i] >> 8); 65 | } 66 | } 67 | 68 | 69 | 70 | static void unpack25519(gf o, const uint8_t *n) 71 | { 72 | for (size_t i = 0; i < 16; i++) 73 | o[i] = n[2 * i] + ((int64_t) n[2 * i + 1] << 8); 74 | o[15] &= 0x7fff; 75 | } 76 | 77 | static void add(gf o, const gf a, const gf b) 78 | { 79 | for (size_t i = 0; i < 16; i++) 80 | o[i] = a[i] + b[i]; 81 | } 82 | 83 | static void sub(gf o, const gf a, const gf b) 84 | { 85 | for (size_t i = 0; i < 16; i++) 86 | o[i] = a[i] - b[i]; 87 | } 88 | 89 | static void mul(gf o, const gf a, const gf b) 90 | { 91 | int64_t t[31]; 92 | 93 | for (size_t i = 0; i < 31; i++) 94 | t[i] = 0; 95 | 96 | for (size_t i = 0; i < 16; i++) 97 | for (size_t j = 0; j < 16; j++) 98 | t[i + j] += a[i] * b[j]; 99 | 100 | for (size_t i = 0; i < 15; i++) 101 | t[i] += 38 * t[i + 16]; 102 | 103 | for (size_t i = 0; i < 16; i++) 104 | o[i] = t[i]; 105 | 106 | car25519(o); 107 | car25519(o); 108 | } 109 | 110 | static void sqr(gf o, const gf a) 111 | { 112 | mul(o, a, a); 113 | } 114 | 115 | static void inv25519(gf o, const gf i) 116 | { 117 | gf c; 118 | for (int a = 0; a < 16; a++) 119 | c[a] = i[a]; 120 | 121 | for (int a = 253; a >= 0; a--) 122 | { 123 | sqr(c, c); 124 | if(a != 2 && a != 4) 125 | mul(c, c, i); 126 | } 127 | 128 | for (int a = 0; a < 16; a++) 129 | o[a] = c[a]; 130 | } 131 | 132 | static 133 | void cf_curve25519_mul(uint8_t out[32], const uint8_t priv[32], const uint8_t pub[32]) 134 | { 135 | uint8_t z[32]; 136 | gf x; 137 | gf a, b, c, d, e, f; 138 | const gf _121665 = {0xDB41, 1}; 139 | 140 | for (size_t i = 0; i < 31; i++) 141 | z[i] = priv[i]; 142 | z[31] = (priv[31] & 127) | 64; 143 | z[0] &= 248; 144 | 145 | unpack25519(x, pub); 146 | 147 | for(size_t i = 0; i < 16; i++) 148 | { 149 | b[i] = x[i]; 150 | d[i] = a[i] = c[i] = 0; 151 | } 152 | 153 | a[0] = d[0] = 1; 154 | 155 | for (int i = 254; i >= 0; i--) 156 | { 157 | int r = (z[i >> 3] >> (i & 7)) & 1; 158 | sel25519(a, b, r); 159 | sel25519(c, d, r); 160 | add(e, a, c); 161 | sub(a, a, c); 162 | add(c, b, d); 163 | sub(b, b, d); 164 | sqr(d, e); 165 | sqr(f, a); 166 | mul(a, c, a); 167 | mul(c, b, e); 168 | add(e, a, c); 169 | sub(a, a, c); 170 | sqr(b, a); 171 | sub(c, d, f); 172 | mul(a, c, _121665); 173 | add(a, a, d); 174 | mul(c, c, a); 175 | mul(a, d, f); 176 | mul(d, b, x); 177 | sqr(b, e); 178 | sel25519(a, b, r); 179 | sel25519(c, d, r); 180 | } 181 | 182 | inv25519(c, c); 183 | mul(a, a, c); 184 | pack25519(out, a); 185 | } 186 | 187 | static 188 | void cf_curve25519_mul_base(uint8_t out[32], const uint8_t priv[32]) 189 | { 190 | const uint8_t _9[32] = {9}; 191 | cf_curve25519_mul(out, priv, _9); 192 | } 193 | -------------------------------------------------------------------------------- /lib/thunks/ecc.h: -------------------------------------------------------------------------------- 1 | #ifndef _EASY_ECC_H_ 2 | #define _EASY_ECC_H_ 3 | 4 | #include 5 | 6 | /* Curve selection options. */ 7 | #define secp128r1 16 8 | #define secp192r1 24 9 | #define secp256r1 32 10 | #define secp384r1 48 11 | #ifndef ECC_CURVE_256 12 | #define ECC_CURVE_256 secp256r1 13 | #endif 14 | 15 | #if (ECC_CURVE_256 != secp128r1 && ECC_CURVE_256 != secp192r1 && ECC_CURVE_256 != secp256r1 && ECC_CURVE_256 != secp384r1) 16 | #error "Must define ECC_CURVE_256 to one of the available curves" 17 | #endif 18 | 19 | #define ECC_BYTES_256 ECC_CURVE_256 20 | #define NUM_ECC_DIGITS_256 (ECC_BYTES_256/8) 21 | 22 | typedef struct EccPoint 23 | { 24 | uint64_t x[NUM_ECC_DIGITS_256]; 25 | uint64_t y[NUM_ECC_DIGITS_256]; 26 | } EccPoint; 27 | 28 | #ifdef __cplusplus 29 | extern "C" 30 | { 31 | #endif 32 | 33 | /* ecc_make_key() function. 34 | Create a public/private key pair. 35 | 36 | Outputs: 37 | p_publicKey - Will be filled in with the public key. 38 | p_privateKey - Will be filled in with the private key. 39 | 40 | Returns 1 if the key pair was generated successfully, 0 if an error occurred. 41 | */ 42 | int ecc_make_key256(uint8_t p_publicKey[ECC_BYTES_256+1], const uint8_t p_privateKey[ECC_BYTES_256]); 43 | 44 | /* ecdh_shared_secret() function. 45 | Compute a shared secret given your secret key and someone else's public key. 46 | Note: It is recommended that you hash the result of ecdh_shared_secret before using it for symmetric encryption or HMAC. 47 | 48 | Inputs: 49 | p_publicKey - The public key of the remote party. 50 | p_privateKey - Your private key. 51 | 52 | Outputs: 53 | p_secret - Will be filled in with the shared secret value. 54 | 55 | Returns 1 if the shared secret was generated successfully, 0 if an error occurred. 56 | */ 57 | int ecdh_shared_secret256(const uint8_t p_publicKey[ECC_BYTES_256+1], const uint8_t p_privateKey[ECC_BYTES_256], uint8_t p_secret[ECC_BYTES_256]); 58 | 59 | /* ecdsa_sign() function. 60 | Generate an ECDSA signature for a given hash value. 61 | 62 | Usage: Compute a hash of the data you wish to sign (SHA-2 is recommended) and pass it in to 63 | this function along with your private key. 64 | 65 | Inputs: 66 | p_privateKey - Your private key. 67 | p_hash - The message hash to sign. 68 | 69 | Outputs: 70 | p_signature - Will be filled in with the signature value. 71 | 72 | Returns 1 if the signature generated successfully, 0 if an error occurred. 73 | */ 74 | int ecdsa_sign256(const uint8_t p_privateKey[ECC_BYTES_256], const uint8_t p_hash[ECC_BYTES_256], uint64_t k[NUM_ECC_DIGITS_256], uint8_t p_signature[ECC_BYTES_256*2]); 75 | 76 | /* ecdsa_verify() function. 77 | Verify an ECDSA signature. 78 | 79 | Usage: Compute the hash of the signed data using the same hash as the signer and 80 | pass it to this function along with the signer's public key and the signature values (r and s). 81 | 82 | Inputs: 83 | p_publicKey - The signer's public key 84 | p_hash - The hash of the signed data. 85 | p_signature - The signature value. 86 | 87 | Returns 1 if the signature is valid, 0 if it is invalid. 88 | */ 89 | int ecdsa_verify256(const uint8_t p_publicKey[ECC_BYTES_256+1], const uint8_t p_hash[ECC_BYTES_256], const uint8_t p_signature[ECC_BYTES_256*2]); 90 | 91 | #ifdef __cplusplus 92 | } /* end of extern "C" */ 93 | #endif 94 | 95 | #endif /* _EASY_ECC_H_ */ 96 | -------------------------------------------------------------------------------- /lib/thunks/ecc384.h: -------------------------------------------------------------------------------- 1 | #ifndef _EASY_ECC384_H_ 2 | #define _EASY_ECC384_H_ 3 | 4 | #include 5 | 6 | #ifndef secp128r1 7 | /* Curve selection options. */ 8 | #define secp128r1 16 9 | #define secp192r1 24 10 | #define secp256r1 32 11 | #define secp384r1 48 12 | #endif 13 | 14 | #ifndef ECC_CURVE_384 15 | #define ECC_CURVE_384 secp384r1 16 | #endif 17 | 18 | #if (ECC_CURVE_384 != secp128r1 && ECC_CURVE_384 != secp192r1 && ECC_CURVE_384 != secp256r1 && ECC_CURVE_384 != secp384r1) 19 | #error "Must define ECC_CURVE_384 to one of the available curves" 20 | #endif 21 | 22 | #define ECC_BYTES_384 ECC_CURVE_384 23 | #define NUM_ECC_DIGITS_384 (ECC_BYTES_384/8) 24 | 25 | typedef struct EccPoint384 26 | { 27 | uint64_t x[NUM_ECC_DIGITS_384]; 28 | uint64_t y[NUM_ECC_DIGITS_384]; 29 | } EccPoint384; 30 | 31 | #ifdef __cplusplus 32 | extern "C" 33 | { 34 | #endif 35 | 36 | /* ecc_make_key() function. 37 | Create a public/private key pair. 38 | 39 | Outputs: 40 | p_publicKey - Will be filled in with the public key. 41 | p_privateKey - Will be filled in with the private key. 42 | 43 | Returns 1 if the key pair was generated successfully, 0 if an error occurred. 44 | */ 45 | int ecc_make_key384(uint8_t p_publicKey[ECC_BYTES_384+1], const uint8_t p_privateKey[ECC_BYTES_384]); 46 | 47 | /* ecdh_shared_secret() function. 48 | Compute a shared secret given your secret key and someone else's public key. 49 | Note: It is recommended that you hash the result of ecdh_shared_secret before using it for symmetric encryption or HMAC. 50 | 51 | Inputs: 52 | p_publicKey - The public key of the remote party. 53 | p_privateKey - Your private key. 54 | 55 | Outputs: 56 | p_secret - Will be filled in with the shared secret value. 57 | 58 | Returns 1 if the shared secret was generated successfully, 0 if an error occurred. 59 | */ 60 | int ecdh_shared_secret384(const uint8_t p_publicKey[ECC_BYTES_384+1], const uint8_t p_privateKey[ECC_BYTES_384], uint8_t p_secret[ECC_BYTES_384]); 61 | 62 | /* ecdsa_sign() function. 63 | Generate an ECDSA signature for a given hash value. 64 | 65 | Usage: Compute a hash of the data you wish to sign (SHA-2 is recommended) and pass it in to 66 | this function along with your private key. 67 | 68 | Inputs: 69 | p_privateKey - Your private key. 70 | p_hash - The message hash to sign. 71 | 72 | Outputs: 73 | p_signature - Will be filled in with the signature value. 74 | 75 | Returns 1 if the signature generated successfully, 0 if an error occurred. 76 | */ 77 | int ecdsa_sign384(const uint8_t p_privateKey[ECC_BYTES_384], const uint8_t p_hash[ECC_BYTES_384], uint64_t k[NUM_ECC_DIGITS_384], uint8_t p_signature[ECC_BYTES_384*2]); 78 | 79 | /* ecdsa_verify() function. 80 | Verify an ECDSA signature. 81 | 82 | Usage: Compute the hash of the signed data using the same hash as the signer and 83 | pass it to this function along with the signer's public key and the signature values (r and s). 84 | 85 | Inputs: 86 | p_publicKey - The signer's public key 87 | p_hash - The hash of the signed data. 88 | p_signature - The signature value. 89 | 90 | Returns 1 if the signature is valid, 0 if it is invalid. 91 | */ 92 | int ecdsa_verify384(const uint8_t p_publicKey[ECC_BYTES_384+1], const uint8_t p_hash[ECC_BYTES_384], const uint8_t p_signature[ECC_BYTES_384*2]); 93 | 94 | #ifdef __cplusplus 95 | } /* end of extern "C" */ 96 | #endif 97 | 98 | #endif /* _EASY_ECC384_H_ */ 99 | -------------------------------------------------------------------------------- /lib/thunks/gf128.c: -------------------------------------------------------------------------------- 1 | /* 2 | * cifra - embedded cryptography library 3 | * Written in 2014 by Joseph Birr-Pixton 4 | * 5 | * To the extent possible under law, the author(s) have dedicated all 6 | * copyright and related and neighboring rights to this software to the 7 | * public domain worldwide. This software is distributed without any 8 | * warranty. 9 | * 10 | * You should have received a copy of the CC0 Public Domain Dedication 11 | * along with this software. If not, see 12 | * . 13 | */ 14 | 15 | /** 16 | * @brief Operations in GF(2^128). 17 | * 18 | * These implementations are constant time, but relatively slow. 19 | */ 20 | 21 | typedef uint32_t cf_gf128[4]; 22 | 23 | static 24 | void cf_gf128_tobytes_be(const cf_gf128 in, uint8_t out[16]) 25 | { 26 | write32_be(in[0], out + 0); 27 | write32_be(in[1], out + 4); 28 | write32_be(in[2], out + 8); 29 | write32_be(in[3], out + 12); 30 | } 31 | 32 | static 33 | void cf_gf128_frombytes_be(const uint8_t in[16], cf_gf128 out) 34 | { 35 | out[0] = read32_be(in + 0); 36 | out[1] = read32_be(in + 4); 37 | out[2] = read32_be(in + 8); 38 | out[3] = read32_be(in + 12); 39 | } 40 | 41 | /* out = 2 * in. Arguments may alias. */ 42 | static 43 | void cf_gf128_double_le(const cf_gf128 in, cf_gf128 out) 44 | { 45 | uint8_t table[2] = { 0x00, 0xe1 }; 46 | uint32_t borrow = 0; 47 | uint32_t inword; 48 | 49 | inword = in[0]; out[0] = (inword >> 1) | (borrow << 31); borrow = inword & 1; 50 | inword = in[1]; out[1] = (inword >> 1) | (borrow << 31); borrow = inword & 1; 51 | inword = in[2]; out[2] = (inword >> 1) | (borrow << 31); borrow = inword & 1; 52 | inword = in[3]; out[3] = (inword >> 1) | (borrow << 31); borrow = inword & 1; 53 | 54 | #if CF_CACHE_SIDE_CHANNEL_PROTECTION 55 | out[0] ^= select_u8(borrow, table, 2) << 24; 56 | #else 57 | out[0] ^= table[borrow] << 24; 58 | #endif 59 | } 60 | 61 | /* out = x + y. Arguments may alias. */ 62 | static 63 | void cf_gf128_add(const cf_gf128 x, const cf_gf128 y, cf_gf128 out) 64 | { 65 | out[0] = x[0] ^ y[0]; 66 | out[1] = x[1] ^ y[1]; 67 | out[2] = x[2] ^ y[2]; 68 | out[3] = x[3] ^ y[3]; 69 | } 70 | 71 | /* out = xy. Arguments may alias. */ 72 | static 73 | void cf_gf128_mul(const cf_gf128 x, const cf_gf128 y, cf_gf128 out) 74 | { 75 | #if CF_TIME_SIDE_CHANNEL_PROTECTION 76 | cf_gf128 zero = { 0 }; 77 | #endif 78 | 79 | /* Z_0 = 0^128 80 | * V_0 = Y */ 81 | cf_gf128 Z, V; 82 | memset(Z, 0, sizeof Z); 83 | memcpy(V, y, sizeof V); 84 | 85 | for (int i = 0; i < 128; i++) 86 | { 87 | uint32_t word = x[i >> 5]; 88 | uint8_t bit = (word >> (31 - (i & 31))) & 1; 89 | 90 | #if CF_TIME_SIDE_CHANNEL_PROTECTION 91 | select_xor128(Z, zero, V, bit); 92 | #else 93 | if (bit) 94 | xor_words(Z, V, 4); 95 | #endif 96 | 97 | cf_gf128_double_le(V, V); 98 | } 99 | 100 | memcpy(out, Z, sizeof Z); 101 | } 102 | 103 | #include 104 | 105 | /* 106 | * Select appropriate inline keyword for the compiler 107 | */ 108 | #if defined __GNUC__ || defined __clang__ 109 | # define INLINE __inline__ 110 | #elif defined (_MSC_VER) 111 | # define INLINE __forceinline 112 | #else 113 | # define INLINE 114 | #endif 115 | 116 | /* 117 | * From https://www.intel.com/content/www/us/en/processors/carry-less-multiplication-instruction-in-gcm-mode-paper.html 118 | */ 119 | static INLINE __m128i gfmul(__m128i a, __m128i b) 120 | { 121 | __m128i tmp2, tmp3, tmp4, tmp5, tmp6, tmp7, tmp8, tmp9; 122 | tmp3 = _mm_clmulepi64_si128(a, b, 0x00); 123 | tmp4 = _mm_clmulepi64_si128(a, b, 0x10); 124 | tmp5 = _mm_clmulepi64_si128(a, b, 0x01); 125 | tmp6 = _mm_clmulepi64_si128(a, b, 0x11); 126 | tmp4 = _mm_xor_si128(tmp4, tmp5); 127 | tmp5 = _mm_slli_si128(tmp4, 8); 128 | tmp4 = _mm_srli_si128(tmp4, 8); 129 | tmp3 = _mm_xor_si128(tmp3, tmp5); 130 | tmp6 = _mm_xor_si128(tmp6, tmp4); 131 | tmp7 = _mm_srli_epi32(tmp3, 31); 132 | tmp8 = _mm_srli_epi32(tmp6, 31); 133 | tmp3 = _mm_slli_epi32(tmp3, 1); 134 | tmp6 = _mm_slli_epi32(tmp6, 1); 135 | tmp9 = _mm_srli_si128(tmp7, 12); 136 | tmp8 = _mm_slli_si128(tmp8, 4); 137 | tmp7 = _mm_slli_si128(tmp7, 4); 138 | tmp3 = _mm_or_si128(tmp3, tmp7); 139 | tmp6 = _mm_or_si128(tmp6, tmp8); 140 | tmp6 = _mm_or_si128(tmp6, tmp9); 141 | tmp7 = _mm_slli_epi32(tmp3, 31); 142 | tmp8 = _mm_slli_epi32(tmp3, 30); 143 | tmp9 = _mm_slli_epi32(tmp3, 25); 144 | tmp7 = _mm_xor_si128(tmp7, tmp8); 145 | tmp7 = _mm_xor_si128(tmp7, tmp9); 146 | tmp8 = _mm_srli_si128(tmp7, 4); 147 | tmp7 = _mm_slli_si128(tmp7, 12); 148 | tmp3 = _mm_xor_si128(tmp3, tmp7); 149 | tmp2 = _mm_srli_epi32(tmp3, 1); 150 | tmp4 = _mm_srli_epi32(tmp3, 2); 151 | tmp5 = _mm_srli_epi32(tmp3, 7); 152 | tmp2 = _mm_xor_si128(tmp2, tmp4); 153 | tmp2 = _mm_xor_si128(tmp2, tmp5); 154 | tmp2 = _mm_xor_si128(tmp2, tmp8); 155 | tmp3 = _mm_xor_si128(tmp3, tmp2); 156 | tmp6 = _mm_xor_si128(tmp6, tmp3); 157 | return tmp6; 158 | } 159 | 160 | static void cf_gf128_mul_fast(const cf_gf128 x, const cf_gf128 y, cf_gf128 out) 161 | { 162 | const __m128i a = _mm_shuffle_epi32(_mm_loadu_si128((const __m128i*)x), _MM_SHUFFLE(0, 1, 2, 3)); 163 | const __m128i b = _mm_shuffle_epi32(_mm_loadu_si128((const __m128i*)y), _MM_SHUFFLE(0, 1, 2, 3)); 164 | _mm_storeu_si128((__m128i*)out, _mm_shuffle_epi32(gfmul(a, b), _MM_SHUFFLE(0, 1, 2, 3))); 165 | } 166 | -------------------------------------------------------------------------------- /lib/thunks/modes.c: -------------------------------------------------------------------------------- 1 | /* 2 | * cifra - embedded cryptography library 3 | * Written in 2014 by Joseph Birr-Pixton 4 | * 5 | * To the extent possible under law, the author(s) have dedicated all 6 | * copyright and related and neighboring rights to this software to the 7 | * public domain worldwide. This software is distributed without any 8 | * warranty. 9 | * 10 | * You should have received a copy of the CC0 Public Domain Dedication 11 | * along with this software. If not, see 12 | * . 13 | */ 14 | 15 | /** 16 | * General block cipher description 17 | * ================================ 18 | * This allows us to implement block cipher modes which can work 19 | * with different block ciphers. 20 | */ 21 | 22 | /* .. c:type:: cf_prp_block 23 | * Block processing function type. 24 | * 25 | * The `in` and `out` blocks may alias. 26 | * 27 | * :rtype: void 28 | * :param ctx: block cipher-specific context object. 29 | * :param in: input block. 30 | * :param out: output block. 31 | */ 32 | typedef void (*cf_prp_block)(void *ctx, const uint8_t *in, uint8_t *out); 33 | 34 | /* .. c:type:: cf_prp 35 | * Describes an PRP in a general way. 36 | * 37 | * .. c:member:: cf_prp.blocksz 38 | * Block size in bytes. Must be no more than :c:macro:`CF_MAXBLOCK`. 39 | * 40 | * .. c:member:: cf_prp.encrypt 41 | * Block encryption function. 42 | * 43 | * .. c:member:: cf_prp.decrypt 44 | * Block decryption function. 45 | */ 46 | typedef struct 47 | { 48 | size_t blocksz; 49 | cf_prp_block encrypt; 50 | cf_prp_block decrypt; 51 | } cf_prp; 52 | 53 | /* .. c:macro:: CF_MAXBLOCK 54 | * The maximum block cipher blocksize we support, in bytes. 55 | */ 56 | #define CF_MAXBLOCK 16 57 | 58 | /** 59 | * CBC mode 60 | * -------- 61 | * This implementation allows encryption or decryption of whole 62 | * blocks in CBC mode. It does not offer a byte-wise incremental 63 | * interface, or do any padding. 64 | * 65 | * This mode provides no useful integrity and should not be used 66 | * directly. 67 | */ 68 | 69 | /* .. c:type:: cf_cbc 70 | * This structure binds together the things needed to encrypt/decrypt whole 71 | * blocks in CBC mode. 72 | * 73 | * .. c:member:: cf_cbc.prp 74 | * How to encrypt or decrypt blocks. This could be, for example, :c:data:`cf_aes`. 75 | * 76 | * .. c:member:: cf_cbc.prpctx 77 | * Private data for prp functions. For a `prp` of `cf_aes`, this would be a 78 | * pointer to a :c:type:`cf_aes_context` instance. 79 | * 80 | * .. c:member:: cf_cbc.block 81 | * The IV or last ciphertext block. 82 | */ 83 | typedef struct 84 | { 85 | const cf_prp *prp; 86 | void *prpctx; 87 | uint8_t block[CF_MAXBLOCK]; 88 | } cf_cbc; 89 | 90 | /** 91 | * Counter mode 92 | * ------------ 93 | * This implementation allows incremental encryption/decryption of 94 | * messages. Encryption and decryption are the same operation. 95 | * 96 | * The counter is always big-endian, but has configurable location 97 | * and size within the nonce block. The counter wraps, so you 98 | * should make sure the length of a message with a given nonce 99 | * doesn't cause nonce reuse. 100 | * 101 | * This mode provides no integrity and should not be used directly. 102 | */ 103 | 104 | /* .. c:type:: cf_ctr 105 | * 106 | * .. c:member:: cf_ctr.prp 107 | * How to encrypt or decrypt blocks. This could be, for example, :c:data:`cf_aes`. 108 | * 109 | * .. c:member:: cf_ctr.prpctx 110 | * Private data for prp functions. For a `prp` of `cf_aes`, this would be a 111 | * pointer to a :c:type:`cf_aes_context` instance. 112 | * 113 | * .. c:member:: cf_ctr.nonce 114 | * The next block to encrypt to get another block of key stream. 115 | * 116 | * .. c:member:: cf_ctr.keymat 117 | * The current block of key stream. 118 | * 119 | * .. c:member:: cf_ctr.nkeymat 120 | * The number of bytes at the end of :c:member:`keymat` that are so-far unused. 121 | * If this is zero, all the bytes are used up and/or of undefined value. 122 | * 123 | * .. c:member:: cf_ctr.counter_offset 124 | * The offset (in bytes) of the counter block within the nonce. 125 | * 126 | * .. c:member:: cf_ctr.counter_width 127 | * The width (in bytes) of the counter block in the nonce. 128 | */ 129 | typedef struct 130 | { 131 | const cf_prp *prp; 132 | void *prpctx; 133 | uint8_t nonce[CF_MAXBLOCK]; 134 | uint8_t keymat[CF_MAXBLOCK]; 135 | size_t nkeymat; 136 | size_t counter_offset; 137 | size_t counter_width; 138 | } cf_ctr; 139 | 140 | /* CBC */ 141 | static 142 | void cf_cbc_init(cf_cbc *ctx, const cf_prp *prp, void *prpctx, const uint8_t iv[CF_MAXBLOCK]) 143 | { 144 | ctx->prp = prp; 145 | ctx->prpctx = prpctx; 146 | memcpy(ctx->block, iv, prp->blocksz); 147 | } 148 | 149 | static 150 | void cf_cbc_encrypt(cf_cbc *ctx, const uint8_t *input, uint8_t *output, size_t blocks) 151 | { 152 | uint8_t buf[CF_MAXBLOCK]; 153 | size_t nblk = ctx->prp->blocksz; 154 | 155 | while (blocks--) 156 | { 157 | xor_bb(buf, input, ctx->block, nblk); 158 | ctx->prp->encrypt(ctx->prpctx, buf, ctx->block); 159 | memcpy(output, ctx->block, nblk); 160 | input += nblk; 161 | output += nblk; 162 | } 163 | } 164 | 165 | static 166 | void cf_cbc_decrypt(cf_cbc *ctx, const uint8_t *input, uint8_t *output, size_t blocks) 167 | { 168 | uint8_t buf[CF_MAXBLOCK]; 169 | size_t nblk = ctx->prp->blocksz; 170 | 171 | while (blocks--) 172 | { 173 | ctx->prp->decrypt(ctx->prpctx, input, buf); 174 | xor_bb(buf, buf, ctx->block, nblk); 175 | memcpy(ctx->block, input, nblk); 176 | memcpy(output, buf, nblk); 177 | input += nblk; 178 | output += nblk; 179 | } 180 | } 181 | 182 | /* CTR */ 183 | static 184 | void cf_ctr_init(cf_ctr *ctx, const cf_prp *prp, void *prpctx, const uint8_t nonce[CF_MAXBLOCK]) 185 | { 186 | memset(ctx, 0, sizeof *ctx); 187 | ctx->counter_offset = 0; 188 | ctx->counter_width = prp->blocksz; 189 | ctx->prp = prp; 190 | ctx->prpctx = prpctx; 191 | ctx->nkeymat = 0; 192 | memcpy(ctx->nonce, nonce, prp->blocksz); 193 | } 194 | 195 | static 196 | void cf_ctr_custom_counter(cf_ctr *ctx, size_t offset, size_t width) 197 | { 198 | assert(ctx->prp->blocksz <= offset + width); 199 | ctx->counter_offset = offset; 200 | ctx->counter_width = width; 201 | } 202 | 203 | static void ctr_next_block(void *vctx, uint8_t *out) 204 | { 205 | cf_ctr *ctx = (cf_ctr *)vctx; 206 | ctx->prp->encrypt(ctx->prpctx, ctx->nonce, out); 207 | incr_be(ctx->nonce + ctx->counter_offset, ctx->counter_width); 208 | } 209 | 210 | static 211 | void cf_ctr_cipher(cf_ctr *ctx, const uint8_t *input, uint8_t *output, size_t bytes) 212 | { 213 | DECLARE_PFN(cf_blockwise_out_fn, ctr_next_block); 214 | cf_blockwise_xor(ctx->keymat, &ctx->nkeymat, 215 | ctx->prp->blocksz, 216 | input, output, bytes, 217 | pfn_ctr_next_block, 218 | ctx); 219 | } 220 | 221 | static 222 | void cf_ctr_discard_block(cf_ctr *ctx) 223 | { 224 | ctx->nkeymat = 0; 225 | } 226 | -------------------------------------------------------------------------------- /lib/thunks/poly1305.c: -------------------------------------------------------------------------------- 1 | /* 2 | * cifra - embedded cryptography library 3 | * Written in 2014 by Joseph Birr-Pixton 4 | * 5 | * To the extent possible under law, the author(s) have dedicated all 6 | * copyright and related and neighboring rights to this software to the 7 | * public domain worldwide. This software is distributed without any 8 | * warranty. 9 | * 10 | * You should have received a copy of the CC0 Public Domain Dedication 11 | * along with this software. If not, see 12 | * . 13 | */ 14 | 15 | typedef struct 16 | { 17 | uint32_t h[17]; 18 | uint32_t r[17]; 19 | uint8_t s[16]; 20 | uint8_t partial[16]; 21 | size_t npartial; 22 | } cf_poly1305; 23 | 24 | static 25 | void cf_poly1305_init(cf_poly1305 *ctx, 26 | const uint8_t r[16], 27 | const uint8_t s[16]) 28 | { 29 | memset(ctx, 0, sizeof *ctx); 30 | 31 | ctx->r[0] = r[0]; 32 | ctx->r[1] = r[1]; 33 | ctx->r[2] = r[2]; 34 | ctx->r[3] = r[3] & 0x0f; 35 | ctx->r[4] = r[4] & 0xfc; 36 | ctx->r[5] = r[5]; 37 | ctx->r[6] = r[6]; 38 | ctx->r[7] = r[7] & 0x0f; 39 | ctx->r[8] = r[8] & 0xfc; 40 | ctx->r[9] = r[9]; 41 | ctx->r[10] = r[10]; 42 | ctx->r[11] = r[11] & 0x0f; 43 | ctx->r[12] = r[12] & 0xfc; 44 | ctx->r[13] = r[13]; 45 | ctx->r[14] = r[14]; 46 | ctx->r[15] = r[15] & 0x0f; 47 | ctx->r[16] = 0; 48 | 49 | memcpy(ctx->s, s, 16); 50 | } 51 | 52 | static void poly1305_add(uint32_t h[17], 53 | const uint32_t x[17]) 54 | { 55 | uint32_t carry = 0; 56 | 57 | for (int i = 0; i < 17; i++) 58 | { 59 | carry += h[i] + x[i]; 60 | h[i] = carry & 0xff; 61 | carry >>= 8; 62 | } 63 | } 64 | 65 | /* Minimal reduction/carry chain. */ 66 | static void poly1305_min_reduce(uint32_t x[17]) 67 | { 68 | uint32_t carry = 0; 69 | for (int i = 0; i < 16; i++) 70 | { 71 | carry += x[i]; 72 | x[i] = carry & 0xff; 73 | carry >>= 8; 74 | } 75 | 76 | /* 2 ** 130 - 5 = 0x3fffffffffffffffffffffffffffffffb 77 | * ^ 78 | * So 2 bits of carry are put into top word. 79 | * Remaining bits get multiplied by 5 and carried back 80 | * into bottom */ 81 | carry += x[16]; 82 | x[16] = carry & 0x03; 83 | carry = 5 * (carry >> 2); 84 | 85 | for (int i = 0; i < 16; i++) 86 | { 87 | carry += x[i]; 88 | x[i] = carry & 0xff; 89 | carry >>= 8; 90 | } 91 | 92 | x[16] += carry; 93 | } 94 | 95 | /* This is - 2 ** 130 - 5 in twos complement. */ 96 | static const uint32_t g_negative_1305[17] = { 97 | 0x05, 0, 0, 0, 0, 0, 0, 0, 0, 0, 98 | 0, 0, 0, 0, 0, 0, 0xfc 99 | }; 100 | 101 | static void poly1305_full_reduce(uint32_t x[17]) 102 | { 103 | uint32_t xsub[17]; 104 | 105 | for (size_t i = 0; i < 17; i++) 106 | xsub[i] = x[i]; 107 | 108 | poly1305_add(xsub, negative_1305); 109 | 110 | /* If x - (2 ** 130 - 5) is negative, then 111 | * x didn't need reduction: we discard the results. 112 | * Do this in a side-channel silent way. */ 113 | uint32_t negative_mask = mask_u32(xsub[16] & 0x80, 0x80); 114 | uint32_t positive_mask = negative_mask ^ 0xffffffff; 115 | 116 | for (size_t i = 0; i < 17; i++) 117 | x[i] = (x[i] & negative_mask) | (xsub[i] & positive_mask); 118 | } 119 | 120 | static void poly1305_mul(uint32_t x[17], 121 | const uint32_t y[17]) 122 | { 123 | uint32_t r[17]; 124 | 125 | for (int i = 0; i < 17; i++) 126 | { 127 | uint32_t accum = 0; 128 | 129 | for (int j = 0; j <= i; j++) 130 | accum += x[j] * y[i - j]; 131 | 132 | /* Add in carries. These get shifted 130 bits 133 | * to the right, with a combination of byte indexing 134 | * and shifting (136 bits right, then 6 bits left). 135 | * 136 | * nb. 5 << 6 is made up of two parts: 137 | * 5: reduction of 2 ** 130 leaves a multiple 5 138 | * shift 6 places left 139 | * 17 * 8: byte indexing shift (136 bits) 140 | * 130: desired shift 141 | */ 142 | for (int j = i + 1; j < 17; j++) 143 | accum += (5 << 6) * x[j] * y[i + 17 - j]; 144 | 145 | r[i] = accum; 146 | } 147 | 148 | poly1305_min_reduce(r); 149 | 150 | for (size_t i = 0; i < 17; i++) 151 | x[i] = r[i]; 152 | } 153 | 154 | static void poly1305_block(cf_poly1305 *ctx, 155 | const uint32_t c[17]) 156 | { 157 | poly1305_add(ctx->h, c); 158 | poly1305_mul(ctx->h, ctx->r); 159 | } 160 | 161 | static void poly1305_whole_block(void *vctx, 162 | const uint8_t *buf) 163 | { 164 | cf_poly1305 *ctx = (cf_poly1305 *)vctx; 165 | uint32_t c[17]; 166 | 167 | for (int i = 0; i < 16; i++) 168 | c[i] = buf[i]; 169 | 170 | c[16] = 1; 171 | poly1305_block(ctx, c); 172 | } 173 | 174 | static void poly1305_last_block(cf_poly1305 *ctx) 175 | { 176 | uint32_t c[17] = { 0 }; 177 | 178 | for (size_t i = 0; i < ctx->npartial; i++) 179 | c[i] = ctx->partial[i]; 180 | 181 | c[ctx->npartial] = 1; 182 | poly1305_block(ctx, c); 183 | } 184 | 185 | static 186 | void cf_poly1305_update(cf_poly1305 *ctx, 187 | const uint8_t *buf, 188 | size_t nbytes) 189 | { 190 | DECLARE_PFN(cf_blockwise_in_fn, poly1305_whole_block); 191 | cf_blockwise_accumulate(ctx->partial, &ctx->npartial, 192 | sizeof ctx->partial, 193 | buf, nbytes, 194 | pfn_poly1305_whole_block, 195 | ctx); 196 | } 197 | 198 | static 199 | void cf_poly1305_finish(cf_poly1305 *ctx, 200 | uint8_t out[16]) 201 | { 202 | if (ctx->npartial) 203 | poly1305_last_block(ctx); 204 | 205 | uint32_t s[17]; 206 | for (size_t i = 0; i < 16; i++) 207 | s[i] = ctx->s[i]; 208 | s[16] = 0; 209 | 210 | poly1305_full_reduce(ctx->h); 211 | poly1305_add(ctx->h, s); 212 | 213 | for (size_t i = 0; i < 16; i++) 214 | out[i] = ctx->h[i]; 215 | 216 | mem_clean(ctx, sizeof *ctx); 217 | } 218 | -------------------------------------------------------------------------------- /lib/thunks/rsa.c: -------------------------------------------------------------------------------- 1 | #ifdef IMPL_GMPRSA_THUNK 2 | 3 | static size_t gmp_rsa_public_encrypt(const size_t flen, const unsigned char* from, const unsigned char* exp, const unsigned char* mod, unsigned char* to) 4 | { 5 | size_t size; 6 | mpz_t n, e; 7 | mpz_t enc, dec; 8 | 9 | mpz_init(n); 10 | mpz_import(n, flen, 1, 1, 1, 0, mod); 11 | mpz_init(e); 12 | mpz_import(e, flen, 1, 1, 1, 0, exp); 13 | 14 | mpz_init(enc); 15 | mpz_init(dec); 16 | mpz_import(dec, flen, 1, 1, 1, 0, from); 17 | 18 | mpz_powm(enc, dec, e, n); 19 | 20 | mpz_clear(dec); 21 | uint8_t *r = (uint8_t *)mpz_export(NULL, &size, 1, 1, 1, 0, enc); 22 | mpz_clear(enc); 23 | if (flen < size) { 24 | memcpy(to, r+size-flen, flen); 25 | } else { 26 | memset(to, 0, flen-size); 27 | memcpy(to+flen-size, r, size); 28 | } 29 | gmp_default_free(r, size); 30 | 31 | return size; 32 | } 33 | 34 | #endif // IMPL_GMPRSA_THUNK 35 | 36 | #ifdef IMPL_SSHRSA_THUNK 37 | 38 | static void rsa_modexp(const uint32_t maxbytes, const uint8_t *base_in, const uint8_t *exp_in, const uint8_t *mod_in, uint8_t *ret_out) 39 | { 40 | Bignum base, exp, mod, ret; 41 | 42 | base = bignum_from_bytes(base_in, maxbytes); 43 | exp = bignum_from_bytes(exp_in, maxbytes); 44 | mod = bignum_from_bytes(mod_in, maxbytes); 45 | ret = modpow(base, exp, mod); 46 | for (int i = maxbytes; i--;) { 47 | *ret_out++ = bignum_byte(ret, i); 48 | } 49 | freebn(base); 50 | freebn(exp); 51 | freebn(mod); 52 | freebn(ret); 53 | } 54 | 55 | /* 56 | * Compute (base ^ exp) % mod, provided mod == p * q, with p,q 57 | * distinct primes, and iqmp is the multiplicative inverse of q mod p. 58 | * Uses Chinese Remainder Theorem to speed computation up over the 59 | * obvious implementation of a single big modpow. 60 | */ 61 | static void rsa_crt_modexp(const uint32_t maxbytes, const uint8_t *base_in, const uint8_t *exp_in, const uint8_t *mod_in, 62 | const uint8_t *p_in, const uint8_t *q_in, const uint8_t *iqmp_in, uint8_t *ret_out) 63 | { 64 | Bignum base, exp, mod, p, q, iqmp; 65 | Bignum pm1, qm1, pexp, qexp, presult, qresult, diff, multiplier, ret0, ret; 66 | 67 | base = bignum_from_bytes(base_in, maxbytes); 68 | exp = bignum_from_bytes(exp_in, maxbytes); 69 | mod = bignum_from_bytes(mod_in, maxbytes); 70 | p = bignum_from_bytes(p_in, maxbytes / 2); 71 | q = bignum_from_bytes(q_in, maxbytes / 2); 72 | iqmp = bignum_from_bytes(iqmp_in, maxbytes / 2); 73 | 74 | /* 75 | * Reduce the exponent mod phi(p) and phi(q), to save time when 76 | * exponentiating mod p and mod q respectively. Of course, since p 77 | * and q are prime, phi(p) == p-1 and similarly for q. 78 | */ 79 | pm1 = copybn(p); 80 | decbn(pm1); 81 | qm1 = copybn(q); 82 | decbn(qm1); 83 | pexp = bigmod(exp, pm1); 84 | qexp = bigmod(exp, qm1); 85 | 86 | /* 87 | * Do the two modpows. 88 | */ 89 | presult = modpow(base, pexp, p); 90 | qresult = modpow(base, qexp, q); 91 | 92 | /* 93 | * Recombine the results. We want a value which is congruent to 94 | * qresult mod q, and to presult mod p. 95 | * 96 | * We know that iqmp * q is congruent to 1 * mod p (by definition 97 | * of iqmp) and to 0 mod q (obviously). So we start with qresult 98 | * (which is congruent to qresult mod both primes), and add on 99 | * (presult-qresult) * (iqmp * q) which adjusts it to be congruent 100 | * to presult mod p without affecting its value mod q. 101 | */ 102 | if (bignum_cmp(presult, qresult) < 0) { 103 | /* 104 | * Can't subtract presult from qresult without first adding on 105 | * p. 106 | */ 107 | Bignum tmp = presult; 108 | presult = bigadd(presult, p); 109 | freebn(tmp); 110 | } 111 | diff = bigsub(presult, qresult); 112 | multiplier = bigmul(iqmp, q); 113 | ret0 = bigmuladd(multiplier, diff, qresult); 114 | 115 | /* 116 | * Finally, reduce the result mod n. 117 | */ 118 | ret = bigmod(ret0, mod); 119 | for (int i = maxbytes; i--;) { 120 | *ret_out++ = bignum_byte(ret, i); 121 | } 122 | 123 | /* 124 | * Free all the intermediate results before returning. 125 | */ 126 | freebn(pm1); 127 | freebn(qm1); 128 | freebn(pexp); 129 | freebn(qexp); 130 | freebn(presult); 131 | freebn(qresult); 132 | freebn(diff); 133 | freebn(multiplier); 134 | freebn(ret0); 135 | freebn(base); 136 | freebn(exp); 137 | freebn(mod); 138 | freebn(p); 139 | freebn(q); 140 | freebn(iqmp); 141 | freebn(ret); 142 | } 143 | 144 | #endif // IMPL_SSHRSA_THUNK -------------------------------------------------------------------------------- /lib/thunks/sha256.c: -------------------------------------------------------------------------------- 1 | /* 2 | * cifra - embedded cryptography library 3 | * Written in 2014 by Joseph Birr-Pixton 4 | * 5 | * To the extent possible under law, the author(s) have dedicated all 6 | * copyright and related and neighboring rights to this software to the 7 | * public domain worldwide. This software is distributed without any 8 | * warranty. 9 | * 10 | * You should have received a copy of the CC0 Public Domain Dedication 11 | * along with this software. If not, see 12 | * . 13 | */ 14 | 15 | /** 16 | * SHA224/SHA256 17 | * ============= 18 | */ 19 | 20 | /* .. c:macro:: CF_SHA224_HASHSZ 21 | * The output size of SHA224: 28 bytes. */ 22 | #define CF_SHA224_HASHSZ 28 23 | 24 | /* .. c:macro:: CF_SHA224_BLOCKSZ 25 | * The block size of SHA224: 64 bytes. */ 26 | #define CF_SHA224_BLOCKSZ 64 27 | 28 | /* .. c:macro:: CF_SHA256_HASHSZ 29 | * The output size of SHA256: 32 bytes. */ 30 | #define CF_SHA256_HASHSZ 32 31 | 32 | /* .. c:macro:: CF_SHA256_BLOCKSZ 33 | * The block size of SHA256: 64 bytes. */ 34 | #define CF_SHA256_BLOCKSZ 64 35 | 36 | /* .. c:type:: cf_sha256_context 37 | * Incremental SHA256 hashing context. 38 | * 39 | * .. c:member:: cf_sha256_context.H 40 | * Intermediate values. 41 | * 42 | * .. c:member:: cf_sha256_context.partial 43 | * Unprocessed input. 44 | * 45 | * .. c:member:: cf_sha256_context.npartial 46 | * Number of bytes of unprocessed input. 47 | * 48 | * .. c:member:: cf_sha256_context.blocks 49 | * Number of full blocks processed. 50 | */ 51 | typedef struct 52 | { 53 | uint32_t H[8]; /* State. */ 54 | uint8_t partial[CF_SHA256_BLOCKSZ]; /* Partial block of input. */ 55 | uint32_t blocks; /* Number of full blocks processed into H. */ 56 | size_t npartial; /* Number of bytes in prefix of partial. */ 57 | } cf_sha256_context; 58 | 59 | void cf_sha256_digest_final(cf_sha256_context *ctx, uint8_t hash[CF_SHA256_HASHSZ]); 60 | 61 | static const uint32_t g_K256[64] = { 62 | 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 63 | 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 64 | 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 65 | 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 66 | 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 67 | 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 68 | 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 69 | 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 70 | 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 71 | 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 72 | 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 73 | 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 74 | 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 75 | 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 76 | 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 77 | 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 78 | }; 79 | 80 | #define CH(x, y, z) (((x) & (y)) ^ (~(x) & (z))) 81 | #define MAJ(x, y, z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z))) 82 | #define BSIG0(x) (rotr32((x), 2) ^ rotr32((x), 13) ^ rotr32((x), 22)) 83 | #define BSIG1(x) (rotr32((x), 6) ^ rotr32((x), 11) ^ rotr32((x), 25)) 84 | #define SSIG0(x) (rotr32((x), 7) ^ rotr32((x), 18) ^ ((x) >> 3)) 85 | #define SSIG1(x) (rotr32((x), 17) ^ rotr32((x), 19) ^ ((x) >> 10)) 86 | 87 | static 88 | void cf_sha256_init(cf_sha256_context *ctx) 89 | { 90 | memset(ctx, 0, sizeof *ctx); 91 | ctx->H[0] = 0x6a09e667; 92 | ctx->H[1] = 0xbb67ae85; 93 | ctx->H[2] = 0x3c6ef372; 94 | ctx->H[3] = 0xa54ff53a; 95 | ctx->H[4] = 0x510e527f; 96 | ctx->H[5] = 0x9b05688c; 97 | ctx->H[6] = 0x1f83d9ab; 98 | ctx->H[7] = 0x5be0cd19; 99 | } 100 | 101 | static 102 | void cf_sha224_init(cf_sha256_context *ctx) 103 | { 104 | memset(ctx, 0, sizeof *ctx); 105 | ctx->H[0] = 0xc1059ed8; 106 | ctx->H[1] = 0x367cd507; 107 | ctx->H[2] = 0x3070dd17; 108 | ctx->H[3] = 0xf70e5939; 109 | ctx->H[4] = 0xffc00b31; 110 | ctx->H[5] = 0x68581511; 111 | ctx->H[6] = 0x64f98fa7; 112 | ctx->H[7] = 0xbefa4fa4; 113 | } 114 | 115 | static void sha256_update_block(void *vctx, const uint8_t *inp) 116 | { 117 | cf_sha256_context *ctx = (cf_sha256_context *)vctx; 118 | 119 | /* This is a 16-word window into the whole W array. */ 120 | uint32_t W[16]; 121 | 122 | uint32_t a = ctx->H[0], 123 | b = ctx->H[1], 124 | c = ctx->H[2], 125 | d = ctx->H[3], 126 | e = ctx->H[4], 127 | f = ctx->H[5], 128 | g = ctx->H[6], 129 | h = ctx->H[7], 130 | Wt; 131 | 132 | for (size_t t = 0; t < 64; t++) 133 | { 134 | /* For W[0..16] we process the input into W. 135 | * For W[16..64] we compute the next W value: 136 | * 137 | * W[t] = SSIG1(W[t - 2]) + W[t - 7] + SSIG0(W[t - 15]) + W[t - 16]; 138 | * 139 | * But all W indices are reduced mod 16 into our window. 140 | */ 141 | if (t < 16) 142 | { 143 | W[t] = Wt = read32_be(inp); 144 | inp += 4; 145 | } else { 146 | Wt = SSIG1(W[(t - 2) % 16]) + 147 | W[(t - 7) % 16] + 148 | SSIG0(W[(t - 15) % 16]) + 149 | W[(t - 16) % 16]; 150 | W[t % 16] = Wt; 151 | } 152 | 153 | uint32_t T1 = h + BSIG1(e) + CH(e, f, g) + K256[t] + Wt; 154 | uint32_t T2 = BSIG0(a) + MAJ(a, b, c); 155 | h = g; 156 | g = f; 157 | f = e; 158 | e = d + T1; 159 | d = c; 160 | c = b; 161 | b = a; 162 | a = T1 + T2; 163 | } 164 | 165 | ctx->H[0] += a; 166 | ctx->H[1] += b; 167 | ctx->H[2] += c; 168 | ctx->H[3] += d; 169 | ctx->H[4] += e; 170 | ctx->H[5] += f; 171 | ctx->H[6] += g; 172 | ctx->H[7] += h; 173 | 174 | ctx->blocks++; 175 | } 176 | 177 | static 178 | void cf_sha256_update(cf_sha256_context *ctx, const void *data, size_t nbytes) 179 | { 180 | DECLARE_PFN(cf_blockwise_in_fn, sha256_update_block); 181 | cf_blockwise_accumulate(ctx->partial, &ctx->npartial, sizeof ctx->partial, 182 | data, nbytes, 183 | pfn_sha256_update_block, ctx); 184 | } 185 | 186 | static 187 | void cf_sha224_update(cf_sha256_context *ctx, const void *data, size_t nbytes) 188 | { 189 | cf_sha256_update(ctx, data, nbytes); 190 | } 191 | 192 | static 193 | void cf_sha256_digest(const cf_sha256_context *ctx, uint8_t hash[CF_SHA256_HASHSZ]) 194 | { 195 | /* We copy the context, so the finalisation doesn't effect the caller's 196 | * context. This means the caller can do: 197 | * 198 | * x = init() 199 | * x.update('hello') 200 | * h1 = x.digest() 201 | * x.update(' world') 202 | * h2 = x.digest() 203 | * 204 | * to get h1 = H('hello') and h2 = H('hello world') 205 | * 206 | * This wouldn't work if we applied MD-padding to *ctx. 207 | */ 208 | 209 | cf_sha256_context ours = *ctx; 210 | cf_sha256_digest_final(&ours, hash); 211 | } 212 | 213 | static 214 | void cf_sha256_digest_final(cf_sha256_context *ctx, uint8_t hash[CF_SHA256_HASHSZ]) 215 | { 216 | DECLARE_PFN(cf_blockwise_in_fn, sha256_update_block); 217 | uint64_t digested_bytes = ctx->blocks; 218 | digested_bytes = digested_bytes * CF_SHA256_BLOCKSZ + ctx->npartial; 219 | uint64_t digested_bits = digested_bytes * 8; 220 | 221 | size_t padbytes = CF_SHA256_BLOCKSZ - ((digested_bytes + 8) % CF_SHA256_BLOCKSZ); 222 | 223 | /* Hash 0x80 00 ... block first. */ 224 | cf_blockwise_acc_pad(ctx->partial, &ctx->npartial, sizeof ctx->partial, 225 | 0x80, 0x00, 0x00, padbytes, 226 | pfn_sha256_update_block, ctx); 227 | 228 | /* Now hash length. */ 229 | uint8_t buf[8]; 230 | write64_be(digested_bits, buf); 231 | cf_sha256_update(ctx, buf, 8); 232 | 233 | /* We ought to have got our padding calculation right! */ 234 | // assert(ctx->npartial == 0); 235 | 236 | write32_be(ctx->H[0], hash + 0); 237 | write32_be(ctx->H[1], hash + 4); 238 | write32_be(ctx->H[2], hash + 8); 239 | write32_be(ctx->H[3], hash + 12); 240 | write32_be(ctx->H[4], hash + 16); 241 | write32_be(ctx->H[5], hash + 20); 242 | write32_be(ctx->H[6], hash + 24); 243 | write32_be(ctx->H[7], hash + 28); 244 | 245 | memset(ctx, 0, sizeof *ctx); 246 | } 247 | 248 | static 249 | void cf_sha224_digest(const cf_sha256_context *ctx, uint8_t hash[CF_SHA224_HASHSZ]) 250 | { 251 | uint8_t full[CF_SHA256_HASHSZ]; 252 | cf_sha256_digest(ctx, full); 253 | memcpy(hash, full, CF_SHA224_HASHSZ); 254 | } 255 | 256 | static 257 | void cf_sha224_digest_final(cf_sha256_context *ctx, uint8_t hash[CF_SHA224_HASHSZ]) 258 | { 259 | uint8_t full[CF_SHA256_HASHSZ]; 260 | cf_sha256_digest_final(ctx, full); 261 | memcpy(hash, full, CF_SHA224_HASHSZ); 262 | } 263 | 264 | #undef CH 265 | #undef MAJ 266 | #undef BSIG0 267 | #undef BSIG1 268 | #undef SSIG0 269 | #undef SSIG1 270 | -------------------------------------------------------------------------------- /lib/thunks/thunks.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "thunks", "thunks.vcxproj", "{171A5FAC-A1C1-4F5F-BDFF-F8C60EF1ED32}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|x64 = Debug|x64 11 | Debug|x86 = Debug|x86 12 | Release|x64 = Release|x64 13 | Release|x86 = Release|x86 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {171A5FAC-A1C1-4F5F-BDFF-F8C60EF1ED32}.Debug|x64.ActiveCfg = Debug|x64 17 | {171A5FAC-A1C1-4F5F-BDFF-F8C60EF1ED32}.Debug|x64.Build.0 = Debug|x64 18 | {171A5FAC-A1C1-4F5F-BDFF-F8C60EF1ED32}.Debug|x86.ActiveCfg = Debug|Win32 19 | {171A5FAC-A1C1-4F5F-BDFF-F8C60EF1ED32}.Debug|x86.Build.0 = Debug|Win32 20 | {171A5FAC-A1C1-4F5F-BDFF-F8C60EF1ED32}.Release|x64.ActiveCfg = Release|x64 21 | {171A5FAC-A1C1-4F5F-BDFF-F8C60EF1ED32}.Release|x64.Build.0 = Release|x64 22 | {171A5FAC-A1C1-4F5F-BDFF-F8C60EF1ED32}.Release|x86.ActiveCfg = Release|Win32 23 | {171A5FAC-A1C1-4F5F-BDFF-F8C60EF1ED32}.Release|x86.Build.0 = Release|Win32 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | EndGlobal 29 | -------------------------------------------------------------------------------- /lib/thunks/thunks.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | Source Files 26 | 27 | 28 | Source Files 29 | 30 | 31 | Source Files 32 | 33 | 34 | Source Files 35 | 36 | 37 | Source Files 38 | 39 | 40 | Source Files 41 | 42 | 43 | Source Files 44 | 45 | 46 | Source Files 47 | 48 | 49 | Source Files 50 | 51 | 52 | Source Files 53 | 54 | 55 | Source Files 56 | 57 | 58 | Source Files 59 | 60 | 61 | Source Files 62 | 63 | 64 | Source Files 65 | 66 | 67 | Source Files 68 | 69 | 70 | Source Files 71 | 72 | 73 | Source Files 74 | 75 | 76 | -------------------------------------------------------------------------------- /lib/thunks/win32_crt.cpp: -------------------------------------------------------------------------------- 1 | #ifdef __cplusplus 2 | extern "C" { 3 | #endif 4 | #pragma function(memset) 5 | void * __cdecl memset(void *dest, int c, size_t count) 6 | { 7 | char *bytes = (char *)dest; 8 | while (count--) 9 | { 10 | *bytes++ = (char)c; 11 | } 12 | return dest; 13 | } 14 | 15 | #pragma function(memcpy) 16 | void * __cdecl memcpy(void *dest, const void *src, size_t count) 17 | { 18 | char *dest8 = (char *)dest; 19 | const char *src8 = (const char *)src; 20 | while (count--) 21 | { 22 | *dest8++ = *src8++; 23 | } 24 | return dest; 25 | } 26 | #ifdef __cplusplus 27 | } 28 | #endif 29 | -------------------------------------------------------------------------------- /samples/README.md: -------------------------------------------------------------------------------- 1 | ## User-provided Samples 2 | 3 | These sample projects are provided by end-users of the repository. Use them to get yourself familiar with simple ways that clients and servers can be implemented. 4 | 5 | - Winsock-simple by [dragokas](https://github.com/dragokas) 6 | - TlsSocketTest by Matías Curihual 7 | -------------------------------------------------------------------------------- /samples/TlsSocketTest/TlsSocket.frm: -------------------------------------------------------------------------------- 1 | VERSION 5.00 2 | Begin VB.Form TlsSocket 3 | BorderStyle = 1 'Fixed Single 4 | Caption = "Test SvTLS" 5 | ClientHeight = 636 6 | ClientLeft = 48 7 | ClientTop = 396 8 | ClientWidth = 3264 9 | LinkTopic = "Form1" 10 | MaxButton = 0 'False 11 | ScaleHeight = 636 12 | ScaleWidth = 3264 13 | StartUpPosition = 2 'CenterScreen 14 | Begin VB.CommandButton Command1 15 | Caption = "Listen" 16 | Height = 495 17 | Left = 960 18 | TabIndex = 0 19 | Top = 120 20 | Width = 1215 21 | End 22 | End 23 | Attribute VB_Name = "TlsSocket" 24 | Attribute VB_GlobalNameSpace = False 25 | Attribute VB_Creatable = False 26 | Attribute VB_PredeclaredId = True 27 | Attribute VB_Exposed = False 28 | Option Explicit 29 | 30 | Private WithEvents Serversck As cTlsSocket 31 | Attribute Serversck.VB_VarHelpID = -1 32 | Private LastID As Long 33 | Private Requests As Collection 34 | 35 | Private Sub Form_Load() 36 | Set Requests = New Collection 37 | End Sub 38 | 39 | Private Sub Form_Unload(Cancel As Integer) 40 | '--- cleanup circular references 41 | Set Requests = Nothing 42 | End Sub 43 | 44 | Private Sub Command1_Click() 45 | Dim sAddr As String 46 | Dim lPort As Long 47 | Dim sClipText As String 48 | Set Serversck = New cTlsSocket 49 | If Not Serversck.InitServerTls() Then 50 | GoTo QH 51 | End If 52 | If Not Serversck.Create(SocketPort:=5880, SocketAddress:="localhost") Then 53 | GoTo QH 54 | End If 55 | If Not Serversck.Listen() Then 56 | GoTo QH 57 | End If 58 | If Serversck.GetSockName(sAddr, lPort) Then 59 | sClipText = "curl https://" & sAddr & ":" & lPort & " -k -v" 60 | Clipboard.Clear 61 | Clipboard.SetText sClipText 62 | MsgBox "Server succesfully listening on " & sAddr & ":" & lPort & " for incomming connections" & vbCrLf & vbCrLf & _ 63 | "Command """ & sClipText & """ copied to clipboard", vbExclamation 64 | End If 65 | QH: 66 | End Sub 67 | 68 | Private Sub Serversck_OnAccept() 69 | Dim oSocket As cTlsSocket 70 | Dim oClient As cClientRequest 71 | 72 | If Not Serversck.Accept(oSocket, UseTls:=True) Then 73 | GoTo QH 74 | End If 75 | Debug.Print "New User" 76 | Set oClient = New cClientRequest 77 | LastID = LastID + 1 78 | oClient.ID = LastID 79 | Set oClient.Socket = oSocket 80 | Set oClient.Callback = Me 81 | Requests.Add oClient, "#" & oClient.ID 82 | QH: 83 | End Sub 84 | 85 | Private Sub Serversck_OnError(ByVal ErrorCode As Long, ByVal EventMask As UcsAsyncSocketEventMaskEnum) 86 | Debug.Print "Critical error: " & Serversck.LastError.Description & " [Serversck_OnError]", Timer 87 | End Sub 88 | 89 | Public Sub ClientOnReceive(Client As cClientRequest) 90 | Dim Svdata() As Byte 91 | If Not Client.Socket.ReceiveArray(Svdata) Then 92 | GoTo QH 93 | End If 94 | Debug.Print Client.ID, StrConv(Svdata, vbUnicode) 95 | QH: 96 | End Sub 97 | 98 | Public Sub ClientOnClose(Client As cClientRequest) 99 | Requests.Remove "#" & Client.ID 100 | Debug.Print Client.ID, "Disconnected" 101 | End Sub 102 | 103 | Public Sub ClientOnError(Client As cClientRequest, ByVal ErrorCode As Long, ByVal EventMask As UcsAsyncSocketEventMaskEnum) 104 | With Client.Socket.LastError 105 | Debug.Print Client.ID, "Critical error: " & .Description & " in " & .Source & " [ClientOnError]", Timer 106 | End With 107 | End Sub 108 | -------------------------------------------------------------------------------- /samples/TlsSocketTest/TlsSocket.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\Windows\SysWOW64\stdole2.tlb#OLE Automation 3 | Class=cAsyncSocket; ..\..\src\cAsyncSocket.cls 4 | Class=cTlsSocket; ..\..\src\cTlsSocket.cls 5 | Module=mdTlsNative; ..\..\src\mdTlsNative.bas 6 | Form=TlsSocket.frm 7 | Class=cClientRequest; cClientRequest.cls 8 | Startup="TlsSocket" 9 | HelpFile="" 10 | Command32="" 11 | Name="Proyecto1" 12 | HelpContextID="0" 13 | CompatibleMode="0" 14 | MajorVer=1 15 | MinorVer=0 16 | RevisionVer=0 17 | AutoIncrementVer=0 18 | ServerSupportFiles=0 19 | VersionCompanyName="Maatooh-Software" 20 | CompilationType=0 21 | OptimizationType=0 22 | FavorPentiumPro(tm)=0 23 | CodeViewDebugInfo=0 24 | NoAliasing=0 25 | BoundsCheck=0 26 | OverflowCheck=0 27 | FlPointCheck=0 28 | FDIVCheck=0 29 | UnroundedFP=0 30 | StartMode=0 31 | Unattended=0 32 | Retained=0 33 | ThreadPerObject=0 34 | MaxNumberOfThreads=1 35 | -------------------------------------------------------------------------------- /samples/TlsSocketTest/cClientRequest.cls: -------------------------------------------------------------------------------- 1 | VERSION 1.0 CLASS 2 | BEGIN 3 | MultiUse = -1 'True 4 | Persistable = 0 'NotPersistable 5 | DataBindingBehavior = 0 'vbNone 6 | DataSourceBehavior = 0 'vbNone 7 | MTSTransactionMode = 0 'NotAnMTSObject 8 | END 9 | Attribute VB_Name = "cClientRequest" 10 | Attribute VB_GlobalNameSpace = False 11 | Attribute VB_Creatable = True 12 | Attribute VB_PredeclaredId = False 13 | Attribute VB_Exposed = False 14 | Option Explicit 15 | 16 | Public ID As Long 17 | Public WithEvents Socket As cTlsSocket 18 | Attribute Socket.VB_VarHelpID = -1 19 | Public Callback As Object '--- can be impl as a weakref not to form circular references 20 | 21 | Private Sub Socket_OnReceive() 22 | Callback.ClientOnReceive Me 23 | End Sub 24 | 25 | Private Sub Socket_OnClose() 26 | Callback.ClientOnClose Me 27 | End Sub 28 | 29 | Private Sub Socket_OnError(ByVal ErrorCode As Long, ByVal EventMask As UcsAsyncSocketEventMaskEnum) 30 | Callback.ClientOnError Me, ErrorCode, EventMask 31 | End Sub 32 | -------------------------------------------------------------------------------- /samples/Winsock-simple/Client/Form1.frx: -------------------------------------------------------------------------------- 1 | {key1=data1, key2=data2} 2 | -------------------------------------------------------------------------------- /samples/Winsock-simple/Client/Project1.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Form=Form1.frm 3 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 4 | UserControl=..\..\..\contrib\ctxWinsock.ctl 5 | Class=cAsyncSocket; ..\..\..\src\cAsyncSocket.cls 6 | Class=cTlsSocket; ..\..\..\src\cTlsSocket.cls 7 | Module=mdTlsThunks; ..\..\..\src\mdTlsThunks.bas 8 | IconForm="Form1" 9 | Startup="Form1" 10 | HelpFile="" 11 | ExeName32="Winsock_Client.exe" 12 | Command32="" 13 | Name="WinsockTest_Client" 14 | HelpContextID="0" 15 | CompatibleMode="0" 16 | MajorVer=1 17 | MinorVer=0 18 | RevisionVer=0 19 | AutoIncrementVer=0 20 | ServerSupportFiles=0 21 | VersionCompanyName="Unicontsoft" 22 | CompilationType=0 23 | OptimizationType=0 24 | FavorPentiumPro(tm)=0 25 | CodeViewDebugInfo=0 26 | NoAliasing=0 27 | BoundsCheck=0 28 | OverflowCheck=0 29 | FlPointCheck=0 30 | FDIVCheck=0 31 | UnroundedFP=0 32 | StartMode=0 33 | Unattended=0 34 | Retained=0 35 | ThreadPerObject=0 36 | MaxNumberOfThreads=1 37 | -------------------------------------------------------------------------------- /samples/Winsock-simple/Server/Form1.frm: -------------------------------------------------------------------------------- 1 | VERSION 5.00 2 | Begin VB.Form Form1 3 | Caption = "Port Listener" 4 | ClientHeight = 5292 5 | ClientLeft = 108 6 | ClientTop = 456 7 | ClientWidth = 7920 8 | LinkTopic = "Form1" 9 | ScaleHeight = 5292 10 | ScaleWidth = 7920 11 | StartUpPosition = 2 'CenterScreen 12 | Begin VB.TextBox txtBody 13 | Height = 1332 14 | Left = 3720 15 | TabIndex = 13 16 | Text = "Nobody can beat Chuck Norris !!!" 17 | Top = 360 18 | Width = 3972 19 | End 20 | Begin VB.CommandButton cmdStop 21 | Caption = "Stop" 22 | Enabled = 0 'False 23 | Height = 492 24 | Left = 2040 25 | TabIndex = 11 26 | Top = 1200 27 | Width = 1332 28 | End 29 | Begin VB.TextBox txtConsole 30 | Appearance = 0 'Flat 31 | BackColor = &H00000000& 32 | BorderStyle = 0 'None 33 | ForeColor = &H0000FF00& 34 | Height = 2892 35 | Left = 120 36 | Locked = -1 'True 37 | MultiLine = -1 'True 38 | ScrollBars = 2 'Vertical 39 | TabIndex = 9 40 | Top = 2280 41 | Width = 3372 42 | End 43 | Begin WinsockTest_Server.ctxWinsock ctxServer 44 | Index = 0 45 | Left = 2760 46 | Top = 120 47 | _ExtentX = 677 48 | _ExtentY = 677 49 | End 50 | Begin VB.OptionButton optHTTPS 51 | Caption = "HTTPS" 52 | Height = 252 53 | Left = 1200 54 | TabIndex = 8 55 | Top = 120 56 | Width = 1092 57 | End 58 | Begin VB.OptionButton optHTTP 59 | Caption = "HTTP" 60 | Height = 252 61 | Left = 240 62 | TabIndex = 7 63 | Top = 120 64 | Value = -1 'True 65 | Width = 852 66 | End 67 | Begin VB.TextBox txtContent 68 | Height = 2892 69 | Left = 3720 70 | Locked = -1 'True 71 | MultiLine = -1 'True 72 | ScrollBars = 2 'Vertical 73 | TabIndex = 6 74 | Top = 2280 75 | Width = 4212 76 | End 77 | Begin VB.TextBox txtPort 78 | Height = 288 79 | Left = 2520 80 | TabIndex = 3 81 | Text = "8088" 82 | Top = 720 83 | Width = 972 84 | End 85 | Begin VB.TextBox txtAddress 86 | Height = 288 87 | Left = 240 88 | TabIndex = 1 89 | Text = "localhost" 90 | Top = 720 91 | Width = 2172 92 | End 93 | Begin VB.CommandButton cmdListen 94 | Caption = "Listen" 95 | Height = 516 96 | Left = 240 97 | TabIndex = 0 98 | Top = 1200 99 | Width = 1524 100 | End 101 | Begin VB.Label Label5 102 | Caption = "Answer on GET request" 103 | Height = 252 104 | Left = 3720 105 | TabIndex = 12 106 | Top = 120 107 | Width = 1932 108 | End 109 | Begin VB.Label Label4 110 | Caption = "Console:" 111 | Height = 252 112 | Left = 120 113 | TabIndex = 10 114 | Top = 1920 115 | Width = 1092 116 | End 117 | Begin VB.Label Label3 118 | Caption = "Incoming Data:" 119 | Height = 252 120 | Left = 3720 121 | TabIndex = 5 122 | Top = 1920 123 | Width = 1092 124 | End 125 | Begin VB.Label Label2 126 | Caption = "Port" 127 | Height = 252 128 | Left = 2520 129 | TabIndex = 4 130 | Top = 480 131 | Width = 732 132 | End 133 | Begin VB.Label Label1 134 | Caption = "Address" 135 | Height = 252 136 | Left = 240 137 | TabIndex = 2 138 | Top = 480 139 | Width = 2172 140 | End 141 | End 142 | Attribute VB_Name = "Form1" 143 | Attribute VB_GlobalNameSpace = False 144 | Attribute VB_Creatable = False 145 | Attribute VB_PredeclaredId = True 146 | Attribute VB_Exposed = False 147 | Option Explicit 148 | 149 | Dim g_sHost As String 150 | Dim g_iPort As Long 151 | 152 | Private Sub cmdListen_Click() 153 | 154 | Dim Protocol As UcsProtocolConstants 155 | 156 | If (optHTTP.Value) Then 157 | Protocol = sckTCPProtocol 158 | Else 159 | Protocol = sckTLSProtocol 160 | End If 161 | 162 | g_sHost = txtAddress.Text 163 | g_iPort = CLng(txtPort.Text) 164 | 165 | ctxServer(0).Close_ 166 | ctxServer(0).Protocol = Protocol 167 | ctxServer(0).Bind g_iPort, g_sHost 168 | 'ctxServer(0).Protocol = sckTCPProtocol 169 | 'ctxServer(0).Bind 8088, "localhost" 170 | ctxServer(0).Listen 171 | 172 | cmdListen.Enabled = False 173 | cmdStop.Enabled = True 174 | txtAddress.Enabled = False 175 | txtPort.Enabled = False 176 | optHTTP.Enabled = False 177 | optHTTPS.Enabled = False 178 | End Sub 179 | 180 | Private Sub cmdStop_Click() 181 | Dim i& 182 | For i = 0 To ctxServer.UBound 183 | If Not (ctxServer(i) Is Nothing) Then 184 | ctxServer(i).Close_ 185 | End If 186 | Next 187 | cmdListen.Enabled = True 188 | cmdStop.Enabled = False 189 | txtAddress.Enabled = True 190 | txtPort.Enabled = True 191 | optHTTP.Enabled = True 192 | optHTTPS.Enabled = True 193 | End Sub 194 | 195 | Private Sub ctxServer_ConnectionRequest(Index As Integer, ByVal requestID As Long) 196 | DebugToConsole "ctxServer_ConnectionRequest, requestID=" & requestID & ", RemoteHostIP=" & ctxServer(Index).RemoteHostIP & ", RemotePort=" & ctxServer(Index).RemotePort 197 | Load ctxServer(ctxServer.UBound + 1) 198 | ctxServer(ctxServer.UBound).Protocol = ctxServer(Index).Protocol 199 | ctxServer(ctxServer.UBound).Accept requestID 200 | End Sub 201 | 202 | Private Sub ctxServer_DataArrival(Index As Integer, ByVal bytesTotal As Long) 203 | Dim sRequest As String 204 | Dim sBody As String 205 | Dim sContent As String 206 | 207 | DebugToConsole "ctxServer_DataArrival, bytesTotal=" & bytesTotal 208 | ctxServer(Index).GetData sRequest 209 | 210 | txtContent.Text = txtContent.Text & sRequest & vbCrLf 211 | 212 | sBody = txtBody.Text 213 | sContent = "HTTP/1.1 200 OK" & vbCrLf & _ 214 | "Content-Type: text/plain; charset=windows-1251" & vbCrLf & _ 215 | "Content-Length: " & Len(sBody) & vbCrLf & _ 216 | "Connection: close" & vbCrLf & vbCrLf & _ 217 | sBody & vbCrLf & vbCrLf 218 | 219 | ctxServer(Index).SendData sContent 220 | ctxServer(Index).Close_ 221 | End Sub 222 | 223 | Private Sub ctxServer_CloseEvent(Index As Integer) 224 | DebugToConsole "ctxServer_CloseEvent", Index 225 | Unload ctxServer(Index) 226 | End Sub 227 | 228 | Private Sub ctxServer_Close(Index As Integer) 229 | DebugToConsole "ctxServer_Close", Index 230 | ctxServer_CloseEvent Index 231 | End Sub 232 | 233 | Private Sub ctxServer_Error(Index As Integer, ByVal Number As Long, Description As String, ByVal Scode As UcsErrorConstants, Source As String, HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) 234 | DebugToConsole "Error: " & Description 235 | End Sub 236 | 237 | Sub DebugToConsole(ParamArray pa()) 238 | Dim s$: s = Join(pa, " ") 239 | txtConsole.Text = vbCrLf & txtConsole.Text & vbCrLf & s 240 | txtConsole.SelStart = Len(txtConsole.Text) 241 | Debug.Print s 242 | End Sub 243 | -------------------------------------------------------------------------------- /samples/Winsock-simple/Server/Project1.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Form=Form1.frm 3 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 4 | UserControl=..\..\..\contrib\ctxWinsock.ctl 5 | Class=cAsyncSocket; ..\..\..\src\cAsyncSocket.cls 6 | Class=cTlsSocket; ..\..\..\src\cTlsSocket.cls 7 | Module=mdTlsThunks; ..\..\..\src\mdTlsThunks.bas 8 | IconForm="Form1" 9 | Startup="Form1" 10 | HelpFile="" 11 | ExeName32="Winsock_Server.exe" 12 | Command32="" 13 | Name="WinsockTest_Server" 14 | HelpContextID="0" 15 | CompatibleMode="0" 16 | MajorVer=1 17 | MinorVer=0 18 | RevisionVer=0 19 | AutoIncrementVer=0 20 | ServerSupportFiles=0 21 | VersionCompanyName="Unicontsoft" 22 | CompilationType=0 23 | OptimizationType=0 24 | FavorPentiumPro(tm)=0 25 | CodeViewDebugInfo=0 26 | NoAliasing=0 27 | BoundsCheck=0 28 | OverflowCheck=0 29 | FlPointCheck=0 30 | FDIVCheck=0 31 | UnroundedFP=0 32 | StartMode=0 33 | Unattended=0 34 | Retained=0 35 | ThreadPerObject=0 36 | MaxNumberOfThreads=1 37 | -------------------------------------------------------------------------------- /samples/vbscript examples/GetGoogle.vbs: -------------------------------------------------------------------------------- 1 | Option Explicit 2 | 3 | ' This is a simple barebones GET for VBScript 4 | ' It works for me in Win7-64. 5 | 6 | ' I put the HttpRequest.dll from here into SysWOW64 and registered it with regsvr32.exe. 7 | 8 | ' Be sure to run using the 32-bit cscript, if you have a 64-bit OS 9 | ' C:\Windows\SysWOW64\cscript.exe GetGoogle.vbs 10 | 11 | Dim URL : URL = "https://www.google.com" 12 | 13 | With CreateObject("HttpRequest.cHttpRequest") 14 | .Open_ "GET", URL 15 | .Send 16 | 17 | If .Status = 200 Then 18 | msgbox "RESPONSE : " & .responseText 19 | Else 20 | msgbox "Oops, Status : " & .Status 21 | End If 22 | End With 23 | -------------------------------------------------------------------------------- /samples/vbscript examples/TwitchVIP.vbs: -------------------------------------------------------------------------------- 1 | Option Explicit 2 | 3 | ' This script sets or unsets someone to be a VIP in a Twitch channel. 4 | ' It works for me in Win7-64. 5 | ' I put the HttpRequest.dll from here into SysWOW64 and registered it with regsvr32.exe. 6 | 7 | ' Example: 8 | ' C:\Windows\SysWOW64\cscript.exe TwitchVIP.vbs 1 username 9 | ' will set username as VIP. (0 to remove) 10 | 11 | Dim clientid, apioauth, broadcasterid 12 | ' Note: These need to be set, and the oAuth needs to permit this action, 13 | ' but that's outside the scope of this example 14 | 15 | Dim URL, userid 16 | URL = "https://api.twitch.tv/helix/users?login=" & WScript.Arguments(1) 17 | 18 | With CreateObject("HttpRequest.cHttpRequest") 19 | .Open_ "GET",URL 20 | .setRequestHeader "Client-Id",clientid 21 | .setRequestHeader "Authorization", "Bearer " & apioauth 22 | .Send 23 | 24 | ' msgbox "RESPONSE : " & .responseText 25 | If .Status = 200 Then 26 | Dim y, html : Set html = CreateObject("htmlfile") 27 | Dim w : Set w = html.parentWindow 28 | w.execScript "var json=" & .responseText & ";var e=new Enumerator(json.data);", "JScript" 29 | While Not w.e.atEnd() 30 | Set y = w.e.item() 31 | userid=y.id 32 | w.e.moveNext 33 | Wend 34 | End If 35 | End With 36 | 37 | With CreateObject("HttpRequest.cHttpRequest") 38 | URL = "https://api.twitch.tv/helix/channels/vips" 39 | If WScript.Arguments(0) = 1 Then 40 | .open_ "POST",URL 41 | Else 42 | .open_ "DELETE",URL 43 | End If 44 | .setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 45 | .setRequestHeader "Authorization", "Bearer " & apioauth 46 | .setRequestHeader "Client-ID",clientid 47 | .send "broadcaster_id=" & broadcasterid & "&user_id=" & userid 48 | 49 | ' msgbox .responseText 50 | 51 | End With -------------------------------------------------------------------------------- /test/BareboneTls/Form2.frm: -------------------------------------------------------------------------------- 1 | VERSION 5.00 2 | Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX" 3 | Begin VB.Form Form2 4 | Caption = "Form2" 5 | ClientHeight = 2316 6 | ClientLeft = 108 7 | ClientTop = 456 8 | ClientWidth = 3624 9 | LinkTopic = "Form2" 10 | ScaleHeight = 2316 11 | ScaleWidth = 3624 12 | StartUpPosition = 2 'CenterScreen 13 | Begin MSWinsockLib.Winsock wscSocket 14 | Index = 0 15 | Left = 2772 16 | Top = 252 17 | _ExtentX = 593 18 | _ExtentY = 593 19 | _Version = 393216 20 | End 21 | Begin VB.Label labInfo 22 | Alignment = 2 'Center 23 | Height = 432 24 | Left = 84 25 | TabIndex = 0 26 | Top = 504 27 | Width = 3456 28 | End 29 | End 30 | Attribute VB_Name = "Form2" 31 | Attribute VB_GlobalNameSpace = False 32 | Attribute VB_Creatable = False 33 | Attribute VB_PredeclaredId = True 34 | Attribute VB_Exposed = False 35 | Option Explicit 36 | 37 | Private m_cCerts As Collection 38 | Private m_cPrivKey As Collection 39 | Private m_uCtx() As UcsTlsContext 40 | 41 | Private Sub Listen(sAddr As String, ByVal lPort As Long) 42 | If Not PkiGenerSelfSignedCertificate(m_cCerts, m_cPrivKey) Then 43 | Exit Sub 44 | End If 45 | ReDim m_uCtx(0 To 0) 46 | wscSocket(0).Bind lPort, sAddr 47 | wscSocket(0).Listen 48 | End Sub 49 | 50 | Private Sub SendData(Index As Integer, baData() As Byte) 51 | Dim baOutput() As Byte 52 | Dim lOutputPos As Long 53 | 54 | If Not TlsSend(m_uCtx(Index), baData, UBound(baData) + 1, baOutput, lOutputPos) Then 55 | OnError Index, TlsGetLastError(m_uCtx(Index)), "TlsSend" 56 | End If 57 | If lOutputPos > 0 Then 58 | wscSocket(Index).SendData baOutput 59 | End If 60 | End Sub 61 | 62 | Private Sub wscSocket_ConnectionRequest(Index As Integer, ByVal requestID As Long) 63 | Load wscSocket(wscSocket.UBound + 1) 64 | wscSocket(wscSocket.UBound).Accept requestID 65 | ReDim Preserve m_uCtx(0 To wscSocket.UBound) 66 | Call TlsInitServer(m_uCtx(wscSocket.UBound), wscSocket(wscSocket.UBound).RemoteHostIP, m_cCerts, m_cPrivKey) 67 | End Sub 68 | 69 | Private Sub wscSocket_DataArrival(Index As Integer, ByVal bytesTotal As Long) 70 | Dim bError As Boolean 71 | Dim baRecv() As Byte 72 | Dim baOutput() As Byte 73 | Dim lOutputPos As Long 74 | Dim baPlainText() As Byte 75 | Dim lSize As Long 76 | 77 | On Error GoTo EH 78 | If TlsIsClosed(m_uCtx(Index)) Or bytesTotal = 0 Then 79 | Exit Sub 80 | End If 81 | wscSocket(Index).GetData baRecv 82 | If Not TlsIsReady(m_uCtx(Index)) Then 83 | lOutputPos = 0 84 | bError = Not TlsHandshake(m_uCtx(Index), baRecv, -1, baOutput, lOutputPos) 85 | If lOutputPos > 0 Then 86 | wscSocket(Index).SendData baOutput 87 | End If 88 | If bError Then 89 | OnError Index, TlsGetLastError(m_uCtx(Index)), "TlsHandshake" 90 | End If 91 | If Not TlsIsReady(m_uCtx(Index)) Then 92 | Exit Sub 93 | End If 94 | OnConnect Index 95 | '--- fall-through to flush application data after TLS handshake (if any) 96 | Erase baRecv 97 | End If 98 | lOutputPos = 0 99 | bError = Not TlsReceive(m_uCtx(Index), baRecv, -1, baPlainText, lSize, baOutput, lOutputPos) 100 | If lOutputPos > 0 Then 101 | wscSocket(Index).SendData baOutput 102 | End If 103 | If lSize > 0 Then 104 | OnDataArrival Index, lSize, baPlainText 105 | End If 106 | If bError Then 107 | OnError Index, TlsGetLastError(m_uCtx(Index)), "TlsReceive" 108 | End If 109 | If TlsIsClosed(m_uCtx(Index)) Then 110 | OnClose Index 111 | End If 112 | Exit Sub 113 | EH: 114 | OnError Index, Err.Description, "wscSocket_DataArrival" 115 | End Sub 116 | 117 | Private Sub wscSocket_Close(Index As Integer) 118 | If Not TlsIsClosed(m_uCtx(Index)) Then 119 | OnClose Index 120 | End If 121 | End Sub 122 | 123 | '= callbacks ============================================================= 124 | 125 | Public Sub OnConnect(Index As Integer) 126 | labInfo.Caption = "Connection " & Index & " from " & wscSocket(Index).RemoteHostIP & " port " & wscSocket(Index).RemotePort 127 | End Sub 128 | 129 | Private Sub OnDataArrival(Index As Integer, ByVal bytesTotal As Long, baData() As Byte) 130 | Dim sResponse As String 131 | 132 | Debug.Print "OnDataArrival, Index=" & Index & ", bytesTotal=" & bytesTotal, Timer 133 | Debug.Print StrConv(baData, vbUnicode) 134 | sResponse = "" & Now & "" 135 | sResponse = "HTTP/1.0 200 Ok" & vbCrLf & _ 136 | "Content-Type: text/html; charset=UTF-8" & vbCrLf & _ 137 | "Content-Length: " & Len(sResponse) & vbCrLf & _ 138 | "Connection: Close" & vbCrLf & vbCrLf & _ 139 | sResponse 140 | SendData Index, StrConv(sResponse, vbFromUnicode) 141 | End Sub 142 | 143 | Public Sub OnClose(Index As Integer) 144 | Debug.Print "OnClose, Index=" & Index, Timer 145 | Unload wscSocket(Index) 146 | End Sub 147 | 148 | Public Sub OnError(Index As Integer, sDescription As String, sSource As String) 149 | Debug.Print "Critical error(" & Index & "): " & sDescription & " in " & sSource, Timer 150 | End Sub 151 | 152 | '= form events =========================================================== 153 | 154 | Private Sub Form_Load() 155 | Listen "0.0.0.0", 10443 156 | labInfo.Caption = "Listening on " & wscSocket(0).LocalIP & " port " & wscSocket(0).LocalPort 157 | End Sub 158 | -------------------------------------------------------------------------------- /test/BareboneTls/Module2.bas: -------------------------------------------------------------------------------- 1 | Attribute VB_Name = "Module2" 2 | Option Explicit 3 | 4 | Public Type UcsTlsContext 5 | Dummy As Long 6 | End Type 7 | 8 | Public Property Get TlsIsClosed(uCtx As UcsTlsContext) As Boolean 9 | 10 | End Property 11 | 12 | Public Property Get TlsIsStarted(uCtx As UcsTlsContext) As Boolean 13 | 14 | End Property 15 | 16 | Public Property Get TlsIsReady(uCtx As UcsTlsContext) As Boolean 17 | 18 | End Property 19 | 20 | Public Property Get TlsIsShutdown(uCtx As UcsTlsContext) As Boolean 21 | 22 | End Property 23 | 24 | '========================================================================= 25 | ' TLS support 26 | '========================================================================= 27 | 28 | Public Function TlsInitClient( _ 29 | uCtx As UcsTlsContext, _ 30 | Optional RemoteHostName As String, _ 31 | Optional ByVal LocalFeatures As Long, _ 32 | Optional ClientCertCallback As Object, _ 33 | Optional AlpnProtocols As String) As Boolean 34 | 35 | End Function 36 | 37 | Public Function TlsInitServer( _ 38 | uCtx As UcsTlsContext, _ 39 | Optional RemoteHostName As String, _ 40 | Optional Certificates As Collection, _ 41 | Optional PrivateKey As Collection, _ 42 | Optional AlpnProtocols As String, _ 43 | Optional ByVal LocalFeatures As Long) As Boolean 44 | 45 | End Function 46 | 47 | Public Function TlsTerminate(uCtx As UcsTlsContext) 48 | 49 | End Function 50 | 51 | Public Function TlsHandshake(uCtx As UcsTlsContext, baInput() As Byte, ByVal lSize As Long, baOutput() As Byte, lOutputPos As Long) As Boolean 52 | 53 | End Function 54 | 55 | Public Function TlsReceive(uCtx As UcsTlsContext, baInput() As Byte, ByVal lSize As Long, baPlainText() As Byte, lPos As Long, baOutput() As Byte, lOutputPos As Long) As Boolean 56 | 57 | End Function 58 | 59 | Public Function TlsSend(uCtx As UcsTlsContext, baPlainText() As Byte, ByVal lSize As Long, baOutput() As Byte, lOutputPos As Long) As Boolean 60 | 61 | End Function 62 | 63 | Public Function TlsShutdown(uCtx As UcsTlsContext, baOutput() As Byte, lPos As Long) As Boolean 64 | 65 | End Function 66 | 67 | Public Function TlsGetLastError(uCtx As UcsTlsContext, Optional LastErrNumber As Long, Optional LastErrSource As String) As String 68 | 69 | End Function 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /test/BareboneTls/Project1.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Form=Form1.frm 3 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 4 | Object={248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0; MSWINSCK.OCX 5 | Module=mdTlsThunks; ..\..\src\mdTlsThunks.bas 6 | Form=Form2.frm 7 | Module=Module1; Module1.bas 8 | IconForm="Form1" 9 | Startup="Form1" 10 | HelpFile="" 11 | Command32="" 12 | Name="BareboneThunks" 13 | HelpContextID="0" 14 | CompatibleMode="0" 15 | MajorVer=1 16 | MinorVer=0 17 | RevisionVer=0 18 | AutoIncrementVer=0 19 | ServerSupportFiles=0 20 | VersionCompanyName="Unicontsoft" 21 | CompilationType=0 22 | OptimizationType=0 23 | FavorPentiumPro(tm)=0 24 | CodeViewDebugInfo=0 25 | NoAliasing=0 26 | BoundsCheck=0 27 | OverflowCheck=0 28 | FlPointCheck=0 29 | FDIVCheck=0 30 | UnroundedFP=0 31 | StartMode=0 32 | Unattended=0 33 | Retained=0 34 | ThreadPerObject=0 35 | MaxNumberOfThreads=1 36 | -------------------------------------------------------------------------------- /test/BareboneTls/Project2.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Form=Form1.frm 3 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 4 | Object={248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0; MSWINSCK.OCX 5 | Module=mdTlsNative; ..\..\src\mdTlsNative.bas 6 | Form=Form2.frm 7 | Module=Module1; Module1.bas 8 | IconForm="Form1" 9 | Startup="Form1" 10 | HelpFile="" 11 | Command32="" 12 | Name="BareboneNative" 13 | HelpContextID="0" 14 | CompatibleMode="0" 15 | MajorVer=1 16 | MinorVer=0 17 | RevisionVer=0 18 | AutoIncrementVer=0 19 | ServerSupportFiles=0 20 | VersionCompanyName="Unicontsoft" 21 | CompilationType=0 22 | OptimizationType=0 23 | FavorPentiumPro(tm)=0 24 | CodeViewDebugInfo=0 25 | NoAliasing=0 26 | BoundsCheck=0 27 | OverflowCheck=0 28 | FlPointCheck=0 29 | FDIVCheck=0 30 | UnroundedFP=0 31 | StartMode=0 32 | Unattended=0 33 | Retained=0 34 | ThreadPerObject=0 35 | MaxNumberOfThreads=1 36 | -------------------------------------------------------------------------------- /test/BareboneTls/Project3.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Form=Form1.frm 3 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 4 | Object={248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0; MSWINSCK.OCX 5 | Module=mdTlsSodium; ..\..\src\mdTlsSodium.bas 6 | Form=Form2.frm 7 | Module=Module1; Module1.bas 8 | IconForm="Form1" 9 | Startup="Form1" 10 | HelpFile="" 11 | Title="Project3" 12 | ExeName32="Project3.exe" 13 | Command32="" 14 | Name="BareboneSodium" 15 | HelpContextID="0" 16 | CompatibleMode="0" 17 | MajorVer=1 18 | MinorVer=0 19 | RevisionVer=0 20 | AutoIncrementVer=0 21 | ServerSupportFiles=0 22 | VersionCompanyName="Unicontsoft" 23 | CondComp="ASYNCSOCKET_NO_TLSSERVER = 0 : Dummy = 1" 24 | CompilationType=0 25 | OptimizationType=1 26 | FavorPentiumPro(tm)=0 27 | CodeViewDebugInfo=0 28 | NoAliasing=-1 29 | BoundsCheck=-1 30 | OverflowCheck=-1 31 | FlPointCheck=-1 32 | FDIVCheck=-1 33 | UnroundedFP=-1 34 | StartMode=0 35 | Unattended=0 36 | Retained=0 37 | ThreadPerObject=0 38 | MaxNumberOfThreads=1 39 | -------------------------------------------------------------------------------- /test/BareboneTls/Project4.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Form=Form1.frm 3 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 4 | Object={248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0; MSWINSCK.OCX 5 | Form=Form2.frm 6 | Module=Module1; Module1.bas 7 | Module=Module2; Module2.bas 8 | IconForm="Form1" 9 | Startup="Form1" 10 | HelpFile="" 11 | Title="Project4" 12 | ExeName32="Project4.exe" 13 | Command32="" 14 | Name="BareboneEmpty" 15 | HelpContextID="0" 16 | CompatibleMode="0" 17 | MajorVer=1 18 | MinorVer=0 19 | RevisionVer=0 20 | AutoIncrementVer=0 21 | ServerSupportFiles=0 22 | VersionCompanyName="Unicontsoft" 23 | CompilationType=0 24 | OptimizationType=1 25 | FavorPentiumPro(tm)=0 26 | CodeViewDebugInfo=0 27 | NoAliasing=-1 28 | BoundsCheck=-1 29 | OverflowCheck=-1 30 | FlPointCheck=-1 31 | FDIVCheck=-1 32 | UnroundedFP=-1 33 | StartMode=0 34 | Unattended=0 35 | Retained=0 36 | ThreadPerObject=0 37 | MaxNumberOfThreads=1 38 | -------------------------------------------------------------------------------- /test/Basic/Form1.frm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wqweto/VbAsyncSocket/f5e922585800e05d42be166fa8301007a24fee94/test/Basic/Form1.frm -------------------------------------------------------------------------------- /test/Basic/Module1.bas: -------------------------------------------------------------------------------- 1 | Attribute VB_Name = "Module1" 2 | Option Explicit 3 | DefObj A-Z 4 | 5 | #Const ImplUseDebugLog = (USE_DEBUG_LOG <> 0) 6 | 7 | '--- for WideCharToMultiByte 8 | Private Const CP_UTF8 As Long = 65001 9 | 10 | Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) 11 | Private Declare Function IsBadReadPtr Lib "kernel32" (ByVal lp As Long, ByVal ucb As Long) As Long 12 | Private Declare Function WideCharToMultiByte Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As Long, ByVal lpUsedDefaultChar As Long) As Long 13 | Private Declare Function MultiByteToWideChar Lib "kernel32" (ByVal CodePage As Long, ByVal dwFlags As Long, lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long 14 | Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long 15 | Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long 16 | 17 | Public Function DesignDumpArray(baData() As Byte, Optional ByVal lPos As Long, Optional ByVal lSize As Long = -1) As String 18 | If lSize < 0 Then 19 | lSize = LenB(CStr(baData)) - lPos 20 | End If 21 | If lSize > 0 Then 22 | DesignDumpArray = DesignDumpMemory(VarPtr(baData(lPos)), lSize) 23 | End If 24 | End Function 25 | 26 | Public Function DesignDumpMemory(ByVal lPtr As Long, ByVal lSize As Long) As String 27 | Dim lIdx As Long 28 | Dim sHex As String 29 | Dim sChar As String 30 | Dim lValue As Long 31 | Dim aResult() As String 32 | 33 | ReDim aResult(0 To (lSize + 15) \ 16) As String 34 | ' Debug.Assert RedimStats("DesignDumpMemory.aResult", UBound(aResult) + 1) 35 | For lIdx = 0 To ((lSize + 15) \ 16) * 16 36 | If lIdx < lSize Then 37 | If IsBadReadPtr(lPtr, 1) = 0 Then 38 | Call CopyMemory(lValue, ByVal lPtr, 1) 39 | sHex = sHex & Right$("0" & Hex$(lValue), 2) & " " 40 | If lValue >= 32 Then 41 | sChar = sChar & Chr$(lValue) 42 | Else 43 | sChar = sChar & "." 44 | End If 45 | Else 46 | sHex = sHex & "?? " 47 | sChar = sChar & "." 48 | End If 49 | Else 50 | sHex = sHex & " " 51 | End If 52 | If ((lIdx + 1) Mod 4) = 0 Then 53 | sHex = sHex & " " 54 | End If 55 | If ((lIdx + 1) Mod 16) = 0 Then 56 | aResult(lIdx \ 16) = Right$("000" & Hex$(lIdx - 15), 4) & " - " & sHex & sChar 57 | sHex = vbNullString 58 | sChar = vbNullString 59 | End If 60 | lPtr = (lPtr Xor &H80000000) + 1 Xor &H80000000 61 | Next 62 | DesignDumpMemory = Join(aResult, vbCrLf) 63 | End Function 64 | 65 | Public Function FromUtf8Array(baText() As Byte) As String 66 | Dim lSize As Long 67 | 68 | If UBound(baText) >= 0 Then 69 | FromUtf8Array = String$(2 * UBound(baText), 0) 70 | lSize = MultiByteToWideChar(CP_UTF8, 0, baText(0), UBound(baText) + 1, StrPtr(FromUtf8Array), Len(FromUtf8Array)) 71 | FromUtf8Array = Left$(FromUtf8Array, lSize) 72 | End If 73 | End Function 74 | 75 | Public Function ToUtf8Array(sText As String) As Byte() 76 | Dim baRetVal() As Byte 77 | Dim lSize As Long 78 | 79 | lSize = WideCharToMultiByte(CP_UTF8, 0, StrPtr(sText), Len(sText), ByVal 0, 0, 0, 0) 80 | If lSize > 0 Then 81 | ReDim baRetVal(0 To lSize - 1) As Byte 82 | Call WideCharToMultiByte(CP_UTF8, 0, StrPtr(sText), Len(sText), baRetVal(0), lSize, 0, 0) 83 | Else 84 | baRetVal = vbNullString 85 | End If 86 | ToUtf8Array = baRetVal 87 | End Function 88 | 89 | Public Property Get TimerEx() As Double 90 | Dim cFreq As Currency 91 | Dim cValue As Currency 92 | 93 | Call QueryPerformanceFrequency(cFreq) 94 | Call QueryPerformanceCounter(cValue) 95 | TimerEx = cValue / cFreq 96 | End Property 97 | 98 | Public Function At(vArray As Variant, ByVal lIdx As Long) As Variant 99 | On Error GoTo QH 100 | At = vArray(lIdx) 101 | QH: 102 | End Function 103 | 104 | #If ImplUseDebugLog Then 105 | Public Sub DebugLog(sModule As String, sFunction As String, sText As String, Optional ByVal eType As LogEventTypeConstants = vbLogEventTypeInformation) 106 | Debug.Print Format$(TimerEx, "0.000") & " " & Switch( _ 107 | eType = vbLogEventTypeError, "[ERROR]", _ 108 | eType = vbLogEventTypeWarning, "[WARN]", _ 109 | True, "[INFO]") & " " & sText & " [" & sModule & "." & sFunction & "]" 110 | End Sub 111 | #End If 112 | -------------------------------------------------------------------------------- /test/Basic/Project1.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 3 | Form=Form1.frm 4 | Class=cAsyncSocket; ..\..\src\cAsyncSocket.cls 5 | Module=Module1; Module1.bas 6 | Class=cHttpDownload; ..\..\contrib\cHttpDownload.cls 7 | Class=cRateLimiter; ..\..\contrib\cRateLimiter.cls 8 | Class=cTlsSocket; ..\..\src\cTlsSocket.cls 9 | Module=mdTlsThunks; ..\..\src\mdTlsThunks.bas 10 | Class=cHttpRequest; ..\..\contrib\cHttpRequest.cls 11 | IconForm="Form1" 12 | Startup="Form1" 13 | HelpFile="" 14 | Title="Project1" 15 | ExeName32="Project1.exe" 16 | Command32="" 17 | Name="Basic" 18 | HelpContextID="0" 19 | CompatibleMode="0" 20 | MajorVer=1 21 | MinorVer=0 22 | RevisionVer=0 23 | AutoIncrementVer=0 24 | ServerSupportFiles=0 25 | VersionCompanyName="Unicontsoft" 26 | CondComp="ASYNCSOCKET_NO_TLSSERVER = 1 : USE_DEBUG_LOG = 1 : USE_TLS = 1 : Dummy = 1" 27 | CompilationType=0 28 | OptimizationType=2 29 | FavorPentiumPro(tm)=0 30 | CodeViewDebugInfo=-1 31 | NoAliasing=0 32 | BoundsCheck=0 33 | OverflowCheck=0 34 | FlPointCheck=0 35 | FDIVCheck=0 36 | UnroundedFP=0 37 | StartMode=0 38 | Unattended=0 39 | Retained=0 40 | ThreadPerObject=0 41 | MaxNumberOfThreads=1 42 | -------------------------------------------------------------------------------- /test/Basic/Project2.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 3 | Form=Form1.frm 4 | Class=cAsyncSocket; ..\..\src\cAsyncSocket.cls 5 | Module=Module1; Module1.bas 6 | Class=cHttpDownload; ..\..\contrib\cHttpDownload.cls 7 | Class=cRateLimiter; ..\..\contrib\cRateLimiter.cls 8 | Class=cTlsSocket; ..\..\src\cTlsSocket.cls 9 | Module=mdTlsNative; ..\..\src\mdTlsNative.bas 10 | Class=cHttpRequest; ..\..\contrib\cHttpRequest.cls 11 | IconForm="Form1" 12 | Startup="Form1" 13 | HelpFile="" 14 | Title="Project1" 15 | ExeName32="Project1.exe" 16 | Command32="" 17 | Name="BasicNative" 18 | HelpContextID="0" 19 | CompatibleMode="0" 20 | MajorVer=1 21 | MinorVer=0 22 | RevisionVer=0 23 | AutoIncrementVer=0 24 | ServerSupportFiles=0 25 | VersionCompanyName="Unicontsoft" 26 | CondComp="ASYNCSOCKET_NO_TLSSERVER = 1 : USE_DEBUG_LOG = 1 : USE_TLS = 1 : Dummy = 1" 27 | CompilationType=0 28 | OptimizationType=2 29 | FavorPentiumPro(tm)=0 30 | CodeViewDebugInfo=-1 31 | NoAliasing=0 32 | BoundsCheck=0 33 | OverflowCheck=0 34 | FlPointCheck=0 35 | FDIVCheck=0 36 | UnroundedFP=0 37 | StartMode=0 38 | Unattended=0 39 | Retained=0 40 | ThreadPerObject=0 41 | MaxNumberOfThreads=1 42 | -------------------------------------------------------------------------------- /test/Chat/Project1.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 3 | Object={8405D0DF-9FDD-4829-AEAD-8E2B0A18FEA4}#1.0#0; Inked.dll 4 | Form=frmMain.frm 5 | Form=frmServer.frm 6 | Class=cClientInfo; cClientInfo.cls 7 | Module=mdGlobals; mdGlobals.bas 8 | Form=frmClient.frm 9 | Class=cAsyncSocket; ..\..\src\cAsyncSocket.cls 10 | IconForm="frmMain" 11 | Startup="frmMain" 12 | HelpFile="" 13 | Title="Project1" 14 | ExeName32="Project1.exe" 15 | Command32="" 16 | Name="Project1" 17 | HelpContextID="0" 18 | CompatibleMode="0" 19 | MajorVer=1 20 | MinorVer=0 21 | RevisionVer=0 22 | AutoIncrementVer=0 23 | ServerSupportFiles=0 24 | VersionCompanyName="Unicontsoft" 25 | CondComp="ASYNCSOCKET_NO_SYNC = 1" 26 | CompilationType=0 27 | OptimizationType=0 28 | FavorPentiumPro(tm)=0 29 | CodeViewDebugInfo=0 30 | NoAliasing=0 31 | BoundsCheck=0 32 | OverflowCheck=0 33 | FlPointCheck=0 34 | FDIVCheck=0 35 | UnroundedFP=0 36 | StartMode=0 37 | Unattended=0 38 | Retained=0 39 | ThreadPerObject=0 40 | MaxNumberOfThreads=1 41 | -------------------------------------------------------------------------------- /test/Chat/cClientInfo.cls: -------------------------------------------------------------------------------- 1 | VERSION 1.0 CLASS 2 | BEGIN 3 | MultiUse = -1 'True 4 | Persistable = 0 'NotPersistable 5 | DataBindingBehavior = 0 'vbNone 6 | DataSourceBehavior = 0 'vbNone 7 | MTSTransactionMode = 0 'NotAnMTSObject 8 | END 9 | Attribute VB_Name = "cClientInfo" 10 | Attribute VB_GlobalNameSpace = False 11 | Attribute VB_Creatable = True 12 | Attribute VB_PredeclaredId = False 13 | Attribute VB_Exposed = False 14 | Option Explicit 15 | DefObj A-Z 16 | 17 | '========================================================================= 18 | ' Constants and member variables 19 | '========================================================================= 20 | 21 | Public ID As String 22 | Public UserName As String 23 | Public Address As String 24 | Public Port As Long 25 | Public WithEvents Socket As cAsyncSocket 26 | Attribute Socket.VB_VarHelpID = -1 27 | Private m_oParent As frmServer 28 | 29 | '========================================================================= 30 | ' Methods 31 | '========================================================================= 32 | 33 | Public Function Init(sID As String, oSocket As cAsyncSocket, oParent As frmServer) 34 | ID = sID 35 | UserName = "Anonymous " & sID 36 | oSocket.GetPeerName Address, Port 37 | Set Socket = oSocket 38 | Set m_oParent = oParent 39 | End Function 40 | 41 | '========================================================================= 42 | ' Events 43 | '========================================================================= 44 | 45 | Private Sub Socket_OnClose() 46 | m_oParent.frTcpClose ID 47 | End Sub 48 | 49 | Private Sub Socket_OnReceive() 50 | m_oParent.frTcpReceive ID, Socket 51 | End Sub 52 | 53 | Private Sub Socket_OnError(ByVal ErrorCode As Long, ByVal EventMask As UcsAsyncSocketEventMaskEnum) 54 | m_oParent.frTcpError ID, Socket, ErrorCode, EventMask 55 | End Sub 56 | 57 | -------------------------------------------------------------------------------- /test/Chat/frmClient.frx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wqweto/VbAsyncSocket/f5e922585800e05d42be166fa8301007a24fee94/test/Chat/frmClient.frx -------------------------------------------------------------------------------- /test/Chat/frmMain.frm: -------------------------------------------------------------------------------- 1 | VERSION 5.00 2 | Begin VB.Form frmMain 3 | Caption = "Sample Chat" 4 | ClientHeight = 3312 5 | ClientLeft = 108 6 | ClientTop = 456 7 | ClientWidth = 4956 8 | LinkTopic = "Form1" 9 | ScaleHeight = 3312 10 | ScaleWidth = 4956 11 | StartUpPosition = 3 'Windows Default 12 | Begin VB.CheckBox chkUdp 13 | Caption = "Use UDP" 14 | Height = 192 15 | Left = 588 16 | TabIndex = 0 17 | Top = 252 18 | Width = 3204 19 | End 20 | Begin VB.TextBox txtServer 21 | Height = 348 22 | Left = 504 23 | TabIndex = 3 24 | Top = 2352 25 | Width = 3960 26 | End 27 | Begin VB.CommandButton Command4 28 | Caption = "Client" 29 | Height = 432 30 | Left = 504 31 | TabIndex = 2 32 | Top = 1512 33 | Width = 1356 34 | End 35 | Begin VB.CommandButton Command3 36 | Caption = "Server" 37 | Height = 432 38 | Left = 504 39 | TabIndex = 1 40 | Top = 756 41 | Width = 1356 42 | End 43 | Begin VB.Label Label3 44 | Caption = "Server IP/Name (leave empty for local)" 45 | Height = 264 46 | Left = 504 47 | TabIndex = 6 48 | Top = 2100 49 | Width = 3120 50 | End 51 | Begin VB.Label Label2 52 | Caption = "(start single instance or restart)" 53 | Height = 264 54 | Left = 2016 55 | TabIndex = 5 56 | Top = 840 57 | Width = 2868 58 | End 59 | Begin VB.Label Label1 60 | Caption = "(start multiple clients)" 61 | Height = 264 62 | Left = 2016 63 | TabIndex = 4 64 | Top = 1596 65 | Width = 2868 66 | End 67 | End 68 | Attribute VB_Name = "frmMain" 69 | Attribute VB_GlobalNameSpace = False 70 | Attribute VB_Creatable = False 71 | Attribute VB_PredeclaredId = True 72 | Attribute VB_Exposed = False 73 | Option Explicit 74 | DefObj A-Z 75 | 76 | Private Const DEF_CHAT_PORT As Long = 5123 77 | 78 | Private Sub Command3_Click() 79 | On Error GoTo EH 80 | With frmServer 81 | .Init DEF_CHAT_PORT, IIf(chkUdp.Value = vbChecked, ucsSckDatagram, ucsSckStream) 82 | End With 83 | EH: 84 | End Sub 85 | 86 | Private Sub Command4_Click() 87 | On Error GoTo EH 88 | With New frmClient 89 | .Init txtServer.Text, DEF_CHAT_PORT, IIf(chkUdp.Value = vbChecked, ucsSckDatagram, ucsSckStream) 90 | End With 91 | EH: 92 | End Sub 93 | 94 | Private Sub Form_Load() 95 | Left = Screen.Width - Width - 1000 96 | End Sub 97 | -------------------------------------------------------------------------------- /test/Chat/frmServer.frx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wqweto/VbAsyncSocket/f5e922585800e05d42be166fa8301007a24fee94/test/Chat/frmServer.frx -------------------------------------------------------------------------------- /test/Chat/mdGlobals.bas: -------------------------------------------------------------------------------- 1 | Attribute VB_Name = "mdGlobals" 2 | Option Explicit 3 | DefObj A-Z 4 | 5 | '========================================================================= 6 | ' API 7 | '========================================================================= 8 | 9 | Private Const WM_VSCROLL As Long = &H115 10 | Private Const SB_BOTTOM As Long = 7 11 | 12 | Private Declare Function SendMessage Lib "user32" Alias "SendMessageW" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long 13 | 14 | '========================================================================= 15 | ' Functions 16 | '========================================================================= 17 | 18 | Public Function RtbAppendLine( _ 19 | rchCtl As InkEdit, _ 20 | sText As String, _ 21 | Optional ByVal HorAlign As SelAlignmentConstants = rtfLeft, _ 22 | Optional ByVal ForeColor As OLE_COLOR = vbWindowText) 23 | rchCtl.SelStart = &H7FFFFFFF 24 | rchCtl.SelAlignment = HorAlign 25 | rchCtl.SelColor = ForeColor 26 | rchCtl.SelText = sText & vbCrLf 27 | rchCtl.SelStart = &H7FFFFFFF 28 | Call SendMessage(rchCtl.hWnd, WM_VSCROLL, SB_BOTTOM, ByVal 0&) 29 | End Function 30 | 31 | 32 | Public Function Printf(ByVal sText As String, ParamArray A() As Variant) As String 33 | Const LNG_PRIVATE As Long = &HE1B6 '-- U+E000 to U+F8FF - Private Use Area (PUA) 34 | Dim lIdx As Long 35 | 36 | For lIdx = UBound(A) To LBound(A) Step -1 37 | sText = Replace(sText, "%" & (lIdx - LBound(A) + 1), Replace(A(lIdx), "%", ChrW$(LNG_PRIVATE))) 38 | Next 39 | Printf = Replace(sText, ChrW$(LNG_PRIVATE), "%") 40 | End Function 41 | 42 | Public Function SearchCollection(ByVal pCol As Object, Index As Variant, Optional RetVal As Variant) As Boolean 43 | On Error GoTo QH 44 | AssignVariant RetVal, pCol.Item(Index) 45 | SearchCollection = True 46 | QH: 47 | End Function 48 | 49 | Public Sub AssignVariant(vDest As Variant, vSrc As Variant) 50 | If IsObject(vSrc) Then 51 | Set vDest = vSrc 52 | Else 53 | vDest = vSrc 54 | End If 55 | End Sub 56 | -------------------------------------------------------------------------------- /test/Crypto/Form1.frm: -------------------------------------------------------------------------------- 1 | VERSION 5.00 2 | Begin VB.Form Form1 3 | Caption = "Form1" 4 | ClientHeight = 3504 5 | ClientLeft = 108 6 | ClientTop = 456 7 | ClientWidth = 5940 8 | LinkTopic = "Form1" 9 | ScaleHeight = 3504 10 | ScaleWidth = 5940 11 | StartUpPosition = 3 'Windows Default 12 | Begin VB.TextBox Text1 13 | Height = 1776 14 | Left = 420 15 | MultiLine = -1 'True 16 | ScrollBars = 2 'Vertical 17 | TabIndex = 6 18 | Text = "Form1.frx":0000 19 | Top = 1596 20 | Width = 5052 21 | End 22 | Begin VB.CommandButton Command6 23 | Caption = "Top Sites" 24 | Height = 516 25 | Left = 3864 26 | TabIndex = 5 27 | Top = 924 28 | Width = 1608 29 | End 30 | Begin VB.CommandButton Command5 31 | Caption = "ECDSA" 32 | Height = 516 33 | Left = 2100 34 | TabIndex = 4 35 | Top = 924 36 | Width = 1608 37 | End 38 | Begin VB.CommandButton Command4 39 | Caption = "ECDH" 40 | Height = 516 41 | Left = 336 42 | TabIndex = 3 43 | Top = 924 44 | Width = 1608 45 | End 46 | Begin VB.CommandButton Command3 47 | Caption = "Chacha20-Poly1305" 48 | Height = 516 49 | Left = 3864 50 | TabIndex = 2 51 | Top = 252 52 | Width = 1608 53 | End 54 | Begin VB.CommandButton Command2 55 | Caption = "AES-CBC" 56 | Height = 516 57 | Left = 2100 58 | TabIndex = 1 59 | Top = 252 60 | Width = 1608 61 | End 62 | Begin VB.CommandButton Command1 63 | Caption = "AES-GCM" 64 | Height = 516 65 | Left = 336 66 | TabIndex = 0 67 | Top = 252 68 | Width = 1608 69 | End 70 | End 71 | Attribute VB_Name = "Form1" 72 | Attribute VB_GlobalNameSpace = False 73 | Attribute VB_Creatable = False 74 | Attribute VB_PredeclaredId = True 75 | Attribute VB_Exposed = False 76 | Option Explicit 77 | DefObj A-Z 78 | 79 | Private Sub Command1_Click() 80 | TestCryptoAesGcm JsonParseObject(ReadTextFile(App.Path & "\wycheproof\testvectors\aes_gcm_test.json")) 81 | End Sub 82 | 83 | Private Sub Command2_Click() 84 | TestCryptoAesCbc JsonParseObject(ReadTextFile(App.Path & "\wycheproof\testvectors\aes_cbc_pkcs5_test.json")) 85 | End Sub 86 | 87 | Private Sub Command3_Click() 88 | TestCryptoChacha20 JsonParseObject(ReadTextFile(App.Path & "\wycheproof\testvectors\chacha20_poly1305_test.json")) 89 | End Sub 90 | 91 | Private Sub Command4_Click() 92 | TestCryptoEcdh JsonParseObject(ReadTextFile(App.Path & "\wycheproof\testvectors\ecdh_test.json")) 93 | End Sub 94 | 95 | Private Sub Command5_Click() 96 | TestCryptoEcdsa JsonParseObject(ReadTextFile(App.Path & "\wycheproof\testvectors\ecdsa_test.json")) 97 | End Sub 98 | 99 | Private Sub Command6_Click() 100 | Dim vElem As Variant 101 | Dim oSocket As cTlsSocket 102 | Dim lIdx As Long 103 | 104 | Set oSocket = New cTlsSocket 105 | Text1.Text = vbNullString 106 | For Each vElem In JsonValue(JsonParseObject(ReadTextFile(App.Path & "\top-sites.json")), "*/rootDomain") 107 | lIdx = lIdx + 1 108 | If lIdx > 0 Then 109 | If Not oSocket.SyncConnect(CStr(vElem), 443, 5000, LocalFeatures:=ucsTlsSupportTls12) Then 110 | Text1.Text = Text1.Text & vElem & vbTab & oSocket.LastError.Description & vbCrLf 111 | Text1.SelStart = Len(Text1.Text) 112 | End If 113 | oSocket.Close_ 114 | DoEvents 115 | End If 116 | Next 117 | End Sub 118 | 119 | Private Sub Form_Resize() 120 | On Error GoTo QH 121 | Text1.Width = ScaleWidth - Text1.Left - Text1.Left 122 | Text1.Height = ScaleHeight - Text1.Top - 240 123 | QH: 124 | End Sub 125 | -------------------------------------------------------------------------------- /test/Crypto/Form1.frx: -------------------------------------------------------------------------------- 1 | Text1 -------------------------------------------------------------------------------- /test/Crypto/Project1.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Form=Form1.frm 3 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 4 | Module=mdJson; mdJson.bas 5 | Module=mdTlsThunks; ..\..\src\mdTlsThunks.bas 6 | Module=mdGlobals; mdGlobals.bas 7 | Class=cAsyncSocket; ..\..\src\cAsyncSocket.cls 8 | Class=cTlsSocket; ..\..\src\cTlsSocket.cls 9 | IconForm="Form1" 10 | Startup="Form1" 11 | HelpFile="" 12 | Title="Project1" 13 | Command32="" 14 | Name="Wycheproof" 15 | HelpContextID="0" 16 | CompatibleMode="0" 17 | MajorVer=1 18 | MinorVer=0 19 | RevisionVer=0 20 | AutoIncrementVer=0 21 | ServerSupportFiles=0 22 | VersionCompanyName="Unicontsoft" 23 | CondComp="ASYNCSOCKET_TEST_CRYPTO = 1 : USE_DEBUG_LOG = 1 : Dummy = 1" 24 | CompilationType=0 25 | OptimizationType=0 26 | FavorPentiumPro(tm)=0 27 | CodeViewDebugInfo=0 28 | NoAliasing=0 29 | BoundsCheck=0 30 | OverflowCheck=0 31 | FlPointCheck=0 32 | FDIVCheck=0 33 | UnroundedFP=0 34 | StartMode=0 35 | Unattended=0 36 | Retained=0 37 | ThreadPerObject=0 38 | MaxNumberOfThreads=1 39 | -------------------------------------------------------------------------------- /test/Crypto/mdGlobals.bas: -------------------------------------------------------------------------------- 1 | Attribute VB_Name = "mdGlobals" 2 | Option Explicit 3 | DefObj A-Z 4 | 5 | #Const ImplUseDebugLog = (USE_DEBUG_LOG <> 0) 6 | 7 | Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) 8 | Private Declare Function IsBadReadPtr Lib "kernel32" (ByVal lp As Long, ByVal ucb As Long) As Long 9 | Private Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long 10 | Private Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long 11 | Private Declare Function IsTextUnicode Lib "advapi32" (lpBuffer As Any, ByVal cb As Long, lpi As Long) As Long 12 | 13 | Public Function DesignDumpArray(baData() As Byte, Optional ByVal Pos As Long, Optional ByVal Size As Long = -1) As String 14 | If Size < 0 Then 15 | Size = UBound(baData) + 1 - Pos 16 | End If 17 | If Size > 0 Then 18 | DesignDumpArray = DesignDumpMemory(VarPtr(baData(Pos)), Size) 19 | End If 20 | End Function 21 | 22 | Public Function DesignDumpMemory(ByVal lPtr As Long, ByVal lSize As Long) As String 23 | Dim lIdx As Long 24 | Dim sHex As String 25 | Dim sChar As String 26 | Dim lValue As Long 27 | Dim aResult() As String 28 | 29 | ReDim aResult(0 To (lSize + 15) \ 16) As String 30 | ' Debug.Assert RedimStats("DesignDumpMemory.aResult", UBound(aResult) + 1) 31 | For lIdx = 0 To ((lSize + 15) \ 16) * 16 32 | If lIdx < lSize Then 33 | If IsBadReadPtr(lPtr, 1) = 0 Then 34 | Call CopyMemory(lValue, ByVal lPtr, 1) 35 | sHex = sHex & Right$("0" & Hex$(lValue), 2) & " " 36 | If lValue >= 32 Then 37 | sChar = sChar & Chr$(lValue) 38 | Else 39 | sChar = sChar & "." 40 | End If 41 | Else 42 | sHex = sHex & "?? " 43 | sChar = sChar & "." 44 | End If 45 | Else 46 | sHex = sHex & " " 47 | End If 48 | If ((lIdx + 1) Mod 4) = 0 Then 49 | sHex = sHex & " " 50 | End If 51 | If ((lIdx + 1) Mod 16) = 0 Then 52 | aResult(lIdx \ 16) = Right$("000" & Hex$(lIdx - 15), 4) & " - " & sHex & sChar 53 | sHex = vbNullString 54 | sChar = vbNullString 55 | End If 56 | lPtr = (lPtr Xor &H80000000) + 1 Xor &H80000000 57 | Next 58 | DesignDumpMemory = Join(aResult, vbCrLf) 59 | End Function 60 | 61 | Public Property Get TimerEx() As Double 62 | Dim cFreq As Currency 63 | Dim cValue As Currency 64 | 65 | Call QueryPerformanceFrequency(cFreq) 66 | Call QueryPerformanceCounter(cValue) 67 | TimerEx = cValue / cFreq 68 | End Property 69 | 70 | #If ImplUseDebugLog Then 71 | Public Sub DebugLog(sModule As String, sFunction As String, sText As String, Optional ByVal eType As LogEventTypeConstants = vbLogEventTypeInformation) 72 | Debug.Print Format$(TimerEx, "0.000") & " " & Switch( _ 73 | eType = vbLogEventTypeError, "[ERROR]", _ 74 | eType = vbLogEventTypeWarning, "[WARN]", _ 75 | True, "[INFO]") & " " & sText & " [" & sModule & "." & sFunction & "]" 76 | End Sub 77 | #End If 78 | 79 | Public Function ReadTextFile(sFile As String) As String 80 | Dim BOM_UTF8 As String: BOM_UTF8 = Chr$(&HEF) & Chr$(&HBB) & Chr$(&HBF) 81 | Dim BOM_UNICODE As String: BOM_UNICODE = Chr$(&HFF) & Chr$(&HFE) 82 | Const ForReading As Long = 1 83 | Dim lSize As Long 84 | Dim sPrefix As String 85 | 86 | With CreateObject("Scripting.FileSystemObject") 87 | lSize = .GetFile(sFile).Size 88 | If lSize = 0 Then 89 | Exit Function 90 | End If 91 | sPrefix = .OpenTextFile(sFile, ForReading).Read(IIf(lSize < 50, lSize, 50)) 92 | If Left$(sPrefix, Len(BOM_UTF8)) <> BOM_UTF8 And Left$(sPrefix, Len(BOM_UNICODE)) <> BOM_UNICODE Then 93 | '--- special xml encoding test 94 | If InStr(1, sPrefix, " 0 And InStr(1, sPrefix, "utf-8", vbTextCompare) > 0 Then 95 | sPrefix = BOM_UTF8 96 | End If 97 | End If 98 | If Left$(sPrefix, Len(BOM_UTF8)) <> BOM_UTF8 Then 99 | On Error GoTo QH 100 | ReadTextFile = .OpenTextFile(sFile, ForReading, False, Left$(sPrefix, Len(BOM_UNICODE)) = BOM_UNICODE Or IsTextUnicode(ByVal sPrefix, Len(sPrefix), &HFFFF& - 2) <> 0).ReadAll() 101 | Else 102 | With CreateObject("ADODB.Stream") 103 | .Open 104 | If Left$(sPrefix, Len(BOM_UNICODE)) = BOM_UNICODE Then 105 | .Charset = "Unicode" 106 | ElseIf Left$(sPrefix, Len(BOM_UTF8)) = BOM_UTF8 Then 107 | .Charset = "UTF-8" 108 | Else 109 | .Charset = "_autodetect_all" 110 | End If 111 | .LoadFromFile sFile 112 | ReadTextFile = .ReadText 113 | End With 114 | End If 115 | End With 116 | QH: 117 | End Function 118 | 119 | Public Function ToHex(baText() As Byte, Optional Delimiter As String = "-") As String 120 | Dim aText() As String 121 | Dim lIdx As Long 122 | 123 | If LenB(CStr(baText)) <> 0 Then 124 | ReDim aText(0 To UBound(baText)) As String 125 | ' Debug.Assert RedimStats("ToHex.aText", 0) 126 | For lIdx = 0 To UBound(baText) 127 | aText(lIdx) = Right$("0" & Hex$(baText(lIdx)), 2) 128 | Next 129 | ToHex = Join(aText, Delimiter) 130 | End If 131 | End Function 132 | 133 | Public Function FromHex(sText As String) As Byte() 134 | Dim baRetVal() As Byte 135 | Dim lIdx As Long 136 | 137 | On Error GoTo QH 138 | '--- check for hexdump delimiter 139 | If sText Like "*[!0-9A-Fa-f]*" Then 140 | ReDim baRetVal(0 To Len(sText) \ 3) As Byte 141 | ' Debug.Assert RedimStats("FromHex.baRetVal", UBound(baRetVal) + 1) 142 | For lIdx = 1 To Len(sText) Step 3 143 | baRetVal(lIdx \ 3) = "&H" & Mid$(sText, lIdx, 2) 144 | Next 145 | ElseIf LenB(sText) <> 0 Then 146 | ReDim baRetVal(0 To Len(sText) \ 2 - 1) As Byte 147 | ' Debug.Assert RedimStats("FromHex.baRetVal", UBound(baRetVal) + 1) 148 | For lIdx = 1 To Len(sText) Step 2 149 | baRetVal(lIdx \ 2) = "&H" & Mid$(sText, lIdx, 2) 150 | Next 151 | Else 152 | baRetVal = vbNullString 153 | End If 154 | FromHex = baRetVal 155 | QH: 156 | End Function 157 | 158 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/ca.key: -------------------------------------------------------------------------------- 1 | -----BEGIN EC PRIVATE KEY----- 2 | MHcCAQEEIChU0fGAx/HGjIzAd7MTJxt3cE6bPuosaVhYdYggOtLRoAoGCCqGSM49 3 | AwEHoUQDQgAE7DQQSekoSAt9e+RfqaZeyFUBHia++NG83Mtjm5Gqb7MkcNQfTVkK 4 | CXU3FetGy9P+9Im2iiXKMFjBJKQydFVXww== 5 | -----END EC PRIVATE KEY----- 6 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/ca.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIICXjCCAgWgAwIBAgIUZX1mdiJCQHIplAWDkekiaXBatvQwCgYIKoZIzj0EAwIw 3 | gYQxCzAJBgNVBAYTAkJHMQwwCgYDVQQIDANOL0ExDjAMBgNVBAcMBVNvZmlhMRQw 4 | EgYDVQQKDAtVbmljb250c29mdDEPMA0GA1UECwwGV1FXLVBDMQwwCgYDVQQDDAN3 5 | cXcxIjAgBgkqhkiG9w0BCQEWE3dxd0B1bmljb250c29mdC5jb20wHhcNMjAwNTEx 6 | MTA0OTU5WhcNMzAwNTA5MTA0OTU5WjCBhDELMAkGA1UEBhMCQkcxDDAKBgNVBAgM 7 | A04vQTEOMAwGA1UEBwwFU29maWExFDASBgNVBAoMC1VuaWNvbnRzb2Z0MQ8wDQYD 8 | VQQLDAZXUVctUEMxDDAKBgNVBAMMA3dxdzEiMCAGCSqGSIb3DQEJARYTd3F3QHVu 9 | aWNvbnRzb2Z0LmNvbTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABOw0EEnpKEgL 10 | fXvkX6mmXshVAR4mvvjRvNzLY5uRqm+zJHDUH01ZCgl1NxXrRsvT/vSJtoolyjBY 11 | wSSkMnRVV8OjUzBRMB0GA1UdDgQWBBR70SSY2LR/prc46ijxteZRUxNiwDAfBgNV 12 | HSMEGDAWgBR70SSY2LR/prc46ijxteZRUxNiwDAPBgNVHRMBAf8EBTADAQH/MAoG 13 | CCqGSM49BAMCA0cAMEQCIGRpOKJXVryduat7xMcgDVFEsyaJXaI8a8oyLYZj+b0W 14 | AiBl7YHEN9q4nNonLRx1sUluDmLEtmqOYhkPl+yFtyLR+w== 15 | -----END CERTIFICATE----- 16 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/client1.csr: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE REQUEST----- 2 | MIIBQDCB5wIBADCBhDELMAkGA1UEBhMCQkcxDDAKBgNVBAgMA04vQTEOMAwGA1UE 3 | BwwFU29maWExFDASBgNVBAoMC1VuaWNvbnRzb2Z0MQ8wDQYDVQQLDAZXUVctUEMx 4 | DDAKBgNVBAMMA3dxdzEiMCAGCSqGSIb3DQEJARYTd3F3QHVuaWNvbnRzb2Z0LmNv 5 | bTBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABIbjfuiMmAoat0XR6dOFs+gfYRyT 6 | 3P2ctozH6S1zsagwmKU7HlRwlon2qNs9eg/b4CZfili/NB16W7UBwTRfHAWgADAK 7 | BggqhkjOPQQDAgNIADBFAiA+GFc5P5ytnFmxJUTnQAdqdZ/K3uG2PV4oT4ioypIZ 8 | lAIhALecX1bZ4bulWgNIpTt29YyBm3EqtGyhNakVegfkBnC1 9 | -----END CERTIFICATE REQUEST----- 10 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/client1.key: -------------------------------------------------------------------------------- 1 | -----BEGIN EC PRIVATE KEY----- 2 | MHcCAQEEICJeWZ9iD3ACwvHYTYjj8QhUWdV3t9Mp2PvexxoEFKPboAoGCCqGSM49 3 | AwEHoUQDQgAEhuN+6IyYChq3RdHp04Wz6B9hHJPc/Zy2jMfpLXOxqDCYpTseVHCW 4 | ifao2z16D9vgJl+KWL80HXpbtQHBNF8cBQ== 5 | -----END EC PRIVATE KEY----- 6 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/client1.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIB8zCCAZkCAgPoMAoGCCqGSM49BAMCMIGEMQswCQYDVQQGEwJCRzEMMAoGA1UE 3 | CAwDTi9BMQ4wDAYDVQQHDAVTb2ZpYTEUMBIGA1UECgwLVW5pY29udHNvZnQxDzAN 4 | BgNVBAsMBldRVy1QQzEMMAoGA1UEAwwDd3F3MSIwIAYJKoZIhvcNAQkBFhN3cXdA 5 | dW5pY29udHNvZnQuY29tMB4XDTIwMDUxMTEwNTUzN1oXDTMwMDUwOTEwNTUzN1ow 6 | gYQxCzAJBgNVBAYTAkJHMQwwCgYDVQQIDANOL0ExDjAMBgNVBAcMBVNvZmlhMRQw 7 | EgYDVQQKDAtVbmljb250c29mdDEPMA0GA1UECwwGV1FXLVBDMQwwCgYDVQQDDAN3 8 | cXcxIjAgBgkqhkiG9w0BCQEWE3dxd0B1bmljb250c29mdC5jb20wWTATBgcqhkjO 9 | PQIBBggqhkjOPQMBBwNCAASG437ojJgKGrdF0enThbPoH2Eck9z9nLaMx+ktc7Go 10 | MJilOx5UcJaJ9qjbPXoP2+AmX4pYvzQdelu1AcE0XxwFMAoGCCqGSM49BAMCA0gA 11 | MEUCIQCi3YB7MMtJ/1/gsGA/UJQaSZUNvD2InoJ6/uv2b1LiKQIgc86RPqnW2ZEa 12 | goaNxZfAz7UClENCt44Y/If2z53N/Wk= 13 | -----END CERTIFICATE----- 14 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/client1.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wqweto/VbAsyncSocket/f5e922585800e05d42be166fa8301007a24fee94/test/Secure/ClientCerts/client1.pfx -------------------------------------------------------------------------------- /test/Secure/ClientCerts/client2.csr: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE REQUEST----- 2 | MIICijCCAXICAQAwRTELMAkGA1UEBhMCQkcxEzARBgNVBAgMClNvbWUtU3RhdGUx 3 | ITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcN 4 | AQEBBQADggEPADCCAQoCggEBAOddIS3C5EERm7pxfoog6MwynXHKI7NwguI4fgeV 5 | H4lvt4A24FHeTmVhunboEvkd//YgSd2C6D5pqk7LPUbRTsXSUpVgGyykqoXwvhbw 6 | HbAGhNVk4mPBbYwk3tU5BP6zeA3+wjjgNj80sDR4kEe6UhXuN4PPDaO49oi5mM4x 7 | VeV+XraSFCUdH5jAnfbulSfF3O0h9kDVTyRcyjX2ipZ+wjyInlKTmIu1GFz6h0oQ 8 | oHl2fbRBimLA8SZfw6VYHXef5rlk527fU7WmYPurIcHjkqEEzMzrIJay8hnp6Oo2 9 | UlN9W5y5U49iD76hLza9TeXerjLTOLG7CYZrF7U7HNmpeEkCAwEAAaAAMA0GCSqG 10 | SIb3DQEBCwUAA4IBAQBbYtYsLYnhwNx1MRtU+HXWM6Q3YJN+QIhvpQJ3chwRE0mV 11 | TyEv3vbZ/Z12d8owQ+pUygZWoQS655SgTVgnu8XwKyUuBzDHA2bG+Uc4vmlMBc8s 12 | r6H+wblu/YWCyVxQvEC6KhrsTOWorvblErxWoL6YOnbKlXpQq4PYu194g+VRpN0q 13 | S1xPeVZIN3QeBmUYY5EvriImNBqpEwL0Qb0ba6csZI8FtTepahf+2T/FcbMEAUAt 14 | 37eMvSxLK00zYXScKS2VgV5si76h8uqnOWYHpLmzO/T2JtYzIJDouFcv4Z4hX0Sa 15 | OtiEB9rQZqwBJuYSfHCQ/lET3kjKr+MIa6Vg9gek 16 | -----END CERTIFICATE REQUEST----- 17 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/client2.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEpAIBAAKCAQEA510hLcLkQRGbunF+iiDozDKdccojs3CC4jh+B5UfiW+3gDbg 3 | Ud5OZWG6dugS+R3/9iBJ3YLoPmmqTss9RtFOxdJSlWAbLKSqhfC+FvAdsAaE1WTi 4 | Y8FtjCTe1TkE/rN4Df7COOA2PzSwNHiQR7pSFe43g88No7j2iLmYzjFV5X5etpIU 5 | JR0fmMCd9u6VJ8Xc7SH2QNVPJFzKNfaKln7CPIieUpOYi7UYXPqHShCgeXZ9tEGK 6 | YsDxJl/DpVgdd5/muWTnbt9TtaZg+6shweOSoQTMzOsglrLyGeno6jZSU31bnLlT 7 | j2IPvqEvNr1N5d6uMtM4sbsJhmsXtTsc2al4SQIDAQABAoIBAGNcEnClnwL2ves+ 8 | 7HoDkhaHsif5CIX6Tqs6WZ2GBEowqRyt9H4UO5S8eKiyF9vWb2NTEIXJv1V2aYZ3 9 | L/Sm1O6N2Zt+74ynlvf4oJoXOpiq5AcE0VjH6/TgrLXjS+raRje0bdKhL4rLZNTT 10 | 5hp3wOLcMAkyNYhXWcDz+9SpSVtJrcY6pXDuHWTuEzuDl7TFwbCXlKCDOfMjoV9p 11 | e7uOH1jzNvAJ2Nysh1gXrZbwZ7YhZsoMUjSOLgAtdWTvVVvpF+6J/yndq3r/QdOz 12 | wTr6qyV2CQNn0MK4siXPHHbNCfKh7qLywxP1UsIIdJtgmRSpoRbNbeO+Eu62sXe/ 13 | wID/9IUCgYEA/h/U9b/Yq01/NEz74pypIrltBvYysPzxiIfNo3cgzbpCKoa+BoND 14 | T44o8vDnJwNQ15iqohCdWsrNQkp8qpDbABtMBHndfFP26d87jAoSuB+77fPXBFDT 15 | AH+d9WQ9vxKpFQlAOowtpcWVqKbNGehh89ycXxD+q1+thIYiOiTIIesCgYEA6RJK 16 | qSe4cLyz3Pci3dX7+CZlsMaMSugEK3Ms1LQJ1qWZFplM2mKNdBpCNxDS0ogHPhNW 17 | sG22vhNbXufcsTksLOwHwLsh5pVoAGTHORkv0je1Gwx677nlZK/L9A06uMkKCcpM 18 | ckfiFO7yYQ/eQxiKZquLwK+NmbGue8IlcctMDZsCgYAe53wcjsIowvnvQFV5j5e6 19 | E1bA+/vaLkipU+qcMIaavkYBqfU+EYSEDkPIl6dkxTB1p6lIoECio/smTqyuiYM4 20 | lhk+iht3XTav5mf1ddwk7CcMnxTM/49JhjuNdi1UKU7Ksh9Pf3ClskurAO38cphG 21 | 8dNKdGSkLrSJY06SR5rXFQKBgQDNt1Roy2NTNmt27Dc9ICMfGx0Ek6q+bt1HUIhQ 22 | UXyhlVZzCOo5k2URBiBNpC7AnknuM3l/jccPK9yo/vPfBPyFxert5P3+HP9H4auq 23 | LR31aTAT6McVkrnzTesxxP+AnAmdLI6fC+1z1pferffatu1pw549e3X7I0ZDe9A7 24 | CVpeYwKBgQDaARpo5lR0ywZZtWRFzxF2J0mcwKZ25HY0XDd/EILrn1bVoJHoUvhj 25 | ETIikrU5zRRslqcI2j+rFtNm9ybyVlUGzor2X8ozBZYopmnbz/h7bCkQpqsvXog6 26 | UEQy3SjALTeq3dWwQvfi/ua8oKy7Ssm78Q0v6gVyg1qkjqrYSt00sg== 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/client2.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIICfDCCAiMCAQIwCgYIKoZIzj0EAwIwgYQxCzAJBgNVBAYTAkJHMQwwCgYDVQQI 3 | DANOL0ExDjAMBgNVBAcMBVNvZmlhMRQwEgYDVQQKDAtVbmljb250c29mdDEPMA0G 4 | A1UECwwGV1FXLVBDMQwwCgYDVQQDDAN3cXcxIjAgBgkqhkiG9w0BCQEWE3dxd0B1 5 | bmljb250c29mdC5jb20wHhcNMjIwNzE2MTIxMTU3WhcNMzIwNzEzMTIxMTU3WjBF 6 | MQswCQYDVQQGEwJCRzETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50 7 | ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB 8 | CgKCAQEA510hLcLkQRGbunF+iiDozDKdccojs3CC4jh+B5UfiW+3gDbgUd5OZWG6 9 | dugS+R3/9iBJ3YLoPmmqTss9RtFOxdJSlWAbLKSqhfC+FvAdsAaE1WTiY8FtjCTe 10 | 1TkE/rN4Df7COOA2PzSwNHiQR7pSFe43g88No7j2iLmYzjFV5X5etpIUJR0fmMCd 11 | 9u6VJ8Xc7SH2QNVPJFzKNfaKln7CPIieUpOYi7UYXPqHShCgeXZ9tEGKYsDxJl/D 12 | pVgdd5/muWTnbt9TtaZg+6shweOSoQTMzOsglrLyGeno6jZSU31bnLlTj2IPvqEv 13 | Nr1N5d6uMtM4sbsJhmsXtTsc2al4SQIDAQABMAoGCCqGSM49BAMCA0cAMEQCIECJ 14 | xkJQR+O814zc/7ABlfuU5QBuIiHYxDyqDZfzaQFxAiA4AxhsyUL5MyAys/w3yryi 15 | Dtt+LfhRE6zZ995V9l3skg== 16 | -----END CERTIFICATE----- 17 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/gen_ca.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | openssl ecparam -genkey -name secp256r1 | openssl ec -out ca.key 4 | openssl req -new -x509 -days 3650 -key ca.key -out ca.pem 5 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/gen_client.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal 3 | set "CLIENT_ID=%~1" 4 | set "CLIENT_SERIAL=%~2" 5 | 6 | if [%CLIENT_ID%]==[] echo usage: %~nx0 ^ ^& exit /b 1 7 | 8 | openssl ecparam -genkey -name secp256r1 | openssl ec -out "%CLIENT_ID%.key" 9 | openssl req -new -key "%CLIENT_ID%.key" -out "%CLIENT_ID%.csr" 10 | openssl x509 -req -days 3650 -in "%CLIENT_ID%.csr" -CA ca.pem -CAkey ca.key -set_serial %CLIENT_SERIAL% -out "%CLIENT_ID%.pem" 11 | openssl pkcs12 -export -out %CLIENT_ID%.full.pfx -inkey "%CLIENT_ID%.key" -in "%CLIENT_ID%.pem" -certfile ca.pem 12 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/rsa/ca_rsa.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIJKQIBAAKCAgEA6qzIyPmmw+BihZalN0IMsRayzpgFderAqdxA6VvsHRaL0PWO 3 | fUXiPqk7wC2s1njKJORlUqH3R0GvRkCBZ4QW8PbZQk9nZk7nQX3KgrJkrKfuPJ+D 4 | DSjT9UqcoPxAtypX3zMJsWHNnugA0Ak7dGH8Zkk/j7REo+J9wyfuwVTlxb4+KLPs 5 | fE0HOvYt694uRY09DpJ9QYoe10dmJdNhv8xhiSXJZa4znCgBFJwd/AsC5DfgD+qE 6 | D/bhhECQL3UeSAFg4GjjaU6414EyKdAUGoxcc6X6QVY3X0J2Bae2WD/T4ZLf/efE 7 | pnPbShY6iEi2PkvuKph+K/vHz+8YZ6xx4Ozs1w4WOH8aPxJk+k9eomHZ4x8aV7CT 8 | rqubEuebQYL3s4Zxr6BKIaI7O7sYgL+GXZGVMHUBDvoAVtQiJs6nw9NxPVnO8wk6 9 | 5YK47iJXqpNOtaJ1ElzRPd0J3GdcXE9Gecm3ABG1dmTxeKUnIenGXk5a9knPjAzB 10 | NPJSRuvdipP+dboUSquVE1sezDd+bXAZK4g3RUJ0J8K0Xa+t1ShgjAvh+KmCcf9/ 11 | vU6X7ds68qWXT7gO1Wyz5eAZ3LNa0reEmwSzCo3sTg5iscGdwA4vipL7KB471lUK 12 | At4YgpXCIR8Ml2SgwlvVoT2V7eurwRbjufQ+y4PX8DIizrkvzjKaj0j887sCAwEA 13 | AQKCAgA+JCa+4SzBrYcpLKzvJvRJaGNMxYCcqt79m68snAS35yJ64KssjPbN9O43 14 | g2DDIhUVE82oxe6+1aveBHK/wIB6qkL5hiExPC+MbgzT8v0powY401xWfbsytuEm 15 | 1+UjG3lNRyyH5ggWzgR5EhjGRrQZOcg0+105sF3w8YANfbeiVzAKgypZ9+O0P0AF 16 | tz21eSLz2uKEGlj0VJm95m155lrlgDYGUMylywoONRDTju7jE9CnJkh2Gb+5z4be 17 | qjqsgoDuq5ecaRYVKsbnv4uzQ/uvPAI6juP2u5iZAsIMOEqY7tWVXGv8T61/vscd 18 | E3P+pwiYCu1OJEvSydNCkK1PDC3ieqDwBnJFw/L7rYKVxcMiEgOz0BHWKh9zB/9G 19 | BK6RptrJwbDvuTgV2C1YcBC4Xi7DW4APJu4raBTTb2sCvrAotSKWCWR6h9UL3rq9 20 | KTBlmHsAK25owgcWI2txb/aM2FpW7npy91/iDDCA+Ln+FG69uKvADiKy55jrm7hB 21 | CvmnsEASFw2DLJ6pbXIiE0jFlgLpGWNUVFCMh0MR8yvt/66SEy2RRaWVY8pgkeX9 22 | fdTD8nCd91Yg6AJjm+SXMUClxQwAGaZK5zEqyXD3S61ZoTCBeJn2cJHF8Ah5TFoQ 23 | e3KYy8CMkehOcGayTCIwSVk0cSofxGmWfEisTiBBXSRoQBSbcQKCAQEA/HdyHIuc 24 | uHfNkNHP6MRev5nwd6GJ1ZXsEK3Ioa6Z6gltn19WxE7uWYsxWQNRU4bRDrTC+00c 25 | MckB5wo1A1p44Cc16hQl3g4AUtalKlpuHh61/zaFWhUwcKRlJm7xaYzEwBijH+g7 26 | dpN1WHw+CQMT4OotsmCzODpqNaEyxne1KgiqUrekVjfBgcn3xTik8E2MoGMJLiUR 27 | XhIunHBEjsV46iAy75SdWcf8g/9Kcj4//0NwsP1FJgLdX9Mg7ZswU9GjwO/6VKsv 28 | p9HkkoQwg7ezC7s0QAGUBYtRYnPlChqAxE/FZmxDyIcQqckb6tAs2xQB+Kye1tMG 29 | KQrH1ErgwEvNNQKCAQEA7fWX7SjBl4w7hKkn6My0mBAOqC2tlZc0mKRGOTnmo0Zo 30 | ljkaUSLeHsxKZMEOrYhJeYrzw/gVsT5zaM3DiicfB/1PfOAtidNw/EfvE7nK0rgB 31 | 0E3626UZUzAmMlAKracSFzSKExqf/MRledmHlclhm2nT3cYDH7wPRWM/HrWI3KO1 32 | 9NrN9XH429Rjpkqi4CEzVTRZvVmPaXtqRJJpe3NjyD1y+FUsgSIiMbJUB8SLjxGL 33 | 2vS6ik4OXmnwbS9xRlFomwfiWhh8Xj9hoAIDXvq40zJgBDaCd+wVqWMcV+6D0A/1 34 | R/LnfiDZ2iyTAUZMxpQ/bkuFqvuhou4RWj5BGV0LLwKCAQEA5ahCRXU6uxNAC9zQ 35 | JA19ow2FkU2A9KnMc9+iAnGx0ROChNdVNn4Wk53q5e3+rNyWj6ofNDqQyzjuYh0G 36 | 3GYQTtdcU/K//5787F4VINHiGcQWCnKuG6PqzIy+9pc1Q66py5SdzwiSDutUc4mR 37 | ZOFH0/EyYHFWlm+M7fwVFDUQt4vqZBRpiYRuooH2wZElePMSWVO9phxSECC6BdSa 38 | iMVZmKBxtT6bZHBnZCt2rTWzQaE3gcjifhFdDy4fZgUlkRnCvF+QeDTAxQhJxH47 39 | I5aXaLSCczb8T47MGaMTVn2FZQ6rouAZSPCUV955ZsC4DRHGOU/Nrd2IXQ1Reewj 40 | RsmtgQKCAQBsexsAxiTDZu23Q7pq+e+WpzNXXkOVP7J9p95PtmK7qpyBZ5TkykWC 41 | utVra0u6trfwTfVlMJMLE5Jfg/aR49XUuZlkL3rzK06F5HFMh9W8DKDfCr3DJlgO 42 | QZQYOb80fUbVB2Gu5eNrw0kTrKSQF5eE11D7+IpyDDTWCDG9WTsEgymvb5SXQZcW 43 | +JNZOwtXrCsPu+bxWIP1rYq7qhembOtfXQwtw/6e/CWkjEycofCRj4k3j3v/Eyif 44 | yCIQVKGKUhCjJIv/UMxZ1wE1hdVtllVuCrgVE5iU/S6+4u0H9TdoQe4rF8SLDoTi 45 | XpHB8f+MRAdKy+krEr4UXf3RKzJYUr+HAoIBAQCorIBHyxM5khz5quu+wx8MR5/4 46 | h8nuKvciOLfzEklQSHWxEzXtXkIOTxygvmz6wdVMR2P8eSJ4Ud7dyj3roe8n9aWD 47 | 8C3NXLTgrh6U2VnDZCiTW8Quz6RsczmXLyWBjLUKYKsUmg3KcsP4fTmT/Qyjt1qH 48 | Kxdls21WA0pRferlbymjcqxI1oC8GG+7EfPQpghcQlqQBd0jgM76cp2IrBpf5LZW 49 | uaL9ftuho3ZrOozR15QFmTjO32fYGCg2jNEnt4cvr8sIFeg5X3EAnpHkawj5ZdxI 50 | aSp/Qfid6N6O9kCohOrxlFRF+XIO1Iy+zw38bbuyUGCd0IpYYpN/rtF8gjLz 51 | -----END RSA PRIVATE KEY----- 52 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/rsa/ca_rsa.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIFrzCCA5egAwIBAgIUZBMQHmI/mxCLQ69awXDMPKNDB3YwDQYJKoZIhvcNAQEL 3 | BQAwZzELMAkGA1UEBhMCQkcxDDAKBgNVBAgMA04vQTEOMAwGA1UEBwwFU29maWEx 4 | FDASBgNVBAoMC1VuaWNvbnRzb2Z0MSQwIgYJKoZIhvcNAQkBFhVhZG1pbkB1bmlj 5 | b250c29mdC5jb20wHhcNMjIwNzE2MTgxOTQ4WhcNMzIwNzEzMTgxOTQ4WjBnMQsw 6 | CQYDVQQGEwJCRzEMMAoGA1UECAwDTi9BMQ4wDAYDVQQHDAVTb2ZpYTEUMBIGA1UE 7 | CgwLVW5pY29udHNvZnQxJDAiBgkqhkiG9w0BCQEWFWFkbWluQHVuaWNvbnRzb2Z0 8 | LmNvbTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAOqsyMj5psPgYoWW 9 | pTdCDLEWss6YBXXqwKncQOlb7B0Wi9D1jn1F4j6pO8AtrNZ4yiTkZVKh90dBr0ZA 10 | gWeEFvD22UJPZ2ZO50F9yoKyZKyn7jyfgw0o0/VKnKD8QLcqV98zCbFhzZ7oANAJ 11 | O3Rh/GZJP4+0RKPifcMn7sFU5cW+Piiz7HxNBzr2LeveLkWNPQ6SfUGKHtdHZiXT 12 | Yb/MYYklyWWuM5woARScHfwLAuQ34A/qhA/24YRAkC91HkgBYOBo42lOuNeBMinQ 13 | FBqMXHOl+kFWN19CdgWntlg/0+GS3/3nxKZz20oWOohItj5L7iqYfiv7x8/vGGes 14 | ceDs7NcOFjh/Gj8SZPpPXqJh2eMfGlewk66rmxLnm0GC97OGca+gSiGiOzu7GIC/ 15 | hl2RlTB1AQ76AFbUIibOp8PTcT1ZzvMJOuWCuO4iV6qTTrWidRJc0T3dCdxnXFxP 16 | RnnJtwARtXZk8XilJyHpxl5OWvZJz4wMwTTyUkbr3YqT/nW6FEqrlRNbHsw3fm1w 17 | GSuIN0VCdCfCtF2vrdUoYIwL4fipgnH/f71Ol+3bOvKll0+4DtVss+XgGdyzWtK3 18 | hJsEswqN7E4OYrHBncAOL4qS+ygeO9ZVCgLeGIKVwiEfDJdkoMJb1aE9le3rq8EW 19 | 47n0PsuD1/AyIs65L84ymo9I/PO7AgMBAAGjUzBRMB0GA1UdDgQWBBT2UzOp8ID3 20 | 3HkdrLMnhEkcruao2jAfBgNVHSMEGDAWgBT2UzOp8ID33HkdrLMnhEkcruao2jAP 21 | BgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4ICAQCqxbomgMsTkP2g8ure 22 | De9dIiYRqdypNwfDIX8a2E/X6NHVtWeCXNh41k8yzry0vRIOFosfKNIYbLs1Vdvx 23 | 3uRJ/zP17nyCEOV8KBfF7L/F9JKul9hqVqTVlH7B7b/UdJb+/xlWDBsuLavV1JKI 24 | ydgrh/4mOJhT+II3vsqky01vsnss0eHRcmgyMKM1NXCE/sWey6F0dDfOFtld855O 25 | d8+YY/2vqtNVxwnvLEVlRSTYibY/8emZ4ziRXZz+49+GkR8Wtoxg4bXaRZy5Nh0t 26 | xdrZ87X7D698vb2ESZRQxXYRXqHDTZXQ5oVF2i+ONM7UlL4yN7PsXK/2s85nwLLK 27 | vyXf+rs6qMy8F9UlQ9w116eH/0Z7rUXXTbHxq0Wd05+OV44HXqSYIRoUIY+AZqY1 28 | TVQnC5Chp1A6l2p3lXe1UgrrQJbp/TsGrctwmuOQV6k32c8Q2Ssw6UE3/ISHDi+W 29 | aOhx3KHk7HuExqcyQSHgXphiDM59o3NAkC9cHPcnqeKDTVfuEafIin2xysNCHLao 30 | odOm0tp2/nyQGb0xc/jHwaBfRcfAOeSG7pNzv2h6DqPiud45RRlQyyQ5IU9bY+SW 31 | VPWS3q1ijp9ObRMD//nWBFWIm/TBsZoTZr9D+X3ZfGpQ78KXCpqqJTlmgpB2g3pH 32 | IvKVcdt8pTpqJd0cubezyK89Dw== 33 | -----END CERTIFICATE----- 34 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/rsa/client2.csr: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE REQUEST----- 2 | MIICrzCCAZcCAQAwajELMAkGA1UEBhMCQkcxEzARBgNVBAgMClNvbWUtU3RhdGUx 3 | ITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEjMCEGCSqGSIb3DQEJ 4 | ARYUdXNlckB1bmljb250c29mdC5jb20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAw 5 | ggEKAoIBAQDcdppKGR0PKfQS0D/P37DW9RmXFOa2EFxi4D2gE+01+kfZvdBcnwHL 6 | GXlVkfFJtRc+dKBKwDqwX9vvrkhurdMaxBTCzGs/Inukqx/vZ+wQVLdFYFiJ+YIc 7 | w3v1Zjh7qM/gYBuWjxWbAzxI0zNcUb4G4tLKB7BwjIkg0ckwAs2XLunwf4l96jF+ 8 | rSTQH8kM6YFF7qBOzI2lZ2LdKJjXHuPF8tSLstdQ2iecGxmiOnY00KDIkBwxSYTs 9 | LQHxc+R/Q0J28hTVJ/7aC5cC6B+rpOc+Tn/878nHzE0SZgY6UiWCBftb/pzLln3/ 10 | xqLkKHuT1avpnH0j+NzJhhGDAGFC6GKrAgMBAAGgADANBgkqhkiG9w0BAQsFAAOC 11 | AQEA1+dp+ANmN1r3+u0eYDQunsnUF889vu+Gr21ptXD/QznUynFaDUiIC4jCFx20 12 | zkg81H3cxbXfm0hXMVX0tvyhWvFJecFN3p0RD5Q7+YFceJa42n1dpqHVDoPY8ZCY 13 | MhQFeW1boOzJA1bk5I1VzSWRVdE3tDF+WgnpOkP1A4Xw32BS8jD6kwa8RhMqYOrp 14 | SfkYKx167wTDvSWPy3AfmC2ptQuImomjRrKZnwsh5kLNR6pkEAUOObVsSCPdkvdu 15 | bO1aQNqILIzyNRGkc/QDjmtyNyh9I1AC14OVVNdS+Yp82V9IVDHy9KnhNR8fqQTi 16 | 9vZO2Gz7Pb3ayTT4MYT3flGIzg== 17 | -----END CERTIFICATE REQUEST----- 18 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/rsa/client2.full.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wqweto/VbAsyncSocket/f5e922585800e05d42be166fa8301007a24fee94/test/Secure/ClientCerts/rsa/client2.full.pfx -------------------------------------------------------------------------------- /test/Secure/ClientCerts/rsa/client2.key: -------------------------------------------------------------------------------- 1 | -----BEGIN RSA PRIVATE KEY----- 2 | MIIEpAIBAAKCAQEA3HaaShkdDyn0EtA/z9+w1vUZlxTmthBcYuA9oBPtNfpH2b3Q 3 | XJ8Byxl5VZHxSbUXPnSgSsA6sF/b765Ibq3TGsQUwsxrPyJ7pKsf72fsEFS3RWBY 4 | ifmCHMN79WY4e6jP4GAblo8VmwM8SNMzXFG+BuLSygewcIyJINHJMALNly7p8H+J 5 | feoxfq0k0B/JDOmBRe6gTsyNpWdi3SiY1x7jxfLUi7LXUNonnBsZojp2NNCgyJAc 6 | MUmE7C0B8XPkf0NCdvIU1Sf+2guXAugfq6TnPk5//O/Jx8xNEmYGOlIlggX7W/6c 7 | y5Z9/8ai5Ch7k9Wr6Zx9I/jcyYYRgwBhQuhiqwIDAQABAoIBAQClnFa02P6yWHKn 8 | +1YkQsZmA8LQcnwJQmsUP50rdiyYgsWctPEj0rKXLjCubi6Sjv3CBfwviw3LVocy 9 | GnsA4YUWszthRgw+Zhn5ji80OZ3keq8d5407QHpUPykMJFjD1AEsbSXsSNoTEzAj 10 | CLypWiJkq9lquLH7KicMx2rZrRFhwg/5Qkfbr80TeEfdGeMbrGqtVSmedbjMmPB3 11 | 4kKhTlWqGPwYyJ+tMwJ2kCwVDLh2Vidkgbg3Y2q4iSx0jfkwg3AaDQRVAQjH1wrf 12 | 8HbCf/yId71MQF6ia8mXVIyRXbcOlZGQQ0mko/JREdQEl9ZRJ0/ayWxhi14+Mt/o 13 | U+tfA/EZAoGBAPcVW2HdFSCO/YIgLwZISqCwzAavc0FISTHs9MSNag+DopoVDSFM 14 | 8tSNVn7BMxjim++YcIPJppX1zCvd27mrymiAmOzH0dO9j1BRte45PEH0iz4GzLV1 15 | PN8uyySjvzuPy8sVmuhURBi4Tboeg60Ue4Eg0IhcfUBHZv5bmNWP1BIHAoGBAORr 16 | UdNY4x94AxE/Rh4I/LbWpTFJctrWEhsoETSEBjbP2nVUEaT9z5VGwg28JCN3RJ7+ 17 | um+65So7Yz/DI13kelnbvUetoQ9AHdnso/MKzPKj/xmmvDF5gtcNgsj40MftKMXp 18 | C6TTL8+RDRuteEea3gHcK50yOrDWVZnpBXHMfXE9AoGBAKMzoCCL/yB6UkhZKbZk 19 | maZ6IMlpqh88t9waP7J0FdIZiJVtwjZ0tRBgKvmecznEtGsVrXve/gGfUTTIXhHN 20 | vuw3xMrhUV8inj7XOewYMgz+kdpuc1iID6T7K4MQBYg5+soxgxhiUHhjhDOjZCY2 21 | mT05h4QKU1DZ2NW5esHVNqaRAoGAXR1SsfFeRQnLVlCQ6NWJD4/Yr4rdg1AY+XEP 22 | 8nSPg/CfgYzP1I6S/Ktnl+nGYZ7RHm+A2OM7YEftYaTO2gu1xMPLGVkyeuEMSjYp 23 | b35Lpe4so+xQc2qxZ7FeS0cC9T08jdE+qeZdayQaT9obWjVLoz55mQ9M1aHKK3/O 24 | dGGEEZECgYBn6bveQqHR+aRJJ41+mQdG3vaJUdc/e71LUqR7cfpacJ5fFLsWk5gZ 25 | run4kIkE+X1e2Asi2Ynr6NMU4QRttf8Z/V8PYkw7Il0MdilbWKPzmRGqQ7o8Cibc 26 | Dl0RICQ0l4mBRvpvB1mcV7RNh2CBRkbGBGViEu1rTXVa56AeScQf+g== 27 | -----END RSA PRIVATE KEY----- 28 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/rsa/client2.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIERTCCAi0CAQIwDQYJKoZIhvcNAQELBQAwZzELMAkGA1UEBhMCQkcxDDAKBgNV 3 | BAgMA04vQTEOMAwGA1UEBwwFU29maWExFDASBgNVBAoMC1VuaWNvbnRzb2Z0MSQw 4 | IgYJKoZIhvcNAQkBFhVhZG1pbkB1bmljb250c29mdC5jb20wHhcNMjIwNzE2MTgy 5 | MDE3WhcNMzIwNzEzMTgyMDE3WjBqMQswCQYDVQQGEwJCRzETMBEGA1UECAwKU29t 6 | ZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMSMwIQYJ 7 | KoZIhvcNAQkBFhR1c2VyQHVuaWNvbnRzb2Z0LmNvbTCCASIwDQYJKoZIhvcNAQEB 8 | BQADggEPADCCAQoCggEBANx2mkoZHQ8p9BLQP8/fsNb1GZcU5rYQXGLgPaAT7TX6 9 | R9m90FyfAcsZeVWR8Um1Fz50oErAOrBf2++uSG6t0xrEFMLMaz8ie6SrH+9n7BBU 10 | t0VgWIn5ghzDe/VmOHuoz+BgG5aPFZsDPEjTM1xRvgbi0soHsHCMiSDRyTACzZcu 11 | 6fB/iX3qMX6tJNAfyQzpgUXuoE7MjaVnYt0omNce48Xy1Iuy11DaJ5wbGaI6djTQ 12 | oMiQHDFJhOwtAfFz5H9DQnbyFNUn/toLlwLoH6uk5z5Of/zvycfMTRJmBjpSJYIF 13 | +1v+nMuWff/GouQoe5PVq+mcfSP43MmGEYMAYULoYqsCAwEAATANBgkqhkiG9w0B 14 | AQsFAAOCAgEAeRTZ9+ShmwnYRtInrKmG8rPb73zFFvdJ+DRqM4ZZcRSVj47tE+rt 15 | XnH0hPxBD5Qf6c0Rc9stBxywKey7mY7/rcMLHCljCPZ9hq3+re/AIfWjBwZz8RGq 16 | jkazZNz+xjomRx9QV2KgmLSgbKGcTB5DHIVTkGjDFm28JVrJ3Abj5KXqq9viQpyG 17 | aGDrMRKjId4j2I2nZ9dhZ54xkgsohLmNquvu1AQ6NDaNtzYqwM9+z3zCnjpk9V7v 18 | dRjy1l5l19nBce3ymYMz3Iu4RcXKfIeSHeGaHdG0PSJjftQdAy5ES8VrLYXhphjg 19 | LvovgvJKPuQlSW/I7hWtE2WufXOtcmratt1hv3nvm962rUomq92K7jK7dBxHSshA 20 | 0nX+13txSnR3L/BF4GV1nRnSCRMS7yOxiXcjCoRk11vqdZUfN4hlI3P7L1/9F8p8 21 | Im1oz2fSeAOnZCwlvynUffDZc/EsK6mjn2BbC8EraMQzQ2GBDFgZ6gr1rfH2LPtL 22 | yAeYbCXt4nCxO+FanjGTlJLwibeaWQ3xvQG+85jbJ1JfcCaebzCUvaevQM/qODrF 23 | eYYG6SImtFa++95oHohmIXxhd8pabPh/zuyQG83BpIuIYkMS1eskuPT0haktn6FH 24 | j7MzGZViR3lEjCz58txvREx5xnXS6wCjD0Q74ZWyRN18YfCGwfBxUos= 25 | -----END CERTIFICATE----- 26 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/rsa/gen_ca_rsa.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | openssl genrsa -out ca_rsa.key 4096 4 | openssl req -new -x509 -days 3650 -key ca_rsa.key -out ca_rsa.pem 5 | -------------------------------------------------------------------------------- /test/Secure/ClientCerts/rsa/gen_client_rsa.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | setlocal 3 | set "CLIENT_ID=%~1" 4 | set "CLIENT_SERIAL=%~2" 5 | 6 | if [%CLIENT_ID%]==[] echo usage: %~nx0 ^ ^& exit /b 1 7 | 8 | openssl genrsa -out "%CLIENT_ID%.key" 2048 9 | openssl req -new -key "%CLIENT_ID%.key" -out "%CLIENT_ID%.csr" 10 | openssl x509 -req -days 3650 -in "%CLIENT_ID%.csr" -CA ca_rsa.pem -CAkey ca_rsa.key -set_serial %CLIENT_SERIAL% -out "%CLIENT_ID%.pem" 11 | openssl pkcs12 -export -out %CLIENT_ID%.full.pfx -inkey "%CLIENT_ID%.key" -in "%CLIENT_ID%.pem" -certfile ca_rsa.pem 12 | -------------------------------------------------------------------------------- /test/Secure/Project1.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 3 | Class=cAsyncSocket; ..\..\src\cAsyncSocket.cls 4 | Module=mdGlobals; mdGlobals.bas 5 | Module=mdTlsThunks; ..\..\src\mdTlsThunks.bas 6 | Class=cRequestHandler; cRequestHandler.cls 7 | Class=cTlsSocket; ..\..\src\cTlsSocket.cls 8 | Form=Form2.frm 9 | IconForm="Form2" 10 | Startup="Form2" 11 | HelpFile="" 12 | Title="Secure" 13 | ExeName32="Project1.exe" 14 | Command32="" 15 | Name="Secure" 16 | HelpContextID="0" 17 | CompatibleMode="0" 18 | MajorVer=1 19 | MinorVer=0 20 | RevisionVer=0 21 | AutoIncrementVer=0 22 | ServerSupportFiles=0 23 | VersionCompanyName="Unicontsoft" 24 | CondComp="ASYNCSOCKET_NO_TLSSERVER = 0 : USE_DEBUG_LOG = 1 : Dummy = 1" 25 | CompilationType=0 26 | OptimizationType=0 27 | FavorPentiumPro(tm)=0 28 | CodeViewDebugInfo=-1 29 | NoAliasing=0 30 | BoundsCheck=0 31 | OverflowCheck=0 32 | FlPointCheck=0 33 | FDIVCheck=0 34 | UnroundedFP=0 35 | StartMode=0 36 | Unattended=0 37 | Retained=0 38 | ThreadPerObject=0 39 | MaxNumberOfThreads=1 40 | -------------------------------------------------------------------------------- /test/Secure/Project2.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 3 | Class=cAsyncSocket; ..\..\src\cAsyncSocket.cls 4 | Module=mdGlobals; mdGlobals.bas 5 | Class=cRequestHandler; cRequestHandler.cls 6 | Class=cTlsSocket; ..\..\src\cTlsSocket.cls 7 | Form=Form2.frm 8 | Module=mdTlsNative; ..\..\src\mdTlsNative.bas 9 | IconForm="Form2" 10 | Startup="Form2" 11 | HelpFile="" 12 | Title="SecureNative" 13 | ExeName32="Project2.exe" 14 | Command32="" 15 | Name="SecureNative" 16 | HelpContextID="0" 17 | CompatibleMode="0" 18 | MajorVer=1 19 | MinorVer=0 20 | RevisionVer=0 21 | AutoIncrementVer=0 22 | ServerSupportFiles=0 23 | VersionCompanyName="Unicontsoft" 24 | CondComp="ASYNCSOCKET_NO_TLSSERVER = 0 : USE_DEBUG_LOG = 1 : Dummy = 1" 25 | CompilationType=0 26 | OptimizationType=0 27 | FavorPentiumPro(tm)=0 28 | CodeViewDebugInfo=-1 29 | NoAliasing=0 30 | BoundsCheck=0 31 | OverflowCheck=0 32 | FlPointCheck=0 33 | FDIVCheck=0 34 | UnroundedFP=0 35 | StartMode=0 36 | Unattended=0 37 | Retained=0 38 | ThreadPerObject=0 39 | MaxNumberOfThreads=1 40 | -------------------------------------------------------------------------------- /test/Secure/Project3.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 3 | Class=cAsyncSocket; ..\..\src\cAsyncSocket.cls 4 | Module=mdGlobals; mdGlobals.bas 5 | Class=cRequestHandler; cRequestHandler.cls 6 | Class=cTlsSocket; ..\..\src\cTlsSocket.cls 7 | Form=Form2.frm 8 | Module=mdTlsSodium; ..\..\src\mdTlsSodium.bas 9 | IconForm="Form2" 10 | Startup="Form2" 11 | HelpFile="" 12 | Title="SecureSodium" 13 | ExeName32="Project3.exe" 14 | Command32="" 15 | Name="SecureSodium" 16 | HelpContextID="0" 17 | CompatibleMode="0" 18 | MajorVer=1 19 | MinorVer=0 20 | RevisionVer=0 21 | AutoIncrementVer=0 22 | ServerSupportFiles=0 23 | VersionCompanyName="Unicontsoft" 24 | CondComp="USE_DEBUG_LOG = 1 : ASYNCSOCKET_NO_LIBSODIUM = 0 : ASYNCSOCKET_NO_TLSSERVER = 0 : Dummy = 1" 25 | CompilationType=0 26 | OptimizationType=0 27 | FavorPentiumPro(tm)=0 28 | CodeViewDebugInfo=-1 29 | NoAliasing=0 30 | BoundsCheck=0 31 | OverflowCheck=0 32 | FlPointCheck=0 33 | FDIVCheck=0 34 | UnroundedFP=0 35 | StartMode=0 36 | Unattended=0 37 | Retained=0 38 | ThreadPerObject=0 39 | MaxNumberOfThreads=1 40 | -------------------------------------------------------------------------------- /test/Secure/cRequestHandler.cls: -------------------------------------------------------------------------------- 1 | VERSION 1.0 CLASS 2 | BEGIN 3 | MultiUse = -1 'True 4 | Persistable = 0 'NotPersistable 5 | DataBindingBehavior = 0 'vbNone 6 | DataSourceBehavior = 0 'vbNone 7 | MTSTransactionMode = 0 'NotAnMTSObject 8 | END 9 | Attribute VB_Name = "cRequestHandler" 10 | Attribute VB_GlobalNameSpace = False 11 | Attribute VB_Creatable = True 12 | Attribute VB_PredeclaredId = False 13 | Attribute VB_Exposed = False 14 | Option Explicit 15 | Private Const MODULE_NAME As String = "cRequestHandler" 16 | 17 | #Const ImplUseDebugLog = (USE_DEBUG_LOG <> 0) 18 | 19 | '========================================================================= 20 | ' API 21 | '========================================================================= 22 | 23 | Private Declare Function vbaObjSetAddref Lib "msvbvm60" Alias "__vbaObjSetAddref" (oDest As Any, ByVal lSrcPtr As Long) As Long 24 | 25 | '========================================================================= 26 | ' Constants and member variables 27 | '========================================================================= 28 | 29 | Private WithEvents m_oSocket As cTlsSocket 30 | Attribute m_oSocket.VB_VarHelpID = -1 31 | Private m_sKey As String 32 | Private m_lParentWeakRef As Long 33 | Private m_sRequest As String 34 | 35 | '========================================================================= 36 | ' Error handling 37 | '========================================================================= 38 | 39 | Private Sub PrintError(sFunction As String) 40 | #If ImplUseDebugLog Then 41 | DebugLog MODULE_NAME, sFunction & "(" & Erl & ")", Err.Description & " &H" & Hex$(Err.Number), vbLogEventTypeError 42 | #Else 43 | Debug.Print "Critical error: " & Err.Description & " [" & MODULE_NAME & "." & sFunction & "]" 44 | #End If 45 | End Sub 46 | 47 | '========================================================================= 48 | ' Properties 49 | '========================================================================= 50 | 51 | Property Get Parent() As Form2 52 | Call vbaObjSetAddref(Parent, m_lParentWeakRef) 53 | End Property 54 | 55 | '========================================================================= 56 | ' Methods 57 | '========================================================================= 58 | 59 | Public Function Init(oSocket As cTlsSocket, sKey As String, oParent As Form2) As Boolean 60 | Set m_oSocket = oSocket 61 | m_sKey = sKey 62 | m_lParentWeakRef = ObjPtr(oParent) 63 | '--- success 64 | Init = True 65 | QH: 66 | End Function 67 | 68 | Private Function HandleRequest(sText As String) As Boolean 69 | Const FUNC_NAME As String = "HandleRequest" 70 | Dim vSplit As Variant 71 | Dim sRetVal As String 72 | 73 | On Error GoTo EH 74 | m_sRequest = m_sRequest & sText 75 | If InStr(m_sRequest, vbCrLf & vbCrLf) > 0 Or InStr(m_sRequest, vbLf & vbLf) > 0 Then 76 | If InStr(m_sRequest, vbCrLf) > 0 Then 77 | vSplit = Split(m_sRequest, vbCrLf) 78 | ElseIf InStr(m_sRequest, vbLf) > 0 Then 79 | vSplit = Split(m_sRequest, vbLf) 80 | Else 81 | Exit Function 82 | End If 83 | Else 84 | Exit Function 85 | End If 86 | vSplit = Split(vSplit(0), " ") 87 | If UBound(vSplit) < 1 Then 88 | Exit Function 89 | End If 90 | #If ImplUseDebugLog Then 91 | DebugLog MODULE_NAME, FUNC_NAME, "Path=" & vSplit(1) 92 | #End If 93 | sRetVal = "" & Now & "" & vbCrLf 94 | sRetVal = "HTTP/1.0 200 Ok" & vbCrLf & _ 95 | "Content-Type: text/html; charset=UTF-8" & vbCrLf & _ 96 | "Content-Length: " & Len(sRetVal) & vbCrLf & _ 97 | "Connection: Close" & vbCrLf & vbCrLf & _ 98 | sRetVal 99 | If Not m_oSocket.SyncSendArray(ToUtf8Array(sRetVal)) Then 100 | GoTo QH 101 | End If 102 | m_oSocket.Close_ 103 | Set m_oSocket = Nothing 104 | '--- success 105 | HandleRequest = True 106 | QH: 107 | Exit Function 108 | EH: 109 | PrintError FUNC_NAME 110 | End Function 111 | 112 | '========================================================================= 113 | ' Socket events 114 | '========================================================================= 115 | 116 | Private Sub m_oSocket_OnClose() 117 | Parent.frRemoveHandler m_sKey 118 | End Sub 119 | 120 | Private Sub m_oSocket_OnError(ByVal ErrorCode As Long, ByVal EventMask As UcsAsyncSocketEventMaskEnum) 121 | Const FUNC_NAME As String = "m_oSocket_OnError" 122 | 123 | With m_oSocket.LastError 124 | If .Number <> 0 Then 125 | #If ImplUseDebugLog Then 126 | DebugLog MODULE_NAME, FUNC_NAME & ", " & Replace(.Source, vbCrLf, ", "), .Description & " &H" & Hex$(.Number), vbLogEventTypeError 127 | #Else 128 | Debug.Print "Error: " & .Description & " &H" & Hex$(.Number) & " [" & MODULE_NAME & "." & FUNC_NAME & ", " & Replace(.Source, vbCrLf, ", ") & "]" 129 | #End If 130 | Parent.frLogError m_sKey, m_oSocket.LastError 131 | End If 132 | End With 133 | Parent.frRemoveHandler m_sKey 134 | End Sub 135 | 136 | Private Sub m_oSocket_OnReceive() 137 | Const FUNC_NAME As String = "m_oSocket_OnReceive" 138 | Dim baRecv() As Byte 139 | 140 | On Error GoTo EH 141 | If Not m_oSocket.ReceiveArray(baRecv) Then 142 | GoTo QH 143 | End If 144 | If Not HandleRequest(FromUtf8Array(baRecv)) Then 145 | GoTo QH 146 | End If 147 | QH: 148 | Exit Sub 149 | EH: 150 | PrintError FUNC_NAME 151 | Resume QH 152 | End Sub 153 | 154 | Private Sub m_oSocket_OnServerCertificate(Certificates As Object, PrivateKey As Object, Confirmed As Boolean) 155 | Debug.Print "SniRequested=" & m_oSocket.SniRequested & ", AlpnNegotiated=" & m_oSocket.AlpnNegotiated, Timer 156 | End Sub 157 | -------------------------------------------------------------------------------- /test/Secure/ca-bundle.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wqweto/VbAsyncSocket/f5e922585800e05d42be166fa8301007a24fee94/test/Secure/ca-bundle.pfx -------------------------------------------------------------------------------- /test/Secure/client1.full.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wqweto/VbAsyncSocket/f5e922585800e05d42be166fa8301007a24fee94/test/Secure/client1.full.pfx -------------------------------------------------------------------------------- /test/Secure/client2.full.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wqweto/VbAsyncSocket/f5e922585800e05d42be166fa8301007a24fee94/test/Secure/client2.full.pfx -------------------------------------------------------------------------------- /test/Secure/drag_to_view_pfx.bat: -------------------------------------------------------------------------------- 1 | @openssl pkcs12 -in %1 -nodes -passin pass:"" | openssl x509 -text 2 | @pause 3 | -------------------------------------------------------------------------------- /test/Secure/eccert.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIICNjCCAd2gAwIBAgIUEPe+pIBYZdk62KhZXPP3VBCVob8wCgYIKoZIzj0EAwIw 3 | cTELMAkGA1UEBhMCQkcxEzARBgNVBAgMClNvbWUtU3RhdGUxDjAMBgNVBAcMBVNv 4 | ZmlhMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxGjAYBgNVBAMM 5 | EXRlc3Quc2VydmVyLmxvY2FsMB4XDTIwMDQyNDIyMzEwOVoXDTIxMDQyNDIyMzEw 6 | OVowcTELMAkGA1UEBhMCQkcxEzARBgNVBAgMClNvbWUtU3RhdGUxDjAMBgNVBAcM 7 | BVNvZmlhMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQxGjAYBgNV 8 | BAMMEXRlc3Quc2VydmVyLmxvY2FsMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE 9 | q6qHuakf3h6F6k0pOmLqt10zvKogbXxdT9sm2SDR9MvEBSY+Xne6vz66dR/t4k9q 10 | jqhIlOeUa6c3cSGzXlKd5aNTMFEwHQYDVR0OBBYEFKxe/C5C4xVPn7lcWoGz9Ojq 11 | LF7+MB8GA1UdIwQYMBaAFKxe/C5C4xVPn7lcWoGz9OjqLF7+MA8GA1UdEwEB/wQF 12 | MAMBAf8wCgYIKoZIzj0EAwIDRwAwRAIgH2dyvKjaPWC6ISNkb1FKOkOKBCCxl3xn 13 | 02uG704PjT0CIBHUHjZM51UH0UqUiD9LUBU1dbzFujLYu8Rev3SDNjGT 14 | -----END CERTIFICATE----- 15 | -------------------------------------------------------------------------------- /test/Secure/eccert.pfx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wqweto/VbAsyncSocket/f5e922585800e05d42be166fa8301007a24fee94/test/Secure/eccert.pfx -------------------------------------------------------------------------------- /test/Secure/ecprivkey.pem: -------------------------------------------------------------------------------- 1 | -----BEGIN EC PRIVATE KEY----- 2 | MHcCAQEEIP4smUt0EmHQvLZbmIhX3pIATGKpN5z7q8rP+bSCoHn2oAoGCCqGSM49 3 | AwEHoUQDQgAEq6qHuakf3h6F6k0pOmLqt10zvKogbXxdT9sm2SDR9MvEBSY+Xne6 4 | vz66dR/t4k9qjqhIlOeUa6c3cSGzXlKd5Q== 5 | -----END EC PRIVATE KEY----- 6 | -------------------------------------------------------------------------------- /test/Secure/test_conn_p521.bat: -------------------------------------------------------------------------------- 1 | openssl s_client -connect 127.0.0.1:10443 -curves P-521:P-384 -state -tls1_3 -------------------------------------------------------------------------------- /test/Winsock/Form1.frm: -------------------------------------------------------------------------------- 1 | VERSION 5.00 2 | Begin VB.Form Form1 3 | Caption = "Form1" 4 | ClientHeight = 2952 5 | ClientLeft = 108 6 | ClientTop = 456 7 | ClientWidth = 3624 8 | LinkTopic = "Form1" 9 | ScaleHeight = 2952 10 | ScaleWidth = 3624 11 | StartUpPosition = 3 'Windows Default 12 | Begin VB.CommandButton Command4 13 | Caption = "HTTPS Server" 14 | Height = 516 15 | Left = 252 16 | TabIndex = 3 17 | Top = 2268 18 | Width = 1524 19 | End 20 | Begin VB.CommandButton Command3 21 | Caption = "HTTPS request" 22 | Height = 516 23 | Left = 252 24 | TabIndex = 2 25 | Top = 1596 26 | Width = 1524 27 | End 28 | Begin VB.CommandButton Command2 29 | Caption = "HTTP Server" 30 | Height = 516 31 | Left = 252 32 | TabIndex = 1 33 | Top = 924 34 | Width = 1524 35 | End 36 | Begin WinsockTest.ctxWinsock ctxServer 37 | Index = 0 38 | Left = 2604 39 | Top = 840 40 | _ExtentX = 677 41 | _ExtentY = 677 42 | End 43 | Begin VB.CommandButton Command1 44 | Caption = "HTTP request" 45 | Height = 516 46 | Left = 252 47 | TabIndex = 0 48 | Top = 252 49 | Width = 1524 50 | End 51 | Begin WinsockTest.ctxWinsock ctxWinsock 52 | Left = 2604 53 | Top = 252 54 | _ExtentX = 677 55 | _ExtentY = 677 56 | End 57 | End 58 | Attribute VB_Name = "Form1" 59 | Attribute VB_GlobalNameSpace = False 60 | Attribute VB_Creatable = False 61 | Attribute VB_PredeclaredId = True 62 | Attribute VB_Exposed = False 63 | Option Explicit 64 | 65 | Private Sub Command1_Click() 66 | ctxWinsock.Protocol = UcsProtocolConstants.sckTCPProtocol 67 | ctxWinsock.Connect "bgdev.org", 80 68 | End Sub 69 | 70 | Private Sub Command3_Click() 71 | ctxWinsock.Protocol = UcsProtocolConstants.sckTLSProtocol 72 | ctxWinsock.Connect "bgdev.org", 443 73 | End Sub 74 | 75 | Private Sub Command2_Click() 76 | ctxServer(0).Close_ 77 | ctxServer(0).Protocol = UcsProtocolConstants.sckTCPProtocol 78 | ctxServer(0).Bind 8088, "127.0.0.1" 79 | ctxServer(0).Listen 80 | Shell "cmd /c start http://localhost:8088/" 81 | End Sub 82 | 83 | Private Sub Command4_Click() 84 | ctxServer(0).Close_ 85 | ctxServer(0).Protocol = UcsProtocolConstants.sckTLSProtocol 86 | ctxServer(0).Bind 8088, "127.0.0.1" 87 | ctxServer(0).Listen ' CertSubject:="68b5220077de8bbeaed8e1c2540fec6c16b418a8" 88 | Shell "cmd /c start https://localhost:8088/" 89 | End Sub 90 | 91 | Private Sub ctxWinsock_Connect() 92 | Dim lIdx As Long 93 | 94 | Debug.Print "Connected to " & ctxWinsock.RemoteHostIP, Timer 95 | ctxWinsock.SendData "GET / HTTP/1.0" & vbCrLf & _ 96 | "Host: www.bgdev.org" & vbCrLf & _ 97 | "Connection: close" & vbCrLf & vbCrLf 98 | For lIdx = 1 To 5000 99 | ctxWinsock.SendData String(1000, "a") 100 | Next 101 | End Sub 102 | 103 | Private Sub ctxWinsock_DataArrival(ByVal bytesTotal As Long) 104 | Dim sBuffer As String 105 | 106 | Debug.Print "DataArrival", bytesTotal 107 | ctxWinsock.PeekData sBuffer 108 | ctxWinsock.GetData sBuffer 109 | Debug.Print sBuffer; 110 | End Sub 111 | 112 | Private Sub ctxWinsock_Error(ByVal Number As Long, Description As String, ByVal Scode As UcsErrorConstants, Source As String, HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) 113 | MsgBox Description & " &H" & Hex$(Number) & " [" & Source & "]", vbCritical, "ctxWinsock_Error" 114 | End Sub 115 | 116 | Private Sub ctxServer_ConnectionRequest(Index As Integer, ByVal requestID As Long) 117 | Debug.Print "ctxServer(" & Index & ")_ConnectionRequest, requestID=" & requestID & ", RemoteHostIP=" & ctxServer(Index).RemoteHostIP & ", RemotePort=" & ctxServer(Index).RemotePort, Timer 118 | Load ctxServer(ctxServer.UBound + 1) 119 | ctxServer(ctxServer.UBound).Accept requestID 120 | ' Debug.Print "ctxServer(" & ctxServer.UBound & ").Protocol=" & ctxServer(ctxServer.UBound).Protocol 121 | End Sub 122 | 123 | Private Sub ctxServer_DataArrival(Index As Integer, ByVal bytesTotal As Long) 124 | Dim sRequest As String 125 | Dim vSplit As Variant 126 | Dim sBody As String 127 | 128 | Debug.Print "ctxServer(" & Index & ")_DataArrival, bytesTotal=" & bytesTotal, Timer 129 | ctxServer(Index).GetData sRequest 130 | vSplit = Split(sRequest, vbCrLf) 131 | If UBound(vSplit) >= 0 Then 132 | Debug.Print vSplit(0) 133 | sBody = "

" & Join(vSplit, "

" & vbCrLf & "

" & Index & ": ") & "

" & vbCrLf & _ 134 | "

" & Index & ": Current time is " & Now & "

" & _ 135 | "

" & Index & ": RemoteHostIP is " & ctxServer(Index).RemoteHostIP & "

" & vbCrLf & _ 136 | "

" & Index & ": RemotePort is " & ctxServer(Index).RemotePort & "

" & vbCrLf & _ 137 | "" & vbCrLf 138 | ctxServer(Index).SendData "HTTP/1.1 200 OK" & vbCrLf & _ 139 | "Content-Type: text/html" & vbCrLf & _ 140 | "Content-Length: " & Len(sBody) & vbCrLf & vbCrLf & _ 141 | sBody 142 | End If 143 | Debug.Print "ctxServer(" & Index & ")_DataArrival, done", Timer 144 | End Sub 145 | 146 | Private Sub ctxServer_CloseEvent(Index As Integer) 147 | Unload ctxServer(Index) 148 | End Sub 149 | 150 | Private Sub ctxServer_Close(Index As Integer) 151 | ctxServer_CloseEvent Index 152 | End Sub 153 | 154 | Private Sub ctxServer_OnServerCertificate(Index As Integer, Socket As Object, Certificates As Object, PrivateKey As Object, Confirmed As Boolean) 155 | Debug.Print "ctxServer(" & Index & ")_OnServerCertificate, SniRequested=" & Socket.SniRequested 156 | End Sub 157 | 158 | Private Sub ctxServer_Error(Index As Integer, ByVal Number As Long, Description As String, ByVal Scode As UcsErrorConstants, Source As String, HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) 159 | MsgBox Description & " &H" & Hex$(Number) & " [" & Source & "]", vbCritical, "ctxServer(" & Index & ")_Error" 160 | End Sub 161 | 162 | -------------------------------------------------------------------------------- /test/Winsock/Form2.frm: -------------------------------------------------------------------------------- 1 | VERSION 5.00 2 | Begin VB.Form Form2 3 | Caption = "Form2" 4 | ClientHeight = 2952 5 | ClientLeft = 108 6 | ClientTop = 456 7 | ClientWidth = 3624 8 | LinkTopic = "Form1" 9 | ScaleHeight = 2952 10 | ScaleWidth = 3624 11 | StartUpPosition = 3 'Windows Default 12 | Begin VB.CommandButton Command4 13 | Caption = "HTTPS Server" 14 | Height = 516 15 | Left = 252 16 | TabIndex = 3 17 | Top = 2268 18 | Width = 1524 19 | End 20 | Begin VB.CommandButton Command3 21 | Caption = "HTTPS request" 22 | Height = 516 23 | Left = 252 24 | TabIndex = 2 25 | Top = 1596 26 | Width = 1524 27 | End 28 | Begin VB.CommandButton Command2 29 | Caption = "HTTP Server" 30 | Height = 516 31 | Left = 252 32 | TabIndex = 1 33 | Top = 924 34 | Width = 1524 35 | End 36 | Begin WinsockNative.ctxWinsock ctxServer 37 | Index = 0 38 | Left = 2604 39 | Top = 840 40 | _ExtentX = 677 41 | _ExtentY = 677 42 | End 43 | Begin VB.CommandButton Command1 44 | Caption = "HTTP request" 45 | Height = 516 46 | Left = 252 47 | TabIndex = 0 48 | Top = 252 49 | Width = 1524 50 | End 51 | Begin WinsockNative.ctxWinsock ctxWinsock 52 | Left = 2604 53 | Top = 252 54 | _ExtentX = 677 55 | _ExtentY = 677 56 | End 57 | End 58 | Attribute VB_Name = "Form2" 59 | Attribute VB_GlobalNameSpace = False 60 | Attribute VB_Creatable = False 61 | Attribute VB_PredeclaredId = True 62 | Attribute VB_Exposed = False 63 | Option Explicit 64 | 65 | Private Sub Command1_Click() 66 | ctxWinsock.Protocol = UcsProtocolConstants.sckTCPProtocol 67 | ctxWinsock.Connect "bgdev.org", 80 68 | End Sub 69 | 70 | Private Sub Command3_Click() 71 | ctxWinsock.Protocol = UcsProtocolConstants.sckTLSProtocol 72 | ctxWinsock.Connect "bgdev.org", 443 73 | End Sub 74 | 75 | Private Sub Command2_Click() 76 | ctxServer(0).Close_ 77 | ctxServer(0).Protocol = UcsProtocolConstants.sckTCPProtocol 78 | ctxServer(0).Bind 8088, "127.0.0.1" 79 | ctxServer(0).Listen 80 | Shell "cmd /c start http://localhost:8088/" 81 | End Sub 82 | 83 | Private Sub Command4_Click() 84 | ctxServer(0).Close_ 85 | ctxServer(0).Protocol = UcsProtocolConstants.sckTLSProtocol 86 | ctxServer(0).Bind 8088, "127.0.0.1" 87 | ctxServer(0).Listen ' CertSubject:="68b5220077de8bbeaed8e1c2540fec6c16b418a8" 88 | Shell "cmd /c start https://localhost:8088/" 89 | End Sub 90 | 91 | Private Sub ctxWinsock_Connect() 92 | Dim lIdx As Long 93 | 94 | Debug.Print "Connected to " & ctxWinsock.RemoteHostIP, Timer 95 | ctxWinsock.SendData "GET / HTTP/1.0" & vbCrLf & _ 96 | "Host: www.bgdev.org" & vbCrLf & _ 97 | "Connection: close" & vbCrLf & vbCrLf 98 | For lIdx = 1 To 5000 99 | ctxWinsock.SendData String(1000, "a") 100 | Next 101 | End Sub 102 | 103 | Private Sub ctxWinsock_DataArrival(ByVal bytesTotal As Long) 104 | Dim sBuffer As String 105 | 106 | Debug.Print "DataArrival", bytesTotal 107 | ctxWinsock.PeekData sBuffer 108 | ctxWinsock.GetData sBuffer 109 | Debug.Print sBuffer; 110 | End Sub 111 | 112 | Private Sub ctxWinsock_Error(ByVal Number As Long, Description As String, ByVal Scode As UcsErrorConstants, Source As String, HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) 113 | MsgBox Description & " &H" & Hex$(Number) & " [" & Source & "]", vbCritical, "ctxWinsock_Error" 114 | End Sub 115 | 116 | Private Sub ctxServer_ConnectionRequest(Index As Integer, ByVal requestID As Long) 117 | Debug.Print "ctxServer(" & Index & ")_ConnectionRequest, requestID=" & requestID & ", RemoteHostIP=" & ctxServer(Index).RemoteHostIP & ", RemotePort=" & ctxServer(Index).RemotePort, Timer 118 | Load ctxServer(ctxServer.UBound + 1) 119 | ctxServer(ctxServer.UBound).Accept requestID 120 | ' Debug.Print "ctxServer(" & ctxServer.UBound & ").Protocol=" & ctxServer(ctxServer.UBound).Protocol 121 | End Sub 122 | 123 | Private Sub ctxServer_DataArrival(Index As Integer, ByVal bytesTotal As Long) 124 | Dim sRequest As String 125 | Dim vSplit As Variant 126 | Dim sBody As String 127 | 128 | Debug.Print "ctxServer(" & Index & ")_DataArrival, bytesTotal=" & bytesTotal, Timer 129 | ctxServer(Index).GetData sRequest 130 | vSplit = Split(sRequest, vbCrLf) 131 | If UBound(vSplit) >= 0 Then 132 | Debug.Print vSplit(0) 133 | sBody = "

" & Join(vSplit, "

" & vbCrLf & "

" & Index & ": ") & "

" & vbCrLf & _ 134 | "

" & Index & ": Current time is " & Now & "

" & _ 135 | "

" & Index & ": RemoteHostIP is " & ctxServer(Index).RemoteHostIP & "

" & vbCrLf & _ 136 | "

" & Index & ": RemotePort is " & ctxServer(Index).RemotePort & "

" & vbCrLf & _ 137 | "" & vbCrLf 138 | ctxServer(Index).SendData "HTTP/1.1 200 OK" & vbCrLf & _ 139 | "Content-Type: text/html" & vbCrLf & _ 140 | "Content-Length: " & Len(sBody) & vbCrLf & vbCrLf & _ 141 | sBody 142 | End If 143 | Debug.Print "ctxServer(" & Index & ")_DataArrival, done", Timer 144 | End Sub 145 | 146 | Private Sub ctxServer_CloseEvent(Index As Integer) 147 | Unload ctxServer(Index) 148 | End Sub 149 | 150 | Private Sub ctxServer_Close(Index As Integer) 151 | ctxServer_CloseEvent Index 152 | End Sub 153 | 154 | Private Sub ctxServer_OnServerCertificate(Index As Integer, Socket As Object, Certificates As Object, PrivateKey As Object, Confirmed As Boolean) 155 | Debug.Print "ctxServer(" & Index & ")_OnServerCertificate, SniRequested=" & Socket.SniRequested 156 | End Sub 157 | 158 | Private Sub ctxServer_Error(Index As Integer, ByVal Number As Long, Description As String, ByVal Scode As UcsErrorConstants, Source As String, HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) 159 | MsgBox Description & " &H" & Hex$(Number) & " [" & Source & "]", vbCritical, "ctxServer(" & Index & ")_Error" 160 | End Sub 161 | 162 | -------------------------------------------------------------------------------- /test/Winsock/Form3.frm: -------------------------------------------------------------------------------- 1 | VERSION 5.00 2 | Object = "{710A6B30-6CD9-4F61-8399-1E51A1E65B86}#2.0#0"; "WinsockTls.ocx" 3 | Begin VB.Form Form2 4 | Caption = "Form2" 5 | ClientHeight = 2952 6 | ClientLeft = 108 7 | ClientTop = 456 8 | ClientWidth = 3624 9 | LinkTopic = "Form1" 10 | ScaleHeight = 2952 11 | ScaleWidth = 3624 12 | StartUpPosition = 3 'Windows Default 13 | Begin VB.CommandButton Command4 14 | Caption = "HTTPS Server" 15 | Height = 516 16 | Left = 252 17 | TabIndex = 3 18 | Top = 2268 19 | Width = 1524 20 | End 21 | Begin VB.CommandButton Command3 22 | Caption = "HTTPS request" 23 | Height = 516 24 | Left = 252 25 | TabIndex = 2 26 | Top = 1596 27 | Width = 1524 28 | End 29 | Begin VB.CommandButton Command2 30 | Caption = "HTTP Server" 31 | Height = 516 32 | Left = 252 33 | TabIndex = 1 34 | Top = 924 35 | Width = 1524 36 | End 37 | Begin WinsockTls.ctxWinsock ctxServer 38 | Index = 0 39 | Left = 2604 40 | Top = 840 41 | _extentx = 677 42 | _extenty = 677 43 | End 44 | Begin VB.CommandButton Command1 45 | Caption = "HTTP request" 46 | Height = 516 47 | Left = 252 48 | TabIndex = 0 49 | Top = 252 50 | Width = 1524 51 | End 52 | Begin WinsockTls.ctxWinsock ctxWinsock 53 | Left = 2604 54 | Top = 252 55 | _extentx = 677 56 | _extenty = 677 57 | End 58 | End 59 | Attribute VB_Name = "Form2" 60 | Attribute VB_GlobalNameSpace = False 61 | Attribute VB_Creatable = False 62 | Attribute VB_PredeclaredId = True 63 | Attribute VB_Exposed = False 64 | Option Explicit 65 | 66 | Private Sub Command1_Click() 67 | ctxWinsock.Protocol = sckTCPProtocol 68 | ctxWinsock.Connect "bgdev.org", 80 69 | End Sub 70 | 71 | Private Sub Command3_Click() 72 | ctxWinsock.Protocol = sckTLSProtocol 73 | ctxWinsock.Connect "bgdev.org", 443 74 | End Sub 75 | 76 | Private Sub Command2_Click() 77 | ctxServer(0).Close_ 78 | ctxServer(0).Protocol = sckTCPProtocol 79 | ctxServer(0).Bind 8088, "127.0.0.1" 80 | ctxServer(0).Listen 81 | Shell "cmd /c start http://localhost:8088/" 82 | End Sub 83 | 84 | Private Sub Command4_Click() 85 | ctxServer(0).Close_ 86 | ctxServer(0).Protocol = sckTLSProtocol 87 | ctxServer(0).Bind 8088, "127.0.0.1" 88 | ctxServer(0).Listen ' CertSubject:="68b5220077de8bbeaed8e1c2540fec6c16b418a8" 89 | Shell "cmd /c start https://localhost:8088/" 90 | End Sub 91 | 92 | Private Sub ctxServer_Error(Index As Integer, ByVal Number As Long, Description As String, ByVal Scode As UcsErrorConstants, Source As String, HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) 93 | MsgBox Description & " &H" & Hex$(Number) & " [" & Source & "]", vbCritical, "ctxServer(" & Index & ")_Error" 94 | End Sub 95 | 96 | Private Sub ctxWinsock_Connect() 97 | Dim lIdx As Long 98 | 99 | Debug.Print "Connected to " & ctxWinsock.RemoteHostIP, Timer 100 | ctxWinsock.SendData "GET / HTTP/1.0" & vbCrLf & _ 101 | "Host: www.bgdev.org" & vbCrLf & _ 102 | "Connection: close" & vbCrLf & vbCrLf 103 | For lIdx = 1 To 5000 104 | ctxWinsock.SendData String(1000, "a") 105 | Next 106 | End Sub 107 | 108 | Private Sub ctxWinsock_DataArrival(ByVal bytesTotal As Long) 109 | Dim sBuffer As String 110 | 111 | Debug.Print "DataArrival", bytesTotal 112 | ctxWinsock.PeekData sBuffer 113 | ctxWinsock.GetData sBuffer 114 | Debug.Print sBuffer; 115 | End Sub 116 | 117 | Private Sub ctxServer_ConnectionRequest(Index As Integer, ByVal requestID As Long) 118 | Debug.Print "ctxServer(" & Index & ")_ConnectionRequest, requestID=" & requestID & ", RemoteHostIP=" & ctxServer(Index).RemoteHostIP & ", RemotePort=" & ctxServer(Index).RemotePort, Timer 119 | Load ctxServer(ctxServer.UBound + 1) 120 | ctxServer(ctxServer.UBound).Protocol = ctxServer(Index).Protocol 121 | ctxServer(ctxServer.UBound).Accept requestID 122 | End Sub 123 | 124 | Private Sub ctxServer_DataArrival(Index As Integer, ByVal bytesTotal As Long) 125 | Dim sRequest As String 126 | Dim vSplit As Variant 127 | Dim sBody As String 128 | 129 | Debug.Print "ctxServer(" & Index & ")_DataArrival, bytesTotal=" & bytesTotal, Timer 130 | ctxServer(Index).GetData sRequest 131 | vSplit = Split(sRequest, vbCrLf) 132 | If UBound(vSplit) >= 0 Then 133 | Debug.Print vSplit(0) 134 | sBody = "

" & Join(vSplit, "

" & vbCrLf & "

" & Index & ": ") & "

" & vbCrLf & _ 135 | "

" & Index & ": Current time is " & Now & "

" & _ 136 | "

" & Index & ": RemoteHostIP is " & ctxServer(Index).RemoteHostIP & "

" & vbCrLf & _ 137 | "

" & Index & ": RemotePort is " & ctxServer(Index).RemotePort & "

" & vbCrLf & _ 138 | "" & vbCrLf 139 | ctxServer(Index).SendData "HTTP/1.1 200 OK" & vbCrLf & _ 140 | "Content-Type: text/html" & vbCrLf & _ 141 | "Content-Length: " & Len(sBody) & vbCrLf & vbCrLf & _ 142 | sBody 143 | End If 144 | Debug.Print "ctxServer(" & Index & ")_DataArrival, done", Timer 145 | End Sub 146 | 147 | Private Sub ctxServer_CloseEvent(Index As Integer) 148 | Unload ctxServer(Index) 149 | End Sub 150 | 151 | Private Sub ctxServer_Close(Index As Integer) 152 | ctxServer_CloseEvent Index 153 | End Sub 154 | 155 | Private Sub ctxWinsock_Error(ByVal Number As Long, Description As String, ByVal Scode As UcsErrorConstants, Source As String, HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) 156 | MsgBox Description & " &H" & Hex$(Number) & " [" & Source & "]", vbCritical, "ctxWinsock_Error" 157 | End Sub 158 | -------------------------------------------------------------------------------- /test/Winsock/Group3.vbg: -------------------------------------------------------------------------------- 1 | VBGROUP 5.0 2 | StartupProject=Project3.vbp 3 | Project=..\..\contrib\dll\WinsockTls.vbp 4 | -------------------------------------------------------------------------------- /test/Winsock/Project1.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Form=Form1.frm 3 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 4 | UserControl=..\..\contrib\ctxWinsock.ctl 5 | Class=cAsyncSocket; ..\..\src\cAsyncSocket.cls 6 | Class=cTlsSocket; ..\..\src\cTlsSocket.cls 7 | Module=mdTlsThunks; ..\..\src\mdTlsThunks.bas 8 | Class=cTlsRemaster; ..\..\contrib\cTlsRemaster.cls 9 | Form=frmRemaster.frm 10 | Class=cClientCallback; cClientCallback.cls 11 | IconForm="Form1" 12 | Startup="Form1" 13 | HelpFile="" 14 | ExeName32="Project1.exe" 15 | Command32="" 16 | Name="WinsockTest" 17 | HelpContextID="0" 18 | CompatibleMode="0" 19 | MajorVer=1 20 | MinorVer=0 21 | RevisionVer=0 22 | AutoIncrementVer=0 23 | ServerSupportFiles=0 24 | VersionCompanyName="Unicontsoft" 25 | CompilationType=0 26 | OptimizationType=0 27 | FavorPentiumPro(tm)=0 28 | CodeViewDebugInfo=0 29 | NoAliasing=0 30 | BoundsCheck=0 31 | OverflowCheck=0 32 | FlPointCheck=0 33 | FDIVCheck=0 34 | UnroundedFP=0 35 | StartMode=0 36 | Unattended=0 37 | Retained=0 38 | ThreadPerObject=0 39 | MaxNumberOfThreads=1 40 | -------------------------------------------------------------------------------- /test/Winsock/Project2.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Form=Form2.frm 3 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 4 | UserControl=..\..\contrib\ctxWinsock.ctl 5 | Class=cAsyncSocket; ..\..\src\cAsyncSocket.cls 6 | Class=cTlsSocket; ..\..\src\cTlsSocket.cls 7 | Module=mdTlsNative; ..\..\src\mdTlsNative.bas 8 | IconForm="Form2" 9 | Startup="Form2" 10 | HelpFile="" 11 | ExeName32="Project2.exe" 12 | Command32="" 13 | Name="WinsockNative" 14 | HelpContextID="0" 15 | CompatibleMode="0" 16 | MajorVer=1 17 | MinorVer=0 18 | RevisionVer=0 19 | AutoIncrementVer=0 20 | ServerSupportFiles=0 21 | VersionCompanyName="Unicontsoft" 22 | CompilationType=0 23 | OptimizationType=0 24 | FavorPentiumPro(tm)=0 25 | CodeViewDebugInfo=0 26 | NoAliasing=0 27 | BoundsCheck=0 28 | OverflowCheck=0 29 | FlPointCheck=0 30 | FDIVCheck=0 31 | UnroundedFP=0 32 | StartMode=0 33 | Unattended=0 34 | Retained=0 35 | ThreadPerObject=0 36 | MaxNumberOfThreads=1 37 | -------------------------------------------------------------------------------- /test/Winsock/Project3.vbp: -------------------------------------------------------------------------------- 1 | Type=Exe 2 | Form=Form3.frm 3 | Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#..\..\..\..\..\Windows\SysWOW64\stdole2.tlb#OLE Automation 4 | Object={710A6B30-6CD9-4F61-8399-1E51A1E65B86}#2.0#0; WinsockTls.ocx 5 | IconForm="Form2" 6 | Startup="Form2" 7 | HelpFile="" 8 | ExeName32="Project2.exe" 9 | Command32="" 10 | Name="WinsockCompiled" 11 | HelpContextID="0" 12 | CompatibleMode="0" 13 | MajorVer=1 14 | MinorVer=0 15 | RevisionVer=0 16 | AutoIncrementVer=0 17 | ServerSupportFiles=0 18 | VersionCompanyName="Unicontsoft" 19 | CompilationType=0 20 | OptimizationType=0 21 | FavorPentiumPro(tm)=0 22 | CodeViewDebugInfo=0 23 | NoAliasing=0 24 | BoundsCheck=0 25 | OverflowCheck=0 26 | FlPointCheck=0 27 | FDIVCheck=0 28 | UnroundedFP=0 29 | StartMode=0 30 | Unattended=0 31 | Retained=0 32 | ThreadPerObject=0 33 | MaxNumberOfThreads=1 34 | -------------------------------------------------------------------------------- /test/Winsock/cClientCallback.cls: -------------------------------------------------------------------------------- 1 | VERSION 1.0 CLASS 2 | BEGIN 3 | MultiUse = -1 'True 4 | Persistable = 0 'NotPersistable 5 | DataBindingBehavior = 0 'vbNone 6 | DataSourceBehavior = 0 'vbNone 7 | MTSTransactionMode = 0 'NotAnMTSObject 8 | END 9 | Attribute VB_Name = "cClientCallback" 10 | Attribute VB_GlobalNameSpace = False 11 | Attribute VB_Creatable = True 12 | Attribute VB_PredeclaredId = False 13 | Attribute VB_Exposed = False 14 | Option Explicit 15 | 16 | Public Index As Long 17 | Public Parent As frmRemaster 18 | Public WithEvents Socket As cTlsRemaster 19 | Attribute Socket.VB_VarHelpID = -1 20 | 21 | Private Sub Socket_DataArrival(ByVal bytesTotal As Long) 22 | Parent.OnDataArrival Index, bytesTotal 23 | End Sub 24 | 25 | Private Sub Socket_CloseSck() 26 | Parent.OnCloseSck Index 27 | End Sub 28 | 29 | -------------------------------------------------------------------------------- /test/Winsock/frmRemaster.frm: -------------------------------------------------------------------------------- 1 | VERSION 5.00 2 | Begin VB.Form frmRemaster 3 | Caption = "frmRemaster" 4 | ClientHeight = 2952 5 | ClientLeft = 108 6 | ClientTop = 456 7 | ClientWidth = 3624 8 | LinkTopic = "Form1" 9 | ScaleHeight = 2952 10 | ScaleWidth = 3624 11 | StartUpPosition = 3 'Windows Default 12 | Begin VB.CommandButton Command4 13 | Caption = "HTTPS Server" 14 | Height = 516 15 | Left = 252 16 | TabIndex = 3 17 | Top = 2268 18 | Width = 1524 19 | End 20 | Begin VB.CommandButton Command3 21 | Caption = "HTTPS request" 22 | Height = 516 23 | Left = 252 24 | TabIndex = 2 25 | Top = 1596 26 | Width = 1524 27 | End 28 | Begin VB.CommandButton Command2 29 | Caption = "HTTP Server" 30 | Height = 516 31 | Left = 252 32 | TabIndex = 1 33 | Top = 924 34 | Width = 1524 35 | End 36 | Begin VB.CommandButton Command1 37 | Caption = "HTTP request" 38 | Height = 516 39 | Left = 252 40 | TabIndex = 0 41 | Top = 252 42 | Width = 1524 43 | End 44 | End 45 | Attribute VB_Name = "frmRemaster" 46 | Attribute VB_GlobalNameSpace = False 47 | Attribute VB_Creatable = False 48 | Attribute VB_PredeclaredId = True 49 | Attribute VB_Exposed = False 50 | Option Explicit 51 | 52 | Private WithEvents m_oClient As cTlsRemaster 53 | Attribute m_oClient.VB_VarHelpID = -1 54 | Private WithEvents m_oServer As cTlsRemaster 55 | Attribute m_oServer.VB_VarHelpID = -1 56 | Private m_cConnPool As New Collection 57 | Attribute m_cConnPool.VB_VarHelpID = -1 58 | 59 | Private Sub Command1_Click() 60 | Set m_oClient = New cTlsRemaster 61 | m_oClient.Protocol = RemasterProtocolConstants.sckTCPProtocol 62 | m_oClient.Connect "bgdev.org", 80 63 | End Sub 64 | 65 | Private Sub Command3_Click() 66 | Set m_oClient = New cTlsRemaster 67 | m_oClient.Protocol = RemasterProtocolConstants.sckTLSProtocol 68 | m_oClient.Connect "bgdev.org", 443 69 | End Sub 70 | 71 | Private Sub Command2_Click() 72 | Set m_oServer = New cTlsRemaster 73 | m_oServer.Protocol = RemasterProtocolConstants.sckTCPProtocol 74 | m_oServer.Bind 8088, "127.0.0.1" 75 | m_oServer.Listen 76 | Shell "cmd /c start http://localhost:8088/" 77 | End Sub 78 | 79 | Private Sub Command4_Click() 80 | Set m_oServer = New cTlsRemaster 81 | m_oServer.Protocol = RemasterProtocolConstants.sckTLSProtocol 82 | m_oServer.Bind 8088, "127.0.0.1" 83 | m_oServer.Listen ' CertSubject:="68b5220077de8bbeaed8e1c2540fec6c16b418a8" 84 | Shell "cmd /c start https://localhost:8088/" 85 | End Sub 86 | 87 | Private Sub m_oClient_Connect() 88 | Dim lIdx As Long 89 | 90 | Debug.Print "Connected to " & m_oClient.RemoteHostIP, Timer 91 | m_oClient.SendData "GET / HTTP/1.0" & vbCrLf & _ 92 | "Host: www.bgdev.org" & vbCrLf & _ 93 | "Connection: close" & vbCrLf & vbCrLf 94 | For lIdx = 1 To 5000 95 | m_oClient.SendData String(1000, "a") 96 | Next 97 | End Sub 98 | 99 | Private Sub m_oClient_DataArrival(ByVal bytesTotal As Long) 100 | Dim sBuffer As String 101 | 102 | Debug.Print "DataArrival", bytesTotal 103 | m_oClient.PeekData sBuffer 104 | m_oClient.GetData sBuffer 105 | Debug.Print sBuffer; 106 | End Sub 107 | 108 | Private Sub m_oServer_ConnectionRequest(ByVal requestID As Long) 109 | Dim oCallback As cClientCallback 110 | 111 | Debug.Print "m_oServer_ConnectionRequest, requestID=" & requestID & ", RemoteHostIP=" & m_oServer.RemoteHostIP & ", RemotePort=" & m_oServer.RemotePort, Timer 112 | Set oCallback = New cClientCallback 113 | m_cConnPool.Add oCallback 114 | oCallback.Index = m_cConnPool.Count 115 | Set oCallback.Parent = Me 116 | Set oCallback.Socket = New cTlsRemaster 117 | oCallback.Socket.Accept requestID 118 | Debug.Print "oCallback.Socket.Protocol=" & oCallback.Socket.Protocol 119 | End Sub 120 | 121 | Public Sub OnDataArrival(Index As Long, ByVal bytesTotal As Long) 122 | Dim sRequest As String 123 | Dim vSplit As Variant 124 | Dim sBody As String 125 | Dim oCallback As cClientCallback 126 | 127 | Set oCallback = m_cConnPool.Item(Index) 128 | Debug.Print "OnDataArrival, Index=" & Index & ", bytesTotal=" & bytesTotal, Timer 129 | oCallback.Socket.GetData sRequest 130 | vSplit = Split(sRequest, vbCrLf) 131 | If UBound(vSplit) >= 0 Then 132 | Debug.Print vSplit(0) 133 | sBody = "

" & Join(vSplit, "

" & vbCrLf & "

" & Index & ": ") & "

" & vbCrLf & _ 134 | "

" & Index & ": Current time is " & Now & "

" & _ 135 | "

" & Index & ": RemoteHostIP is " & oCallback.Socket.RemoteHostIP & "

" & vbCrLf & _ 136 | "

" & Index & ": RemotePort is " & oCallback.Socket.RemotePort & "

" & vbCrLf & _ 137 | "" & vbCrLf 138 | oCallback.Socket.SendData "HTTP/1.1 200 OK" & vbCrLf & _ 139 | "Content-Type: text/html" & vbCrLf & _ 140 | "Content-Length: " & Len(sBody) & vbCrLf & vbCrLf & _ 141 | sBody 142 | End If 143 | Debug.Print "OnDataArrival, Index=" & Index & ", done", Timer 144 | End Sub 145 | 146 | Public Sub OnCloseSck(Index As Long) 147 | m_cConnPool.Remove Index 148 | If m_cConnPool.Count >= Index Then 149 | m_cConnPool.Add Nothing, Before:=Index 150 | End If 151 | Do While m_cConnPool.Count > 0 152 | If Not m_cConnPool.Item(m_cConnPool.Count) Is Nothing Then 153 | Exit Do 154 | End If 155 | m_cConnPool.Remove m_cConnPool.Count 156 | Loop 157 | End Sub 158 | 159 | 'Private Sub m_oClient_Error(ByVal Number As Long, Description As String, ByVal sCode As UcsErrorConstants, Source As String, HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) 160 | ' MsgBox Description & " &H" & Hex$(Number) & " [" & Source & "]", vbCritical, "m_oClient_Error" 161 | 'End Sub 162 | 163 | 'Private Sub m_oServer_Error(Index As Integer, ByVal Number As Long, Description As String, ByVal sCode As UcsErrorConstants, Source As String, HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) 164 | ' MsgBox Description & " &H" & Hex$(Number) & " [" & Source & "]", vbCritical, "m_oServer(" & Index & ")_Error" 165 | 'End Sub 166 | 167 | --------------------------------------------------------------------------------