2
0

a_print.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "crypto/ctype.h"
  11. #include "internal/cryptlib.h"
  12. #include <openssl/asn1.h>
  13. int ASN1_PRINTABLE_type(const unsigned char *s, int len)
  14. {
  15. int c;
  16. int ia5 = 0;
  17. int t61 = 0;
  18. if (len <= 0)
  19. len = -1;
  20. if (s == NULL)
  21. return V_ASN1_PRINTABLESTRING;
  22. while ((*s) && (len-- != 0)) {
  23. c = *(s++);
  24. if (!ossl_isasn1print(c))
  25. ia5 = 1;
  26. if (!ossl_isascii(c))
  27. t61 = 1;
  28. }
  29. if (t61)
  30. return V_ASN1_T61STRING;
  31. if (ia5)
  32. return V_ASN1_IA5STRING;
  33. return V_ASN1_PRINTABLESTRING;
  34. }
  35. int ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s)
  36. {
  37. int i;
  38. unsigned char *p;
  39. if (s->type != V_ASN1_UNIVERSALSTRING)
  40. return 0;
  41. if ((s->length % 4) != 0)
  42. return 0;
  43. p = s->data;
  44. for (i = 0; i < s->length; i += 4) {
  45. if ((p[0] != '\0') || (p[1] != '\0') || (p[2] != '\0'))
  46. break;
  47. else
  48. p += 4;
  49. }
  50. if (i < s->length)
  51. return 0;
  52. p = s->data;
  53. for (i = 3; i < s->length; i += 4) {
  54. *(p++) = s->data[i];
  55. }
  56. *(p) = '\0';
  57. s->length /= 4;
  58. s->type = ASN1_PRINTABLE_type(s->data, s->length);
  59. return 1;
  60. }
  61. int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v)
  62. {
  63. int i, n;
  64. char buf[80];
  65. const char *p;
  66. if (v == NULL)
  67. return 0;
  68. n = 0;
  69. p = (const char *)v->data;
  70. for (i = 0; i < v->length; i++) {
  71. if ((p[i] > '~') || ((p[i] < ' ') &&
  72. (p[i] != '\n') && (p[i] != '\r')))
  73. buf[n] = '.';
  74. else
  75. buf[n] = p[i];
  76. n++;
  77. if (n >= 80) {
  78. if (BIO_write(bp, buf, n) <= 0)
  79. return 0;
  80. n = 0;
  81. }
  82. }
  83. if (n > 0)
  84. if (BIO_write(bp, buf, n) <= 0)
  85. return 0;
  86. return 1;
  87. }