Blame


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"
6 18824b58 2008-08-03 rsc
7 18824b58 2008-08-03 rsc AUTOLIB(ssl)
8 18824b58 2008-08-03 rsc
9 18824b58 2008-08-03 rsc static void
10 18824b58 2008-08-03 rsc httpsinit(void)
11 18824b58 2008-08-03 rsc {
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();
16 18824b58 2008-08-03 rsc }
17 18824b58 2008-08-03 rsc
18 18824b58 2008-08-03 rsc struct Pfd
19 18824b58 2008-08-03 rsc {
20 18824b58 2008-08-03 rsc BIO *sbio;
21 18824b58 2008-08-03 rsc };
22 18824b58 2008-08-03 rsc
23 18824b58 2008-08-03 rsc static Pfd*
24 18824b58 2008-08-03 rsc opensslconnect(char *host)
25 18824b58 2008-08-03 rsc {
26 18824b58 2008-08-03 rsc Pfd *pfd;
27 18824b58 2008-08-03 rsc BIO *sbio;
28 18824b58 2008-08-03 rsc SSL_CTX *ctx;
29 18824b58 2008-08-03 rsc SSL *ssl;
30 18824b58 2008-08-03 rsc static int didinit;
31 18824b58 2008-08-03 rsc char buf[1024];
32 18824b58 2008-08-03 rsc
33 18824b58 2008-08-03 rsc if(!didinit){
34 18824b58 2008-08-03 rsc httpsinit();
35 18824b58 2008-08-03 rsc didinit = 1;
36 18824b58 2008-08-03 rsc }
37 18824b58 2008-08-03 rsc
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);
42 18824b58 2008-08-03 rsc
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);
45 18824b58 2008-08-03 rsc
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);
50 18824b58 2008-08-03 rsc return nil;
51 18824b58 2008-08-03 rsc }
52 18824b58 2008-08-03 rsc
53 18824b58 2008-08-03 rsc pfd = emalloc(sizeof *pfd);
54 18824b58 2008-08-03 rsc pfd->sbio = sbio;
55 18824b58 2008-08-03 rsc return pfd;
56 18824b58 2008-08-03 rsc }
57 18824b58 2008-08-03 rsc
58 18824b58 2008-08-03 rsc static void
59 18824b58 2008-08-03 rsc opensslclose(Pfd *pfd)
60 18824b58 2008-08-03 rsc {
61 18824b58 2008-08-03 rsc if(pfd == nil)
62 18824b58 2008-08-03 rsc return;
63 18824b58 2008-08-03 rsc BIO_free_all(pfd->sbio);
64 18824b58 2008-08-03 rsc free(pfd);
65 18824b58 2008-08-03 rsc }
66 18824b58 2008-08-03 rsc
67 18824b58 2008-08-03 rsc static int
68 18824b58 2008-08-03 rsc opensslwrite(Pfd *pfd, void *v, int n)
69 18824b58 2008-08-03 rsc {
70 18824b58 2008-08-03 rsc int m, total;
71 18824b58 2008-08-03 rsc char *p;
72 18824b58 2008-08-03 rsc
73 18824b58 2008-08-03 rsc p = v;
74 18824b58 2008-08-03 rsc total = 0;
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)
78 18824b58 2008-08-03 rsc return m;
79 18824b58 2008-08-03 rsc return total;
80 18824b58 2008-08-03 rsc }
81 18824b58 2008-08-03 rsc total += m;
82 18824b58 2008-08-03 rsc }
83 18824b58 2008-08-03 rsc return total;
84 18824b58 2008-08-03 rsc }
85 18824b58 2008-08-03 rsc
86 18824b58 2008-08-03 rsc static int
87 18824b58 2008-08-03 rsc opensslread(Pfd *pfd, void *v, int n)
88 18824b58 2008-08-03 rsc {
89 18824b58 2008-08-03 rsc return BIO_read(pfd->sbio, v, n);
90 18824b58 2008-08-03 rsc }
91 18824b58 2008-08-03 rsc
92 18824b58 2008-08-03 rsc Protocol https =
93 18824b58 2008-08-03 rsc {
94 18824b58 2008-08-03 rsc opensslconnect,
95 18824b58 2008-08-03 rsc opensslread,
96 18824b58 2008-08-03 rsc opensslwrite,
97 18824b58 2008-08-03 rsc opensslclose
98 18824b58 2008-08-03 rsc };