f_int.c 3.8 KB

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