bf_nbio.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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_lcl.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. /* TODO: Convert to new style write function */
  34. bwrite_conv,
  35. nbiof_write,
  36. /* TODO: Convert to new style read function */
  37. bread_conv,
  38. nbiof_read,
  39. nbiof_puts,
  40. nbiof_gets,
  41. nbiof_ctrl,
  42. nbiof_new,
  43. nbiof_free,
  44. nbiof_callback_ctrl,
  45. };
  46. const BIO_METHOD *BIO_f_nbio_test(void)
  47. {
  48. return &methods_nbiof;
  49. }
  50. static int nbiof_new(BIO *bi)
  51. {
  52. NBIO_TEST *nt;
  53. if ((nt = OPENSSL_zalloc(sizeof(*nt))) == NULL)
  54. return 0;
  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. long ret = 1;
  153. if (b->next_bio == NULL)
  154. return 0;
  155. switch (cmd) {
  156. default:
  157. ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
  158. break;
  159. }
  160. return ret;
  161. }
  162. static int nbiof_gets(BIO *bp, char *buf, int size)
  163. {
  164. if (bp->next_bio == NULL)
  165. return 0;
  166. return BIO_gets(bp->next_bio, buf, size);
  167. }
  168. static int nbiof_puts(BIO *bp, const char *str)
  169. {
  170. if (bp->next_bio == NULL)
  171. return 0;
  172. return BIO_puts(bp->next_bio, str);
  173. }