a_print.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright 1995-2021 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/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 (s == NULL)
  19. return V_ASN1_PRINTABLESTRING;
  20. if (len < 0)
  21. len = strlen((const char *)s);
  22. while (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. }