f_string.c 3.3 KB

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