f_int.c 3.7 KB

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