[331] | 1 | /*
|
---|
| 2 | * Copyright 1995-2016 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 <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 (len <= 0)
|
---|
| 22 | len = -1;
|
---|
| 23 | if (s == NULL)
|
---|
| 24 | return (V_ASN1_PRINTABLESTRING);
|
---|
| 25 |
|
---|
| 26 | while ((*s) && (len-- != 0)) {
|
---|
| 27 | c = *(s++);
|
---|
| 28 | #ifndef CHARSET_EBCDIC
|
---|
| 29 | if (!(((c >= 'a') && (c <= 'z')) ||
|
---|
| 30 | ((c >= 'A') && (c <= 'Z')) ||
|
---|
| 31 | ((c >= '0') && (c <= '9')) ||
|
---|
| 32 | (c == ' ') || (c == '\'') ||
|
---|
| 33 | (c == '(') || (c == ')') ||
|
---|
| 34 | (c == '+') || (c == ',') ||
|
---|
| 35 | (c == '-') || (c == '.') ||
|
---|
| 36 | (c == '/') || (c == ':') || (c == '=') || (c == '?')))
|
---|
| 37 | ia5 = 1;
|
---|
| 38 | if (c & 0x80)
|
---|
| 39 | t61 = 1;
|
---|
| 40 | #else
|
---|
| 41 | if (!isalnum(c) && (c != ' ') && strchr("'()+,-./:=?", c) == NULL)
|
---|
| 42 | ia5 = 1;
|
---|
| 43 | if (os_toascii[c] & 0x80)
|
---|
| 44 | t61 = 1;
|
---|
| 45 | #endif
|
---|
| 46 | }
|
---|
| 47 | if (t61)
|
---|
| 48 | return (V_ASN1_T61STRING);
|
---|
| 49 | if (ia5)
|
---|
| 50 | return (V_ASN1_IA5STRING);
|
---|
| 51 | return (V_ASN1_PRINTABLESTRING);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | int ASN1_UNIVERSALSTRING_to_string(ASN1_UNIVERSALSTRING *s)
|
---|
| 55 | {
|
---|
| 56 | int i;
|
---|
| 57 | unsigned char *p;
|
---|
| 58 |
|
---|
| 59 | if (s->type != V_ASN1_UNIVERSALSTRING)
|
---|
| 60 | return (0);
|
---|
| 61 | if ((s->length % 4) != 0)
|
---|
| 62 | return (0);
|
---|
| 63 | p = s->data;
|
---|
| 64 | for (i = 0; i < s->length; i += 4) {
|
---|
| 65 | if ((p[0] != '\0') || (p[1] != '\0') || (p[2] != '\0'))
|
---|
| 66 | break;
|
---|
| 67 | else
|
---|
| 68 | p += 4;
|
---|
| 69 | }
|
---|
| 70 | if (i < s->length)
|
---|
| 71 | return (0);
|
---|
| 72 | p = s->data;
|
---|
| 73 | for (i = 3; i < s->length; i += 4) {
|
---|
| 74 | *(p++) = s->data[i];
|
---|
| 75 | }
|
---|
| 76 | *(p) = '\0';
|
---|
| 77 | s->length /= 4;
|
---|
| 78 | s->type = ASN1_PRINTABLE_type(s->data, s->length);
|
---|
| 79 | return (1);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | int ASN1_STRING_print(BIO *bp, const ASN1_STRING *v)
|
---|
| 83 | {
|
---|
| 84 | int i, n;
|
---|
| 85 | char buf[80];
|
---|
| 86 | const char *p;
|
---|
| 87 |
|
---|
| 88 | if (v == NULL)
|
---|
| 89 | return (0);
|
---|
| 90 | n = 0;
|
---|
| 91 | p = (const char *)v->data;
|
---|
| 92 | for (i = 0; i < v->length; i++) {
|
---|
| 93 | if ((p[i] > '~') || ((p[i] < ' ') &&
|
---|
| 94 | (p[i] != '\n') && (p[i] != '\r')))
|
---|
| 95 | buf[n] = '.';
|
---|
| 96 | else
|
---|
| 97 | buf[n] = p[i];
|
---|
| 98 | n++;
|
---|
| 99 | if (n >= 80) {
|
---|
| 100 | if (BIO_write(bp, buf, n) <= 0)
|
---|
| 101 | return (0);
|
---|
| 102 | n = 0;
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | if (n > 0)
|
---|
| 106 | if (BIO_write(bp, buf, n) <= 0)
|
---|
| 107 | return (0);
|
---|
| 108 | return (1);
|
---|
| 109 | }
|
---|