diff options
| author | cvs2svn <admin@example.com> | 2002-05-15 02:29:24 +0000 |
|---|---|---|
| committer | cvs2svn <admin@example.com> | 2002-05-15 02:29:24 +0000 |
| commit | 027351f729b9e837200dae6e1520cda6577ab930 (patch) | |
| tree | e25a717057aa4529e433fc3b1fac8d4df8db3a5c /src/lib/libcrypto/bio | |
| parent | aeeae06a79815dc190061534d47236cec09f9e32 (diff) | |
| download | openbsd-027351f729b9e837200dae6e1520cda6577ab930.tar.gz openbsd-027351f729b9e837200dae6e1520cda6577ab930.tar.bz2 openbsd-027351f729b9e837200dae6e1520cda6577ab930.zip | |
This commit was manufactured by cvs2git to create branch 'unlabeled-1.1.1'.
Diffstat (limited to 'src/lib/libcrypto/bio')
| -rw-r--r-- | src/lib/libcrypto/bio/bf_lbuf.c | 397 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bss_bio.c | 588 | ||||
| -rw-r--r-- | src/lib/libcrypto/bio/bss_log.c | 232 |
3 files changed, 1217 insertions, 0 deletions
diff --git a/src/lib/libcrypto/bio/bf_lbuf.c b/src/lib/libcrypto/bio/bf_lbuf.c new file mode 100644 index 0000000000..7bcf8ed941 --- /dev/null +++ b/src/lib/libcrypto/bio/bf_lbuf.c | |||
| @@ -0,0 +1,397 @@ | |||
| 1 | /* crypto/bio/bf_buff.c */ | ||
| 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 | #include <stdio.h> | ||
| 60 | #include <errno.h> | ||
| 61 | #include "cryptlib.h" | ||
| 62 | #include <openssl/bio.h> | ||
| 63 | #include <openssl/evp.h> | ||
| 64 | |||
| 65 | static int linebuffer_write(BIO *h, const char *buf,int num); | ||
| 66 | static int linebuffer_read(BIO *h, char *buf, int size); | ||
| 67 | static int linebuffer_puts(BIO *h, const char *str); | ||
| 68 | static int linebuffer_gets(BIO *h, char *str, int size); | ||
| 69 | static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2); | ||
| 70 | static int linebuffer_new(BIO *h); | ||
| 71 | static int linebuffer_free(BIO *data); | ||
| 72 | static long linebuffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp); | ||
| 73 | |||
| 74 | /* A 10k maximum should be enough for most purposes */ | ||
| 75 | #define DEFAULT_LINEBUFFER_SIZE 1024*10 | ||
| 76 | |||
| 77 | /* #define DEBUG */ | ||
| 78 | |||
| 79 | static BIO_METHOD methods_linebuffer= | ||
| 80 | { | ||
| 81 | BIO_TYPE_LINEBUFFER, | ||
| 82 | "linebuffer", | ||
| 83 | linebuffer_write, | ||
| 84 | linebuffer_read, | ||
| 85 | linebuffer_puts, | ||
| 86 | linebuffer_gets, | ||
| 87 | linebuffer_ctrl, | ||
| 88 | linebuffer_new, | ||
| 89 | linebuffer_free, | ||
| 90 | linebuffer_callback_ctrl, | ||
| 91 | }; | ||
| 92 | |||
| 93 | BIO_METHOD *BIO_f_linebuffer(void) | ||
| 94 | { | ||
| 95 | return(&methods_linebuffer); | ||
| 96 | } | ||
| 97 | |||
| 98 | typedef struct bio_linebuffer_ctx_struct | ||
| 99 | { | ||
| 100 | char *obuf; /* the output char array */ | ||
| 101 | int obuf_size; /* how big is the output buffer */ | ||
| 102 | int obuf_len; /* how many bytes are in it */ | ||
| 103 | } BIO_LINEBUFFER_CTX; | ||
| 104 | |||
| 105 | static int linebuffer_new(BIO *bi) | ||
| 106 | { | ||
| 107 | BIO_LINEBUFFER_CTX *ctx; | ||
| 108 | |||
| 109 | ctx=(BIO_LINEBUFFER_CTX *)OPENSSL_malloc(sizeof(BIO_LINEBUFFER_CTX)); | ||
| 110 | if (ctx == NULL) return(0); | ||
| 111 | ctx->obuf=(char *)OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE); | ||
| 112 | if (ctx->obuf == NULL) { OPENSSL_free(ctx); return(0); } | ||
| 113 | ctx->obuf_size=DEFAULT_LINEBUFFER_SIZE; | ||
| 114 | ctx->obuf_len=0; | ||
| 115 | |||
| 116 | bi->init=1; | ||
| 117 | bi->ptr=(char *)ctx; | ||
| 118 | bi->flags=0; | ||
| 119 | return(1); | ||
| 120 | } | ||
| 121 | |||
| 122 | static int linebuffer_free(BIO *a) | ||
| 123 | { | ||
| 124 | BIO_LINEBUFFER_CTX *b; | ||
| 125 | |||
| 126 | if (a == NULL) return(0); | ||
| 127 | b=(BIO_LINEBUFFER_CTX *)a->ptr; | ||
| 128 | if (b->obuf != NULL) OPENSSL_free(b->obuf); | ||
| 129 | OPENSSL_free(a->ptr); | ||
| 130 | a->ptr=NULL; | ||
| 131 | a->init=0; | ||
| 132 | a->flags=0; | ||
| 133 | return(1); | ||
| 134 | } | ||
| 135 | |||
| 136 | static int linebuffer_read(BIO *b, char *out, int outl) | ||
| 137 | { | ||
| 138 | int ret=0; | ||
| 139 | |||
| 140 | if (out == NULL) return(0); | ||
| 141 | if (b->next_bio == NULL) return(0); | ||
| 142 | ret=BIO_read(b->next_bio,out,outl); | ||
| 143 | BIO_clear_retry_flags(b); | ||
| 144 | BIO_copy_next_retry(b); | ||
| 145 | return(ret); | ||
| 146 | } | ||
| 147 | |||
| 148 | static int linebuffer_write(BIO *b, const char *in, int inl) | ||
| 149 | { | ||
| 150 | int i,num=0,foundnl; | ||
| 151 | BIO_LINEBUFFER_CTX *ctx; | ||
| 152 | |||
| 153 | if ((in == NULL) || (inl <= 0)) return(0); | ||
| 154 | ctx=(BIO_LINEBUFFER_CTX *)b->ptr; | ||
| 155 | if ((ctx == NULL) || (b->next_bio == NULL)) return(0); | ||
| 156 | |||
| 157 | BIO_clear_retry_flags(b); | ||
| 158 | |||
| 159 | do | ||
| 160 | { | ||
| 161 | const char *p; | ||
| 162 | |||
| 163 | for(p = in; p < in + inl && *p != '\n'; p++) | ||
| 164 | ; | ||
| 165 | if (*p == '\n') | ||
| 166 | { | ||
| 167 | p++; | ||
| 168 | foundnl = 1; | ||
| 169 | } | ||
| 170 | else | ||
| 171 | foundnl = 0; | ||
| 172 | |||
| 173 | /* If a NL was found and we already have text in the save | ||
| 174 | buffer, concatenate them and write */ | ||
| 175 | while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len) | ||
| 176 | && ctx->obuf_len > 0) | ||
| 177 | { | ||
| 178 | int orig_olen = ctx->obuf_len; | ||
| 179 | |||
| 180 | i = ctx->obuf_size - ctx->obuf_len; | ||
| 181 | if (p - in > 0) | ||
| 182 | { | ||
| 183 | if (i >= p - in) | ||
| 184 | { | ||
| 185 | memcpy(&(ctx->obuf[ctx->obuf_len]), | ||
| 186 | in,p - in); | ||
| 187 | ctx->obuf_len += p - in; | ||
| 188 | inl -= p - in; | ||
| 189 | num += p - in; | ||
| 190 | in = p; | ||
| 191 | } | ||
| 192 | else | ||
| 193 | { | ||
| 194 | memcpy(&(ctx->obuf[ctx->obuf_len]), | ||
| 195 | in,i); | ||
| 196 | ctx->obuf_len += i; | ||
| 197 | inl -= i; | ||
| 198 | in += i; | ||
| 199 | num += i; | ||
| 200 | } | ||
| 201 | } | ||
| 202 | |||
| 203 | #ifdef DEBUG | ||
| 204 | BIO_write(b->next_bio, "<*<", 3); | ||
| 205 | #endif | ||
| 206 | i=BIO_write(b->next_bio, | ||
| 207 | ctx->obuf, ctx->obuf_len); | ||
| 208 | if (i <= 0) | ||
| 209 | { | ||
| 210 | ctx->obuf_len = orig_olen; | ||
| 211 | BIO_copy_next_retry(b); | ||
| 212 | |||
| 213 | #ifdef DEBUG | ||
| 214 | BIO_write(b->next_bio, ">*>", 3); | ||
| 215 | #endif | ||
| 216 | if (i < 0) return((num > 0)?num:i); | ||
| 217 | if (i == 0) return(num); | ||
| 218 | } | ||
| 219 | #ifdef DEBUG | ||
| 220 | BIO_write(b->next_bio, ">*>", 3); | ||
| 221 | #endif | ||
| 222 | if (i < ctx->obuf_len) | ||
| 223 | memmove(ctx->obuf, ctx->obuf + i, | ||
| 224 | ctx->obuf_len - i); | ||
| 225 | ctx->obuf_len-=i; | ||
| 226 | } | ||
| 227 | |||
| 228 | /* Now that the save buffer is emptied, let's write the input | ||
| 229 | buffer if a NL was found and there is anything to write. */ | ||
| 230 | if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) | ||
| 231 | { | ||
| 232 | #ifdef DEBUG | ||
| 233 | BIO_write(b->next_bio, "<*<", 3); | ||
| 234 | #endif | ||
| 235 | i=BIO_write(b->next_bio,in,p - in); | ||
| 236 | if (i <= 0) | ||
| 237 | { | ||
| 238 | BIO_copy_next_retry(b); | ||
| 239 | #ifdef DEBUG | ||
| 240 | BIO_write(b->next_bio, ">*>", 3); | ||
| 241 | #endif | ||
| 242 | if (i < 0) return((num > 0)?num:i); | ||
| 243 | if (i == 0) return(num); | ||
| 244 | } | ||
| 245 | #ifdef DEBUG | ||
| 246 | BIO_write(b->next_bio, ">*>", 3); | ||
| 247 | #endif | ||
| 248 | num+=i; | ||
| 249 | in+=i; | ||
| 250 | inl-=i; | ||
| 251 | } | ||
| 252 | } | ||
| 253 | while(foundnl && inl > 0); | ||
| 254 | /* We've written as much as we can. The rest of the input buffer, if | ||
| 255 | any, is text that doesn't and with a NL and therefore needs to be | ||
| 256 | saved for the next trip. */ | ||
| 257 | if (inl > 0) | ||
| 258 | { | ||
| 259 | memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl); | ||
| 260 | ctx->obuf_len += inl; | ||
| 261 | num += inl; | ||
| 262 | } | ||
| 263 | return num; | ||
| 264 | } | ||
| 265 | |||
| 266 | static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr) | ||
| 267 | { | ||
| 268 | BIO *dbio; | ||
| 269 | BIO_LINEBUFFER_CTX *ctx; | ||
| 270 | long ret=1; | ||
| 271 | char *p; | ||
| 272 | int r; | ||
| 273 | int obs; | ||
| 274 | |||
| 275 | ctx=(BIO_LINEBUFFER_CTX *)b->ptr; | ||
| 276 | |||
| 277 | switch (cmd) | ||
| 278 | { | ||
| 279 | case BIO_CTRL_RESET: | ||
| 280 | ctx->obuf_len=0; | ||
| 281 | if (b->next_bio == NULL) return(0); | ||
| 282 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 283 | break; | ||
| 284 | case BIO_CTRL_INFO: | ||
| 285 | ret=(long)ctx->obuf_len; | ||
| 286 | break; | ||
| 287 | case BIO_CTRL_WPENDING: | ||
| 288 | ret=(long)ctx->obuf_len; | ||
| 289 | if (ret == 0) | ||
| 290 | { | ||
| 291 | if (b->next_bio == NULL) return(0); | ||
| 292 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 293 | } | ||
| 294 | break; | ||
| 295 | case BIO_C_SET_BUFF_SIZE: | ||
| 296 | obs=(int)num; | ||
| 297 | p=ctx->obuf; | ||
| 298 | if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) | ||
| 299 | { | ||
| 300 | p=(char *)OPENSSL_malloc((int)num); | ||
| 301 | if (p == NULL) | ||
| 302 | goto malloc_error; | ||
| 303 | } | ||
| 304 | if (ctx->obuf != p) | ||
| 305 | { | ||
| 306 | if (ctx->obuf_len > obs) | ||
| 307 | { | ||
| 308 | ctx->obuf_len = obs; | ||
| 309 | } | ||
| 310 | memcpy(p, ctx->obuf, ctx->obuf_len); | ||
| 311 | OPENSSL_free(ctx->obuf); | ||
| 312 | ctx->obuf=p; | ||
| 313 | ctx->obuf_size=obs; | ||
| 314 | } | ||
| 315 | break; | ||
| 316 | case BIO_C_DO_STATE_MACHINE: | ||
| 317 | if (b->next_bio == NULL) return(0); | ||
| 318 | BIO_clear_retry_flags(b); | ||
| 319 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 320 | BIO_copy_next_retry(b); | ||
| 321 | break; | ||
| 322 | |||
| 323 | case BIO_CTRL_FLUSH: | ||
| 324 | if (b->next_bio == NULL) return(0); | ||
| 325 | if (ctx->obuf_len <= 0) | ||
| 326 | { | ||
| 327 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 328 | break; | ||
| 329 | } | ||
| 330 | |||
| 331 | for (;;) | ||
| 332 | { | ||
| 333 | BIO_clear_retry_flags(b); | ||
| 334 | if (ctx->obuf_len > 0) | ||
| 335 | { | ||
| 336 | r=BIO_write(b->next_bio, | ||
| 337 | ctx->obuf, ctx->obuf_len); | ||
| 338 | #if 0 | ||
| 339 | fprintf(stderr,"FLUSH %3d -> %3d\n",ctx->obuf_len,r); | ||
| 340 | #endif | ||
| 341 | BIO_copy_next_retry(b); | ||
| 342 | if (r <= 0) return((long)r); | ||
| 343 | if (r < ctx->obuf_len) | ||
| 344 | memmove(ctx->obuf, ctx->obuf + r, | ||
| 345 | ctx->obuf_len - r); | ||
| 346 | ctx->obuf_len-=r; | ||
| 347 | } | ||
| 348 | else | ||
| 349 | { | ||
| 350 | ctx->obuf_len=0; | ||
| 351 | ret=1; | ||
| 352 | break; | ||
| 353 | } | ||
| 354 | } | ||
| 355 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 356 | break; | ||
| 357 | case BIO_CTRL_DUP: | ||
| 358 | dbio=(BIO *)ptr; | ||
| 359 | if ( !BIO_set_write_buffer_size(dbio,ctx->obuf_size)) | ||
| 360 | ret=0; | ||
| 361 | break; | ||
| 362 | default: | ||
| 363 | if (b->next_bio == NULL) return(0); | ||
| 364 | ret=BIO_ctrl(b->next_bio,cmd,num,ptr); | ||
| 365 | break; | ||
| 366 | } | ||
| 367 | return(ret); | ||
| 368 | malloc_error: | ||
| 369 | BIOerr(BIO_F_LINEBUFFER_CTRL,ERR_R_MALLOC_FAILURE); | ||
| 370 | return(0); | ||
| 371 | } | ||
| 372 | |||
| 373 | static long linebuffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) | ||
| 374 | { | ||
| 375 | long ret=1; | ||
| 376 | |||
| 377 | if (b->next_bio == NULL) return(0); | ||
| 378 | switch (cmd) | ||
| 379 | { | ||
| 380 | default: | ||
| 381 | ret=BIO_callback_ctrl(b->next_bio,cmd,fp); | ||
| 382 | break; | ||
| 383 | } | ||
| 384 | return(ret); | ||
| 385 | } | ||
| 386 | |||
| 387 | static int linebuffer_gets(BIO *b, char *buf, int size) | ||
| 388 | { | ||
| 389 | if (b->next_bio == NULL) return(0); | ||
| 390 | return(BIO_gets(b->next_bio,buf,size)); | ||
| 391 | } | ||
| 392 | |||
| 393 | static int linebuffer_puts(BIO *b, const char *str) | ||
| 394 | { | ||
| 395 | return(linebuffer_write(b,str,strlen(str))); | ||
| 396 | } | ||
| 397 | |||
diff --git a/src/lib/libcrypto/bio/bss_bio.c b/src/lib/libcrypto/bio/bss_bio.c new file mode 100644 index 0000000000..562e9d8de2 --- /dev/null +++ b/src/lib/libcrypto/bio/bss_bio.c | |||
| @@ -0,0 +1,588 @@ | |||
| 1 | /* crypto/bio/bss_bio.c -*- Mode: C; c-file-style: "eay" -*- */ | ||
| 2 | |||
| 3 | /* Special method for a BIO where the other endpoint is also a BIO | ||
| 4 | * of this kind, handled by the same thread (i.e. the "peer" is actually | ||
| 5 | * ourselves, wearing a different hat). | ||
| 6 | * Such "BIO pairs" are mainly for using the SSL library with I/O interfaces | ||
| 7 | * for which no specific BIO method is available. | ||
| 8 | * See ssl/ssltest.c for some hints on how this can be used. */ | ||
| 9 | |||
| 10 | #ifndef BIO_PAIR_DEBUG | ||
| 11 | # undef NDEBUG /* avoid conflicting definitions */ | ||
| 12 | # define NDEBUG | ||
| 13 | #endif | ||
| 14 | |||
| 15 | #include <assert.h> | ||
| 16 | #include <stdlib.h> | ||
| 17 | #include <string.h> | ||
| 18 | |||
| 19 | #include <openssl/bio.h> | ||
| 20 | #include <openssl/err.h> | ||
| 21 | #include <openssl/crypto.h> | ||
| 22 | |||
| 23 | static int bio_new(BIO *bio); | ||
| 24 | static int bio_free(BIO *bio); | ||
| 25 | static int bio_read(BIO *bio, char *buf, int size); | ||
| 26 | static int bio_write(BIO *bio, char *buf, int num); | ||
| 27 | static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr); | ||
| 28 | static int bio_puts(BIO *bio, char *str); | ||
| 29 | |||
| 30 | static int bio_make_pair(BIO *bio1, BIO *bio2); | ||
| 31 | static void bio_destroy_pair(BIO *bio); | ||
| 32 | |||
| 33 | static BIO_METHOD methods_biop = | ||
| 34 | { | ||
| 35 | BIO_TYPE_BIO, | ||
| 36 | "BIO pair", | ||
| 37 | bio_write, | ||
| 38 | bio_read, | ||
| 39 | bio_puts, | ||
| 40 | NULL /* no bio_gets */, | ||
| 41 | bio_ctrl, | ||
| 42 | bio_new, | ||
| 43 | bio_free | ||
| 44 | }; | ||
| 45 | |||
| 46 | BIO_METHOD *BIO_s_bio(void) | ||
| 47 | { | ||
| 48 | return &methods_biop; | ||
| 49 | } | ||
| 50 | |||
| 51 | struct bio_bio_st | ||
| 52 | { | ||
| 53 | BIO *peer; /* NULL if buf == NULL. | ||
| 54 | * If peer != NULL, then peer->ptr is also a bio_bio_st, | ||
| 55 | * and its "peer" member points back to us. | ||
| 56 | * peer != NULL iff init != 0 in the BIO. */ | ||
| 57 | |||
| 58 | /* This is for what we write (i.e. reading uses peer's struct): */ | ||
| 59 | int closed; /* valid iff peer != NULL */ | ||
| 60 | size_t len; /* valid iff buf != NULL; 0 if peer == NULL */ | ||
| 61 | size_t offset; /* valid iff buf != NULL; 0 if len == 0 */ | ||
| 62 | size_t size; | ||
| 63 | char *buf; /* "size" elements (if != NULL) */ | ||
| 64 | |||
| 65 | size_t request; /* valid iff peer != NULL; 0 if len != 0, | ||
| 66 | * otherwise set by peer to number of bytes | ||
| 67 | * it (unsuccesfully) tried to read, | ||
| 68 | * never more than buffer space (size-len) warrants. */ | ||
| 69 | }; | ||
| 70 | |||
| 71 | static int bio_new(BIO *bio) | ||
| 72 | { | ||
| 73 | struct bio_bio_st *b; | ||
| 74 | |||
| 75 | b = Malloc(sizeof *b); | ||
| 76 | if (b == NULL) | ||
| 77 | return 0; | ||
| 78 | |||
| 79 | b->peer = NULL; | ||
| 80 | b->size = 17*1024; /* enough for one TLS record (just a default) */ | ||
| 81 | b->buf = NULL; | ||
| 82 | |||
| 83 | bio->ptr = b; | ||
| 84 | return 1; | ||
| 85 | } | ||
| 86 | |||
| 87 | |||
| 88 | static int bio_free(BIO *bio) | ||
| 89 | { | ||
| 90 | struct bio_bio_st *b; | ||
| 91 | |||
| 92 | if (bio == NULL) | ||
| 93 | return 0; | ||
| 94 | b = bio->ptr; | ||
| 95 | |||
| 96 | assert(b != NULL); | ||
| 97 | |||
| 98 | if (b->peer) | ||
| 99 | bio_destroy_pair(bio); | ||
| 100 | |||
| 101 | if (b->buf != NULL) | ||
| 102 | { | ||
| 103 | Free(b->buf); | ||
| 104 | } | ||
| 105 | |||
| 106 | Free(b); | ||
| 107 | |||
| 108 | return 1; | ||
| 109 | } | ||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | static int bio_read(BIO *bio, char *buf, int size_) | ||
| 114 | { | ||
| 115 | size_t size = size_; | ||
| 116 | size_t rest; | ||
| 117 | struct bio_bio_st *b, *peer_b; | ||
| 118 | |||
| 119 | BIO_clear_retry_flags(bio); | ||
| 120 | |||
| 121 | if (!bio->init) | ||
| 122 | return 0; | ||
| 123 | |||
| 124 | b = bio->ptr; | ||
| 125 | assert(b != NULL); | ||
| 126 | assert(b->peer != NULL); | ||
| 127 | peer_b = b->peer->ptr; | ||
| 128 | assert(peer_b != NULL); | ||
| 129 | assert(peer_b->buf != NULL); | ||
| 130 | |||
| 131 | peer_b->request = 0; /* will be set in "retry_read" situation */ | ||
| 132 | |||
| 133 | if (buf == NULL || size == 0) | ||
| 134 | return 0; | ||
| 135 | |||
| 136 | if (peer_b->len == 0) | ||
| 137 | { | ||
| 138 | if (peer_b->closed) | ||
| 139 | return 0; /* writer has closed, and no data is left */ | ||
| 140 | else | ||
| 141 | { | ||
| 142 | BIO_set_retry_read(bio); /* buffer is empty */ | ||
| 143 | if (size <= peer_b->size) | ||
| 144 | peer_b->request = size; | ||
| 145 | else | ||
| 146 | /* don't ask for more than the peer can | ||
| 147 | * deliver in one write */ | ||
| 148 | peer_b->request = peer_b->size; | ||
| 149 | return -1; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | /* we can read */ | ||
| 154 | if (peer_b->len < size) | ||
| 155 | size = peer_b->len; | ||
| 156 | |||
| 157 | /* now read "size" bytes */ | ||
| 158 | |||
| 159 | rest = size; | ||
| 160 | |||
| 161 | assert(rest > 0); | ||
| 162 | do /* one or two iterations */ | ||
| 163 | { | ||
| 164 | size_t chunk; | ||
| 165 | |||
| 166 | assert(rest <= peer_b->len); | ||
| 167 | if (peer_b->offset + rest <= peer_b->size) | ||
| 168 | chunk = rest; | ||
| 169 | else | ||
| 170 | /* wrap around ring buffer */ | ||
| 171 | chunk = peer_b->size - peer_b->offset; | ||
| 172 | assert(peer_b->offset + chunk <= peer_b->size); | ||
| 173 | |||
| 174 | memcpy(buf, peer_b->buf + peer_b->offset, chunk); | ||
| 175 | |||
| 176 | peer_b->len -= chunk; | ||
| 177 | if (peer_b->len) | ||
| 178 | { | ||
| 179 | peer_b->offset += chunk; | ||
| 180 | assert(peer_b->offset <= peer_b->size); | ||
| 181 | if (peer_b->offset == peer_b->size) | ||
| 182 | peer_b->offset = 0; | ||
| 183 | buf += chunk; | ||
| 184 | } | ||
| 185 | else | ||
| 186 | { | ||
| 187 | /* buffer now empty, no need to advance "buf" */ | ||
| 188 | assert(chunk == rest); | ||
| 189 | peer_b->offset = 0; | ||
| 190 | } | ||
| 191 | rest -= chunk; | ||
| 192 | } | ||
| 193 | while (rest); | ||
| 194 | |||
| 195 | return size; | ||
| 196 | } | ||
| 197 | |||
| 198 | static int bio_write(BIO *bio, char *buf, int num_) | ||
| 199 | { | ||
| 200 | size_t num = num_; | ||
| 201 | size_t rest; | ||
| 202 | struct bio_bio_st *b; | ||
| 203 | |||
| 204 | BIO_clear_retry_flags(bio); | ||
| 205 | |||
| 206 | if (!bio->init || buf == NULL || num == 0) | ||
| 207 | return 0; | ||
| 208 | |||
| 209 | b = bio->ptr; | ||
| 210 | assert(b != NULL); | ||
| 211 | assert(b->peer != NULL); | ||
| 212 | assert(b->buf != NULL); | ||
| 213 | |||
| 214 | b->request = 0; | ||
| 215 | if (b->closed) | ||
| 216 | { | ||
| 217 | /* we already closed */ | ||
| 218 | BIOerr(BIO_F_BIO_WRITE, BIO_R_BROKEN_PIPE); | ||
| 219 | return -1; | ||
| 220 | } | ||
| 221 | |||
| 222 | assert(b->len <= b->size); | ||
| 223 | |||
| 224 | if (b->len == b->size) | ||
| 225 | { | ||
| 226 | BIO_set_retry_write(bio); /* buffer is full */ | ||
| 227 | return -1; | ||
| 228 | } | ||
| 229 | |||
| 230 | /* we can write */ | ||
| 231 | if (num > b->size - b->len) | ||
| 232 | num = b->size - b->len; | ||
| 233 | |||
| 234 | /* now write "num" bytes */ | ||
| 235 | |||
| 236 | rest = num; | ||
| 237 | |||
| 238 | assert(rest > 0); | ||
| 239 | do /* one or two iterations */ | ||
| 240 | { | ||
| 241 | size_t write_offset; | ||
| 242 | size_t chunk; | ||
| 243 | |||
| 244 | assert(b->len + rest <= b->size); | ||
| 245 | |||
| 246 | write_offset = b->offset + b->len; | ||
| 247 | if (write_offset >= b->size) | ||
| 248 | write_offset -= b->size; | ||
| 249 | /* b->buf[write_offset] is the first byte we can write to. */ | ||
| 250 | |||
| 251 | if (write_offset + rest <= b->size) | ||
| 252 | chunk = rest; | ||
| 253 | else | ||
| 254 | /* wrap around ring buffer */ | ||
| 255 | chunk = b->size - write_offset; | ||
| 256 | |||
| 257 | memcpy(b->buf + write_offset, buf, chunk); | ||
| 258 | |||
| 259 | b->len += chunk; | ||
| 260 | |||
| 261 | assert(b->len <= b->size); | ||
| 262 | |||
| 263 | rest -= chunk; | ||
| 264 | buf += chunk; | ||
| 265 | } | ||
| 266 | while (rest); | ||
| 267 | |||
| 268 | return num; | ||
| 269 | } | ||
| 270 | |||
| 271 | |||
| 272 | static long bio_ctrl(BIO *bio, int cmd, long num, void *ptr) | ||
| 273 | { | ||
| 274 | long ret; | ||
| 275 | struct bio_bio_st *b = bio->ptr; | ||
| 276 | |||
| 277 | assert(b != NULL); | ||
| 278 | |||
| 279 | switch (cmd) | ||
| 280 | { | ||
| 281 | /* specific CTRL codes */ | ||
| 282 | |||
| 283 | case BIO_C_SET_WRITE_BUF_SIZE: | ||
| 284 | if (b->peer) | ||
| 285 | { | ||
| 286 | BIOerr(BIO_F_BIO_CTRL, BIO_R_IN_USE); | ||
| 287 | ret = 0; | ||
| 288 | } | ||
| 289 | else if (num == 0) | ||
| 290 | { | ||
| 291 | BIOerr(BIO_F_BIO_CTRL, BIO_R_INVALID_ARGUMENT); | ||
| 292 | ret = 0; | ||
| 293 | } | ||
| 294 | else | ||
| 295 | { | ||
| 296 | size_t new_size = num; | ||
| 297 | |||
| 298 | if (b->size != new_size) | ||
| 299 | { | ||
| 300 | if (b->buf) | ||
| 301 | { | ||
| 302 | Free(b->buf); | ||
| 303 | b->buf = NULL; | ||
| 304 | } | ||
| 305 | b->size = new_size; | ||
| 306 | } | ||
| 307 | ret = 1; | ||
| 308 | } | ||
| 309 | break; | ||
| 310 | |||
| 311 | case BIO_C_GET_WRITE_BUF_SIZE: | ||
| 312 | num = (long) b->size; | ||
| 313 | |||
| 314 | case BIO_C_MAKE_BIO_PAIR: | ||
| 315 | { | ||
| 316 | BIO *other_bio = ptr; | ||
| 317 | |||
| 318 | if (bio_make_pair(bio, other_bio)) | ||
| 319 | ret = 1; | ||
| 320 | else | ||
| 321 | ret = 0; | ||
| 322 | } | ||
| 323 | break; | ||
| 324 | |||
| 325 | case BIO_C_DESTROY_BIO_PAIR: | ||
| 326 | /* Effects both BIOs in the pair -- call just once! | ||
| 327 | * Or let BIO_free(bio1); BIO_free(bio2); do the job. */ | ||
| 328 | bio_destroy_pair(bio); | ||
| 329 | ret = 1; | ||
| 330 | break; | ||
| 331 | |||
| 332 | case BIO_C_GET_WRITE_GUARANTEE: | ||
| 333 | /* How many bytes can the caller feed to the next write | ||
| 334 | * withouth having to keep any? */ | ||
| 335 | if (b->peer == NULL || b->closed) | ||
| 336 | ret = 0; | ||
| 337 | else | ||
| 338 | ret = (long) b->size - b->len; | ||
| 339 | break; | ||
| 340 | |||
| 341 | case BIO_C_GET_READ_REQUEST: | ||
| 342 | /* If the peer unsuccesfully tried to read, how many bytes | ||
| 343 | * were requested? (As with BIO_CTRL_PENDING, that number | ||
| 344 | * can usually be treated as boolean.) */ | ||
| 345 | ret = (long) b->request; | ||
| 346 | break; | ||
| 347 | |||
| 348 | case BIO_C_SHUTDOWN_WR: | ||
| 349 | /* similar to shutdown(..., SHUT_WR) */ | ||
| 350 | b->closed = 1; | ||
| 351 | ret = 1; | ||
| 352 | break; | ||
| 353 | |||
| 354 | |||
| 355 | /* standard CTRL codes follow */ | ||
| 356 | |||
| 357 | case BIO_CTRL_RESET: | ||
| 358 | if (b->buf != NULL) | ||
| 359 | { | ||
| 360 | b->len = 0; | ||
| 361 | b->offset = 0; | ||
| 362 | } | ||
| 363 | ret = 0; | ||
| 364 | break; | ||
| 365 | |||
| 366 | case BIO_CTRL_GET_CLOSE: | ||
| 367 | ret = bio->shutdown; | ||
| 368 | break; | ||
| 369 | |||
| 370 | case BIO_CTRL_SET_CLOSE: | ||
| 371 | bio->shutdown = (int) num; | ||
| 372 | ret = 1; | ||
| 373 | break; | ||
| 374 | |||
| 375 | case BIO_CTRL_PENDING: | ||
| 376 | if (b->peer != NULL) | ||
| 377 | { | ||
| 378 | struct bio_bio_st *peer_b = b->peer->ptr; | ||
| 379 | |||
| 380 | ret = (long) peer_b->len; | ||
| 381 | } | ||
| 382 | else | ||
| 383 | ret = 0; | ||
| 384 | break; | ||
| 385 | |||
| 386 | case BIO_CTRL_WPENDING: | ||
| 387 | if (b->buf != NULL) | ||
| 388 | ret = (long) b->len; | ||
| 389 | else | ||
| 390 | ret = 0; | ||
| 391 | break; | ||
| 392 | |||
| 393 | case BIO_CTRL_DUP: | ||
| 394 | /* See BIO_dup_chain for circumstances we have to expect. */ | ||
| 395 | { | ||
| 396 | BIO *other_bio = ptr; | ||
| 397 | struct bio_bio_st *other_b; | ||
| 398 | |||
| 399 | assert(other_bio != NULL); | ||
| 400 | other_b = other_bio->ptr; | ||
| 401 | assert(other_b != NULL); | ||
| 402 | |||
| 403 | assert(other_b->buf == NULL); /* other_bio is always fresh */ | ||
| 404 | |||
| 405 | other_b->size = b->size; | ||
| 406 | } | ||
| 407 | |||
| 408 | ret = 1; | ||
| 409 | break; | ||
| 410 | |||
| 411 | case BIO_CTRL_FLUSH: | ||
| 412 | ret = 1; | ||
| 413 | break; | ||
| 414 | |||
| 415 | case BIO_CTRL_EOF: | ||
| 416 | { | ||
| 417 | BIO *other_bio = ptr; | ||
| 418 | |||
| 419 | if (other_bio) | ||
| 420 | { | ||
| 421 | struct bio_bio_st *other_b = other_bio->ptr; | ||
| 422 | |||
| 423 | assert(other_b != NULL); | ||
| 424 | ret = other_b->len == 0 && other_b->closed; | ||
| 425 | } | ||
| 426 | else | ||
| 427 | ret = 1; | ||
| 428 | } | ||
| 429 | break; | ||
| 430 | |||
| 431 | default: | ||
| 432 | ret = 0; | ||
| 433 | } | ||
| 434 | return ret; | ||
| 435 | } | ||
| 436 | |||
| 437 | static int bio_puts(BIO *bio, char *str) | ||
| 438 | { | ||
| 439 | return bio_write(bio, str, strlen(str)); | ||
| 440 | } | ||
| 441 | |||
| 442 | |||
| 443 | static int bio_make_pair(BIO *bio1, BIO *bio2) | ||
| 444 | { | ||
| 445 | struct bio_bio_st *b1, *b2; | ||
| 446 | |||
| 447 | assert(bio1 != NULL); | ||
| 448 | assert(bio2 != NULL); | ||
| 449 | |||
| 450 | b1 = bio1->ptr; | ||
| 451 | b2 = bio2->ptr; | ||
| 452 | |||
| 453 | if (b1->peer != NULL || b2->peer != NULL) | ||
| 454 | { | ||
| 455 | BIOerr(BIO_F_BIO_MAKE_PAIR, BIO_R_IN_USE); | ||
| 456 | return 0; | ||
| 457 | } | ||
| 458 | |||
| 459 | if (b1->buf == NULL) | ||
| 460 | { | ||
| 461 | b1->buf = Malloc(b1->size); | ||
| 462 | if (b1->buf == NULL) | ||
| 463 | { | ||
| 464 | BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE); | ||
| 465 | return 0; | ||
| 466 | } | ||
| 467 | b1->len = 0; | ||
| 468 | b1->offset = 0; | ||
| 469 | } | ||
| 470 | |||
| 471 | if (b2->buf == NULL) | ||
| 472 | { | ||
| 473 | b2->buf = Malloc(b2->size); | ||
| 474 | if (b2->buf == NULL) | ||
| 475 | { | ||
| 476 | BIOerr(BIO_F_BIO_MAKE_PAIR, ERR_R_MALLOC_FAILURE); | ||
| 477 | return 0; | ||
| 478 | } | ||
| 479 | b2->len = 0; | ||
| 480 | b2->offset = 0; | ||
| 481 | } | ||
| 482 | |||
| 483 | b1->peer = bio2; | ||
| 484 | b1->closed = 0; | ||
| 485 | b1->request = 0; | ||
| 486 | b2->peer = bio1; | ||
| 487 | b2->closed = 0; | ||
| 488 | b2->request = 0; | ||
| 489 | |||
| 490 | bio1->init = 1; | ||
| 491 | bio2->init = 1; | ||
| 492 | |||
| 493 | return 1; | ||
| 494 | } | ||
| 495 | |||
| 496 | static void bio_destroy_pair(BIO *bio) | ||
| 497 | { | ||
| 498 | struct bio_bio_st *b = bio->ptr; | ||
| 499 | |||
| 500 | if (b != NULL) | ||
| 501 | { | ||
| 502 | BIO *peer_bio = b->peer; | ||
| 503 | |||
| 504 | if (peer_bio != NULL) | ||
| 505 | { | ||
| 506 | struct bio_bio_st *peer_b = peer_bio->ptr; | ||
| 507 | |||
| 508 | assert(peer_b != NULL); | ||
| 509 | assert(peer_b->peer == bio); | ||
| 510 | |||
| 511 | peer_b->peer = NULL; | ||
| 512 | peer_bio->init = 0; | ||
| 513 | assert(peer_b->buf != NULL); | ||
| 514 | peer_b->len = 0; | ||
| 515 | peer_b->offset = 0; | ||
| 516 | |||
| 517 | b->peer = NULL; | ||
| 518 | bio->init = 0; | ||
| 519 | assert(b->buf != NULL); | ||
| 520 | b->len = 0; | ||
| 521 | b->offset = 0; | ||
| 522 | } | ||
| 523 | } | ||
| 524 | } | ||
| 525 | |||
| 526 | |||
| 527 | /* Exported convenience functions */ | ||
| 528 | int BIO_new_bio_pair(BIO **bio1_p, size_t writebuf1, | ||
| 529 | BIO **bio2_p, size_t writebuf2) | ||
| 530 | { | ||
| 531 | BIO *bio1 = NULL, *bio2 = NULL; | ||
| 532 | long r; | ||
| 533 | int ret = 0; | ||
| 534 | |||
| 535 | bio1 = BIO_new(BIO_s_bio()); | ||
| 536 | if (bio1 == NULL) | ||
| 537 | goto err; | ||
| 538 | bio2 = BIO_new(BIO_s_bio()); | ||
| 539 | if (bio2 == NULL) | ||
| 540 | goto err; | ||
| 541 | |||
| 542 | if (writebuf1) | ||
| 543 | { | ||
| 544 | r = BIO_set_write_buf_size(bio1, writebuf1); | ||
| 545 | if (!r) | ||
| 546 | goto err; | ||
| 547 | } | ||
| 548 | if (writebuf2) | ||
| 549 | { | ||
| 550 | r = BIO_set_write_buf_size(bio2, writebuf2); | ||
| 551 | if (!r) | ||
| 552 | goto err; | ||
| 553 | } | ||
| 554 | |||
| 555 | r = BIO_make_bio_pair(bio1, bio2); | ||
| 556 | if (!r) | ||
| 557 | goto err; | ||
| 558 | ret = 1; | ||
| 559 | |||
| 560 | err: | ||
| 561 | if (ret == 0) | ||
| 562 | { | ||
| 563 | if (bio1) | ||
| 564 | { | ||
| 565 | BIO_free(bio1); | ||
| 566 | bio1 = NULL; | ||
| 567 | } | ||
| 568 | if (bio2) | ||
| 569 | { | ||
| 570 | BIO_free(bio2); | ||
| 571 | bio2 = NULL; | ||
| 572 | } | ||
| 573 | } | ||
| 574 | |||
| 575 | *bio1_p = bio1; | ||
| 576 | *bio2_p = bio2; | ||
| 577 | return ret; | ||
| 578 | } | ||
| 579 | |||
| 580 | size_t BIO_ctrl_get_write_guarantee(BIO *bio) | ||
| 581 | { | ||
| 582 | return BIO_ctrl(bio, BIO_C_GET_WRITE_GUARANTEE, 0, NULL); | ||
| 583 | } | ||
| 584 | |||
| 585 | size_t BIO_ctrl_get_read_request(BIO *bio) | ||
| 586 | { | ||
| 587 | return BIO_ctrl(bio, BIO_C_GET_READ_REQUEST, 0, NULL); | ||
| 588 | } | ||
diff --git a/src/lib/libcrypto/bio/bss_log.c b/src/lib/libcrypto/bio/bss_log.c new file mode 100644 index 0000000000..db82e757e7 --- /dev/null +++ b/src/lib/libcrypto/bio/bss_log.c | |||
| @@ -0,0 +1,232 @@ | |||
| 1 | /* crypto/bio/bss_log.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1999 The OpenSSL Project. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in | ||
| 14 | * the documentation and/or other materials provided with the | ||
| 15 | * distribution. | ||
| 16 | * | ||
| 17 | * 3. All advertising materials mentioning features or use of this | ||
| 18 | * software must display the following acknowledgment: | ||
| 19 | * "This product includes software developed by the OpenSSL Project | ||
| 20 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" | ||
| 21 | * | ||
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 23 | * endorse or promote products derived from this software without | ||
| 24 | * prior written permission. For written permission, please contact | ||
| 25 | * licensing@OpenSSL.org. | ||
| 26 | * | ||
| 27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 28 | * nor may "OpenSSL" appear in their names without prior written | ||
| 29 | * permission of the OpenSSL Project. | ||
| 30 | * | ||
| 31 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 32 | * acknowledgment: | ||
| 33 | * "This product includes software developed by the OpenSSL Project | ||
| 34 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" | ||
| 35 | * | ||
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 48 | * ==================================================================== | ||
| 49 | * | ||
| 50 | * This product includes cryptographic software written by Eric Young | ||
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 52 | * Hudson (tjh@cryptsoft.com). | ||
| 53 | * | ||
| 54 | */ | ||
| 55 | |||
| 56 | /* | ||
| 57 | Why BIO_s_log? | ||
| 58 | |||
| 59 | BIO_s_log is useful for system daemons (or services under NT). | ||
| 60 | It is one-way BIO, it sends all stuff to syslogd (or event log | ||
| 61 | under NT). | ||
| 62 | |||
| 63 | */ | ||
| 64 | |||
| 65 | |||
| 66 | #include <stdio.h> | ||
| 67 | #include <errno.h> | ||
| 68 | |||
| 69 | #ifndef WIN32 | ||
| 70 | #ifdef __ultrix | ||
| 71 | #include <sys/syslog.h> | ||
| 72 | #else | ||
| 73 | #include <syslog.h> | ||
| 74 | #endif | ||
| 75 | #endif | ||
| 76 | |||
| 77 | #include "cryptlib.h" | ||
| 78 | #include <openssl/buffer.h> | ||
| 79 | #include <openssl/err.h> | ||
| 80 | #ifndef NO_SYSLOG | ||
| 81 | |||
| 82 | |||
| 83 | static int MS_CALLBACK slg_write(BIO *h,char *buf,int num); | ||
| 84 | static int MS_CALLBACK slg_puts(BIO *h,char *str); | ||
| 85 | static long MS_CALLBACK slg_ctrl(BIO *h,int cmd,long arg1,char *arg2); | ||
| 86 | static int MS_CALLBACK slg_new(BIO *h); | ||
| 87 | static int MS_CALLBACK slg_free(BIO *data); | ||
| 88 | static int xopenlog(BIO* bp, const char* name, int level); | ||
| 89 | static int xcloselog(BIO* bp); | ||
| 90 | |||
| 91 | static BIO_METHOD methods_slg= | ||
| 92 | { | ||
| 93 | BIO_TYPE_MEM,"syslog", | ||
| 94 | slg_write, | ||
| 95 | NULL, | ||
| 96 | slg_puts, | ||
| 97 | NULL, | ||
| 98 | slg_ctrl, | ||
| 99 | slg_new, | ||
| 100 | slg_free, | ||
| 101 | }; | ||
| 102 | |||
| 103 | BIO_METHOD *BIO_s_log(void) | ||
| 104 | { | ||
| 105 | return(&methods_slg); | ||
| 106 | } | ||
| 107 | |||
| 108 | static int MS_CALLBACK slg_new(BIO *bi) | ||
| 109 | { | ||
| 110 | bi->init=1; | ||
| 111 | bi->num=0; | ||
| 112 | bi->ptr=NULL; | ||
| 113 | #ifndef WIN32 | ||
| 114 | xopenlog(bi, "application", LOG_DAEMON); | ||
| 115 | #else | ||
| 116 | xopenlog(bi, "application", 0); | ||
| 117 | #endif | ||
| 118 | return(1); | ||
| 119 | } | ||
| 120 | |||
| 121 | static int MS_CALLBACK slg_free(BIO *a) | ||
| 122 | { | ||
| 123 | if (a == NULL) return(0); | ||
| 124 | xcloselog(a); | ||
| 125 | return(1); | ||
| 126 | } | ||
| 127 | |||
| 128 | static int MS_CALLBACK slg_write(BIO *b, char *in, int inl) | ||
| 129 | { | ||
| 130 | int ret= inl; | ||
| 131 | char* buf= in; | ||
| 132 | char* pp; | ||
| 133 | #if defined(WIN32) | ||
| 134 | LPTSTR lpszStrings[1]; | ||
| 135 | WORD evtype= EVENTLOG_ERROR_TYPE; | ||
| 136 | #else | ||
| 137 | int priority; | ||
| 138 | #endif | ||
| 139 | |||
| 140 | if((buf= (char *)Malloc(inl+ 1)) == NULL){ | ||
| 141 | return(0); | ||
| 142 | } | ||
| 143 | strncpy(buf, in, inl); | ||
| 144 | buf[inl]= '\0'; | ||
| 145 | #if defined(WIN32) | ||
| 146 | if(strncmp(buf, "ERR ", 4) == 0){ | ||
| 147 | evtype= EVENTLOG_ERROR_TYPE; | ||
| 148 | pp= buf+ 4; | ||
| 149 | }else if(strncmp(buf, "WAR ", 4) == 0){ | ||
| 150 | evtype= EVENTLOG_WARNING_TYPE; | ||
| 151 | pp= buf+ 4; | ||
| 152 | }else if(strncmp(buf, "INF ", 4) == 0){ | ||
| 153 | evtype= EVENTLOG_INFORMATION_TYPE; | ||
| 154 | pp= buf+ 4; | ||
| 155 | }else{ | ||
| 156 | evtype= EVENTLOG_ERROR_TYPE; | ||
| 157 | pp= buf; | ||
| 158 | } | ||
| 159 | lpszStrings[0]= pp; | ||
| 160 | |||
| 161 | if(b->ptr) | ||
| 162 | ReportEvent(b->ptr, evtype, 0, 1024, NULL, 1, 0, | ||
| 163 | lpszStrings, NULL); | ||
| 164 | #else | ||
| 165 | if(strncmp(buf, "ERR ", 4) == 0){ | ||
| 166 | priority= LOG_ERR; | ||
| 167 | pp= buf+ 4; | ||
| 168 | }else if(strncmp(buf, "WAR ", 4) == 0){ | ||
| 169 | priority= LOG_WARNING; | ||
| 170 | pp= buf+ 4; | ||
| 171 | }else if(strncmp(buf, "INF ", 4) == 0){ | ||
| 172 | priority= LOG_INFO; | ||
| 173 | pp= buf+ 4; | ||
| 174 | }else{ | ||
| 175 | priority= LOG_ERR; | ||
| 176 | pp= buf; | ||
| 177 | } | ||
| 178 | |||
| 179 | syslog(priority, "%s", pp); | ||
| 180 | #endif | ||
| 181 | Free(buf); | ||
| 182 | return(ret); | ||
| 183 | } | ||
| 184 | |||
| 185 | static long MS_CALLBACK slg_ctrl(BIO *b, int cmd, long num, char *ptr) | ||
| 186 | { | ||
| 187 | switch (cmd) | ||
| 188 | { | ||
| 189 | case BIO_CTRL_SET: | ||
| 190 | xcloselog(b); | ||
| 191 | xopenlog(b, ptr, num); | ||
| 192 | break; | ||
| 193 | default: | ||
| 194 | break; | ||
| 195 | } | ||
| 196 | return(0); | ||
| 197 | } | ||
| 198 | |||
| 199 | static int MS_CALLBACK slg_puts(BIO *bp, char *str) | ||
| 200 | { | ||
| 201 | int n,ret; | ||
| 202 | |||
| 203 | n=strlen(str); | ||
| 204 | ret=slg_write(bp,str,n); | ||
| 205 | return(ret); | ||
| 206 | } | ||
| 207 | |||
| 208 | static int xopenlog(BIO* bp, const char* name, int level) | ||
| 209 | { | ||
| 210 | #if defined(WIN32) | ||
| 211 | if((bp->ptr= (char *)RegisterEventSource(NULL, name)) == NULL){ | ||
| 212 | return(0); | ||
| 213 | } | ||
| 214 | #else | ||
| 215 | openlog(name, LOG_PID|LOG_CONS, level); | ||
| 216 | #endif | ||
| 217 | return(1); | ||
| 218 | } | ||
| 219 | |||
| 220 | static int xcloselog(BIO* bp) | ||
| 221 | { | ||
| 222 | #if defined(WIN32) | ||
| 223 | if(bp->ptr) | ||
| 224 | DeregisterEventSource((HANDLE)(bp->ptr)); | ||
| 225 | bp->ptr= NULL; | ||
| 226 | #else | ||
| 227 | closelog(); | ||
| 228 | #endif | ||
| 229 | return(1); | ||
| 230 | } | ||
| 231 | |||
| 232 | #endif | ||
