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