A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.3 KiB

  1. /*
  2. * Copyright 1995-2016 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 <ctype.h>
  11. #include "internal/cryptlib.h"
  12. #include <openssl/buffer.h>
  13. #include <openssl/asn1.h>
  14. int i2a_ASN1_INTEGER(BIO *bp, 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. #ifndef CHARSET_EBCDIC
  71. if (!(((buf[j] >= '0') && (buf[j] <= '9')) ||
  72. ((buf[j] >= 'a') && (buf[j] <= 'f')) ||
  73. ((buf[j] >= 'A') && (buf[j] <= 'F'))))
  74. #else
  75. /*
  76. * This #ifdef is not strictly necessary, since the characters
  77. * A...F a...f 0...9 are contiguous (yes, even in EBCDIC - but
  78. * not the whole alphabet). Nevertheless, isxdigit() is faster.
  79. */
  80. if (!isxdigit(buf[j]))
  81. #endif
  82. {
  83. i = j;
  84. break;
  85. }
  86. }
  87. buf[i] = '\0';
  88. /*
  89. * We have now cleared all the crap off the end of the line
  90. */
  91. if (i < 2)
  92. goto err;
  93. bufp = (unsigned char *)buf;
  94. if (first) {
  95. first = 0;
  96. if ((bufp[0] == '0') && (buf[1] == '0')) {
  97. bufp += 2;
  98. i -= 2;
  99. }
  100. }
  101. k = 0;
  102. i -= again;
  103. if (i % 2 != 0) {
  104. ASN1err(ASN1_F_A2I_ASN1_INTEGER, ASN1_R_ODD_NUMBER_OF_CHARS);
  105. return 0;
  106. }
  107. i /= 2;
  108. if (num + i > slen) {
  109. sp = OPENSSL_clear_realloc(s, slen, num + i * 2);
  110. if (sp == NULL) {
  111. ASN1err(ASN1_F_A2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
  112. OPENSSL_free(s);
  113. return 0;
  114. }
  115. s = sp;
  116. slen = num + i * 2;
  117. }
  118. for (j = 0; j < i; j++, k += 2) {
  119. for (n = 0; n < 2; n++) {
  120. m = OPENSSL_hexchar2int(bufp[k + n]);
  121. if (m < 0) {
  122. ASN1err(ASN1_F_A2I_ASN1_INTEGER,
  123. ASN1_R_NON_HEX_CHARACTERS);
  124. goto err;
  125. }
  126. s[num + j] <<= 4;
  127. s[num + j] |= m;
  128. }
  129. }
  130. num += i;
  131. if (again)
  132. bufsize = BIO_gets(bp, buf, size);
  133. else
  134. break;
  135. }
  136. bs->length = num;
  137. bs->data = s;
  138. return 1;
  139. err:
  140. ASN1err(ASN1_F_A2I_ASN1_INTEGER, ASN1_R_SHORT_LINE);
  141. return 0;
  142. }
  143. int i2a_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *a)
  144. {
  145. return i2a_ASN1_INTEGER(bp, a);
  146. }
  147. int a2i_ASN1_ENUMERATED(BIO *bp, ASN1_ENUMERATED *bs, char *buf, int size)
  148. {
  149. int rv = a2i_ASN1_INTEGER(bp, bs, buf, size);
  150. if (rv == 1)
  151. bs->type = V_ASN1_INTEGER | (bs->type & V_ASN1_NEG);
  152. return rv;
  153. }