x509_obj.c 5.0 KB

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