diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib/libcrypto/crypto/Makefile | 4 | ||||
| -rw-r--r-- | src/lib/libcrypto/evp/bio_ok.c | 625 | ||||
| -rw-r--r-- | src/lib/libcrypto/evp/evp.h | 3 | ||||
| -rw-r--r-- | src/lib/libssl/src/crypto/evp/bio_ok.c | 625 | ||||
| -rw-r--r-- | src/lib/libssl/src/crypto/evp/evp.h | 3 |
5 files changed, 4 insertions, 1256 deletions
diff --git a/src/lib/libcrypto/crypto/Makefile b/src/lib/libcrypto/crypto/Makefile index c27985e113..514c563c13 100644 --- a/src/lib/libcrypto/crypto/Makefile +++ b/src/lib/libcrypto/crypto/Makefile | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | # $OpenBSD: Makefile,v 1.40 2014/06/11 15:08:41 deraadt Exp $ | 1 | # $OpenBSD: Makefile,v 1.41 2014/06/24 19:31:50 miod Exp $ |
| 2 | 2 | ||
| 3 | LIB= crypto | 3 | LIB= crypto |
| 4 | 4 | ||
| @@ -149,7 +149,7 @@ SRCS+= m_null.c m_md4.c m_md5.c m_sha.c m_sha1.c m_wp.c | |||
| 149 | SRCS+= m_dss.c m_dss1.c m_mdc2.c m_ripemd.c m_ecdsa.c | 149 | SRCS+= m_dss.c m_dss1.c m_mdc2.c m_ripemd.c m_ecdsa.c |
| 150 | SRCS+= p_open.c p_seal.c p_sign.c p_verify.c p_lib.c p_enc.c p_dec.c | 150 | SRCS+= p_open.c p_seal.c p_sign.c p_verify.c p_lib.c p_enc.c p_dec.c |
| 151 | SRCS+= bio_md.c bio_b64.c bio_enc.c evp_err.c e_null.c | 151 | SRCS+= bio_md.c bio_b64.c bio_enc.c evp_err.c e_null.c |
| 152 | SRCS+= c_all.c c_allc.c c_alld.c evp_lib.c bio_ok.c | 152 | SRCS+= c_all.c c_allc.c c_alld.c evp_lib.c |
| 153 | SRCS+= evp_pkey.c evp_pbe.c p5_crpt.c p5_crpt2.c | 153 | SRCS+= evp_pkey.c evp_pbe.c p5_crpt.c p5_crpt2.c |
| 154 | SRCS+= e_old.c pmeth_lib.c pmeth_fn.c pmeth_gn.c m_sigver.c | 154 | SRCS+= e_old.c pmeth_lib.c pmeth_fn.c pmeth_gn.c m_sigver.c |
| 155 | SRCS+= e_aes_cbc_hmac_sha1.c e_rc4_hmac_md5.c | 155 | SRCS+= e_aes_cbc_hmac_sha1.c e_rc4_hmac_md5.c |
diff --git a/src/lib/libcrypto/evp/bio_ok.c b/src/lib/libcrypto/evp/bio_ok.c deleted file mode 100644 index e3390e2d92..0000000000 --- a/src/lib/libcrypto/evp/bio_ok.c +++ /dev/null | |||
| @@ -1,625 +0,0 @@ | |||
| 1 | /* $OpenBSD: bio_ok.c,v 1.14 2014/06/12 15:49:29 deraadt Exp $ */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* | ||
| 60 | From: Arne Ansper <arne@cyber.ee> | ||
| 61 | |||
| 62 | Why BIO_f_reliable? | ||
| 63 | |||
| 64 | I wrote function which took BIO* as argument, read data from it | ||
| 65 | and processed it. Then I wanted to store the input file in | ||
| 66 | encrypted form. OK I pushed BIO_f_cipher to the BIO stack | ||
| 67 | and everything was OK. BUT if user types wrong password | ||
| 68 | BIO_f_cipher outputs only garbage and my function crashes. Yes | ||
| 69 | I can and I should fix my function, but BIO_f_cipher is | ||
| 70 | easy way to add encryption support to many existing applications | ||
| 71 | and it's hard to debug and fix them all. | ||
| 72 | |||
| 73 | So I wanted another BIO which would catch the incorrect passwords and | ||
| 74 | file damages which cause garbage on BIO_f_cipher's output. | ||
| 75 | |||
| 76 | The easy way is to push the BIO_f_md and save the checksum at | ||
| 77 | the end of the file. However there are several problems with this | ||
| 78 | approach: | ||
| 79 | |||
| 80 | 1) you must somehow separate checksum from actual data. | ||
| 81 | 2) you need lot's of memory when reading the file, because you | ||
| 82 | must read to the end of the file and verify the checksum before | ||
| 83 | letting the application to read the data. | ||
| 84 | |||
| 85 | BIO_f_reliable tries to solve both problems, so that you can | ||
| 86 | read and write arbitrary long streams using only fixed amount | ||
| 87 | of memory. | ||
| 88 | |||
| 89 | BIO_f_reliable splits data stream into blocks. Each block is prefixed | ||
| 90 | with it's length and suffixed with it's digest. So you need only | ||
| 91 | several Kbytes of memory to buffer single block before verifying | ||
| 92 | it's digest. | ||
| 93 | |||
| 94 | BIO_f_reliable goes further and adds several important capabilities: | ||
| 95 | |||
| 96 | 1) the digest of the block is computed over the whole stream | ||
| 97 | -- so nobody can rearrange the blocks or remove or replace them. | ||
| 98 | |||
| 99 | 2) to detect invalid passwords right at the start BIO_f_reliable | ||
| 100 | adds special prefix to the stream. In order to avoid known plain-text | ||
| 101 | attacks this prefix is generated as follows: | ||
| 102 | |||
| 103 | *) digest is initialized with random seed instead of | ||
| 104 | standardized one. | ||
| 105 | *) same seed is written to output | ||
| 106 | *) well-known text is then hashed and the output | ||
| 107 | of the digest is also written to output. | ||
| 108 | |||
| 109 | reader can now read the seed from stream, hash the same string | ||
| 110 | and then compare the digest output. | ||
| 111 | |||
| 112 | Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I | ||
| 113 | initially wrote and tested this code on x86 machine and wrote the | ||
| 114 | digests out in machine-dependent order :( There are people using | ||
| 115 | this code and I cannot change this easily without making existing | ||
| 116 | data files unreadable. | ||
| 117 | |||
| 118 | */ | ||
| 119 | |||
| 120 | #include <stdio.h> | ||
| 121 | #include <errno.h> | ||
| 122 | #include <assert.h> | ||
| 123 | #include <machine/endian.h> | ||
| 124 | #include "cryptlib.h" | ||
| 125 | #include <openssl/buffer.h> | ||
| 126 | #include <openssl/bio.h> | ||
| 127 | #include <openssl/evp.h> | ||
| 128 | #include <openssl/rand.h> | ||
| 129 | |||
| 130 | static int ok_write(BIO *h, const char *buf, int num); | ||
| 131 | static int ok_read(BIO *h, char *buf, int size); | ||
| 132 | static long ok_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 133 | static int ok_new(BIO *h); | ||
| 134 | static int ok_free(BIO *data); | ||
| 135 | static long ok_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); | ||
| 136 | |||
| 137 | static int sig_out(BIO* b); | ||
| 138 | static int sig_in(BIO* b); | ||
| 139 | static int block_out(BIO* b); | ||
| 140 | static int block_in(BIO* b); | ||
| 141 | #define OK_BLOCK_SIZE (1024*4) | ||
| 142 | #define OK_BLOCK_BLOCK 4 | ||
| 143 | #define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE) | ||
| 144 | #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back." | ||
| 145 | |||
| 146 | typedef struct ok_struct { | ||
| 147 | size_t buf_len; | ||
| 148 | size_t buf_off; | ||
| 149 | size_t buf_len_save; | ||
| 150 | size_t buf_off_save; | ||
| 151 | int cont; /* <= 0 when finished */ | ||
| 152 | int finished; | ||
| 153 | EVP_MD_CTX md; | ||
| 154 | int blockout; /* output block is ready */ | ||
| 155 | int sigio; /* must process signature */ | ||
| 156 | unsigned char buf[IOBS]; | ||
| 157 | } BIO_OK_CTX; | ||
| 158 | |||
| 159 | static BIO_METHOD methods_ok = { | ||
| 160 | .type = BIO_TYPE_CIPHER, | ||
| 161 | .name = "reliable", | ||
| 162 | .bwrite = ok_write, | ||
| 163 | .bread = ok_read, | ||
| 164 | .ctrl = ok_ctrl, | ||
| 165 | .create = ok_new, | ||
| 166 | .destroy = ok_free, | ||
| 167 | .callback_ctrl = ok_callback_ctrl | ||
| 168 | }; | ||
| 169 | |||
| 170 | BIO_METHOD * | ||
| 171 | BIO_f_reliable(void) | ||
| 172 | { | ||
| 173 | return (&methods_ok); | ||
| 174 | } | ||
| 175 | |||
| 176 | static int | ||
| 177 | ok_new(BIO *bi) | ||
| 178 | { | ||
| 179 | BIO_OK_CTX *ctx; | ||
| 180 | |||
| 181 | ctx = malloc(sizeof(BIO_OK_CTX)); | ||
| 182 | if (ctx == NULL) | ||
| 183 | return (0); | ||
| 184 | |||
| 185 | ctx->buf_len = 0; | ||
| 186 | ctx->buf_off = 0; | ||
| 187 | ctx->buf_len_save = 0; | ||
| 188 | ctx->buf_off_save = 0; | ||
| 189 | ctx->cont = 1; | ||
| 190 | ctx->finished = 0; | ||
| 191 | ctx->blockout = 0; | ||
| 192 | ctx->sigio = 1; | ||
| 193 | |||
| 194 | EVP_MD_CTX_init(&ctx->md); | ||
| 195 | |||
| 196 | bi->init = 0; | ||
| 197 | bi->ptr = (char *)ctx; | ||
| 198 | bi->flags = 0; | ||
| 199 | return (1); | ||
| 200 | } | ||
| 201 | |||
| 202 | static int | ||
| 203 | ok_free(BIO *a) | ||
| 204 | { | ||
| 205 | if (a == NULL) | ||
| 206 | return (0); | ||
| 207 | EVP_MD_CTX_cleanup(&((BIO_OK_CTX *)a->ptr)->md); | ||
| 208 | OPENSSL_cleanse(a->ptr, sizeof(BIO_OK_CTX)); | ||
| 209 | free(a->ptr); | ||
| 210 | a->ptr = NULL; | ||
| 211 | a->init = 0; | ||
| 212 | a->flags = 0; | ||
| 213 | return (1); | ||
| 214 | } | ||
| 215 | |||
| 216 | static int | ||
| 217 | ok_read(BIO *b, char *out, int outl) | ||
| 218 | { | ||
| 219 | int ret = 0, i, n; | ||
| 220 | BIO_OK_CTX *ctx; | ||
| 221 | |||
| 222 | if (out == NULL) | ||
| 223 | return (0); | ||
| 224 | ctx = (BIO_OK_CTX *)b->ptr; | ||
| 225 | |||
| 226 | if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) | ||
| 227 | return (0); | ||
| 228 | |||
| 229 | while (outl > 0) { | ||
| 230 | |||
| 231 | /* copy clean bytes to output buffer */ | ||
| 232 | if (ctx->blockout) { | ||
| 233 | i = ctx->buf_len - ctx->buf_off; | ||
| 234 | if (i > outl) | ||
| 235 | i = outl; | ||
| 236 | memcpy(out, &(ctx->buf[ctx->buf_off]), i); | ||
| 237 | ret += i; | ||
| 238 | out += i; | ||
| 239 | outl -= i; | ||
| 240 | ctx->buf_off += i; | ||
| 241 | |||
| 242 | /* all clean bytes are out */ | ||
| 243 | if (ctx->buf_len == ctx->buf_off) { | ||
| 244 | ctx->buf_off = 0; | ||
| 245 | |||
| 246 | /* copy start of the next block into proper place */ | ||
| 247 | if (ctx->buf_len_save - ctx->buf_off_save > 0) { | ||
| 248 | ctx->buf_len = ctx->buf_len_save - ctx->buf_off_save; | ||
| 249 | memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]), | ||
| 250 | ctx->buf_len); | ||
| 251 | } else { | ||
| 252 | ctx->buf_len = 0; | ||
| 253 | } | ||
| 254 | ctx->blockout = 0; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | /* output buffer full -- cancel */ | ||
| 259 | if (outl == 0) | ||
| 260 | break; | ||
| 261 | |||
| 262 | /* no clean bytes in buffer -- fill it */ | ||
| 263 | n = IOBS - ctx->buf_len; | ||
| 264 | i = BIO_read(b->next_bio, &(ctx->buf[ctx->buf_len]), n); | ||
| 265 | |||
| 266 | if (i <= 0) break; /* nothing new */ | ||
| 267 | |||
| 268 | ctx->buf_len += i; | ||
| 269 | |||
| 270 | /* no signature yet -- check if we got one */ | ||
| 271 | if (ctx->sigio == 1) { | ||
| 272 | if (!sig_in(b)) { | ||
| 273 | BIO_clear_retry_flags(b); | ||
| 274 | return 0; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | /* signature ok -- check if we got block */ | ||
| 279 | if (ctx->sigio == 0) { | ||
| 280 | if (!block_in(b)) { | ||
| 281 | BIO_clear_retry_flags(b); | ||
| 282 | return 0; | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | /* invalid block -- cancel */ | ||
| 287 | if (ctx->cont <= 0) | ||
| 288 | break; | ||
| 289 | |||
| 290 | } | ||
| 291 | |||
| 292 | BIO_clear_retry_flags(b); | ||
| 293 | BIO_copy_next_retry(b); | ||
| 294 | return (ret); | ||
| 295 | } | ||
| 296 | |||
| 297 | static int | ||
| 298 | ok_write(BIO *b, const char *in, int inl) | ||
| 299 | { | ||
| 300 | int ret = 0, n, i; | ||
| 301 | BIO_OK_CTX *ctx; | ||
| 302 | |||
| 303 | if (inl <= 0) | ||
| 304 | return inl; | ||
| 305 | |||
| 306 | ctx = (BIO_OK_CTX *)b->ptr; | ||
| 307 | ret = inl; | ||
| 308 | |||
| 309 | if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) | ||
| 310 | return (0); | ||
| 311 | |||
| 312 | if (ctx->sigio && !sig_out(b)) | ||
| 313 | return 0; | ||
| 314 | |||
| 315 | do { | ||
| 316 | BIO_clear_retry_flags(b); | ||
| 317 | n = ctx->buf_len - ctx->buf_off; | ||
| 318 | while (ctx->blockout && n > 0) { | ||
| 319 | i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); | ||
| 320 | if (i <= 0) { | ||
| 321 | BIO_copy_next_retry(b); | ||
| 322 | if (!BIO_should_retry(b)) | ||
| 323 | ctx->cont = 0; | ||
| 324 | return (i); | ||
| 325 | } | ||
| 326 | ctx->buf_off += i; | ||
| 327 | n -= i; | ||
| 328 | } | ||
| 329 | |||
| 330 | /* at this point all pending data has been written */ | ||
| 331 | ctx->blockout = 0; | ||
| 332 | if (ctx->buf_len == ctx->buf_off) { | ||
| 333 | ctx->buf_len = OK_BLOCK_BLOCK; | ||
| 334 | ctx->buf_off = 0; | ||
| 335 | } | ||
| 336 | |||
| 337 | if ((in == NULL) || (inl <= 0)) | ||
| 338 | return (0); | ||
| 339 | |||
| 340 | n = (inl + ctx->buf_len > OK_BLOCK_SIZE + OK_BLOCK_BLOCK) ? | ||
| 341 | (int)(OK_BLOCK_SIZE + OK_BLOCK_BLOCK - ctx->buf_len) : inl; | ||
| 342 | |||
| 343 | memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])), | ||
| 344 | (unsigned char *)in, n); | ||
| 345 | ctx->buf_len += n; | ||
| 346 | inl -= n; | ||
| 347 | in += n; | ||
| 348 | |||
| 349 | if (ctx->buf_len >= OK_BLOCK_SIZE + OK_BLOCK_BLOCK) { | ||
| 350 | if (!block_out(b)) { | ||
| 351 | BIO_clear_retry_flags(b); | ||
| 352 | return 0; | ||
| 353 | } | ||
| 354 | } | ||
| 355 | } while (inl > 0); | ||
| 356 | |||
| 357 | BIO_clear_retry_flags(b); | ||
| 358 | BIO_copy_next_retry(b); | ||
| 359 | return (ret); | ||
| 360 | } | ||
| 361 | |||
| 362 | static long | ||
| 363 | ok_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 364 | { | ||
| 365 | BIO_OK_CTX *ctx; | ||
| 366 | EVP_MD *md; | ||
| 367 | const EVP_MD **ppmd; | ||
| 368 | long ret = 1; | ||
| 369 | int i; | ||
| 370 | |||
| 371 | ctx = b->ptr; | ||
| 372 | |||
| 373 | switch (cmd) { | ||
| 374 | case BIO_CTRL_RESET: | ||
| 375 | ctx->buf_len = 0; | ||
| 376 | ctx->buf_off = 0; | ||
| 377 | ctx->buf_len_save = 0; | ||
| 378 | ctx->buf_off_save = 0; | ||
| 379 | ctx->cont = 1; | ||
| 380 | ctx->finished = 0; | ||
| 381 | ctx->blockout = 0; | ||
| 382 | ctx->sigio = 1; | ||
| 383 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
| 384 | break; | ||
| 385 | case BIO_CTRL_EOF: /* More to read */ | ||
| 386 | if (ctx->cont <= 0) | ||
| 387 | ret = 1; | ||
| 388 | else | ||
| 389 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
| 390 | break; | ||
| 391 | case BIO_CTRL_PENDING: /* More to read in buffer */ | ||
| 392 | case BIO_CTRL_WPENDING: /* More to read in buffer */ | ||
| 393 | ret = ctx->blockout ? ctx->buf_len - ctx->buf_off : 0; | ||
| 394 | if (ret <= 0) | ||
| 395 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
| 396 | break; | ||
| 397 | case BIO_CTRL_FLUSH: | ||
| 398 | /* do a final write */ | ||
| 399 | if (ctx->blockout == 0) | ||
| 400 | if (!block_out(b)) | ||
| 401 | return 0; | ||
| 402 | |||
| 403 | while (ctx->blockout) { | ||
| 404 | i = ok_write(b, NULL, 0); | ||
| 405 | if (i < 0) { | ||
| 406 | ret = i; | ||
| 407 | break; | ||
| 408 | } | ||
| 409 | } | ||
| 410 | |||
| 411 | ctx->finished = 1; | ||
| 412 | ctx->buf_off = ctx->buf_len = 0; | ||
| 413 | ctx->cont = (int)ret; | ||
| 414 | |||
| 415 | /* Finally flush the underlying BIO */ | ||
| 416 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
| 417 | break; | ||
| 418 | case BIO_C_DO_STATE_MACHINE: | ||
| 419 | BIO_clear_retry_flags(b); | ||
| 420 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
| 421 | BIO_copy_next_retry(b); | ||
| 422 | break; | ||
| 423 | case BIO_CTRL_INFO: | ||
| 424 | ret = (long)ctx->cont; | ||
| 425 | break; | ||
| 426 | case BIO_C_SET_MD: | ||
| 427 | md = ptr; | ||
| 428 | if (!EVP_DigestInit_ex(&ctx->md, md, NULL)) | ||
| 429 | return 0; | ||
| 430 | b->init = 1; | ||
| 431 | break; | ||
| 432 | case BIO_C_GET_MD: | ||
| 433 | if (b->init) { | ||
| 434 | ppmd = ptr; | ||
| 435 | *ppmd = ctx->md.digest; | ||
| 436 | } else | ||
| 437 | ret = 0; | ||
| 438 | break; | ||
| 439 | default: | ||
| 440 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
| 441 | break; | ||
| 442 | } | ||
| 443 | return (ret); | ||
| 444 | } | ||
| 445 | |||
| 446 | static long | ||
| 447 | ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | ||
| 448 | { | ||
| 449 | long ret = 1; | ||
| 450 | |||
| 451 | if (b->next_bio == NULL) | ||
| 452 | return (0); | ||
| 453 | switch (cmd) { | ||
| 454 | default: | ||
| 455 | ret = BIO_callback_ctrl(b->next_bio, cmd, fp); | ||
| 456 | break; | ||
| 457 | } | ||
| 458 | return (ret); | ||
| 459 | } | ||
| 460 | |||
| 461 | static void | ||
| 462 | longswap(void *_ptr, size_t len) | ||
| 463 | { | ||
| 464 | if (_BYTE_ORDER == _LITTLE_ENDIAN) { | ||
| 465 | size_t i; | ||
| 466 | unsigned char *p = _ptr, c; | ||
| 467 | |||
| 468 | for (i = 0; i < len; i += 4) { | ||
| 469 | c = p[0], p[0] = p[3], p[3] = c; | ||
| 470 | c = p[1], p[1] = p[2], p[2] = c; | ||
| 471 | } | ||
| 472 | } | ||
| 473 | } | ||
| 474 | |||
| 475 | static int | ||
| 476 | sig_out(BIO* b) | ||
| 477 | { | ||
| 478 | BIO_OK_CTX *ctx; | ||
| 479 | EVP_MD_CTX *md; | ||
| 480 | |||
| 481 | ctx = b->ptr; | ||
| 482 | md = &ctx->md; | ||
| 483 | |||
| 484 | if (ctx->buf_len + 2* md->digest->md_size > OK_BLOCK_SIZE) | ||
| 485 | return 1; | ||
| 486 | |||
| 487 | if (!EVP_DigestInit_ex(md, md->digest, NULL)) | ||
| 488 | goto berr; | ||
| 489 | /* FIXME: there's absolutely no guarantee this makes any sense at all, | ||
| 490 | * particularly now EVP_MD_CTX has been restructured. | ||
| 491 | */ | ||
| 492 | RAND_pseudo_bytes(md->md_data, md->digest->md_size); | ||
| 493 | memcpy(&(ctx->buf[ctx->buf_len]), md->md_data, md->digest->md_size); | ||
| 494 | longswap(&(ctx->buf[ctx->buf_len]), md->digest->md_size); | ||
| 495 | ctx->buf_len += md->digest->md_size; | ||
| 496 | |||
| 497 | if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN))) | ||
| 498 | goto berr; | ||
| 499 | if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL)) | ||
| 500 | goto berr; | ||
| 501 | ctx->buf_len += md->digest->md_size; | ||
| 502 | ctx->blockout = 1; | ||
| 503 | ctx->sigio = 0; | ||
| 504 | return 1; | ||
| 505 | berr: | ||
| 506 | BIO_clear_retry_flags(b); | ||
| 507 | return 0; | ||
| 508 | } | ||
| 509 | |||
| 510 | static int | ||
| 511 | sig_in(BIO* b) | ||
| 512 | { | ||
| 513 | BIO_OK_CTX *ctx; | ||
| 514 | EVP_MD_CTX *md; | ||
| 515 | unsigned char tmp[EVP_MAX_MD_SIZE]; | ||
| 516 | int ret = 0; | ||
| 517 | |||
| 518 | ctx = b->ptr; | ||
| 519 | md = &ctx->md; | ||
| 520 | |||
| 521 | if ((int)(ctx->buf_len - ctx->buf_off) < 2*md->digest->md_size) | ||
| 522 | return 1; | ||
| 523 | |||
| 524 | if (!EVP_DigestInit_ex(md, md->digest, NULL)) | ||
| 525 | goto berr; | ||
| 526 | memcpy(md->md_data, &(ctx->buf[ctx->buf_off]), md->digest->md_size); | ||
| 527 | longswap(md->md_data, md->digest->md_size); | ||
| 528 | ctx->buf_off += md->digest->md_size; | ||
| 529 | |||
| 530 | if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN))) | ||
| 531 | goto berr; | ||
| 532 | if (!EVP_DigestFinal_ex(md, tmp, NULL)) | ||
| 533 | goto berr; | ||
| 534 | ret = memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0; | ||
| 535 | ctx->buf_off += md->digest->md_size; | ||
| 536 | if (ret == 1) { | ||
| 537 | ctx->sigio = 0; | ||
| 538 | if (ctx->buf_len != ctx->buf_off) { | ||
| 539 | memmove(ctx->buf, &(ctx->buf[ctx->buf_off]), | ||
| 540 | ctx->buf_len - ctx->buf_off); | ||
| 541 | } | ||
| 542 | ctx->buf_len -= ctx->buf_off; | ||
| 543 | ctx->buf_off = 0; | ||
| 544 | } else { | ||
| 545 | ctx->cont = 0; | ||
| 546 | } | ||
| 547 | return 1; | ||
| 548 | berr: | ||
| 549 | BIO_clear_retry_flags(b); | ||
| 550 | return 0; | ||
| 551 | } | ||
| 552 | |||
| 553 | static int | ||
| 554 | block_out(BIO* b) | ||
| 555 | { | ||
| 556 | BIO_OK_CTX *ctx; | ||
| 557 | EVP_MD_CTX *md; | ||
| 558 | unsigned long tl; | ||
| 559 | |||
| 560 | ctx = b->ptr; | ||
| 561 | md = &ctx->md; | ||
| 562 | |||
| 563 | tl = ctx->buf_len - OK_BLOCK_BLOCK; | ||
| 564 | ctx->buf[0] = (unsigned char)(tl >> 24); | ||
| 565 | ctx->buf[1] = (unsigned char)(tl >> 16); | ||
| 566 | ctx->buf[2] = (unsigned char)(tl >> 8); | ||
| 567 | ctx->buf[3] = (unsigned char)(tl); | ||
| 568 | if (!EVP_DigestUpdate(md, | ||
| 569 | (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl)) | ||
| 570 | goto berr; | ||
| 571 | if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL)) | ||
| 572 | goto berr; | ||
| 573 | ctx->buf_len += md->digest->md_size; | ||
| 574 | ctx->blockout = 1; | ||
| 575 | return 1; | ||
| 576 | berr: | ||
| 577 | BIO_clear_retry_flags(b); | ||
| 578 | return 0; | ||
| 579 | } | ||
| 580 | |||
| 581 | static int | ||
| 582 | block_in(BIO* b) | ||
| 583 | { | ||
| 584 | BIO_OK_CTX *ctx; | ||
| 585 | EVP_MD_CTX *md; | ||
| 586 | unsigned long tl = 0; | ||
| 587 | unsigned char tmp[EVP_MAX_MD_SIZE]; | ||
| 588 | |||
| 589 | ctx = b->ptr; | ||
| 590 | md = &ctx->md; | ||
| 591 | |||
| 592 | assert(sizeof(tl) >= OK_BLOCK_BLOCK); /* always true */ | ||
| 593 | tl = ctx->buf[0]; | ||
| 594 | tl <<= 8; | ||
| 595 | tl |= ctx->buf[1]; | ||
| 596 | tl <<= 8; | ||
| 597 | tl |= ctx->buf[2]; | ||
| 598 | tl <<= 8; | ||
| 599 | tl |= ctx->buf[3]; | ||
| 600 | |||
| 601 | if (ctx->buf_len < tl + OK_BLOCK_BLOCK + md->digest->md_size) | ||
| 602 | return 1; | ||
| 603 | |||
| 604 | if (!EVP_DigestUpdate(md, | ||
| 605 | (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl)) | ||
| 606 | goto berr; | ||
| 607 | if (!EVP_DigestFinal_ex(md, tmp, NULL)) | ||
| 608 | goto berr; | ||
| 609 | if (memcmp(&(ctx->buf[tl + OK_BLOCK_BLOCK]), tmp, | ||
| 610 | md->digest->md_size) == 0) { | ||
| 611 | /* there might be parts from next block lurking around ! */ | ||
| 612 | ctx->buf_off_save = tl + OK_BLOCK_BLOCK + md->digest->md_size; | ||
| 613 | ctx->buf_len_save = ctx->buf_len; | ||
| 614 | ctx->buf_off = OK_BLOCK_BLOCK; | ||
| 615 | ctx->buf_len = tl + OK_BLOCK_BLOCK; | ||
| 616 | ctx->blockout = 1; | ||
| 617 | } else { | ||
| 618 | ctx->cont = 0; | ||
| 619 | } | ||
| 620 | return 1; | ||
| 621 | |||
| 622 | berr: | ||
| 623 | BIO_clear_retry_flags(b); | ||
| 624 | return 0; | ||
| 625 | } | ||
diff --git a/src/lib/libcrypto/evp/evp.h b/src/lib/libcrypto/evp/evp.h index b504301b0a..41b0259402 100644 --- a/src/lib/libcrypto/evp/evp.h +++ b/src/lib/libcrypto/evp/evp.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: evp.h,v 1.37 2014/06/12 15:49:29 deraadt Exp $ */ | 1 | /* $OpenBSD: evp.h,v 1.38 2014/06/24 19:31:50 miod Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -636,7 +636,6 @@ int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key); | |||
| 636 | BIO_METHOD *BIO_f_md(void); | 636 | BIO_METHOD *BIO_f_md(void); |
| 637 | BIO_METHOD *BIO_f_base64(void); | 637 | BIO_METHOD *BIO_f_base64(void); |
| 638 | BIO_METHOD *BIO_f_cipher(void); | 638 | BIO_METHOD *BIO_f_cipher(void); |
| 639 | BIO_METHOD *BIO_f_reliable(void); | ||
| 640 | void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k, | 639 | void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k, |
| 641 | const unsigned char *i, int enc); | 640 | const unsigned char *i, int enc); |
| 642 | #endif | 641 | #endif |
diff --git a/src/lib/libssl/src/crypto/evp/bio_ok.c b/src/lib/libssl/src/crypto/evp/bio_ok.c deleted file mode 100644 index e3390e2d92..0000000000 --- a/src/lib/libssl/src/crypto/evp/bio_ok.c +++ /dev/null | |||
| @@ -1,625 +0,0 @@ | |||
| 1 | /* $OpenBSD: bio_ok.c,v 1.14 2014/06/12 15:49:29 deraadt Exp $ */ | ||
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * This package is an SSL implementation written | ||
| 6 | * by Eric Young (eay@cryptsoft.com). | ||
| 7 | * The implementation was written so as to conform with Netscapes SSL. | ||
| 8 | * | ||
| 9 | * This library is free for commercial and non-commercial use as long as | ||
| 10 | * the following conditions are aheared to. The following conditions | ||
| 11 | * apply to all code found in this distribution, be it the RC4, RSA, | ||
| 12 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | ||
| 13 | * included with this distribution is covered by the same copyright terms | ||
| 14 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | ||
| 15 | * | ||
| 16 | * Copyright remains Eric Young's, and as such any Copyright notices in | ||
| 17 | * the code are not to be removed. | ||
| 18 | * If this package is used in a product, Eric Young should be given attribution | ||
| 19 | * as the author of the parts of the library used. | ||
| 20 | * This can be in the form of a textual message at program startup or | ||
| 21 | * in documentation (online or textual) provided with the package. | ||
| 22 | * | ||
| 23 | * Redistribution and use in source and binary forms, with or without | ||
| 24 | * modification, are permitted provided that the following conditions | ||
| 25 | * are met: | ||
| 26 | * 1. Redistributions of source code must retain the copyright | ||
| 27 | * notice, this list of conditions and the following disclaimer. | ||
| 28 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 29 | * notice, this list of conditions and the following disclaimer in the | ||
| 30 | * documentation and/or other materials provided with the distribution. | ||
| 31 | * 3. All advertising materials mentioning features or use of this software | ||
| 32 | * must display the following acknowledgement: | ||
| 33 | * "This product includes cryptographic software written by | ||
| 34 | * Eric Young (eay@cryptsoft.com)" | ||
| 35 | * The word 'cryptographic' can be left out if the rouines from the library | ||
| 36 | * being used are not cryptographic related :-). | ||
| 37 | * 4. If you include any Windows specific code (or a derivative thereof) from | ||
| 38 | * the apps directory (application code) you must include an acknowledgement: | ||
| 39 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | ||
| 40 | * | ||
| 41 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | ||
| 42 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 43 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 44 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | ||
| 45 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
| 46 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
| 47 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 48 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
| 49 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
| 50 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
| 51 | * SUCH DAMAGE. | ||
| 52 | * | ||
| 53 | * The licence and distribution terms for any publically available version or | ||
| 54 | * derivative of this code cannot be changed. i.e. this code cannot simply be | ||
| 55 | * copied and put under another distribution licence | ||
| 56 | * [including the GNU Public Licence.] | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* | ||
| 60 | From: Arne Ansper <arne@cyber.ee> | ||
| 61 | |||
| 62 | Why BIO_f_reliable? | ||
| 63 | |||
| 64 | I wrote function which took BIO* as argument, read data from it | ||
| 65 | and processed it. Then I wanted to store the input file in | ||
| 66 | encrypted form. OK I pushed BIO_f_cipher to the BIO stack | ||
| 67 | and everything was OK. BUT if user types wrong password | ||
| 68 | BIO_f_cipher outputs only garbage and my function crashes. Yes | ||
| 69 | I can and I should fix my function, but BIO_f_cipher is | ||
| 70 | easy way to add encryption support to many existing applications | ||
| 71 | and it's hard to debug and fix them all. | ||
| 72 | |||
| 73 | So I wanted another BIO which would catch the incorrect passwords and | ||
| 74 | file damages which cause garbage on BIO_f_cipher's output. | ||
| 75 | |||
| 76 | The easy way is to push the BIO_f_md and save the checksum at | ||
| 77 | the end of the file. However there are several problems with this | ||
| 78 | approach: | ||
| 79 | |||
| 80 | 1) you must somehow separate checksum from actual data. | ||
| 81 | 2) you need lot's of memory when reading the file, because you | ||
| 82 | must read to the end of the file and verify the checksum before | ||
| 83 | letting the application to read the data. | ||
| 84 | |||
| 85 | BIO_f_reliable tries to solve both problems, so that you can | ||
| 86 | read and write arbitrary long streams using only fixed amount | ||
| 87 | of memory. | ||
| 88 | |||
| 89 | BIO_f_reliable splits data stream into blocks. Each block is prefixed | ||
| 90 | with it's length and suffixed with it's digest. So you need only | ||
| 91 | several Kbytes of memory to buffer single block before verifying | ||
| 92 | it's digest. | ||
| 93 | |||
| 94 | BIO_f_reliable goes further and adds several important capabilities: | ||
| 95 | |||
| 96 | 1) the digest of the block is computed over the whole stream | ||
| 97 | -- so nobody can rearrange the blocks or remove or replace them. | ||
| 98 | |||
| 99 | 2) to detect invalid passwords right at the start BIO_f_reliable | ||
| 100 | adds special prefix to the stream. In order to avoid known plain-text | ||
| 101 | attacks this prefix is generated as follows: | ||
| 102 | |||
| 103 | *) digest is initialized with random seed instead of | ||
| 104 | standardized one. | ||
| 105 | *) same seed is written to output | ||
| 106 | *) well-known text is then hashed and the output | ||
| 107 | of the digest is also written to output. | ||
| 108 | |||
| 109 | reader can now read the seed from stream, hash the same string | ||
| 110 | and then compare the digest output. | ||
| 111 | |||
| 112 | Bad things: BIO_f_reliable knows what's going on in EVP_Digest. I | ||
| 113 | initially wrote and tested this code on x86 machine and wrote the | ||
| 114 | digests out in machine-dependent order :( There are people using | ||
| 115 | this code and I cannot change this easily without making existing | ||
| 116 | data files unreadable. | ||
| 117 | |||
| 118 | */ | ||
| 119 | |||
| 120 | #include <stdio.h> | ||
| 121 | #include <errno.h> | ||
| 122 | #include <assert.h> | ||
| 123 | #include <machine/endian.h> | ||
| 124 | #include "cryptlib.h" | ||
| 125 | #include <openssl/buffer.h> | ||
| 126 | #include <openssl/bio.h> | ||
| 127 | #include <openssl/evp.h> | ||
| 128 | #include <openssl/rand.h> | ||
| 129 | |||
| 130 | static int ok_write(BIO *h, const char *buf, int num); | ||
| 131 | static int ok_read(BIO *h, char *buf, int size); | ||
| 132 | static long ok_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 133 | static int ok_new(BIO *h); | ||
| 134 | static int ok_free(BIO *data); | ||
| 135 | static long ok_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); | ||
| 136 | |||
| 137 | static int sig_out(BIO* b); | ||
| 138 | static int sig_in(BIO* b); | ||
| 139 | static int block_out(BIO* b); | ||
| 140 | static int block_in(BIO* b); | ||
| 141 | #define OK_BLOCK_SIZE (1024*4) | ||
| 142 | #define OK_BLOCK_BLOCK 4 | ||
| 143 | #define IOBS (OK_BLOCK_SIZE+ OK_BLOCK_BLOCK+ 3*EVP_MAX_MD_SIZE) | ||
| 144 | #define WELLKNOWN "The quick brown fox jumped over the lazy dog's back." | ||
| 145 | |||
| 146 | typedef struct ok_struct { | ||
| 147 | size_t buf_len; | ||
| 148 | size_t buf_off; | ||
| 149 | size_t buf_len_save; | ||
| 150 | size_t buf_off_save; | ||
| 151 | int cont; /* <= 0 when finished */ | ||
| 152 | int finished; | ||
| 153 | EVP_MD_CTX md; | ||
| 154 | int blockout; /* output block is ready */ | ||
| 155 | int sigio; /* must process signature */ | ||
| 156 | unsigned char buf[IOBS]; | ||
| 157 | } BIO_OK_CTX; | ||
| 158 | |||
| 159 | static BIO_METHOD methods_ok = { | ||
| 160 | .type = BIO_TYPE_CIPHER, | ||
| 161 | .name = "reliable", | ||
| 162 | .bwrite = ok_write, | ||
| 163 | .bread = ok_read, | ||
| 164 | .ctrl = ok_ctrl, | ||
| 165 | .create = ok_new, | ||
| 166 | .destroy = ok_free, | ||
| 167 | .callback_ctrl = ok_callback_ctrl | ||
| 168 | }; | ||
| 169 | |||
| 170 | BIO_METHOD * | ||
| 171 | BIO_f_reliable(void) | ||
| 172 | { | ||
| 173 | return (&methods_ok); | ||
| 174 | } | ||
| 175 | |||
| 176 | static int | ||
| 177 | ok_new(BIO *bi) | ||
| 178 | { | ||
| 179 | BIO_OK_CTX *ctx; | ||
| 180 | |||
| 181 | ctx = malloc(sizeof(BIO_OK_CTX)); | ||
| 182 | if (ctx == NULL) | ||
| 183 | return (0); | ||
| 184 | |||
| 185 | ctx->buf_len = 0; | ||
| 186 | ctx->buf_off = 0; | ||
| 187 | ctx->buf_len_save = 0; | ||
| 188 | ctx->buf_off_save = 0; | ||
| 189 | ctx->cont = 1; | ||
| 190 | ctx->finished = 0; | ||
| 191 | ctx->blockout = 0; | ||
| 192 | ctx->sigio = 1; | ||
| 193 | |||
| 194 | EVP_MD_CTX_init(&ctx->md); | ||
| 195 | |||
| 196 | bi->init = 0; | ||
| 197 | bi->ptr = (char *)ctx; | ||
| 198 | bi->flags = 0; | ||
| 199 | return (1); | ||
| 200 | } | ||
| 201 | |||
| 202 | static int | ||
| 203 | ok_free(BIO *a) | ||
| 204 | { | ||
| 205 | if (a == NULL) | ||
| 206 | return (0); | ||
| 207 | EVP_MD_CTX_cleanup(&((BIO_OK_CTX *)a->ptr)->md); | ||
| 208 | OPENSSL_cleanse(a->ptr, sizeof(BIO_OK_CTX)); | ||
| 209 | free(a->ptr); | ||
| 210 | a->ptr = NULL; | ||
| 211 | a->init = 0; | ||
| 212 | a->flags = 0; | ||
| 213 | return (1); | ||
| 214 | } | ||
| 215 | |||
| 216 | static int | ||
| 217 | ok_read(BIO *b, char *out, int outl) | ||
| 218 | { | ||
| 219 | int ret = 0, i, n; | ||
| 220 | BIO_OK_CTX *ctx; | ||
| 221 | |||
| 222 | if (out == NULL) | ||
| 223 | return (0); | ||
| 224 | ctx = (BIO_OK_CTX *)b->ptr; | ||
| 225 | |||
| 226 | if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) | ||
| 227 | return (0); | ||
| 228 | |||
| 229 | while (outl > 0) { | ||
| 230 | |||
| 231 | /* copy clean bytes to output buffer */ | ||
| 232 | if (ctx->blockout) { | ||
| 233 | i = ctx->buf_len - ctx->buf_off; | ||
| 234 | if (i > outl) | ||
| 235 | i = outl; | ||
| 236 | memcpy(out, &(ctx->buf[ctx->buf_off]), i); | ||
| 237 | ret += i; | ||
| 238 | out += i; | ||
| 239 | outl -= i; | ||
| 240 | ctx->buf_off += i; | ||
| 241 | |||
| 242 | /* all clean bytes are out */ | ||
| 243 | if (ctx->buf_len == ctx->buf_off) { | ||
| 244 | ctx->buf_off = 0; | ||
| 245 | |||
| 246 | /* copy start of the next block into proper place */ | ||
| 247 | if (ctx->buf_len_save - ctx->buf_off_save > 0) { | ||
| 248 | ctx->buf_len = ctx->buf_len_save - ctx->buf_off_save; | ||
| 249 | memmove(ctx->buf, &(ctx->buf[ctx->buf_off_save]), | ||
| 250 | ctx->buf_len); | ||
| 251 | } else { | ||
| 252 | ctx->buf_len = 0; | ||
| 253 | } | ||
| 254 | ctx->blockout = 0; | ||
| 255 | } | ||
| 256 | } | ||
| 257 | |||
| 258 | /* output buffer full -- cancel */ | ||
| 259 | if (outl == 0) | ||
| 260 | break; | ||
| 261 | |||
| 262 | /* no clean bytes in buffer -- fill it */ | ||
| 263 | n = IOBS - ctx->buf_len; | ||
| 264 | i = BIO_read(b->next_bio, &(ctx->buf[ctx->buf_len]), n); | ||
| 265 | |||
| 266 | if (i <= 0) break; /* nothing new */ | ||
| 267 | |||
| 268 | ctx->buf_len += i; | ||
| 269 | |||
| 270 | /* no signature yet -- check if we got one */ | ||
| 271 | if (ctx->sigio == 1) { | ||
| 272 | if (!sig_in(b)) { | ||
| 273 | BIO_clear_retry_flags(b); | ||
| 274 | return 0; | ||
| 275 | } | ||
| 276 | } | ||
| 277 | |||
| 278 | /* signature ok -- check if we got block */ | ||
| 279 | if (ctx->sigio == 0) { | ||
| 280 | if (!block_in(b)) { | ||
| 281 | BIO_clear_retry_flags(b); | ||
| 282 | return 0; | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | /* invalid block -- cancel */ | ||
| 287 | if (ctx->cont <= 0) | ||
| 288 | break; | ||
| 289 | |||
| 290 | } | ||
| 291 | |||
| 292 | BIO_clear_retry_flags(b); | ||
| 293 | BIO_copy_next_retry(b); | ||
| 294 | return (ret); | ||
| 295 | } | ||
| 296 | |||
| 297 | static int | ||
| 298 | ok_write(BIO *b, const char *in, int inl) | ||
| 299 | { | ||
| 300 | int ret = 0, n, i; | ||
| 301 | BIO_OK_CTX *ctx; | ||
| 302 | |||
| 303 | if (inl <= 0) | ||
| 304 | return inl; | ||
| 305 | |||
| 306 | ctx = (BIO_OK_CTX *)b->ptr; | ||
| 307 | ret = inl; | ||
| 308 | |||
| 309 | if ((ctx == NULL) || (b->next_bio == NULL) || (b->init == 0)) | ||
| 310 | return (0); | ||
| 311 | |||
| 312 | if (ctx->sigio && !sig_out(b)) | ||
| 313 | return 0; | ||
| 314 | |||
| 315 | do { | ||
| 316 | BIO_clear_retry_flags(b); | ||
| 317 | n = ctx->buf_len - ctx->buf_off; | ||
| 318 | while (ctx->blockout && n > 0) { | ||
| 319 | i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n); | ||
| 320 | if (i <= 0) { | ||
| 321 | BIO_copy_next_retry(b); | ||
| 322 | if (!BIO_should_retry(b)) | ||
| 323 | ctx->cont = 0; | ||
| 324 | return (i); | ||
| 325 | } | ||
| 326 | ctx->buf_off += i; | ||
| 327 | n -= i; | ||
| 328 | } | ||
| 329 | |||
| 330 | /* at this point all pending data has been written */ | ||
| 331 | ctx->blockout = 0; | ||
| 332 | if (ctx->buf_len == ctx->buf_off) { | ||
| 333 | ctx->buf_len = OK_BLOCK_BLOCK; | ||
| 334 | ctx->buf_off = 0; | ||
| 335 | } | ||
| 336 | |||
| 337 | if ((in == NULL) || (inl <= 0)) | ||
| 338 | return (0); | ||
| 339 | |||
| 340 | n = (inl + ctx->buf_len > OK_BLOCK_SIZE + OK_BLOCK_BLOCK) ? | ||
| 341 | (int)(OK_BLOCK_SIZE + OK_BLOCK_BLOCK - ctx->buf_len) : inl; | ||
| 342 | |||
| 343 | memcpy((unsigned char *)(&(ctx->buf[ctx->buf_len])), | ||
| 344 | (unsigned char *)in, n); | ||
| 345 | ctx->buf_len += n; | ||
| 346 | inl -= n; | ||
| 347 | in += n; | ||
| 348 | |||
| 349 | if (ctx->buf_len >= OK_BLOCK_SIZE + OK_BLOCK_BLOCK) { | ||
| 350 | if (!block_out(b)) { | ||
| 351 | BIO_clear_retry_flags(b); | ||
| 352 | return 0; | ||
| 353 | } | ||
| 354 | } | ||
| 355 | } while (inl > 0); | ||
| 356 | |||
| 357 | BIO_clear_retry_flags(b); | ||
| 358 | BIO_copy_next_retry(b); | ||
| 359 | return (ret); | ||
| 360 | } | ||
| 361 | |||
| 362 | static long | ||
| 363 | ok_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 364 | { | ||
| 365 | BIO_OK_CTX *ctx; | ||
| 366 | EVP_MD *md; | ||
| 367 | const EVP_MD **ppmd; | ||
| 368 | long ret = 1; | ||
| 369 | int i; | ||
| 370 | |||
| 371 | ctx = b->ptr; | ||
| 372 | |||
| 373 | switch (cmd) { | ||
| 374 | case BIO_CTRL_RESET: | ||
| 375 | ctx->buf_len = 0; | ||
| 376 | ctx->buf_off = 0; | ||
| 377 | ctx->buf_len_save = 0; | ||
| 378 | ctx->buf_off_save = 0; | ||
| 379 | ctx->cont = 1; | ||
| 380 | ctx->finished = 0; | ||
| 381 | ctx->blockout = 0; | ||
| 382 | ctx->sigio = 1; | ||
| 383 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
| 384 | break; | ||
| 385 | case BIO_CTRL_EOF: /* More to read */ | ||
| 386 | if (ctx->cont <= 0) | ||
| 387 | ret = 1; | ||
| 388 | else | ||
| 389 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
| 390 | break; | ||
| 391 | case BIO_CTRL_PENDING: /* More to read in buffer */ | ||
| 392 | case BIO_CTRL_WPENDING: /* More to read in buffer */ | ||
| 393 | ret = ctx->blockout ? ctx->buf_len - ctx->buf_off : 0; | ||
| 394 | if (ret <= 0) | ||
| 395 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
| 396 | break; | ||
| 397 | case BIO_CTRL_FLUSH: | ||
| 398 | /* do a final write */ | ||
| 399 | if (ctx->blockout == 0) | ||
| 400 | if (!block_out(b)) | ||
| 401 | return 0; | ||
| 402 | |||
| 403 | while (ctx->blockout) { | ||
| 404 | i = ok_write(b, NULL, 0); | ||
| 405 | if (i < 0) { | ||
| 406 | ret = i; | ||
| 407 | break; | ||
| 408 | } | ||
| 409 | } | ||
| 410 | |||
| 411 | ctx->finished = 1; | ||
| 412 | ctx->buf_off = ctx->buf_len = 0; | ||
| 413 | ctx->cont = (int)ret; | ||
| 414 | |||
| 415 | /* Finally flush the underlying BIO */ | ||
| 416 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
| 417 | break; | ||
| 418 | case BIO_C_DO_STATE_MACHINE: | ||
| 419 | BIO_clear_retry_flags(b); | ||
| 420 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
| 421 | BIO_copy_next_retry(b); | ||
| 422 | break; | ||
| 423 | case BIO_CTRL_INFO: | ||
| 424 | ret = (long)ctx->cont; | ||
| 425 | break; | ||
| 426 | case BIO_C_SET_MD: | ||
| 427 | md = ptr; | ||
| 428 | if (!EVP_DigestInit_ex(&ctx->md, md, NULL)) | ||
| 429 | return 0; | ||
| 430 | b->init = 1; | ||
| 431 | break; | ||
| 432 | case BIO_C_GET_MD: | ||
| 433 | if (b->init) { | ||
| 434 | ppmd = ptr; | ||
| 435 | *ppmd = ctx->md.digest; | ||
| 436 | } else | ||
| 437 | ret = 0; | ||
| 438 | break; | ||
| 439 | default: | ||
| 440 | ret = BIO_ctrl(b->next_bio, cmd, num, ptr); | ||
| 441 | break; | ||
| 442 | } | ||
| 443 | return (ret); | ||
| 444 | } | ||
| 445 | |||
| 446 | static long | ||
| 447 | ok_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | ||
| 448 | { | ||
| 449 | long ret = 1; | ||
| 450 | |||
| 451 | if (b->next_bio == NULL) | ||
| 452 | return (0); | ||
| 453 | switch (cmd) { | ||
| 454 | default: | ||
| 455 | ret = BIO_callback_ctrl(b->next_bio, cmd, fp); | ||
| 456 | break; | ||
| 457 | } | ||
| 458 | return (ret); | ||
| 459 | } | ||
| 460 | |||
| 461 | static void | ||
| 462 | longswap(void *_ptr, size_t len) | ||
| 463 | { | ||
| 464 | if (_BYTE_ORDER == _LITTLE_ENDIAN) { | ||
| 465 | size_t i; | ||
| 466 | unsigned char *p = _ptr, c; | ||
| 467 | |||
| 468 | for (i = 0; i < len; i += 4) { | ||
| 469 | c = p[0], p[0] = p[3], p[3] = c; | ||
| 470 | c = p[1], p[1] = p[2], p[2] = c; | ||
| 471 | } | ||
| 472 | } | ||
| 473 | } | ||
| 474 | |||
| 475 | static int | ||
| 476 | sig_out(BIO* b) | ||
| 477 | { | ||
| 478 | BIO_OK_CTX *ctx; | ||
| 479 | EVP_MD_CTX *md; | ||
| 480 | |||
| 481 | ctx = b->ptr; | ||
| 482 | md = &ctx->md; | ||
| 483 | |||
| 484 | if (ctx->buf_len + 2* md->digest->md_size > OK_BLOCK_SIZE) | ||
| 485 | return 1; | ||
| 486 | |||
| 487 | if (!EVP_DigestInit_ex(md, md->digest, NULL)) | ||
| 488 | goto berr; | ||
| 489 | /* FIXME: there's absolutely no guarantee this makes any sense at all, | ||
| 490 | * particularly now EVP_MD_CTX has been restructured. | ||
| 491 | */ | ||
| 492 | RAND_pseudo_bytes(md->md_data, md->digest->md_size); | ||
| 493 | memcpy(&(ctx->buf[ctx->buf_len]), md->md_data, md->digest->md_size); | ||
| 494 | longswap(&(ctx->buf[ctx->buf_len]), md->digest->md_size); | ||
| 495 | ctx->buf_len += md->digest->md_size; | ||
| 496 | |||
| 497 | if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN))) | ||
| 498 | goto berr; | ||
| 499 | if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL)) | ||
| 500 | goto berr; | ||
| 501 | ctx->buf_len += md->digest->md_size; | ||
| 502 | ctx->blockout = 1; | ||
| 503 | ctx->sigio = 0; | ||
| 504 | return 1; | ||
| 505 | berr: | ||
| 506 | BIO_clear_retry_flags(b); | ||
| 507 | return 0; | ||
| 508 | } | ||
| 509 | |||
| 510 | static int | ||
| 511 | sig_in(BIO* b) | ||
| 512 | { | ||
| 513 | BIO_OK_CTX *ctx; | ||
| 514 | EVP_MD_CTX *md; | ||
| 515 | unsigned char tmp[EVP_MAX_MD_SIZE]; | ||
| 516 | int ret = 0; | ||
| 517 | |||
| 518 | ctx = b->ptr; | ||
| 519 | md = &ctx->md; | ||
| 520 | |||
| 521 | if ((int)(ctx->buf_len - ctx->buf_off) < 2*md->digest->md_size) | ||
| 522 | return 1; | ||
| 523 | |||
| 524 | if (!EVP_DigestInit_ex(md, md->digest, NULL)) | ||
| 525 | goto berr; | ||
| 526 | memcpy(md->md_data, &(ctx->buf[ctx->buf_off]), md->digest->md_size); | ||
| 527 | longswap(md->md_data, md->digest->md_size); | ||
| 528 | ctx->buf_off += md->digest->md_size; | ||
| 529 | |||
| 530 | if (!EVP_DigestUpdate(md, WELLKNOWN, strlen(WELLKNOWN))) | ||
| 531 | goto berr; | ||
| 532 | if (!EVP_DigestFinal_ex(md, tmp, NULL)) | ||
| 533 | goto berr; | ||
| 534 | ret = memcmp(&(ctx->buf[ctx->buf_off]), tmp, md->digest->md_size) == 0; | ||
| 535 | ctx->buf_off += md->digest->md_size; | ||
| 536 | if (ret == 1) { | ||
| 537 | ctx->sigio = 0; | ||
| 538 | if (ctx->buf_len != ctx->buf_off) { | ||
| 539 | memmove(ctx->buf, &(ctx->buf[ctx->buf_off]), | ||
| 540 | ctx->buf_len - ctx->buf_off); | ||
| 541 | } | ||
| 542 | ctx->buf_len -= ctx->buf_off; | ||
| 543 | ctx->buf_off = 0; | ||
| 544 | } else { | ||
| 545 | ctx->cont = 0; | ||
| 546 | } | ||
| 547 | return 1; | ||
| 548 | berr: | ||
| 549 | BIO_clear_retry_flags(b); | ||
| 550 | return 0; | ||
| 551 | } | ||
| 552 | |||
| 553 | static int | ||
| 554 | block_out(BIO* b) | ||
| 555 | { | ||
| 556 | BIO_OK_CTX *ctx; | ||
| 557 | EVP_MD_CTX *md; | ||
| 558 | unsigned long tl; | ||
| 559 | |||
| 560 | ctx = b->ptr; | ||
| 561 | md = &ctx->md; | ||
| 562 | |||
| 563 | tl = ctx->buf_len - OK_BLOCK_BLOCK; | ||
| 564 | ctx->buf[0] = (unsigned char)(tl >> 24); | ||
| 565 | ctx->buf[1] = (unsigned char)(tl >> 16); | ||
| 566 | ctx->buf[2] = (unsigned char)(tl >> 8); | ||
| 567 | ctx->buf[3] = (unsigned char)(tl); | ||
| 568 | if (!EVP_DigestUpdate(md, | ||
| 569 | (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl)) | ||
| 570 | goto berr; | ||
| 571 | if (!EVP_DigestFinal_ex(md, &(ctx->buf[ctx->buf_len]), NULL)) | ||
| 572 | goto berr; | ||
| 573 | ctx->buf_len += md->digest->md_size; | ||
| 574 | ctx->blockout = 1; | ||
| 575 | return 1; | ||
| 576 | berr: | ||
| 577 | BIO_clear_retry_flags(b); | ||
| 578 | return 0; | ||
| 579 | } | ||
| 580 | |||
| 581 | static int | ||
| 582 | block_in(BIO* b) | ||
| 583 | { | ||
| 584 | BIO_OK_CTX *ctx; | ||
| 585 | EVP_MD_CTX *md; | ||
| 586 | unsigned long tl = 0; | ||
| 587 | unsigned char tmp[EVP_MAX_MD_SIZE]; | ||
| 588 | |||
| 589 | ctx = b->ptr; | ||
| 590 | md = &ctx->md; | ||
| 591 | |||
| 592 | assert(sizeof(tl) >= OK_BLOCK_BLOCK); /* always true */ | ||
| 593 | tl = ctx->buf[0]; | ||
| 594 | tl <<= 8; | ||
| 595 | tl |= ctx->buf[1]; | ||
| 596 | tl <<= 8; | ||
| 597 | tl |= ctx->buf[2]; | ||
| 598 | tl <<= 8; | ||
| 599 | tl |= ctx->buf[3]; | ||
| 600 | |||
| 601 | if (ctx->buf_len < tl + OK_BLOCK_BLOCK + md->digest->md_size) | ||
| 602 | return 1; | ||
| 603 | |||
| 604 | if (!EVP_DigestUpdate(md, | ||
| 605 | (unsigned char*) &(ctx->buf[OK_BLOCK_BLOCK]), tl)) | ||
| 606 | goto berr; | ||
| 607 | if (!EVP_DigestFinal_ex(md, tmp, NULL)) | ||
| 608 | goto berr; | ||
| 609 | if (memcmp(&(ctx->buf[tl + OK_BLOCK_BLOCK]), tmp, | ||
| 610 | md->digest->md_size) == 0) { | ||
| 611 | /* there might be parts from next block lurking around ! */ | ||
| 612 | ctx->buf_off_save = tl + OK_BLOCK_BLOCK + md->digest->md_size; | ||
| 613 | ctx->buf_len_save = ctx->buf_len; | ||
| 614 | ctx->buf_off = OK_BLOCK_BLOCK; | ||
| 615 | ctx->buf_len = tl + OK_BLOCK_BLOCK; | ||
| 616 | ctx->blockout = 1; | ||
| 617 | } else { | ||
| 618 | ctx->cont = 0; | ||
| 619 | } | ||
| 620 | return 1; | ||
| 621 | |||
| 622 | berr: | ||
| 623 | BIO_clear_retry_flags(b); | ||
| 624 | return 0; | ||
| 625 | } | ||
diff --git a/src/lib/libssl/src/crypto/evp/evp.h b/src/lib/libssl/src/crypto/evp/evp.h index b504301b0a..41b0259402 100644 --- a/src/lib/libssl/src/crypto/evp/evp.h +++ b/src/lib/libssl/src/crypto/evp/evp.h | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: evp.h,v 1.37 2014/06/12 15:49:29 deraadt Exp $ */ | 1 | /* $OpenBSD: evp.h,v 1.38 2014/06/24 19:31:50 miod Exp $ */ |
| 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 3 | * All rights reserved. | 3 | * All rights reserved. |
| 4 | * | 4 | * |
| @@ -636,7 +636,6 @@ int EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, unsigned char *key); | |||
| 636 | BIO_METHOD *BIO_f_md(void); | 636 | BIO_METHOD *BIO_f_md(void); |
| 637 | BIO_METHOD *BIO_f_base64(void); | 637 | BIO_METHOD *BIO_f_base64(void); |
| 638 | BIO_METHOD *BIO_f_cipher(void); | 638 | BIO_METHOD *BIO_f_cipher(void); |
| 639 | BIO_METHOD *BIO_f_reliable(void); | ||
| 640 | void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k, | 639 | void BIO_set_cipher(BIO *b, const EVP_CIPHER *c, const unsigned char *k, |
| 641 | const unsigned char *i, int enc); | 640 | const unsigned char *i, int enc); |
| 642 | #endif | 641 | #endif |
