2
0

bio_ndef.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright 2008-2021 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 <openssl/asn1.h>
  10. #include <openssl/asn1t.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/err.h>
  13. #include <stdio.h>
  14. /* Experimental NDEF ASN1 BIO support routines */
  15. /*
  16. * The usage is quite simple, initialize an ASN1 structure, get a BIO from it
  17. * then any data written through the BIO will end up translated to
  18. * appropriate format on the fly. The data is streamed out and does *not*
  19. * need to be all held in memory at once. When the BIO is flushed the output
  20. * is finalized and any signatures etc written out. The BIO is a 'proper'
  21. * BIO and can handle non blocking I/O correctly. The usage is simple. The
  22. * implementation is *not*...
  23. */
  24. /* BIO support data stored in the ASN1 BIO ex_arg */
  25. typedef struct ndef_aux_st {
  26. /* ASN1 structure this BIO refers to */
  27. ASN1_VALUE *val;
  28. const ASN1_ITEM *it;
  29. /* Top of the BIO chain */
  30. BIO *ndef_bio;
  31. /* Output BIO */
  32. BIO *out;
  33. /* Boundary where content is inserted */
  34. unsigned char **boundary;
  35. /* DER buffer start */
  36. unsigned char *derbuf;
  37. } NDEF_SUPPORT;
  38. static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
  39. static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
  40. void *parg);
  41. static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
  42. static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
  43. void *parg);
  44. BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
  45. {
  46. NDEF_SUPPORT *ndef_aux = NULL;
  47. BIO *asn_bio = NULL;
  48. const ASN1_AUX *aux = it->funcs;
  49. ASN1_STREAM_ARG sarg;
  50. if (!aux || !aux->asn1_cb) {
  51. ASN1err(ASN1_F_BIO_NEW_NDEF, ASN1_R_STREAMING_NOT_SUPPORTED);
  52. return NULL;
  53. }
  54. ndef_aux = OPENSSL_zalloc(sizeof(*ndef_aux));
  55. asn_bio = BIO_new(BIO_f_asn1());
  56. if (ndef_aux == NULL || asn_bio == NULL)
  57. goto err;
  58. /* ASN1 bio needs to be next to output BIO */
  59. out = BIO_push(asn_bio, out);
  60. if (out == NULL)
  61. goto err;
  62. BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free);
  63. BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free);
  64. /*
  65. * Now let callback prepends any digest, cipher etc BIOs ASN1 structure
  66. * needs.
  67. */
  68. sarg.out = out;
  69. sarg.ndef_bio = NULL;
  70. sarg.boundary = NULL;
  71. if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0)
  72. goto err;
  73. ndef_aux->val = val;
  74. ndef_aux->it = it;
  75. ndef_aux->ndef_bio = sarg.ndef_bio;
  76. ndef_aux->boundary = sarg.boundary;
  77. ndef_aux->out = out;
  78. BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux);
  79. return sarg.ndef_bio;
  80. err:
  81. BIO_free(asn_bio);
  82. OPENSSL_free(ndef_aux);
  83. return NULL;
  84. }
  85. static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  86. {
  87. NDEF_SUPPORT *ndef_aux;
  88. unsigned char *p;
  89. int derlen;
  90. if (!parg)
  91. return 0;
  92. ndef_aux = *(NDEF_SUPPORT **)parg;
  93. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  94. if (derlen < 0)
  95. return 0;
  96. if ((p = OPENSSL_malloc(derlen)) == NULL) {
  97. ASN1err(ASN1_F_NDEF_PREFIX, ERR_R_MALLOC_FAILURE);
  98. return 0;
  99. }
  100. ndef_aux->derbuf = p;
  101. *pbuf = p;
  102. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  103. if (!*ndef_aux->boundary)
  104. return 0;
  105. *plen = *ndef_aux->boundary - *pbuf;
  106. return 1;
  107. }
  108. static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
  109. void *parg)
  110. {
  111. NDEF_SUPPORT *ndef_aux;
  112. if (!parg)
  113. return 0;
  114. ndef_aux = *(NDEF_SUPPORT **)parg;
  115. if (ndef_aux == NULL)
  116. return 0;
  117. OPENSSL_free(ndef_aux->derbuf);
  118. ndef_aux->derbuf = NULL;
  119. *pbuf = NULL;
  120. *plen = 0;
  121. return 1;
  122. }
  123. static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
  124. void *parg)
  125. {
  126. NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
  127. if (!ndef_prefix_free(b, pbuf, plen, parg))
  128. return 0;
  129. OPENSSL_free(*pndef_aux);
  130. *pndef_aux = NULL;
  131. return 1;
  132. }
  133. static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  134. {
  135. NDEF_SUPPORT *ndef_aux;
  136. unsigned char *p;
  137. int derlen;
  138. const ASN1_AUX *aux;
  139. ASN1_STREAM_ARG sarg;
  140. if (!parg)
  141. return 0;
  142. ndef_aux = *(NDEF_SUPPORT **)parg;
  143. aux = ndef_aux->it->funcs;
  144. /* Finalize structures */
  145. sarg.ndef_bio = ndef_aux->ndef_bio;
  146. sarg.out = ndef_aux->out;
  147. sarg.boundary = ndef_aux->boundary;
  148. if (aux->asn1_cb(ASN1_OP_STREAM_POST,
  149. &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
  150. return 0;
  151. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  152. if ((p = OPENSSL_malloc(derlen)) == NULL) {
  153. ASN1err(ASN1_F_NDEF_SUFFIX, ERR_R_MALLOC_FAILURE);
  154. return 0;
  155. }
  156. ndef_aux->derbuf = p;
  157. *pbuf = p;
  158. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  159. if (!*ndef_aux->boundary)
  160. return 0;
  161. *pbuf = *ndef_aux->boundary;
  162. *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);
  163. return 1;
  164. }