x509_obj.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "internal/cryptlib.h"
  11. #include <openssl/objects.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/buffer.h>
  14. #include "crypto/x509.h"
  15. DEFINE_STACK_OF(X509_NAME_ENTRY)
  16. /*
  17. * Limit to ensure we don't overflow: much greater than
  18. * anything encountered in practice.
  19. */
  20. #define NAME_ONELINE_MAX (1024 * 1024)
  21. char *X509_NAME_oneline(const X509_NAME *a, char *buf, int len)
  22. {
  23. const X509_NAME_ENTRY *ne;
  24. int i;
  25. int n, lold, l, l1, l2, num, j, type;
  26. const char *s;
  27. char *p;
  28. unsigned char *q;
  29. BUF_MEM *b = NULL;
  30. static const char hex[17] = "0123456789ABCDEF";
  31. int gs_doit[4];
  32. char tmp_buf[80];
  33. #ifdef CHARSET_EBCDIC
  34. unsigned char ebcdic_buf[1024];
  35. #endif
  36. if (buf == NULL) {
  37. if ((b = BUF_MEM_new()) == NULL)
  38. goto err;
  39. if (!BUF_MEM_grow(b, 200))
  40. goto err;
  41. b->data[0] = '\0';
  42. len = 200;
  43. } else if (len == 0) {
  44. return NULL;
  45. }
  46. if (a == NULL) {
  47. if (b) {
  48. buf = b->data;
  49. OPENSSL_free(b);
  50. }
  51. strncpy(buf, "NO X509_NAME", len);
  52. buf[len - 1] = '\0';
  53. return buf;
  54. }
  55. len--; /* space for '\0' */
  56. l = 0;
  57. for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) {
  58. ne = sk_X509_NAME_ENTRY_value(a->entries, i);
  59. n = OBJ_obj2nid(ne->object);
  60. if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) {
  61. i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object);
  62. s = tmp_buf;
  63. }
  64. l1 = strlen(s);
  65. type = ne->value->type;
  66. num = ne->value->length;
  67. if (num > NAME_ONELINE_MAX) {
  68. X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG);
  69. goto end;
  70. }
  71. q = ne->value->data;
  72. #ifdef CHARSET_EBCDIC
  73. if (type == V_ASN1_GENERALSTRING ||
  74. type == V_ASN1_VISIBLESTRING ||
  75. type == V_ASN1_PRINTABLESTRING ||
  76. type == V_ASN1_TELETEXSTRING ||
  77. type == V_ASN1_IA5STRING) {
  78. if (num > (int)sizeof(ebcdic_buf))
  79. num = sizeof(ebcdic_buf);
  80. ascii2ebcdic(ebcdic_buf, q, num);
  81. q = ebcdic_buf;
  82. }
  83. #endif
  84. if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) {
  85. gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0;
  86. for (j = 0; j < num; j++)
  87. if (q[j] != 0)
  88. gs_doit[j & 3] = 1;
  89. if (gs_doit[0] | gs_doit[1] | gs_doit[2])
  90. gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
  91. else {
  92. gs_doit[0] = gs_doit[1] = gs_doit[2] = 0;
  93. gs_doit[3] = 1;
  94. }
  95. } else
  96. gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1;
  97. for (l2 = j = 0; j < num; j++) {
  98. if (!gs_doit[j & 3])
  99. continue;
  100. l2++;
  101. #ifndef CHARSET_EBCDIC
  102. if ((q[j] < ' ') || (q[j] > '~'))
  103. l2 += 3;
  104. #else
  105. if ((os_toascii[q[j]] < os_toascii[' ']) ||
  106. (os_toascii[q[j]] > os_toascii['~']))
  107. l2 += 3;
  108. #endif
  109. }
  110. lold = l;
  111. l += 1 + l1 + 1 + l2;
  112. if (l > NAME_ONELINE_MAX) {
  113. X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG);
  114. goto end;
  115. }
  116. if (b != NULL) {
  117. if (!BUF_MEM_grow(b, l + 1))
  118. goto err;
  119. p = &(b->data[lold]);
  120. } else if (l > len) {
  121. break;
  122. } else
  123. p = &(buf[lold]);
  124. *(p++) = '/';
  125. memcpy(p, s, (unsigned int)l1);
  126. p += l1;
  127. *(p++) = '=';
  128. #ifndef CHARSET_EBCDIC /* q was assigned above already. */
  129. q = ne->value->data;
  130. #endif
  131. for (j = 0; j < num; j++) {
  132. if (!gs_doit[j & 3])
  133. continue;
  134. #ifndef CHARSET_EBCDIC
  135. n = q[j];
  136. if ((n < ' ') || (n > '~')) {
  137. *(p++) = '\\';
  138. *(p++) = 'x';
  139. *(p++) = hex[(n >> 4) & 0x0f];
  140. *(p++) = hex[n & 0x0f];
  141. } else
  142. *(p++) = n;
  143. #else
  144. n = os_toascii[q[j]];
  145. if ((n < os_toascii[' ']) || (n > os_toascii['~'])) {
  146. *(p++) = '\\';
  147. *(p++) = 'x';
  148. *(p++) = hex[(n >> 4) & 0x0f];
  149. *(p++) = hex[n & 0x0f];
  150. } else
  151. *(p++) = q[j];
  152. #endif
  153. }
  154. *p = '\0';
  155. }
  156. if (b != NULL) {
  157. p = b->data;
  158. OPENSSL_free(b);
  159. } else
  160. p = buf;
  161. if (i == 0)
  162. *p = '\0';
  163. return p;
  164. err:
  165. X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE);
  166. end:
  167. BUF_MEM_free(b);
  168. return NULL;
  169. }