2
0

bf_nbio.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright 1995-2021 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. return 0;
  53. nt->lrn = -1;
  54. nt->lwn = -1;
  55. bi->ptr = (char *)nt;
  56. bi->init = 1;
  57. return 1;
  58. }
  59. static int nbiof_free(BIO *a)
  60. {
  61. if (a == NULL)
  62. return 0;
  63. OPENSSL_free(a->ptr);
  64. a->ptr = NULL;
  65. a->init = 0;
  66. a->flags = 0;
  67. return 1;
  68. }
  69. static int nbiof_read(BIO *b, char *out, int outl)
  70. {
  71. int ret = 0;
  72. int num;
  73. unsigned char n;
  74. if (out == NULL)
  75. return 0;
  76. if (b->next_bio == NULL)
  77. return 0;
  78. BIO_clear_retry_flags(b);
  79. if (RAND_priv_bytes(&n, 1) <= 0)
  80. return -1;
  81. num = (n & 0x07);
  82. if (outl > num)
  83. outl = num;
  84. if (num == 0) {
  85. ret = -1;
  86. BIO_set_retry_read(b);
  87. } else {
  88. ret = BIO_read(b->next_bio, out, outl);
  89. if (ret < 0)
  90. BIO_copy_next_retry(b);
  91. }
  92. return ret;
  93. }
  94. static int nbiof_write(BIO *b, const char *in, int inl)
  95. {
  96. NBIO_TEST *nt;
  97. int ret = 0;
  98. int num;
  99. unsigned char n;
  100. if ((in == NULL) || (inl <= 0))
  101. return 0;
  102. if (b->next_bio == NULL)
  103. return 0;
  104. nt = (NBIO_TEST *)b->ptr;
  105. BIO_clear_retry_flags(b);
  106. if (nt->lwn > 0) {
  107. num = nt->lwn;
  108. nt->lwn = 0;
  109. } else {
  110. if (RAND_priv_bytes(&n, 1) <= 0)
  111. return -1;
  112. num = (n & 7);
  113. }
  114. if (inl > num)
  115. inl = num;
  116. if (num == 0) {
  117. ret = -1;
  118. BIO_set_retry_write(b);
  119. } else {
  120. ret = BIO_write(b->next_bio, in, inl);
  121. if (ret < 0) {
  122. BIO_copy_next_retry(b);
  123. nt->lwn = inl;
  124. }
  125. }
  126. return ret;
  127. }
  128. static long nbiof_ctrl(BIO *b, int cmd, long num, void *ptr)
  129. {
  130. long ret;
  131. if (b->next_bio == NULL)
  132. return 0;
  133. switch (cmd) {
  134. case BIO_C_DO_STATE_MACHINE:
  135. BIO_clear_retry_flags(b);
  136. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  137. BIO_copy_next_retry(b);
  138. break;
  139. case BIO_CTRL_DUP:
  140. ret = 0L;
  141. break;
  142. default:
  143. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  144. break;
  145. }
  146. return ret;
  147. }
  148. static long nbiof_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  149. {
  150. if (b->next_bio == NULL)
  151. return 0;
  152. return BIO_callback_ctrl(b->next_bio, cmd, fp);
  153. }
  154. static int nbiof_gets(BIO *bp, char *buf, int size)
  155. {
  156. if (bp->next_bio == NULL)
  157. return 0;
  158. return BIO_gets(bp->next_bio, buf, size);
  159. }
  160. static int nbiof_puts(BIO *bp, const char *str)
  161. {
  162. if (bp->next_bio == NULL)
  163. return 0;
  164. return BIO_puts(bp->next_bio, str);
  165. }