2
0

f_int.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright 1995-2017 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/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. ASN1err(ASN1_F_A2I_ASN1_INTEGER, 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. ASN1err(ASN1_F_A2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
  102. OPENSSL_free(s);
  103. return 0;
  104. }
  105. s = sp;
  106. slen = num + i * 2;
  107. }
  108. for (j = 0; j < i; j++, k += 2) {
  109. for (n = 0; n < 2; n++) {
  110. m = OPENSSL_hexchar2int(bufp[k + n]);
  111. if (m < 0) {
  112. ASN1err(ASN1_F_A2I_ASN1_INTEGER,
  113. ASN1_R_NON_HEX_CHARACTERS);
  114. goto err;
  115. }
  116. s[num + j] <<= 4;
  117. s[num + j] |= m;
  118. }
  119. }
  120. num += i;
  121. if (again)
  122. bufsize = BIO_gets(bp, buf, size);
  123. else
  124. break;
  125. }
  126. bs->length = num;
  127. bs->data = s;
  128. return 1;
  129. err:
  130. ASN1err(ASN1_F_A2I_ASN1_INTEGER, ASN1_R_SHORT_LINE);
  131. OPENSSL_free(s);
  132. return 0;
  133. }
  134. int i2a_ASN1_ENUMERATED(BIO *bp, const ASN1_ENUMERATED *a)
  135. {
  136. return i2a_ASN1_INTEGER(bp, a);
  137. }
  138. int a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size)
  139. {
  140. int rv = a2i_ASN1_INTEGER(bp, bs, buf, size);
  141. if (rv == 1)
  142. bs->type = V_ASN1_INTEGER | (bs->type & V_ASN1_NEG);
  143. return rv;
  144. }