a_i2d_fp.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "internal/cryptlib.h"
  11. #include <openssl/buffer.h>
  12. #include <openssl/asn1.h>
  13. #ifndef NO_OLD_ASN1
  14. # ifndef OPENSSL_NO_STDIO
  15. int ASN1_i2d_fp(i2d_of_void *i2d, FILE *out, const void *x)
  16. {
  17. BIO *b;
  18. int ret;
  19. if ((b = BIO_new(BIO_s_file())) == NULL) {
  20. ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
  21. return 0;
  22. }
  23. BIO_set_fp(b, out, BIO_NOCLOSE);
  24. ret = ASN1_i2d_bio(i2d, b, x);
  25. BIO_free(b);
  26. return ret;
  27. }
  28. # endif
  29. int ASN1_i2d_bio(i2d_of_void *i2d, BIO *out, const void *x)
  30. {
  31. char *b;
  32. unsigned char *p;
  33. int i, j = 0, n, ret = 1;
  34. n = i2d(x, NULL);
  35. if (n <= 0)
  36. return 0;
  37. b = OPENSSL_malloc(n);
  38. if (b == NULL)
  39. return 0;
  40. p = (unsigned char *)b;
  41. i2d(x, &p);
  42. for (;;) {
  43. i = BIO_write(out, &(b[j]), n);
  44. if (i == n)
  45. break;
  46. if (i <= 0) {
  47. ret = 0;
  48. break;
  49. }
  50. j += i;
  51. n -= i;
  52. }
  53. OPENSSL_free(b);
  54. return ret;
  55. }
  56. #endif
  57. #ifndef OPENSSL_NO_STDIO
  58. int ASN1_item_i2d_fp(const ASN1_ITEM *it, FILE *out, const void *x)
  59. {
  60. BIO *b;
  61. int ret;
  62. if ((b = BIO_new(BIO_s_file())) == NULL) {
  63. ERR_raise(ERR_LIB_ASN1, ERR_R_BUF_LIB);
  64. return 0;
  65. }
  66. BIO_set_fp(b, out, BIO_NOCLOSE);
  67. ret = ASN1_item_i2d_bio(it, b, x);
  68. BIO_free(b);
  69. return ret;
  70. }
  71. #endif
  72. int ASN1_item_i2d_bio(const ASN1_ITEM *it, BIO *out, const void *x)
  73. {
  74. unsigned char *b = NULL;
  75. int i, j = 0, n, ret = 1;
  76. n = ASN1_item_i2d(x, &b, it);
  77. if (b == NULL) {
  78. ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB);
  79. return 0;
  80. }
  81. for (;;) {
  82. i = BIO_write(out, &(b[j]), n);
  83. if (i == n)
  84. break;
  85. if (i <= 0) {
  86. ret = 0;
  87. break;
  88. }
  89. j += i;
  90. n -= i;
  91. }
  92. OPENSSL_free(b);
  93. return ret;
  94. }
  95. BIO *ASN1_item_i2d_mem_bio(const ASN1_ITEM *it, const ASN1_VALUE *val)
  96. {
  97. BIO *res;
  98. if (it == NULL || val == NULL) {
  99. ERR_raise(ERR_LIB_ASN1, ERR_R_PASSED_NULL_PARAMETER);
  100. return NULL;
  101. }
  102. if ((res = BIO_new(BIO_s_mem())) == NULL)
  103. return NULL;
  104. if (ASN1_item_i2d_bio(it, res, val) <= 0) {
  105. BIO_free(res);
  106. res = NULL;
  107. }
  108. return res;
  109. }