2 * ustream-ssl - library for SSL over ustream
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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.
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.
19 #include <sys/types.h>
24 #include "ustream-ssl.h"
25 #include "ustream-internal.h"
27 static int urandom_fd
= -1;
29 static int s_ustream_read(void *ctx
, unsigned char *buf
, size_t len
)
31 struct ustream
*s
= ctx
;
38 sbuf
= ustream_get_read_buf(s
, &slen
);
43 return POLARSSL_ERR_NET_WANT_READ
;
45 memcpy(buf
, sbuf
, slen
);
46 ustream_consume(s
, slen
);
51 static int s_ustream_write(void *ctx
, const unsigned char *buf
, size_t len
)
53 struct ustream
*s
= ctx
;
55 len
= ustream_write(s
, (const char *) buf
, len
, false);
56 if (len
< 0 || s
->write_error
)
57 return POLARSSL_ERR_NET_SEND_FAILED
;
62 __hidden
void ustream_set_io(void *ctx
, void *ssl
, struct ustream
*conn
)
64 ssl_set_bio(ssl
, s_ustream_read
, conn
, s_ustream_write
, conn
);
67 static bool urandom_init(void)
72 urandom_fd
= open("/dev/urandom", O_RDONLY
);
79 static int _urandom(void *ctx
, unsigned char *out
, size_t len
)
81 read(urandom_fd
, out
, len
);
85 __hidden
void * __ustream_ssl_context_new(bool server
)
87 struct ustream_polarssl_ctx
*uctx
;
92 uctx
= calloc(1, sizeof(*uctx
));
96 uctx
->server
= server
;
97 rsa_init(&uctx
->key
, RSA_PKCS_V15
, 0);
102 __hidden
int __ustream_ssl_set_crt_file(void *ctx
, const char *file
)
104 struct ustream_polarssl_ctx
*uctx
= ctx
;
106 if (x509parse_crtfile(&uctx
->cert
, file
))
112 __hidden
int __ustream_ssl_set_key_file(void *ctx
, const char *file
)
114 struct ustream_polarssl_ctx
*uctx
= ctx
;
116 if (x509parse_keyfile(&uctx
->key
, file
, NULL
))
122 __hidden
void __ustream_ssl_context_free(void *ctx
)
124 struct ustream_polarssl_ctx
*uctx
= ctx
;
126 rsa_free(&uctx
->key
);
127 x509_free(&uctx
->cert
);
131 static void ustream_ssl_error(struct ustream_ssl
*us
, int ret
)
134 uloop_timeout_set(&us
->error_timer
, 0);
137 static bool ssl_do_wait(int ret
)
140 case POLARSSL_ERR_NET_WANT_READ
:
141 case POLARSSL_ERR_NET_WANT_WRITE
:
148 __hidden
enum ssl_conn_status
__ustream_ssl_connect(struct ustream_ssl
*us
)
153 r
= ssl_handshake(ssl
);
158 return U_SSL_PENDING
;
160 ustream_ssl_error(us
, r
);
164 __hidden
int __ustream_ssl_write(struct ustream_ssl
*us
, const char *buf
, int len
)
167 int ret
= ssl_write(ssl
, (const unsigned char *) buf
, len
);
170 if (ssl_do_wait(ret
))
173 ustream_ssl_error(us
, ret
);
180 __hidden
int __ustream_ssl_read(struct ustream_ssl
*us
, char *buf
, int len
)
182 int ret
= ssl_read(us
->ssl
, (unsigned char *) buf
, len
);
185 if (ssl_do_wait(ret
))
186 return U_SSL_PENDING
;
188 ustream_ssl_error(us
, ret
);
195 __hidden
void *__ustream_ssl_session_new(void *ctx
)
197 struct ustream_polarssl_ctx
*uctx
= ctx
;
201 ssl
= calloc(1, sizeof(ssl_context
));
212 auth
= SSL_VERIFY_NONE
;
215 auth
= SSL_VERIFY_OPTIONAL
;
218 ssl_set_endpoint(ssl
, ep
);
219 ssl_set_authmode(ssl
, auth
);
220 ssl_set_rng(ssl
, _urandom
, NULL
);
224 ssl_set_ca_chain(ssl
, uctx
->cert
.next
, NULL
, NULL
);
225 ssl_set_own_cert(ssl
, &uctx
->cert
, &uctx
->key
);
228 ssl_session_reset(ssl
);
233 __hidden
void __ustream_ssl_session_free(void *ssl
)