1 18824b58 2008-08-03 rsc #include <u.h>
2 18824b58 2008-08-03 rsc #include <openssl/bio.h>
3 18824b58 2008-08-03 rsc #include <openssl/ssl.h>
4 18824b58 2008-08-03 rsc #include <openssl/err.h>
5 18824b58 2008-08-03 rsc #include "a.h"
10 18824b58 2008-08-03 rsc httpsinit(void)
12 18824b58 2008-08-03 rsc ERR_load_crypto_strings();
13 18824b58 2008-08-03 rsc ERR_load_SSL_strings();
14 18824b58 2008-08-03 rsc SSL_load_error_strings();
15 18824b58 2008-08-03 rsc SSL_library_init();
24 18824b58 2008-08-03 rsc opensslconnect(char *host)
28 18824b58 2008-08-03 rsc SSL_CTX *ctx;
30 18824b58 2008-08-03 rsc static int didinit;
31 18824b58 2008-08-03 rsc char buf[1024];
33 18824b58 2008-08-03 rsc if(!didinit){
38 18824b58 2008-08-03 rsc ctx = SSL_CTX_new(SSLv23_client_method());
39 18824b58 2008-08-03 rsc sbio = BIO_new_ssl_connect(ctx);
40 18824b58 2008-08-03 rsc BIO_get_ssl(sbio, &ssl);
41 18824b58 2008-08-03 rsc SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
43 18824b58 2008-08-03 rsc snprint(buf, sizeof buf, "%s:https", host);
44 18824b58 2008-08-03 rsc BIO_set_conn_hostname(sbio, buf);
46 18824b58 2008-08-03 rsc if(BIO_do_connect(sbio) <= 0 || BIO_do_handshake(sbio) <= 0){
47 18824b58 2008-08-03 rsc ERR_error_string_n(ERR_get_error(), buf, sizeof buf);
48 18824b58 2008-08-03 rsc BIO_free_all(sbio);
49 18824b58 2008-08-03 rsc werrstr("openssl: %s", buf);
53 18824b58 2008-08-03 rsc pfd = emalloc(sizeof *pfd);
54 18824b58 2008-08-03 rsc pfd->sbio = sbio;
59 18824b58 2008-08-03 rsc opensslclose(Pfd *pfd)
61 18824b58 2008-08-03 rsc if(pfd == nil)
63 18824b58 2008-08-03 rsc BIO_free_all(pfd->sbio);
68 18824b58 2008-08-03 rsc opensslwrite(Pfd *pfd, void *v, int n)
70 18824b58 2008-08-03 rsc int m, total;
75 18824b58 2008-08-03 rsc while(total < n){
76 18824b58 2008-08-03 rsc if((m = BIO_write(pfd->sbio, p+total, n-total)) <= 0){
77 18824b58 2008-08-03 rsc if(total == 0)
79 18824b58 2008-08-03 rsc return total;
83 18824b58 2008-08-03 rsc return total;
87 18824b58 2008-08-03 rsc opensslread(Pfd *pfd, void *v, int n)
89 18824b58 2008-08-03 rsc return BIO_read(pfd->sbio, v, n);
92 18824b58 2008-08-03 rsc Protocol https =
94 18824b58 2008-08-03 rsc opensslconnect,
96 18824b58 2008-08-03 rsc opensslwrite,