bio_ndef.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright 2008-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 <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. /* unfortunately cannot constify this due to CMS_stream() and PKCS7_stream() */
  45. BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
  46. {
  47. NDEF_SUPPORT *ndef_aux = NULL;
  48. BIO *asn_bio = NULL;
  49. const ASN1_AUX *aux = it->funcs;
  50. ASN1_STREAM_ARG sarg;
  51. if (!aux || !aux->asn1_cb) {
  52. ERR_raise(ERR_LIB_ASN1, ASN1_R_STREAMING_NOT_SUPPORTED);
  53. return NULL;
  54. }
  55. ndef_aux = OPENSSL_zalloc(sizeof(*ndef_aux));
  56. asn_bio = BIO_new(BIO_f_asn1());
  57. if (ndef_aux == NULL || asn_bio == NULL)
  58. goto err;
  59. /* ASN1 bio needs to be next to output BIO */
  60. out = BIO_push(asn_bio, out);
  61. if (out == NULL)
  62. goto err;
  63. BIO_asn1_set_prefix(asn_bio, ndef_prefix, ndef_prefix_free);
  64. BIO_asn1_set_suffix(asn_bio, ndef_suffix, ndef_suffix_free);
  65. /*
  66. * Now let callback prepends any digest, cipher etc BIOs ASN1 structure
  67. * needs.
  68. */
  69. sarg.out = out;
  70. sarg.ndef_bio = NULL;
  71. sarg.boundary = NULL;
  72. if (aux->asn1_cb(ASN1_OP_STREAM_PRE, &val, it, &sarg) <= 0)
  73. goto err;
  74. ndef_aux->val = val;
  75. ndef_aux->it = it;
  76. ndef_aux->ndef_bio = sarg.ndef_bio;
  77. ndef_aux->boundary = sarg.boundary;
  78. ndef_aux->out = out;
  79. BIO_ctrl(asn_bio, BIO_C_SET_EX_ARG, 0, ndef_aux);
  80. return sarg.ndef_bio;
  81. err:
  82. BIO_free(asn_bio);
  83. OPENSSL_free(ndef_aux);
  84. return NULL;
  85. }
  86. static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  87. {
  88. NDEF_SUPPORT *ndef_aux;
  89. unsigned char *p;
  90. int derlen;
  91. if (parg == NULL)
  92. return 0;
  93. ndef_aux = *(NDEF_SUPPORT **)parg;
  94. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  95. if (derlen < 0)
  96. return 0;
  97. if ((p = OPENSSL_malloc(derlen)) == NULL)
  98. return 0;
  99. ndef_aux->derbuf = p;
  100. *pbuf = p;
  101. ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  102. if (*ndef_aux->boundary == NULL)
  103. return 0;
  104. *plen = *ndef_aux->boundary - *pbuf;
  105. return 1;
  106. }
  107. static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen,
  108. void *parg)
  109. {
  110. NDEF_SUPPORT *ndef_aux;
  111. if (parg == NULL)
  112. return 0;
  113. ndef_aux = *(NDEF_SUPPORT **)parg;
  114. if (ndef_aux == NULL)
  115. return 0;
  116. OPENSSL_free(ndef_aux->derbuf);
  117. ndef_aux->derbuf = NULL;
  118. *pbuf = NULL;
  119. *plen = 0;
  120. return 1;
  121. }
  122. static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen,
  123. void *parg)
  124. {
  125. NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
  126. if (!ndef_prefix_free(b, pbuf, plen, parg))
  127. return 0;
  128. OPENSSL_free(*pndef_aux);
  129. *pndef_aux = NULL;
  130. return 1;
  131. }
  132. static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
  133. {
  134. NDEF_SUPPORT *ndef_aux;
  135. unsigned char *p;
  136. int derlen;
  137. const ASN1_AUX *aux;
  138. ASN1_STREAM_ARG sarg;
  139. if (parg == NULL)
  140. return 0;
  141. ndef_aux = *(NDEF_SUPPORT **)parg;
  142. aux = ndef_aux->it->funcs;
  143. /* Finalize structures */
  144. sarg.ndef_bio = ndef_aux->ndef_bio;
  145. sarg.out = ndef_aux->out;
  146. sarg.boundary = ndef_aux->boundary;
  147. if (aux->asn1_cb(ASN1_OP_STREAM_POST,
  148. &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
  149. return 0;
  150. derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
  151. if (derlen < 0)
  152. return 0;
  153. if ((p = OPENSSL_malloc(derlen)) == NULL)
  154. return 0;
  155. ndef_aux->derbuf = p;
  156. *pbuf = p;
  157. derlen = ASN1_item_ndef_i2d(ndef_aux->val, &p, ndef_aux->it);
  158. if (*ndef_aux->boundary == NULL)
  159. return 0;
  160. *pbuf = *ndef_aux->boundary;
  161. *plen = derlen - (*ndef_aux->boundary - ndef_aux->derbuf);
  162. return 1;
  163. }