f_string.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright 1995-2024 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 "crypto/ctype.h"
  11. #include "internal/cryptlib.h"
  12. #include <openssl/buffer.h>
  13. #include <openssl/asn1.h>
  14. int i2a_ASN1_STRING(BIO *bp, const ASN1_STRING *a, int type)
  15. {
  16. int i, n = 0;
  17. char buf[2];
  18. if (a == NULL)
  19. return 0;
  20. if (a->length == 0) {
  21. if (BIO_write(bp, "0", 1) != 1)
  22. goto err;
  23. n = 1;
  24. } else {
  25. for (i = 0; i < a->length; i++) {
  26. if ((i != 0) && (i % 35 == 0)) {
  27. if (BIO_write(bp, "\\\n", 2) != 2)
  28. goto err;
  29. n += 2;
  30. }
  31. ossl_to_hex(buf, a->data[i]);
  32. if (BIO_write(bp, buf, 2) != 2)
  33. goto err;
  34. n += 2;
  35. }
  36. }
  37. return n;
  38. err:
  39. return -1;
  40. }
  41. int a2i_ASN1_STRING(BIO *bp, ASN1_STRING *bs, char *buf, int size)
  42. {
  43. int i, j, k, m, n, again, bufsize;
  44. unsigned char *s = NULL, *sp;
  45. unsigned char *bufp;
  46. int num = 0, slen = 0, first = 1;
  47. bufsize = BIO_gets(bp, buf, size);
  48. for (;;) {
  49. if (bufsize < 1) {
  50. if (first)
  51. break;
  52. else
  53. goto err;
  54. }
  55. first = 0;
  56. i = bufsize;
  57. if (buf[i - 1] == '\n')
  58. buf[--i] = '\0';
  59. if (i == 0)
  60. goto err;
  61. if (buf[i - 1] == '\r')
  62. buf[--i] = '\0';
  63. if (i == 0)
  64. goto err;
  65. again = (buf[i - 1] == '\\');
  66. for (j = i - 1; j > 0; j--) {
  67. if (!ossl_isxdigit(buf[j])) {
  68. i = j;
  69. break;
  70. }
  71. }
  72. buf[i] = '\0';
  73. /*
  74. * We have now cleared all the crap off the end of the line
  75. */
  76. if (i < 2)
  77. goto err;
  78. bufp = (unsigned char *)buf;
  79. k = 0;
  80. i -= again;
  81. if (i % 2 != 0) {
  82. ERR_raise(ERR_LIB_ASN1, ASN1_R_ODD_NUMBER_OF_CHARS);
  83. OPENSSL_free(s);
  84. return 0;
  85. }
  86. i /= 2;
  87. if (num + i > slen) {
  88. sp = OPENSSL_realloc(s, (unsigned int)num + i * 2);
  89. if (sp == NULL) {
  90. OPENSSL_free(s);
  91. return 0;
  92. }
  93. s = sp;
  94. slen = num + i * 2;
  95. }
  96. for (j = 0; j < i; j++, k += 2) {
  97. for (n = 0; n < 2; n++) {
  98. m = OPENSSL_hexchar2int(bufp[k + n]);
  99. if (m < 0) {
  100. ERR_raise(ERR_LIB_ASN1, ASN1_R_NON_HEX_CHARACTERS);
  101. OPENSSL_free(s);
  102. return 0;
  103. }
  104. s[num + j] <<= 4;
  105. s[num + j] |= m;
  106. }
  107. }
  108. num += i;
  109. if (again)
  110. bufsize = BIO_gets(bp, buf, size);
  111. else
  112. break;
  113. }
  114. bs->length = num;
  115. bs->data = s;
  116. return 1;
  117. err:
  118. ERR_raise(ERR_LIB_ASN1, ASN1_R_SHORT_LINE);
  119. OPENSSL_free(s);
  120. return 0;
  121. }