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