This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
fetching_20a_20secure_20web_20page [2018/03/31 14:19] 127.0.0.1 external edit |
fetching_20a_20secure_20web_20page [2018/07/30 23:42] (current) richardrussell Updated to TLS v1.2 |
||
---|---|---|---|
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//\\ \\ The procedure listed below fetches the contents of a secure (**https: |
- | * The directory from which the application will be loaded (@dir$). | + | If you want to distribute **libssl-1_1.dll** and **libcrypto-1_1.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 ' | + | |
- | * The system directory (e.g. **C: | + | |
- | * The Windows directory (e.g. **C: | + | |
- | * One of the directories listed in the PATH environment variable. | + | |
- | \\ | + | |
- | port$ = " | + | |
- | host$ = " | + | |
- | page$ = "/ | + | |
- | file$ = @tmp$ + " | + | |
- | PROCsslfetch(port$, | + | |
- | This fetches the page https:// | + | |
- | DEF PROCsslfetch(port$, | + | |
- | LOCAL libssl%, libeay%, meth%, ctx%, sock%, temp%, res%, ssl%, sbio%, file% | + | |
- | LOCAL req$, buf&() | + | |
- | FIONBIO = & | + | The procedure should be called in the following context: |
- | BIO_NOCLOSE = 0 | + | |
- | BUFSIZ = 256 | + | |
- | ON ERROR LOCAL RESTORE ERROR : INSTALL | + | <code bb4w> |
- | | + | port$ = " |
+ | host$ = " | ||
+ | page$ = "/ | ||
+ | file$ = @tmp$ + "sslcheck.html" | ||
+ | | ||
+ | </ | ||
- | SYS " | + | This fetches the page https:// |
- | IF libssl% = 0 PROCsslcleanup | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | |
- | SYS " | + | Here is the procedure: |
- | IF libeay% = 0 PROCsslcleanup | + | |
- | SYS " | + | |
- | REM Global system initialisation: | + | <code bb4w> |
- | SYS `SSL_library_init` | + | DEF PROCsslfetch(port$, |
+ | LOCAL libssl%, libeay%, meth%, ctx%, sock%, temp%, res%, ssl%, sbio%, file% | ||
+ | LOCAL req$, buf&() | ||
- | REM Create SSL context: | + | FIONBIO = & |
- | SYS `SSLv23_method` TO meth% | + | |
- | SYS `SSL_CTX_new`, | + | BUFSIZ = 256 |
- | IF ctx% = 0 PROCsslcleanup : ERROR 100, " | + | |
- | REM Connect the TCP socket: | + | ON ERROR LOCAL RESTORE ERROR : INSTALL @lib$+"SOCKLIB" |
- | sock% = FN_tcpconnect(host$, port$) | + | PROC_initsockets |
- | IF sock% < 0 PROCsslcleanup : ERROR 100, "Cannot connect to " | + | |
- | temp% = 0 | + | |
- | | + | IF libeay% = 0 PROCsslcleanup : ERROR 100, " |
- | IF res% PROCsslcleanup : ERROR 105, " | + | SYS " |
- | REM Connect the SSL socket: | + | SYS " |
- | SYS `SSL_new`, ctx% TO ssl% | + | IF libssl% = 0 PROCsslcleanup |
- | SYS `BIO_new_socket`, sock%, BIO_NOCLOSE | + | SYS " |
- | SYS `SSL_set_bio`, ssl%, sbio%, sbio% | + | SYS " |
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
+ | SYS " | ||
- | | + | REM Create SSL context: |
- | IF res% <= 0 PROCsslcleanup : ERROR 100, "SSL connect | + | |
+ | SYS `SSL_CTX_new`, meth% TO ctx% | ||
+ | IF ctx% = 0 PROCsslcleanup : ERROR 100, "SSL_CTX_new | ||
- | | + | |
- | | + | |
- | req$ += " | + | IF sock% < 0 PROCsslcleanup |
- | req$ += " | + | |
- | req$ += CHR$13 + CHR$10 | + | |
- | | + | temp% = 0 |
- | IF res% <> LEN(req$) | + | |
+ | IF res% PROCsslcleanup : ERROR 105, "Cannot set socket to blocking" | ||
- | | + | |
- | DIM buf& | + | SYS `SSL_new`, ctx% TO ssl% |
+ | SYS `BIO_new_socket`, | ||
+ | SYS `SSL_set_bio`, | ||
- | file% = OPENOUT(file$) | + | |
- | REPEAT | + | IF res% <= 0 PROCsslcleanup : ERROR 100, " |
- | | + | |
- | IF res% > 0 SYS " | + | |
- | UNTIL res% <= 0 | + | |
- | CLOSE #file% | + | |
- | IF res% PROCsslcleanup : ERROR 100, " | + | |
- | | + | |
- | | + | |
- | | + | req$ += " |
+ | req$ += "Host: " + host$ + ":" | ||
+ | req$ += CHR$13 + CHR$10 | ||
- | DEF PROCsslcleanup | + | SYS `SSL_write`, |
- | sock% += 0 : IF sock% PROC_closesocket(sock%) : sock% = 0 | + | IF res% <> LEN(req$) |
- | ctx% += 0 : IF ctx% SYS `SSL_CTX_free`, | + | |
- | libssl% += 0 : IF libssl% SYS " | + | REM Copy the requested page to a file: |
- | libeay% += 0 : IF libeay% SYS " | + | DIM buf& |
- | PROC_exitsockets | + | |
- | ENDPROC | + | 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 | ||
+ | </ |