return -1/0 instead of ssl specific return codes when setting crt/key files
[project/ustream-ssl.git] / ustream-io.c
1 #include <string.h>
2
3 #include <libubox/ustream.h>
4
5 #include "ustream-io.h"
6 #include "ustream-ssl.h"
7
8 #ifdef CYASSL_OPENSSL_H_
9
10 /* not defined in the header file */
11 typedef int (*CallbackIORecv)(char *buf, int sz, void *ctx);
12 typedef int (*CallbackIOSend)(char *buf, int sz, void *ctx);
13
14 void SetCallbackIORecv_Ctx(SSL_CTX*, CallbackIORecv);
15 void SetCallbackIOSend_Ctx(SSL_CTX*, CallbackIOSend);
16 void SetCallbackIO_ReadCtx(SSL* ssl, void *rctx);
17 void SetCallbackIO_WriteCtx(SSL* ssl, void *wctx);
18
19 static int s_ustream_read(char *buf, int len, void *ctx)
20 {
21         struct ustream *s = ctx;
22         char *sbuf;
23         int slen;
24
25         if (s->eof)
26                 return -3;
27
28         sbuf = ustream_get_read_buf(s, &slen);
29         if (slen > len)
30                 slen = len;
31
32         if (!slen)
33                 return -2;
34
35         memcpy(buf, sbuf, slen);
36         ustream_consume(s, slen);
37
38         return slen;
39 }
40
41 static int s_ustream_write(char *buf, int len, void *ctx)
42 {
43         struct ustream *s = ctx;
44
45         return ustream_write(s, buf, len, false);
46 }
47
48 void ustream_set_io(SSL_CTX *ctx, SSL *ssl, struct ustream *conn)
49 {
50         SetCallbackIO_ReadCtx(ssl, conn);
51         SetCallbackIO_WriteCtx(ssl, conn);
52         SetCallbackIORecv_Ctx(ctx, s_ustream_read);
53         SetCallbackIOSend_Ctx(ctx, s_ustream_write);
54 }
55
56 #else
57
58 static int
59 s_ustream_new(BIO *b)
60 {
61         b->init = 1;
62         b->num = 0;
63         b->ptr = NULL;
64         b->flags = 0;
65         return 1;
66 }
67
68 static int
69 s_ustream_free(BIO *b)
70 {
71         if (!b)
72                 return 0;
73
74         b->ptr = NULL;
75         b->init = 0;
76         b->flags = 0;
77         return 1;
78 }
79
80 static int
81 s_ustream_read(BIO *b, char *buf, int len)
82 {
83         struct ustream *s;
84         char *sbuf;
85         int slen;
86
87         if (!buf || len <= 0)
88                 return 0;
89
90         s = (struct ustream *)b->ptr;
91         if (!s)
92                 return 0;
93
94         sbuf = ustream_get_read_buf(s, &slen);
95
96         BIO_clear_retry_flags(b);
97         if (!slen) {
98                 BIO_set_retry_read(b);
99                 return -1;
100         }
101
102         if (slen > len)
103                 slen = len;
104
105         memcpy(buf, sbuf, slen);
106         ustream_consume(s, slen);
107
108         return slen;
109 }
110
111 static int
112 s_ustream_write(BIO *b, const char *buf, int len)
113 {
114         struct ustream *s;
115
116         if (!buf || len <= 0)
117                 return 0;
118
119         s = (struct ustream *)b->ptr;
120         if (!s)
121                 return 0;
122
123         return ustream_write(s, buf, len, false);
124 }
125
126 static int
127 s_ustream_gets(BIO *b, char *buf, int len)
128 {
129         return -1;
130 }
131
132 static int
133 s_ustream_puts(BIO *b, const char *str)
134 {
135         return s_ustream_write(b, str, strlen(str));
136 }
137
138 static long s_ustream_ctrl(BIO *b, int cmd, long num, void *ptr)
139 {
140         switch (cmd) {
141         case BIO_CTRL_FLUSH:
142                 return 1;
143         default:
144                 return 0;
145         };
146 }
147
148 static BIO_METHOD methods_ustream = {
149         100 | BIO_TYPE_SOURCE_SINK,
150         "ustream",
151         s_ustream_write,
152         s_ustream_read,
153         s_ustream_puts,
154         s_ustream_gets,
155         s_ustream_ctrl,
156         s_ustream_new,
157         s_ustream_free,
158         NULL,
159 };
160
161 static BIO *ustream_bio_new(struct ustream *s)
162 {
163         BIO *bio;
164
165         bio = BIO_new(&methods_ustream);
166         bio->ptr = s;
167         return bio;
168 }
169
170 void ustream_set_io(SSL_CTX *ctx, SSL *ssl, struct ustream *conn)
171 {
172         BIO *bio = ustream_bio_new(conn);
173         SSL_set_bio(ssl, bio, bio);
174 }
175
176 #endif