polarssl: fix error check on write
[project/ustream-ssl.git] / ustream-openssl.c
1 /*
2  * ustream-ssl - library for SSL over ustream
3  *
4  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #include "ustream-ssl.h"
20 #include "ustream-internal.h"
21
22 __hidden void * __ustream_ssl_context_new(bool server)
23 {
24         static bool _init = false;
25         const void *m;
26         SSL_CTX *c;
27
28         if (!_init) {
29                 SSL_load_error_strings();
30                 SSL_library_init();
31                 _init = true;
32         }
33
34 #ifdef CYASSL_OPENSSL_H_
35         if (server)
36                 m = SSLv23_server_method();
37         else
38                 m = SSLv23_client_method();
39 #else
40         if (server)
41                 m = TLSv1_server_method();
42         else
43                 m = TLSv1_client_method();
44 #endif
45
46         c = SSL_CTX_new((void *) m);
47         if (!c)
48                 return NULL;
49
50         if (server)
51                 SSL_CTX_set_verify(c, SSL_VERIFY_NONE, NULL);
52
53         return c;
54 }
55
56 __hidden int __ustream_ssl_set_crt_file(void *ctx, const char *file)
57 {
58         int ret;
59
60         ret = SSL_CTX_use_certificate_file(ctx, file, SSL_FILETYPE_PEM);
61         if (ret < 1)
62                 ret = SSL_CTX_use_certificate_file(ctx, file, SSL_FILETYPE_ASN1);
63
64         if (ret < 1)
65                 return -1;
66
67         return 0;
68 }
69
70 __hidden int __ustream_ssl_set_key_file(void *ctx, const char *file)
71 {
72         int ret;
73
74         ret = SSL_CTX_use_PrivateKey_file(ctx, file, SSL_FILETYPE_PEM);
75         if (ret < 1)
76                 ret = SSL_CTX_use_PrivateKey_file(ctx, file, SSL_FILETYPE_ASN1);
77
78         if (ret < 1)
79                 return -1;
80
81         return 0;
82 }
83
84 __hidden void __ustream_ssl_context_free(void *ctx)
85 {
86         SSL_CTX_free(ctx);
87 }
88
89 static void ustream_ssl_error(struct ustream_ssl *us, int ret)
90 {
91         us->error = ret;
92         uloop_timeout_set(&us->error_timer, 0);
93 }
94
95 __hidden enum ssl_conn_status __ustream_ssl_connect(struct ustream_ssl *us)
96 {
97         void *ssl = us->ssl;
98         int r;
99
100         if (us->server)
101                 r = SSL_accept(ssl);
102         else
103                 r = SSL_connect(ssl);
104
105         if (r == 1)
106                 return U_SSL_OK;
107
108         r = SSL_get_error(ssl, r);
109         if (r == SSL_ERROR_WANT_READ || r == SSL_ERROR_WANT_WRITE)
110                 return U_SSL_PENDING;
111
112         ustream_ssl_error(us, r);
113         return U_SSL_ERROR;
114 }
115
116 __hidden int __ustream_ssl_write(struct ustream_ssl *us, const char *buf, int len)
117 {
118         void *ssl = us->ssl;
119         int ret = SSL_write(ssl, buf, len);
120
121         if (ret < 0) {
122                 int err = SSL_get_error(ssl, ret);
123                 if (err == SSL_ERROR_WANT_WRITE)
124                         return 0;
125
126                 ustream_ssl_error(us, err);
127                 return -1;
128         }
129
130         return ret;
131 }
132
133 __hidden int __ustream_ssl_read(struct ustream_ssl *us, char *buf, int len)
134 {
135         int ret = SSL_read(us->ssl, buf, len);
136
137         if (ret < 0) {
138                 ret = SSL_get_error(us->ssl, ret);
139                 if (ret == SSL_ERROR_WANT_READ)
140                         return U_SSL_PENDING;
141
142                 ustream_ssl_error(us, ret);
143                 return U_SSL_ERROR;
144         }
145
146         return ret;
147 }
148