155714Skris/* crypto/x509/x509_obj.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8280304Sjkim * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15280304Sjkim * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22280304Sjkim * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40280304Sjkim * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52280304Sjkim * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include "cryptlib.h" 6155714Skris#include <openssl/lhash.h> 6255714Skris#include <openssl/objects.h> 6355714Skris#include <openssl/x509.h> 6455714Skris#include <openssl/buffer.h> 6555714Skris 66298999Sjkim/* 67298999Sjkim * Limit to ensure we don't overflow: much greater than 68298999Sjkim * anything enountered in practice. 69298999Sjkim */ 70298999Sjkim 71298999Sjkim#define NAME_ONELINE_MAX (1024 * 1024) 72298999Sjkim 7355714Skrischar *X509_NAME_oneline(X509_NAME *a, char *buf, int len) 74280304Sjkim{ 75280304Sjkim X509_NAME_ENTRY *ne; 76280304Sjkim int i; 77280304Sjkim int n, lold, l, l1, l2, num, j, type; 78280304Sjkim const char *s; 79280304Sjkim char *p; 80280304Sjkim unsigned char *q; 81280304Sjkim BUF_MEM *b = NULL; 82280304Sjkim static const char hex[17] = "0123456789ABCDEF"; 83280304Sjkim int gs_doit[4]; 84280304Sjkim char tmp_buf[80]; 8555714Skris#ifdef CHARSET_EBCDIC 86280304Sjkim char ebcdic_buf[1024]; 8755714Skris#endif 8855714Skris 89280304Sjkim if (buf == NULL) { 90280304Sjkim if ((b = BUF_MEM_new()) == NULL) 91280304Sjkim goto err; 92280304Sjkim if (!BUF_MEM_grow(b, 200)) 93280304Sjkim goto err; 94280304Sjkim b->data[0] = '\0'; 95280304Sjkim len = 200; 96298999Sjkim } else if (len == 0) { 97298999Sjkim return NULL; 98280304Sjkim } 99280304Sjkim if (a == NULL) { 100280304Sjkim if (b) { 101280304Sjkim buf = b->data; 102280304Sjkim OPENSSL_free(b); 103280304Sjkim } 104280304Sjkim strncpy(buf, "NO X509_NAME", len); 105280304Sjkim buf[len - 1] = '\0'; 106280304Sjkim return buf; 107280304Sjkim } 10855714Skris 109280304Sjkim len--; /* space for '\0' */ 110280304Sjkim l = 0; 111280304Sjkim for (i = 0; i < sk_X509_NAME_ENTRY_num(a->entries); i++) { 112280304Sjkim ne = sk_X509_NAME_ENTRY_value(a->entries, i); 113280304Sjkim n = OBJ_obj2nid(ne->object); 114280304Sjkim if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { 115280304Sjkim i2t_ASN1_OBJECT(tmp_buf, sizeof(tmp_buf), ne->object); 116280304Sjkim s = tmp_buf; 117280304Sjkim } 118280304Sjkim l1 = strlen(s); 11955714Skris 120280304Sjkim type = ne->value->type; 121280304Sjkim num = ne->value->length; 122298999Sjkim if (num > NAME_ONELINE_MAX) { 123298999Sjkim X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG); 124298999Sjkim goto end; 125298999Sjkim } 126280304Sjkim q = ne->value->data; 12755714Skris#ifdef CHARSET_EBCDIC 128280304Sjkim if (type == V_ASN1_GENERALSTRING || 129280304Sjkim type == V_ASN1_VISIBLESTRING || 130280304Sjkim type == V_ASN1_PRINTABLESTRING || 131280304Sjkim type == V_ASN1_TELETEXSTRING || 132280304Sjkim type == V_ASN1_VISIBLESTRING || type == V_ASN1_IA5STRING) { 133298999Sjkim if (num > (int)sizeof(ebcdic_buf)) 134298999Sjkim num = sizeof(ebcdic_buf); 135298999Sjkim ascii2ebcdic(ebcdic_buf, q, num); 136280304Sjkim q = ebcdic_buf; 137280304Sjkim } 13855714Skris#endif 13955714Skris 140280304Sjkim if ((type == V_ASN1_GENERALSTRING) && ((num % 4) == 0)) { 141280304Sjkim gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 0; 142280304Sjkim for (j = 0; j < num; j++) 143280304Sjkim if (q[j] != 0) 144280304Sjkim gs_doit[j & 3] = 1; 14555714Skris 146280304Sjkim if (gs_doit[0] | gs_doit[1] | gs_doit[2]) 147280304Sjkim gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1; 148280304Sjkim else { 149280304Sjkim gs_doit[0] = gs_doit[1] = gs_doit[2] = 0; 150280304Sjkim gs_doit[3] = 1; 151280304Sjkim } 152280304Sjkim } else 153280304Sjkim gs_doit[0] = gs_doit[1] = gs_doit[2] = gs_doit[3] = 1; 15455714Skris 155280304Sjkim for (l2 = j = 0; j < num; j++) { 156280304Sjkim if (!gs_doit[j & 3]) 157280304Sjkim continue; 158280304Sjkim l2++; 15955714Skris#ifndef CHARSET_EBCDIC 160280304Sjkim if ((q[j] < ' ') || (q[j] > '~')) 161280304Sjkim l2 += 3; 16255714Skris#else 163280304Sjkim if ((os_toascii[q[j]] < os_toascii[' ']) || 164280304Sjkim (os_toascii[q[j]] > os_toascii['~'])) 165280304Sjkim l2 += 3; 16655714Skris#endif 167280304Sjkim } 16855714Skris 169280304Sjkim lold = l; 170280304Sjkim l += 1 + l1 + 1 + l2; 171298999Sjkim if (l > NAME_ONELINE_MAX) { 172298999Sjkim X509err(X509_F_X509_NAME_ONELINE, X509_R_NAME_TOO_LONG); 173298999Sjkim goto end; 174298999Sjkim } 175280304Sjkim if (b != NULL) { 176280304Sjkim if (!BUF_MEM_grow(b, l + 1)) 177280304Sjkim goto err; 178280304Sjkim p = &(b->data[lold]); 179280304Sjkim } else if (l > len) { 180280304Sjkim break; 181280304Sjkim } else 182280304Sjkim p = &(buf[lold]); 183280304Sjkim *(p++) = '/'; 184280304Sjkim memcpy(p, s, (unsigned int)l1); 185280304Sjkim p += l1; 186280304Sjkim *(p++) = '='; 18755714Skris 188280304Sjkim#ifndef CHARSET_EBCDIC /* q was assigned above already. */ 189280304Sjkim q = ne->value->data; 19055714Skris#endif 19155714Skris 192280304Sjkim for (j = 0; j < num; j++) { 193280304Sjkim if (!gs_doit[j & 3]) 194280304Sjkim continue; 19555714Skris#ifndef CHARSET_EBCDIC 196280304Sjkim n = q[j]; 197280304Sjkim if ((n < ' ') || (n > '~')) { 198280304Sjkim *(p++) = '\\'; 199280304Sjkim *(p++) = 'x'; 200280304Sjkim *(p++) = hex[(n >> 4) & 0x0f]; 201280304Sjkim *(p++) = hex[n & 0x0f]; 202280304Sjkim } else 203280304Sjkim *(p++) = n; 20455714Skris#else 205280304Sjkim n = os_toascii[q[j]]; 206280304Sjkim if ((n < os_toascii[' ']) || (n > os_toascii['~'])) { 207280304Sjkim *(p++) = '\\'; 208280304Sjkim *(p++) = 'x'; 209280304Sjkim *(p++) = hex[(n >> 4) & 0x0f]; 210280304Sjkim *(p++) = hex[n & 0x0f]; 211280304Sjkim } else 212280304Sjkim *(p++) = q[j]; 21355714Skris#endif 214280304Sjkim } 215280304Sjkim *p = '\0'; 216280304Sjkim } 217280304Sjkim if (b != NULL) { 218280304Sjkim p = b->data; 219280304Sjkim OPENSSL_free(b); 220280304Sjkim } else 221280304Sjkim p = buf; 222280304Sjkim if (i == 0) 223280304Sjkim *p = '\0'; 224280304Sjkim return (p); 225280304Sjkim err: 226280304Sjkim X509err(X509_F_X509_NAME_ONELINE, ERR_R_MALLOC_FAILURE); 227298999Sjkim end: 228298999Sjkim BUF_MEM_free(b); 229280304Sjkim return (NULL); 230280304Sjkim} 231