2020-02-03 07:00:36 +03:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-08-06 06:54:47 +03:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
module http
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
import strings
|
2019-08-13 09:56:18 +03:00
|
|
|
// On linux, prefer a localy build openssl, because it is
|
|
|
|
// much more likely for it to be newer, than the system
|
|
|
|
// openssl from libssl-dev. If there is no local openssl,
|
|
|
|
// the next flag is harmless, since it will still use the
|
|
|
|
// (older) system openssl.
|
|
|
|
#flag linux -I/usr/local/include/openssl -L/usr/local/lib
|
2019-08-09 13:52:14 +03:00
|
|
|
#flag -l ssl -l crypto
|
2019-08-06 20:07:02 +03:00
|
|
|
// MacPorts
|
2019-09-27 01:35:25 +03:00
|
|
|
#flag darwin -I/opt/local/include
|
2019-08-06 20:07:02 +03:00
|
|
|
#flag darwin -L/opt/local/lib
|
2019-08-08 01:53:23 +03:00
|
|
|
// Brew
|
2019-09-27 01:35:25 +03:00
|
|
|
#flag darwin -I/usr/local/opt/openssl/include
|
2019-08-08 01:53:23 +03:00
|
|
|
#flag darwin -L/usr/local/opt/openssl/lib
|
2019-08-06 06:54:47 +03:00
|
|
|
#include <openssl/ssl.h>
|
|
|
|
struct C.SSL {
|
2019-11-24 06:27:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn C.SSL_library_init()
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.TLSv1_2_method() voidptr
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.SSL_CTX_set_options()
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.SSL_CTX_new() voidptr
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.SSL_CTX_set_verify_depth()
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.SSL_CTX_load_verify_locations() int
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.BIO_new_ssl_connect() voidptr
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.BIO_set_conn_hostname() int
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.BIO_get_ssl()
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.SSL_set_cipher_list() int
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.BIO_do_connect() int
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.BIO_do_handshake() int
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.SSL_get_peer_certificate() int
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.SSL_get_verify_result() int
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.SSL_set_tlsext_host_name() int
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.BIO_puts()
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.BIO_read()
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.BIO_free_all()
|
2019-12-22 01:41:42 +03:00
|
|
|
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.SSL_CTX_free()
|
2019-08-06 06:54:47 +03:00
|
|
|
|
2019-12-22 01:41:42 +03:00
|
|
|
|
2019-09-25 23:28:51 +03:00
|
|
|
fn init() int {
|
2019-11-24 06:27:02 +03:00
|
|
|
C.SSL_library_init()
|
2019-09-25 23:28:51 +03:00
|
|
|
return 1
|
2019-08-06 06:54:47 +03:00
|
|
|
}
|
|
|
|
|
2019-10-10 20:24:36 +03:00
|
|
|
fn (req &Request) ssl_do(port int, method, host_name, path string) ?Response {
|
2019-12-22 01:41:42 +03:00
|
|
|
// ssl_method := C.SSLv23_method()
|
2019-11-24 06:27:02 +03:00
|
|
|
ssl_method := C.TLSv1_2_method()
|
|
|
|
if isnil(method) {
|
|
|
|
}
|
|
|
|
ctx := C.SSL_CTX_new(ssl_method)
|
|
|
|
if isnil(ctx) {
|
|
|
|
}
|
|
|
|
C.SSL_CTX_set_verify_depth(ctx, 4)
|
|
|
|
flags := C.SSL_OP_NO_SSLv2 | C.SSL_OP_NO_SSLv3 | C.SSL_OP_NO_COMPRESSION
|
|
|
|
C.SSL_CTX_set_options(ctx, flags)
|
|
|
|
mut res := C.SSL_CTX_load_verify_locations(ctx, 'random-org-chain.pem', 0)
|
2019-08-06 06:54:47 +03:00
|
|
|
if res != 1 {
|
2019-11-24 06:27:02 +03:00
|
|
|
}
|
|
|
|
web := C.BIO_new_ssl_connect(ctx)
|
|
|
|
if isnil(ctx) {
|
|
|
|
}
|
2019-08-21 20:04:06 +03:00
|
|
|
addr := host_name + ':' + port.str()
|
2019-11-24 06:27:02 +03:00
|
|
|
res = C.BIO_set_conn_hostname(web, addr.str)
|
2019-08-06 06:54:47 +03:00
|
|
|
if res != 1 {
|
2019-11-24 06:27:02 +03:00
|
|
|
}
|
2019-12-04 13:08:28 +03:00
|
|
|
ssl := &C.SSL(0)
|
2019-11-24 06:27:02 +03:00
|
|
|
C.BIO_get_ssl(web, &ssl)
|
|
|
|
if isnil(ssl) {
|
|
|
|
}
|
|
|
|
preferred_ciphers := 'HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4'
|
|
|
|
res = C.SSL_set_cipher_list(ssl, preferred_ciphers.str)
|
2019-08-06 06:54:47 +03:00
|
|
|
if res != 1 {
|
2019-11-24 06:27:02 +03:00
|
|
|
}
|
|
|
|
res = C.SSL_set_tlsext_host_name(ssl, host_name.str)
|
|
|
|
res = C.BIO_do_connect(web)
|
2019-12-09 13:31:24 +03:00
|
|
|
if res != 1 {
|
|
|
|
return error('cannot connect the endpoint')
|
|
|
|
}
|
2019-11-24 06:27:02 +03:00
|
|
|
res = C.BIO_do_handshake(web)
|
2019-12-06 15:24:53 +03:00
|
|
|
C.SSL_get_peer_certificate(ssl)
|
2019-11-24 06:27:02 +03:00
|
|
|
res = C.SSL_get_verify_result(ssl)
|
2019-12-22 01:41:42 +03:00
|
|
|
// /////
|
2019-08-25 01:48:06 +03:00
|
|
|
s := req.build_request_headers(method, host_name, path)
|
2019-11-24 06:27:02 +03:00
|
|
|
C.BIO_puts(web, s.str)
|
|
|
|
mut sb := strings.new_builder(100)
|
2019-08-06 06:54:47 +03:00
|
|
|
for {
|
2019-11-24 06:27:02 +03:00
|
|
|
buff := [1536]byte
|
2019-12-22 01:41:42 +03:00
|
|
|
len := int(C.BIO_read(web, buff, 1536))
|
2019-11-24 06:27:02 +03:00
|
|
|
if len > 0 {
|
|
|
|
sb.write(tos(buff, len))
|
|
|
|
}
|
2019-08-06 06:54:47 +03:00
|
|
|
else {
|
2019-11-24 06:27:02 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isnil(web) {
|
2019-08-06 06:54:47 +03:00
|
|
|
C.BIO_free_all(web)
|
2019-11-24 06:27:02 +03:00
|
|
|
}
|
|
|
|
if !isnil(ctx) {
|
|
|
|
C.SSL_CTX_free(ctx)
|
2019-08-10 11:05:59 +03:00
|
|
|
}
|
2019-08-17 15:50:47 +03:00
|
|
|
return parse_response(sb.str())
|
2019-08-06 06:54:47 +03:00
|
|
|
}
|
2019-12-22 01:41:42 +03:00
|
|
|
|