bf_nbio.c 4.1 KB

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