bf_nbio.c 4.0 KB

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