f_string.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright 1995-2017 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 <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. ASN1err(ASN1_F_A2I_ASN1_STRING, 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. ASN1err(ASN1_F_A2I_ASN1_STRING, ERR_R_MALLOC_FAILURE);
  93. OPENSSL_free(s);
  94. return 0;
  95. }
  96. s = sp;
  97. slen = num + i * 2;
  98. }
  99. for (j = 0; j < i; j++, k += 2) {
  100. for (n = 0; n < 2; n++) {
  101. m = OPENSSL_hexchar2int(bufp[k + n]);
  102. if (m < 0) {
  103. ASN1err(ASN1_F_A2I_ASN1_STRING,
  104. ASN1_R_NON_HEX_CHARACTERS);
  105. OPENSSL_free(s);
  106. return 0;
  107. }
  108. s[num + j] <<= 4;
  109. s[num + j] |= m;
  110. }
  111. }
  112. num += i;
  113. if (again)
  114. bufsize = BIO_gets(bp, buf, size);
  115. else
  116. break;
  117. }
  118. bs->length = num;
  119. bs->data = s;
  120. return 1;
  121. err:
  122. ASN1err(ASN1_F_A2I_ASN1_STRING, ASN1_R_SHORT_LINE);
  123. OPENSSL_free(s);
  124. return 0;
  125. }