fetching_20a_20secure_20web_20page
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
fetching_20a_20secure_20web_20page [2018/03/31 13:19] – external edit 127.0.0.1 | fetching_20a_20secure_20web_20page [2025/03/23 15:13] (current) – richardrussell | ||
---|---|---|---|
Line 1: | Line 1: | ||
=====Fetching a secure web page===== | =====Fetching a secure web page===== | ||
- | //by Richard Russell, July 2009//\\ \\ The procedure listed below fetches the contents of a secure (**https: | + | //by Richard Russell, July 2009, updated July 2018 and March 2025// |
- | * The directory from which the application will be loaded (@dir$). | + | The procedure listed below fetches |
- | * The ' | + | |
- | * The system directory (e.g. **C: | + | |
- | * The Windows directory (e.g. **C: | + | |
- | * One of the directories listed in the PATH environment variable. | + | |
- | \\ If you want to distribute **libssl32.dll** and **libeay32.dll** with your application then store them in **@dir$** and embed them in the executable. Alternatively you can put them in **@lib$** (or a sub-directory) but in that case you will need to amend the procedure below to load them explicitly from that location.\\ \\ The procedure should be called | + | |
- | port$ = " | + | |
- | host$ = "www.fortify.net" | + | |
- | page$ = "/sslcheck.html" | + | |
- | file$ = @tmp$ + " | + | |
- | PROCsslfetch(port$, host$, page$, file$) | + | |
- | This fetches the page https://www.fortify.net/sslcheck.html to the file **sslcheck.html**.\\ \\ Here is the procedure:\\ \\ | + | |
- | DEF PROCsslfetch(port$, | + | |
- | LOCAL libssl%, libeay%, meth%, ctx%, sock%, temp%, res%, ssl%, sbio%, file% | + | |
- | LOCAL req$, buf&() | + | |
- | FIONBIO | + | <code bb4w> |
- | | + | url$ = " |
- | | + | |
+ | SYS " | ||
+ | SYS " | ||
+ | SYS `URLDownloadToFile`, | ||
+ | IF fail% ERROR 100, "File download failed" | ||
+ | OSCLI "RUN notepad """ | ||
+ | </ | ||
- | ON ERROR LOCAL RESTORE ERROR : INSTALL @lib$+" | + | If you find this code doesn' |
- | | + | [[http:// |
- | SYS " | + | If you want to distribute **libssl-3.dll** and **libcrypto-3.dll** with your application then store them in **@dir$** and embed them in the executable. Alternatively you can put them in **@lib$** (or a sub-directory) but in that case you will need to amend the procedure below to load them explicitly from that location. |
- | IF libssl% = 0 PROCsslcleanup : ERROR 100, " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | The procedure should be called in the following context: |
- | IF libeay% = 0 PROCsslcleanup | + | |
- | SYS " | + | |
- | REM Global system initialisation: | + | <code bb4w> |
- | SYS `SSL_library_init` | + | port$ = " |
+ | host$ = " | ||
+ | page$ = "/ | ||
+ | file$ = @tmp$ + " | ||
+ | PROCsslfetch(port$, | ||
+ | </ | ||
- | REM Create SSL context: | + | This fetches the page https:// |
- | SYS `SSLv23_method` TO meth% | + | |
- | SYS `SSL_CTX_new`, | + | |
- | IF ctx% = 0 PROCsslcleanup : ERROR 100, " | + | |
- | REM Connect | + | Here is the procedure: |
- | sock% = FN_tcpconnect(host$, | + | |
- | IF sock% < 0 PROCsslcleanup : ERROR 100, " | + | |
- | temp% = 0 | + | <code bb4w> |
- | SYS `ioctlsocket`, sock%, FIONBIO, ^temp% TO res% | + | DEF PROCsslfetch(port$, host$, page$, file$) |
- | IF res% PROCsslcleanup : ERROR 105, " | + | LOCAL libssl%, libeay%, meth%, ctx%, sock%, temp%, res%, ssl%, sbio%, file% |
+ | LOCAL req$, buf&() | ||
- | REM Connect the SSL socket: | + | FIONBIO = & |
- | SYS `SSL_new`, ctx% TO ssl% | + | BIO_NOCLOSE |
- | SYS `BIO_new_socket`, | + | |
- | SYS `SSL_set_bio`, | + | |
- | SYS `SSL_connect`, | + | ON ERROR LOCAL RESTORE |
- | IF res% <= 0 PROCsslcleanup : ERROR 100, "SSL connect failed: " + STR$res% | + | PROC_initsockets |
- | REM Request the page: | + | SYS "LoadLibrary", "libcrypto-3.dll" |
- | req$ = "GET " | + | IF libeay% |
- | req$ += "User-Agent: BB4W" | + | |
- | req$ += "Host: " | + | |
- | req$ += CHR$13 + CHR$10 | + | |
- | | + | |
- | | + | IF libssl% = 0 PROCsslcleanup : ERROR 100, " |
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
- | | + | |
- | DIM buf& | + | SYS `TLSv1_2_method` TO meth% |
+ | SYS `SSL_CTX_new`, | ||
+ | IF ctx% = 0 PROCsslcleanup : ERROR 100, " | ||
- | file% = OPENOUT(file$) | + | REM Connect the TCP socket: |
- | REPEAT | + | sock% = FN_tcpconnect(host$, port$) |
- | SYS `SSL_read`, ssl%, ^buf&(0), BUFSIZ TO res% | + | IF sock% < 0 PROCsslcleanup : ERROR 100, "Cannot connect to " + host$ |
- | IF res% > 0 SYS " | + | |
- | UNTIL res% <= 0 | + | |
- | CLOSE #file% | + | |
- | IF res% PROCsslcleanup : ERROR 100, "SSL read failed: | + | |
- | REM Tidy up before exit: | + | temp% = 0 |
- | | + | SYS `ioctlsocket`, |
- | | + | IF res% PROCsslcleanup : ERROR 105, " |
- | DEF PROCsslcleanup | + | REM Connect the SSL socket: |
- | sock% += 0 : IF sock% PROC_closesocket(sock%) : sock% = 0 | + | SYS `SSL_new`, ctx% TO ssl% |
- | ctx% += 0 : IF ctx% SYS `SSL_CTX_free`, | + | SYS `BIO_new_socket`, |
- | libssl% += 0 : IF libssl% SYS " | + | SYS `SSL_set_bio`, |
- | libeay% += 0 : IF libeay% SYS " | + | |
- | PROC_exitsockets | + | SYS `SSL_connect`, |
- | ENDPROC | + | IF res% <= 0 PROCsslcleanup |
+ | |||
+ | REM Request the page: | ||
+ | req$ = "GET " + page$ + " HTTP/ | ||
+ | req$ += " | ||
+ | req$ += "Host: " + host$ + ":" | ||
+ | req$ += CHR$13 + CHR$10 | ||
+ | |||
+ | SYS `SSL_write`, | ||
+ | IF res% <> LEN(req$) PROCsslcleanup : ERROR 100, "SSL write failed: " + STR$res% | ||
+ | |||
+ | REM Copy the requested page to a file: | ||
+ | DIM buf& | ||
+ | |||
+ | file% = OPENOUT(file$) | ||
+ | REPEAT | ||
+ | | ||
+ | IF res% > 0 SYS " | ||
+ | UNTIL res% <= 0 | ||
+ | CLOSE #file% | ||
+ | IF res% PROCsslcleanup : ERROR 100, "SSL read failed: " + STR$res% | ||
+ | |||
+ | REM Tidy up before exit: | ||
+ | PROCsslcleanup | ||
+ | ENDPROC | ||
+ | |||
+ | DEF PROCsslcleanup | ||
+ | | ||
+ | ctx% += 0 : IF ctx% SYS `SSL_CTX_free`, | ||
+ | libssl% += 0 : IF libssl% SYS " | ||
+ | libeay% += 0 : IF libeay% SYS " | ||
+ | PROC_exitsockets | ||
+ | ENDPROC | ||
+ | </ |
fetching_20a_20secure_20web_20page.1522502359.txt.gz · Last modified: 2024/01/05 00:17 (external edit)