Fix segfault in RSA_free() (and DSA/DH/EC_KEY)
[openssl.git] / crypto / bio / bf_nbio.c
1 /*
2  * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdio.h>
11 #include <errno.h>
12 #include "bio_lcl.h"
13 #include "internal/cryptlib.h"
14 #include <openssl/rand.h>
15
16 /*
17  * BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
18  */
19
20 static int nbiof_write(BIO *h, const char *buf, int num);
21 static int nbiof_read(BIO *h, char *buf, int size);
22 static int nbiof_puts(BIO *h, const char *str);
23 static int nbiof_gets(BIO *h, char *str, int size);
24 static long nbiof_ctrl(BIO *h, int cmd, long arg1, void *arg2);
25 static int nbiof_new(BIO *h);
26 static int nbiof_free(BIO *data);
27 static long nbiof_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
28 typedef struct nbio_test_st {
29     /* only set if we sent a 'should retry' error */
30     int lrn;
31     int lwn;
32 } NBIO_TEST;
33
34 static const BIO_METHOD methods_nbiof = {
35     BIO_TYPE_NBIO_TEST,
36     "non-blocking IO test filter",
37     /* TODO: Convert to new style write function */
38     bwrite_conv,
39     nbiof_write,
40     /* TODO: Convert to new style read function */
41     bread_conv,
42     nbiof_read,
43     nbiof_puts,
44     nbiof_gets,
45     nbiof_ctrl,
46     nbiof_new,
47     nbiof_free,
48     nbiof_callback_ctrl,
49 };
50
51 const BIO_METHOD *BIO_f_nbio_test(void)
52 {
53     return &methods_nbiof;
54 }
55
56 static int nbiof_new(BIO *bi)
57 {
58     NBIO_TEST *nt;
59
60     if ((nt = OPENSSL_zalloc(sizeof(*nt))) == NULL) {
61         BIOerr(BIO_F_NBIOF_NEW, ERR_R_MALLOC_FAILURE);
62         return 0;
63     }
64     nt->lrn = -1;
65     nt->lwn = -1;
66     bi->ptr = (char *)nt;
67     bi->init = 1;
68     return 1;
69 }
70
71 static int nbiof_free(BIO *a)
72 {
73     if (a == NULL)
74         return 0;
75     OPENSSL_free(a->ptr);
76     a->ptr = NULL;
77     a->init = 0;
78     a->flags = 0;
79     return 1;
80 }
81
82 static int nbiof_read(BIO *b, char *out, int outl)
83 {
84     int ret = 0;
85     int num;
86     unsigned char n;
87
88     if (out == NULL)
89         return 0;
90     if (b->next_bio == NULL)
91         return 0;
92
93     BIO_clear_retry_flags(b);
94     if (RAND_priv_bytes(&n, 1) <= 0)
95         return -1;
96     num = (n & 0x07);
97
98     if (outl > num)
99         outl = num;
100
101     if (num == 0) {
102         ret = -1;
103         BIO_set_retry_read(b);
104     } else {
105         ret = BIO_read(b->next_bio, out, outl);
106         if (ret < 0)
107             BIO_copy_next_retry(b);
108     }
109     return ret;
110 }
111
112 static int nbiof_write(BIO *b, const char *in, int inl)
113 {
114     NBIO_TEST *nt;
115     int ret = 0;
116     int num;
117     unsigned char n;
118
119     if ((in == NULL) || (inl <= 0))
120         return 0;
121     if (b->next_bio == NULL)
122         return 0;
123     nt = (NBIO_TEST *)b->ptr;
124
125     BIO_clear_retry_flags(b);
126
127     if (nt->lwn > 0) {
128         num = nt->lwn;
129         nt->lwn = 0;
130     } else {
131         if (RAND_priv_bytes(&n, 1) <= 0)
132             return -1;
133         num = (n & 7);
134     }
135
136     if (inl > num)
137         inl = num;
138
139     if (num == 0) {
140         ret = -1;
141         BIO_set_retry_write(b);
142     } else {
143         ret = BIO_write(b->next_bio, in, inl);
144         if (ret < 0) {
145             BIO_copy_next_retry(b);
146             nt->lwn = inl;
147         }
148     }
149     return ret;
150 }
151
152 static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
153 {
154     long ret;
155
156     if (b->next_bio == NULL)
157         return 0;
158     switch (cmd) {
159     case BIO_C_DO_STATE_MACHINE:
160         BIO_clear_retry_flags(b);
161         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
162         BIO_copy_next_retry(b);
163         break;
164     case BIO_CTRL_DUP:
165         ret = 0L;
166         break;
167     default:
168         ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
169         break;
170     }
171     return ret;
172 }
173
174 static long nbiof_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
175 {
176     long ret = 1;
177
178     if (b->next_bio == NULL)
179         return 0;
180     switch (cmd) {
181     default:
182         ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
183         break;
184     }
185     return ret;
186 }
187
188 static int nbiof_gets(BIO *bp, char *buf, int size)
189 {
190     if (bp->next_bio == NULL)
191         return 0;
192     return BIO_gets(bp->next_bio, buf, size);
193 }
194
195 static int nbiof_puts(BIO *bp, const char *str)
196 {
197     if (bp->next_bio == NULL)
198         return 0;
199     return BIO_puts(bp->next_bio, str);
200 }