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