]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/a_print.c
Reorganize private crypto header files
[thirdparty/openssl.git] / crypto / asn1 / a_print.c
CommitLineData
2039c421 1/*
a1df06b3 2 * Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
365a2d99 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
d02b48c6
RE
8 */
9
10#include <stdio.h>
25f2138b 11#include "crypto/ctype.h"
b39fc560 12#include "internal/cryptlib.h"
ec577822 13#include <openssl/asn1.h>
d02b48c6 14
6343829a 15int ASN1_PRINTABLE_type(const unsigned char *s, int len)
0f113f3e
MC
16{
17 int c;
18 int ia5 = 0;
19 int t61 = 0;
d02b48c6 20
0f113f3e
MC
21 if (len <= 0)
22 len = -1;
23 if (s == NULL)
26a7d938 24 return V_ASN1_PRINTABLESTRING;
d02b48c6 25
0f113f3e
MC
26 while ((*s) && (len-- != 0)) {
27 c = *(s++);
a1df06b3 28 if (!ossl_isasn1print(c))
0f113f3e 29 ia5 = 1;
a1df06b3 30 if (!ossl_isascii(c))
0f113f3e 31 t61 = 1;
0f113f3e
MC
32 }
33 if (t61)
26a7d938 34 return V_ASN1_T61STRING;
0f113f3e 35 if (ia5)
26a7d938
K
36 return V_ASN1_IA5STRING;
37 return V_ASN1_PRINTABLESTRING;
0f113f3e 38}
d02b48c6 39
6b691a5c 40int ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s)
0f113f3e
MC
41{
42 int i;
43 unsigned char *p;
d02b48c6 44
0f113f3e 45 if (s->type != V_ASN1_UNIVERSALSTRING)
26a7d938 46 return 0;
0f113f3e 47 if ((s->length % 4) != 0)
26a7d938 48 return 0;
0f113f3e
MC
49 p = s->data;
50 for (i = 0; i < s->length; i += 4) {
51 if ((p[0] != '\0') || (p[1] != '\0') || (p[2] != '\0'))
52 break;
53 else
54 p += 4;
55 }
56 if (i < s->length)
26a7d938 57 return 0;
0f113f3e
MC
58 p = s->data;
59 for (i = 3; i < s->length; i += 4) {
60 *(p++) = s->data[i];
61 }
62 *(p) = '\0';
63 s->length /= 4;
64 s->type = ASN1_PRINTABLE_type(s->data, s->length);
208fb891 65 return 1;
0f113f3e 66}
0d0099ea
DSH
67
68int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v)
69{
70 int i, n;
71 char buf[80];
72 const char *p;
73
74 if (v == NULL)
26a7d938 75 return 0;
0d0099ea
DSH
76 n = 0;
77 p = (const char *)v->data;
78 for (i = 0; i < v->length; i++) {
79 if ((p[i] > '~') || ((p[i] < ' ') &&
80 (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)
26a7d938 87 return 0;
0d0099ea
DSH
88 n = 0;
89 }
90 }
91 if (n > 0)
92 if (BIO_write(bp, buf, n) <= 0)
26a7d938 93 return 0;
208fb891 94 return 1;
0d0099ea 95}