2016-05-18 02:52:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
|
2015-01-22 11:40:55 +08:00
|
|
|
*
|
2016-05-18 02:52:22 +08:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1998-12-21 18:52:47 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <errno.h>
|
2016-03-22 17:21:29 +08:00
|
|
|
#include "bio_lcl.h"
|
2015-05-14 22:56:48 +08:00
|
|
|
#include "internal/cryptlib.h"
|
1999-04-24 06:13:45 +08:00
|
|
|
#include <openssl/rand.h>
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
/*
|
|
|
|
* BIO_put and BIO_get both add to the digest, BIO_gets returns the digest
|
|
|
|
*/
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
static int nbiof_write(BIO *h, const char *buf, int num);
|
|
|
|
static int nbiof_read(BIO *h, char *buf, int size);
|
|
|
|
static int nbiof_puts(BIO *h, const char *str);
|
|
|
|
static int nbiof_gets(BIO *h, char *str, int size);
|
|
|
|
static long nbiof_ctrl(BIO *h, int cmd, long arg1, void *arg2);
|
1998-12-21 18:52:47 +08:00
|
|
|
static int nbiof_new(BIO *h);
|
|
|
|
static int nbiof_free(BIO *data);
|
2015-01-22 11:40:55 +08:00
|
|
|
static long nbiof_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
|
|
|
|
typedef struct nbio_test_st {
|
|
|
|
/* only set if we sent a 'should retry' error */
|
|
|
|
int lrn;
|
|
|
|
int lwn;
|
|
|
|
} NBIO_TEST;
|
|
|
|
|
2016-03-20 00:32:14 +08:00
|
|
|
static const BIO_METHOD methods_nbiof = {
|
2015-01-22 11:40:55 +08:00
|
|
|
BIO_TYPE_NBIO_TEST,
|
|
|
|
"non-blocking IO test filter",
|
2016-10-20 22:18:39 +08:00
|
|
|
/* TODO: Convert to new style write function */
|
|
|
|
bwrite_conv,
|
2015-01-22 11:40:55 +08:00
|
|
|
nbiof_write,
|
2016-09-06 00:26:58 +08:00
|
|
|
/* TODO: Convert to new style read function */
|
|
|
|
bread_conv,
|
2015-01-22 11:40:55 +08:00
|
|
|
nbiof_read,
|
|
|
|
nbiof_puts,
|
|
|
|
nbiof_gets,
|
|
|
|
nbiof_ctrl,
|
|
|
|
nbiof_new,
|
|
|
|
nbiof_free,
|
|
|
|
nbiof_callback_ctrl,
|
|
|
|
};
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2016-03-20 00:32:14 +08:00
|
|
|
const BIO_METHOD *BIO_f_nbio_test(void)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-10-17 22:04:09 +08:00
|
|
|
return &methods_nbiof;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
static int nbiof_new(BIO *bi)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
NBIO_TEST *nt;
|
|
|
|
|
2015-09-03 21:15:26 +08:00
|
|
|
if ((nt = OPENSSL_zalloc(sizeof(*nt))) == NULL)
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
nt->lrn = -1;
|
|
|
|
nt->lwn = -1;
|
|
|
|
bi->ptr = (char *)nt;
|
|
|
|
bi->init = 1;
|
2017-10-09 19:05:58 +08:00
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
static int nbiof_free(BIO *a)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (a == NULL)
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
2015-05-01 22:02:07 +08:00
|
|
|
OPENSSL_free(a->ptr);
|
2015-01-22 11:40:55 +08:00
|
|
|
a->ptr = NULL;
|
|
|
|
a->init = 0;
|
|
|
|
a->flags = 0;
|
2017-10-09 19:05:58 +08:00
|
|
|
return 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
static int nbiof_read(BIO *b, char *out, int outl)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
int num;
|
|
|
|
unsigned char n;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (out == NULL)
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (b->next_bio == NULL)
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
BIO_clear_retry_flags(b);
|
2015-02-26 19:57:37 +08:00
|
|
|
if (RAND_bytes(&n, 1) <= 0)
|
|
|
|
return -1;
|
2015-01-22 11:40:55 +08:00
|
|
|
num = (n & 0x07);
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (outl > num)
|
|
|
|
outl = num;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (num == 0) {
|
|
|
|
ret = -1;
|
|
|
|
BIO_set_retry_read(b);
|
2015-02-06 23:54:20 +08:00
|
|
|
} else {
|
2015-01-22 11:40:55 +08:00
|
|
|
ret = BIO_read(b->next_bio, out, outl);
|
|
|
|
if (ret < 0)
|
|
|
|
BIO_copy_next_retry(b);
|
|
|
|
}
|
2017-10-17 22:04:09 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2000-05-16 06:54:43 +08:00
|
|
|
static int nbiof_write(BIO *b, const char *in, int inl)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
NBIO_TEST *nt;
|
|
|
|
int ret = 0;
|
|
|
|
int num;
|
|
|
|
unsigned char n;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if ((in == NULL) || (inl <= 0))
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
if (b->next_bio == NULL)
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
nt = (NBIO_TEST *)b->ptr;
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
BIO_clear_retry_flags(b);
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
if (nt->lwn > 0) {
|
|
|
|
num = nt->lwn;
|
|
|
|
nt->lwn = 0;
|
|
|
|
} else {
|
2015-02-26 19:57:37 +08:00
|
|
|
if (RAND_bytes(&n, 1) <= 0)
|
|
|
|
return -1;
|
2015-01-22 11:40:55 +08:00
|
|
|
num = (n & 7);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inl > num)
|
|
|
|
inl = num;
|
|
|
|
|
|
|
|
if (num == 0) {
|
|
|
|
ret = -1;
|
|
|
|
BIO_set_retry_write(b);
|
2015-02-06 23:54:20 +08:00
|
|
|
} else {
|
2015-01-22 11:40:55 +08:00
|
|
|
ret = BIO_write(b->next_bio, in, inl);
|
|
|
|
if (ret < 0) {
|
|
|
|
BIO_copy_next_retry(b);
|
|
|
|
nt->lwn = inl;
|
|
|
|
}
|
|
|
|
}
|
2017-10-17 22:04:09 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2000-05-16 06:54:43 +08:00
|
|
|
static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
long ret;
|
|
|
|
|
|
|
|
if (b->next_bio == NULL)
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
switch (cmd) {
|
|
|
|
case BIO_C_DO_STATE_MACHINE:
|
|
|
|
BIO_clear_retry_flags(b);
|
|
|
|
ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
|
|
|
|
BIO_copy_next_retry(b);
|
|
|
|
break;
|
|
|
|
case BIO_CTRL_DUP:
|
|
|
|
ret = 0L;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
|
|
|
|
break;
|
|
|
|
}
|
2017-10-17 22:04:09 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2000-06-21 10:25:30 +08:00
|
|
|
static long nbiof_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
long ret = 1;
|
|
|
|
|
|
|
|
if (b->next_bio == NULL)
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
switch (cmd) {
|
|
|
|
default:
|
|
|
|
ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
|
|
|
|
break;
|
|
|
|
}
|
2017-10-17 22:04:09 +08:00
|
|
|
return ret;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
2000-02-21 07:43:02 +08:00
|
|
|
|
1999-04-20 05:31:43 +08:00
|
|
|
static int nbiof_gets(BIO *bp, char *buf, int size)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (bp->next_bio == NULL)
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
|
|
|
return BIO_gets(bp->next_bio, buf, size);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
1998-12-21 18:52:47 +08:00
|
|
|
|
2000-05-16 06:54:43 +08:00
|
|
|
static int nbiof_puts(BIO *bp, const char *str)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
if (bp->next_bio == NULL)
|
2017-10-17 22:04:09 +08:00
|
|
|
return 0;
|
|
|
|
return BIO_puts(bp->next_bio, str);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|