Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssl/src/crypto/bio/bf_nbio.c |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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; never executed: return &methods_nbiof; | 0 | ||||||||||||
54 | } | - | ||||||||||||
55 | - | |||||||||||||
56 | static int nbiof_new(BIO *bi) | - | ||||||||||||
57 | { | - | ||||||||||||
58 | NBIO_TEST *nt; | - | ||||||||||||
59 | - | |||||||||||||
60 | if ((nt = OPENSSL_zalloc(sizeof(*nt))) == NULL) {
| 0 | ||||||||||||
61 | BIOerr(BIO_F_NBIOF_NEW, ERR_R_MALLOC_FAILURE); | - | ||||||||||||
62 | return 0; never executed: return 0; | 0 | ||||||||||||
63 | } | - | ||||||||||||
64 | nt->lrn = -1; | - | ||||||||||||
65 | nt->lwn = -1; | - | ||||||||||||
66 | bi->ptr = (char *)nt; | - | ||||||||||||
67 | bi->init = 1; | - | ||||||||||||
68 | return 1; never executed: return 1; | 0 | ||||||||||||
69 | } | - | ||||||||||||
70 | - | |||||||||||||
71 | static int nbiof_free(BIO *a) | - | ||||||||||||
72 | { | - | ||||||||||||
73 | if (a == NULL)
| 0 | ||||||||||||
74 | return 0; never executed: return 0; | 0 | ||||||||||||
75 | OPENSSL_free(a->ptr); | - | ||||||||||||
76 | a->ptr = NULL; | - | ||||||||||||
77 | a->init = 0; | - | ||||||||||||
78 | a->flags = 0; | - | ||||||||||||
79 | return 1; never executed: return 1; | 0 | ||||||||||||
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)
| 0 | ||||||||||||
89 | return 0; never executed: return 0; | 0 | ||||||||||||
90 | if (b->next_bio == NULL)
| 0 | ||||||||||||
91 | return 0; never executed: return 0; | 0 | ||||||||||||
92 | - | |||||||||||||
93 | BIO_clear_retry_flags(b); | - | ||||||||||||
94 | if (RAND_priv_bytes(&n, 1) <= 0)
| 0 | ||||||||||||
95 | return -1; never executed: return -1; | 0 | ||||||||||||
96 | num = (n & 0x07); | - | ||||||||||||
97 | - | |||||||||||||
98 | if (outl > num)
| 0 | ||||||||||||
99 | outl = num; never executed: outl = num; | 0 | ||||||||||||
100 | - | |||||||||||||
101 | if (num == 0) {
| 0 | ||||||||||||
102 | ret = -1; | - | ||||||||||||
103 | BIO_set_retry_read(b); | - | ||||||||||||
104 | } else { never executed: end of block | 0 | ||||||||||||
105 | ret = BIO_read(b->next_bio, out, outl); | - | ||||||||||||
106 | if (ret < 0)
| 0 | ||||||||||||
107 | BIO_copy_next_retry(b); never executed: BIO_copy_next_retry(b); | 0 | ||||||||||||
108 | } never executed: end of block | 0 | ||||||||||||
109 | return ret; never executed: return ret; | 0 | ||||||||||||
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))
| 0 | ||||||||||||
120 | return 0; never executed: return 0; | 0 | ||||||||||||
121 | if (b->next_bio == NULL)
| 0 | ||||||||||||
122 | return 0; never executed: return 0; | 0 | ||||||||||||
123 | nt = (NBIO_TEST *)b->ptr; | - | ||||||||||||
124 | - | |||||||||||||
125 | BIO_clear_retry_flags(b); | - | ||||||||||||
126 | - | |||||||||||||
127 | if (nt->lwn > 0) {
| 0 | ||||||||||||
128 | num = nt->lwn; | - | ||||||||||||
129 | nt->lwn = 0; | - | ||||||||||||
130 | } else { never executed: end of block | 0 | ||||||||||||
131 | if (RAND_priv_bytes(&n, 1) <= 0)
| 0 | ||||||||||||
132 | return -1; never executed: return -1; | 0 | ||||||||||||
133 | num = (n & 7); | - | ||||||||||||
134 | } never executed: end of block | 0 | ||||||||||||
135 | - | |||||||||||||
136 | if (inl > num)
| 0 | ||||||||||||
137 | inl = num; never executed: inl = num; | 0 | ||||||||||||
138 | - | |||||||||||||
139 | if (num == 0) {
| 0 | ||||||||||||
140 | ret = -1; | - | ||||||||||||
141 | BIO_set_retry_write(b); | - | ||||||||||||
142 | } else { never executed: end of block | 0 | ||||||||||||
143 | ret = BIO_write(b->next_bio, in, inl); | - | ||||||||||||
144 | if (ret < 0) {
| 0 | ||||||||||||
145 | BIO_copy_next_retry(b); | - | ||||||||||||
146 | nt->lwn = inl; | - | ||||||||||||
147 | } never executed: end of block | 0 | ||||||||||||
148 | } never executed: end of block | 0 | ||||||||||||
149 | return ret; never executed: return ret; | 0 | ||||||||||||
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)
| 0 | ||||||||||||
157 | return 0; never executed: return 0; | 0 | ||||||||||||
158 | switch (cmd) { | - | ||||||||||||
159 | case BIO_C_DO_STATE_MACHINE: never executed: case 101: | 0 | ||||||||||||
160 | BIO_clear_retry_flags(b); | - | ||||||||||||
161 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | - | ||||||||||||
162 | BIO_copy_next_retry(b); | - | ||||||||||||
163 | break; never executed: break; | 0 | ||||||||||||
164 | case BIO_CTRL_DUP: never executed: case 12: | 0 | ||||||||||||
165 | ret = 0L; | - | ||||||||||||
166 | break; never executed: break; | 0 | ||||||||||||
167 | default: never executed: default: | 0 | ||||||||||||
168 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | - | ||||||||||||
169 | break; never executed: break; | 0 | ||||||||||||
170 | } | - | ||||||||||||
171 | return ret; never executed: return ret; | 0 | ||||||||||||
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)
| 0 | ||||||||||||
179 | return 0; never executed: return 0; | 0 | ||||||||||||
180 | switch (cmd) { | - | ||||||||||||
181 | default: never executed: default: | 0 | ||||||||||||
182 | ret = BIO_callback_ctrl(b->next_bio, cmd, fp); | - | ||||||||||||
183 | break; never executed: break; | 0 | ||||||||||||
184 | } | - | ||||||||||||
185 | return ret; never executed: return ret; | 0 | ||||||||||||
186 | } | - | ||||||||||||
187 | - | |||||||||||||
188 | static int nbiof_gets(BIO *bp, char *buf, int size) | - | ||||||||||||
189 | { | - | ||||||||||||
190 | if (bp->next_bio == NULL)
| 0 | ||||||||||||
191 | return 0; never executed: return 0; | 0 | ||||||||||||
192 | return BIO_gets(bp->next_bio, buf, size); never executed: return BIO_gets(bp->next_bio, buf, size); | 0 | ||||||||||||
193 | } | - | ||||||||||||
194 | - | |||||||||||||
195 | static int nbiof_puts(BIO *bp, const char *str) | - | ||||||||||||
196 | { | - | ||||||||||||
197 | if (bp->next_bio == NULL)
| 0 | ||||||||||||
198 | return 0; never executed: return 0; | 0 | ||||||||||||
199 | return BIO_puts(bp->next_bio, str); never executed: return BIO_puts(bp->next_bio, str); | 0 | ||||||||||||
200 | } | - | ||||||||||||
Source code | Switch to Preprocessed file |