LCOV - code coverage report
Current view: top level - src - dn.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 0 667 0.0 %
Date: 2016-12-15 12:55:14 Functions: 0 23 0.0 %

          Line data    Source code
       1             : /* dn.c - Distinguished Name helper functions
       2             :  * Copyright (C) 2001, 2006, 2012 g10 Code GmbH
       3             :  *
       4             :  * This file is part of KSBA.
       5             :  *
       6             :  * KSBA is free software; you can redistribute it and/or modify
       7             :  * it under the terms of either
       8             :  *
       9             :  *   - the GNU Lesser General Public License as published by the Free
      10             :  *     Software Foundation; either version 3 of the License, or (at
      11             :  *     your option) any later version.
      12             :  *
      13             :  * or
      14             :  *
      15             :  *   - the GNU General Public License as published by the Free
      16             :  *     Software Foundation; either version 2 of the License, or (at
      17             :  *     your option) any later version.
      18             :  *
      19             :  * or both in parallel, as here.
      20             :  *
      21             :  * KSBA is distributed in the hope that it will be useful, but WITHOUT
      22             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
      23             :  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
      24             :  * License for more details.
      25             :  *
      26             :  * You should have received a copies of the GNU General Public License
      27             :  * and the GNU Lesser General Public License along with this program;
      28             :  * if not, see <http://www.gnu.org/licenses/>.
      29             :  */
      30             : 
      31             : /* Reference is RFC-2253 */
      32             : 
      33             : #include <config.h>
      34             : #include <stdio.h>
      35             : #include <stdlib.h>
      36             : #include <string.h>
      37             : #include <assert.h>
      38             : 
      39             : #include "util.h"
      40             : #include "asn1-func.h"
      41             : #include "ber-help.h"
      42             : #include "ber-decoder.h"
      43             : 
      44             : static const struct {
      45             :   const char *name;
      46             :   int source; /* 0 = unknown
      47             :                  1 = rfc2253
      48             :                  2 = David Chadwick, July 2003
      49             :                  <draft-ietf-pkix-dnstrings-02.txt>
      50             :                  3 = Peter Gutmann
      51             :               */
      52             :   const char *description;
      53             :   size_t      oidlen;
      54             :   const unsigned char *oid;  /* DER encoded OID.  */
      55             :   const char *oidstr;        /* OID as dotted string.  */
      56             : } oid_name_tbl[] = {
      57             : {"CN", 1, "CommonName",            3, "\x55\x04\x03", "2.5.4.3" },
      58             : {"SN", 2, "Surname",               3, "\x55\x04\x04", "2.5.4.4" },
      59             : {"SERIALNUMBER", 2, "SerialNumber",3, "\x55\x04\x05", "2.5.4.5" },
      60             : {"C",  1, "CountryName",           3, "\x55\x04\x06", "2.5.4.6" },
      61             : {"L" , 1, "LocalityName",          3, "\x55\x04\x07", "2.5.4.7" },
      62             : {"ST", 1, "StateOrProvince",       3, "\x55\x04\x08", "2.5.4.8" },
      63             : {"STREET", 1, "StreetAddress",     3, "\x55\x04\x09", "2.5.4.9" },
      64             : {"O",  1, "OrganizationName",      3, "\x55\x04\x0a", "2.5.4.10" },
      65             : {"OU", 1, "OrganizationalUnit",    3, "\x55\x04\x0b", "2.5.4.11" },
      66             : {"T",  2, "Title",                 3, "\x55\x04\x0c", "2.5.4.12" },
      67             : {"D",  3, "Description",           3, "\x55\x04\x0d", "2.5.4.13" },
      68             : {"BC", 3, "BusinessCategory",      3, "\x55\x04\x0f", "2.5.4.15" },
      69             : {"ADDR", 2, "PostalAddress",       3, "\x55\x04\x11", "2.5.4.16" },
      70             : {"POSTALCODE" , 0, "PostalCode",   3, "\x55\x04\x11", "2.5.4.17" },
      71             : {"GN", 2, "GivenName",             3, "\x55\x04\x2a", "2.5.4.42" },
      72             : {"PSEUDO", 2, "Pseudonym",         3, "\x55\x04\x41", "2.5.4.65" },
      73             : {"DC", 1, "domainComponent",      10,
      74             :     "\x09\x92\x26\x89\x93\xF2\x2C\x64\x01\x19", "0.9.2342.19200300.100.1.25" },
      75             : {"UID", 1, "userid",              10,
      76             :     "\x09\x92\x26\x89\x93\xF2\x2C\x64\x01\x01", "0.9.2342.19200300.100.1.1 " },
      77             : {"EMAIL", 3, "emailAddress",       9,
      78             :     "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x01",     "1.2.840.113549.1.9.1" },
      79             : { NULL }
      80             : };
      81             : 
      82             : 
      83             : #define N 0x00
      84             : #define P 0x01
      85             : static unsigned char charclasses[128] = {
      86             :   N, N, N, N, N, N, N, N,  N, N, N, N, N, N, N, N,
      87             :   N, N, N, N, N, N, N, N,  N, N, N, N, N, N, N, N,
      88             :   P, N, N, N, N, N, N, P,  P, P, N, P, P, P, P, P,
      89             :   P, P, P, P, P, P, P, P,  P, P, P, N, N, P, N, P,
      90             :   N, P, P, P, P, P, P, P,  P, P, P, P, P, P, P, P,
      91             :   P, P, P, P, P, P, P, P,  P, P, P, N, N, N, N, N,
      92             :   N, P, P, P, P, P, P, P,  P, P, P, P, P, P, P, P,
      93             :   P, P, P, P, P, P, P, P,  P, P, P, N, N, N, N, N
      94             : };
      95             : #undef N
      96             : #undef P
      97             : 
      98             : struct stringbuf {
      99             :   size_t len;
     100             :   size_t size;
     101             :   char *buf;
     102             :   int out_of_core;
     103             : };
     104             : 
     105             : 
     106             : 
     107             : static void
     108           0 : init_stringbuf (struct stringbuf *sb, int initiallen)
     109             : {
     110           0 :   sb->len = 0;
     111           0 :   sb->size = initiallen;
     112           0 :   sb->out_of_core = 0;
     113             :   /* allocate one more, so that get_stringbuf can append a nul */
     114           0 :   sb->buf = xtrymalloc (initiallen+1);
     115           0 :   if (!sb->buf)
     116           0 :       sb->out_of_core = 1;
     117           0 : }
     118             : 
     119             : static void
     120           0 : deinit_stringbuf (struct stringbuf *sb)
     121             : {
     122           0 :   xfree (sb->buf);
     123           0 :   sb->buf = NULL;
     124           0 :   sb->out_of_core = 1; /* make sure the caller does an init before reuse */
     125           0 : }
     126             : 
     127             : 
     128             : static void
     129           0 : put_stringbuf (struct stringbuf *sb, const char *text)
     130             : {
     131           0 :   size_t n = strlen (text);
     132             : 
     133           0 :   if (sb->out_of_core)
     134           0 :     return;
     135             : 
     136           0 :   if (sb->len + n >= sb->size)
     137             :     {
     138             :       char *p;
     139             : 
     140           0 :       sb->size += n + 100;
     141           0 :       p = xtryrealloc (sb->buf, sb->size);
     142           0 :       if ( !p)
     143             :         {
     144           0 :           sb->out_of_core = 1;
     145           0 :           return;
     146             :         }
     147           0 :       sb->buf = p;
     148             :     }
     149           0 :   memcpy (sb->buf+sb->len, text, n);
     150           0 :   sb->len += n;
     151             : }
     152             : 
     153             : static void
     154           0 : put_stringbuf_mem (struct stringbuf *sb, const char *text, size_t n)
     155             : {
     156           0 :   if (sb->out_of_core)
     157           0 :     return;
     158             : 
     159           0 :   if (sb->len + n >= sb->size)
     160             :     {
     161             :       char *p;
     162             : 
     163           0 :       sb->size += n + 100;
     164           0 :       p = xtryrealloc (sb->buf, sb->size);
     165           0 :       if ( !p)
     166             :         {
     167           0 :           sb->out_of_core = 1;
     168           0 :           return;
     169             :         }
     170           0 :       sb->buf = p;
     171             :     }
     172           0 :   memcpy (sb->buf+sb->len, text, n);
     173           0 :   sb->len += n;
     174             : }
     175             : 
     176             : static void
     177           0 : put_stringbuf_mem_skip (struct stringbuf *sb, const char *text, size_t n,
     178             :                         int skip)
     179             : {
     180             :   char *p;
     181             : 
     182           0 :   if (!skip)
     183             :     {
     184           0 :       put_stringbuf_mem (sb, text, n);
     185           0 :       return;
     186             :     }
     187           0 :   if (sb->out_of_core)
     188           0 :     return;
     189             : 
     190           0 :   if (sb->len + n >= sb->size)
     191             :     {
     192             :       /* Note: we allocate too much here, but we don't care. */
     193           0 :       sb->size += n + 100;
     194           0 :       p = xtryrealloc (sb->buf, sb->size);
     195           0 :       if ( !p)
     196             :         {
     197           0 :           sb->out_of_core = 1;
     198           0 :           return;
     199             :         }
     200           0 :       sb->buf = p;
     201             :     }
     202           0 :   p = sb->buf+sb->len;
     203           0 :   while (n > skip)
     204             :     {
     205           0 :       text += skip;
     206           0 :       n -= skip;
     207           0 :       *p++ = *text++;
     208           0 :       n--;
     209           0 :       sb->len++;
     210             :     }
     211             : }
     212             : 
     213             : static char *
     214           0 : get_stringbuf (struct stringbuf *sb)
     215             : {
     216             :   char *p;
     217             : 
     218           0 :   if (sb->out_of_core)
     219             :     {
     220           0 :       xfree (sb->buf); sb->buf = NULL;
     221           0 :       return NULL;
     222             :     }
     223             : 
     224           0 :   sb->buf[sb->len] = 0;
     225           0 :   p = sb->buf;
     226           0 :   sb->buf = NULL;
     227           0 :   sb->out_of_core = 1; /* make sure the caller does an init before reuse */
     228           0 :   return p;
     229             : }
     230             : 
     231             : 
     232             : /* This function is used for 1 byte encodings to insert any required
     233             :    quoting.  It does not do the quoting for a space or hash mark at
     234             :    the beginning of a string or a space as the last character of a
     235             :    string.  It will do steps of SKIP+1 characters, assuming that these
     236             :    SKIP characters are null octets. */
     237             : static void
     238           0 : append_quoted (struct stringbuf *sb, const unsigned char *value, size_t length,
     239             :                int skip)
     240             : {
     241             :   unsigned char tmp[4];
     242           0 :   const unsigned char *s = value;
     243           0 :   size_t n = 0;
     244             : 
     245             :   for (;;)
     246             :     {
     247           0 :       for (value = s; n+skip < length; n++, s++)
     248             :         {
     249           0 :           s += skip;
     250           0 :           n += skip;
     251           0 :           if (*s < ' ' || *s > 126 || strchr (",+\"\\<>;", *s) )
     252             :             break;
     253             :         }
     254             : 
     255           0 :       if (s != value)
     256           0 :         put_stringbuf_mem_skip (sb, value, s-value, skip);
     257           0 :       if (n+skip >= length)
     258           0 :         return; /* ready */
     259           0 :       s += skip;
     260           0 :       n += skip;
     261           0 :       if ( *s < ' ' || *s > 126 )
     262             :         {
     263           0 :           snprintf (tmp, sizeof tmp, "\\%02X", *s);
     264           0 :           put_stringbuf_mem (sb, tmp, 3);
     265             :         }
     266             :       else
     267             :         {
     268           0 :           tmp[0] = '\\';
     269           0 :           tmp[1] = *s;
     270           0 :           put_stringbuf_mem (sb, tmp, 2);
     271             :         }
     272           0 :       n++; s++;
     273             :     }
     274             : }
     275             : 
     276             : 
     277             : /* Append VALUE of LENGTH and TYPE to SB.  Do the required quoting. */
     278             : static void
     279           0 : append_utf8_value (const unsigned char *value, size_t length,
     280             :                    struct stringbuf *sb)
     281             : {
     282             :   unsigned char tmp[6];
     283             :   const unsigned char *s;
     284             :   size_t n;
     285             :   int i, nmore;
     286             : 
     287           0 :   if (length && (*value == ' ' || *value == '#'))
     288             :     {
     289           0 :       tmp[0] = '\\';
     290           0 :       tmp[1] = *value;
     291           0 :       put_stringbuf_mem (sb, tmp, 2);
     292           0 :       value++;
     293           0 :       length--;
     294             :     }
     295           0 :   if (length && value[length-1] == ' ')
     296             :     {
     297           0 :       tmp[0] = '\\';
     298           0 :       tmp[1] = ' ';
     299           0 :       put_stringbuf_mem (sb, tmp, 2);
     300           0 :       length--;
     301             :     }
     302             : 
     303           0 :   for (s=value, n=0;;)
     304             :     {
     305           0 :       for (value = s; n < length && !(*s & 0x80); n++, s++)
     306             :         ;
     307           0 :       if (s != value)
     308           0 :         append_quoted (sb, value, s-value, 0);
     309           0 :       if (n==length)
     310           0 :         return; /* ready */
     311           0 :       if (!(*s & 0x80))
     312           0 :         nmore = 0; /* Not expected here: high bit not set.  */
     313           0 :       else if ( (*s & 0xe0) == 0xc0 ) /* 110x xxxx */
     314           0 :         nmore = 1;
     315           0 :       else if ( (*s & 0xf0) == 0xe0 ) /* 1110 xxxx */
     316           0 :         nmore = 2;
     317           0 :       else if ( (*s & 0xf8) == 0xf0 ) /* 1111 0xxx */
     318           0 :         nmore = 3;
     319           0 :       else if ( (*s & 0xfc) == 0xf8 ) /* 1111 10xx */
     320           0 :         nmore = 4;
     321           0 :       else if ( (*s & 0xfe) == 0xfc ) /* 1111 110x */
     322           0 :         nmore = 5;
     323             :       else /* Invalid encoding */
     324           0 :         nmore = 0;
     325             : 
     326           0 :       if (!nmore)
     327             :         {
     328             :           /* Encoding error:  We quote the bad byte.  */
     329           0 :           snprintf (tmp, sizeof tmp, "\\%02X", *s);
     330           0 :           put_stringbuf_mem (sb, tmp, 3);
     331           0 :           s++; n++;
     332             :         }
     333             :       else
     334             :         {
     335           0 :           tmp[0] = *s++; n++;
     336           0 :           for (i=1; n < length && i <= nmore; i++)
     337             :             {
     338           0 :               if ( (*s & 0xc0) != 0x80)
     339           0 :                 break; /* Invalid encoding - let the next cycle detect this. */
     340           0 :               tmp[i] = *s++;
     341           0 :               n++;
     342             :             }
     343           0 :           put_stringbuf_mem (sb, tmp, i);
     344             :         }
     345             :     }
     346             : }
     347             : 
     348             : /* Append VALUE of LENGTH and TYPE to SB.  Do character set conversion
     349             :    and quoting */
     350             : static void
     351           0 : append_latin1_value (const unsigned char *value, size_t length,
     352             :                      struct stringbuf *sb)
     353             : {
     354             :   unsigned char tmp[2];
     355             :   const unsigned char *s;
     356             :   size_t n;
     357             : 
     358           0 :   if (length && (*value == ' ' || *value == '#'))
     359             :     {
     360           0 :       tmp[0] = '\\';
     361           0 :       tmp[1] = *value;
     362           0 :       put_stringbuf_mem (sb, tmp, 2);
     363           0 :       value++;
     364           0 :       length--;
     365             :     }
     366           0 :   if (length && value[length-1] == ' ')
     367             :     {
     368           0 :       tmp[0] = '\\';
     369           0 :       tmp[1] = ' ';
     370           0 :       put_stringbuf_mem (sb, tmp, 2);
     371           0 :       length--;
     372             :     }
     373             : 
     374           0 :   for (s=value, n=0;;)
     375             :     {
     376           0 :       for (value = s; n < length && !(*s & 0x80); n++, s++)
     377             :         ;
     378           0 :       if (s != value)
     379           0 :         append_quoted (sb, value, s-value, 0);
     380           0 :       if (n==length)
     381           0 :         return; /* ready */
     382           0 :       assert ((*s & 0x80));
     383           0 :       tmp[0] = 0xc0 | ((*s >> 6) & 3);
     384           0 :       tmp[1] = 0x80 | ( *s & 0x3f );
     385           0 :       put_stringbuf_mem (sb, tmp, 2);
     386           0 :       n++; s++;
     387             :     }
     388             : }
     389             : 
     390             : /* Append VALUE of LENGTH and TYPE to SB.  Do UCS-4 to utf conversion
     391             :    and and quoting */
     392             : static void
     393           0 : append_ucs4_value (const unsigned char *value, size_t length,
     394             :                    struct stringbuf *sb)
     395             : {
     396             :   unsigned char tmp[7];
     397             :   const unsigned char *s;
     398             :   size_t n;
     399             :   unsigned int c;
     400             :   int i;
     401             : 
     402           0 :   if (length>3 && !value[0] && !value[1] && !value[2]
     403           0 :       && (value[3] == ' ' || value[3] == '#'))
     404             :     {
     405           0 :       tmp[0] = '\\';
     406           0 :       tmp[1] = *value;
     407           0 :       put_stringbuf_mem (sb, tmp, 2);
     408           0 :       value += 4;
     409           0 :       length -= 4;
     410             :     }
     411           0 :   if (length>3 && !value[0] && !value[1] && !value[2] && value[3] == ' ')
     412             :     {
     413           0 :       tmp[0] = '\\';
     414           0 :       tmp[1] = ' ';
     415           0 :       put_stringbuf_mem (sb, tmp, 2);
     416           0 :       length -= 4;
     417             :     }
     418             : 
     419           0 :   for (s=value, n=0;;)
     420             :     {
     421           0 :       for (value = s; n+3 < length
     422           0 :              && !s[0] && !s[1] && !s[2] && !(s[3] & 0x80); n += 4, s += 4)
     423             :         ;
     424           0 :       if (s != value)
     425           0 :         append_quoted (sb, value, s-value, 3);
     426           0 :       if (n>=length)
     427           0 :         return; /* ready */
     428           0 :       if (n < 4)
     429             :         { /* This is an invalid encoding - better stop after adding
     430             :              one impossible characater */
     431           0 :           put_stringbuf_mem (sb, "\xff", 1);
     432           0 :           return;
     433             :         }
     434           0 :       c = *s++ << 24;
     435           0 :       c |= *s++ << 16;
     436           0 :       c |= *s++ << 8;
     437           0 :       c |= *s++;
     438           0 :       n += 4;
     439           0 :       i=0;
     440           0 :       if (c < (1<<11))
     441             :         {
     442           0 :           tmp[i++] = 0xc0 | ( c >>  6);
     443           0 :           tmp[i++] = 0x80 | ( c        & 0x3f);
     444             :         }
     445           0 :       else if (c < (1<<16))
     446             :         {
     447           0 :           tmp[i++] = 0xe0 | ( c >> 12);
     448           0 :           tmp[i++] = 0x80 | ((c >>  6) & 0x3f);
     449           0 :           tmp[i++] = 0x80 | ( c        & 0x3f);
     450             :         }
     451           0 :       else if (c < (1<<21))
     452             :         {
     453           0 :           tmp[i++] = 0xf0 | ( c >> 18);
     454           0 :           tmp[i++] = 0x80 | ((c >> 12) & 0x3f);
     455           0 :           tmp[i++] = 0x80 | ((c >>  6) & 0x3f);
     456           0 :           tmp[i++] = 0x80 | ( c        & 0x3f);
     457             :         }
     458           0 :       else if (c < (1<<26))
     459             :         {
     460           0 :           tmp[i++] = 0xf8 | ( c >> 24);
     461           0 :           tmp[i++] = 0x80 | ((c >> 18) & 0x3f);
     462           0 :           tmp[i++] = 0x80 | ((c >> 12) & 0x3f);
     463           0 :           tmp[i++] = 0x80 | ((c >>  6) & 0x3f);
     464           0 :           tmp[i++] = 0x80 | ( c        & 0x3f);
     465             :         }
     466             :       else
     467             :         {
     468           0 :           tmp[i++] = 0xfc | ( c >> 30);
     469           0 :           tmp[i++] = 0x80 | ((c >> 24) & 0x3f);
     470           0 :           tmp[i++] = 0x80 | ((c >> 18) & 0x3f);
     471           0 :           tmp[i++] = 0x80 | ((c >> 12) & 0x3f);
     472           0 :           tmp[i++] = 0x80 | ((c >>  6) & 0x3f);
     473           0 :           tmp[i++] = 0x80 | ( c        & 0x3f);
     474             :         }
     475           0 :       put_stringbuf_mem (sb, tmp, i);
     476             :     }
     477             : }
     478             : 
     479             : /* Append VALUE of LENGTH and TYPE to SB.  Do UCS-2 to utf conversion
     480             :    and and quoting */
     481             : static void
     482           0 : append_ucs2_value (const unsigned char *value, size_t length,
     483             :                    struct stringbuf *sb)
     484             : {
     485             :   unsigned char tmp[3];
     486             :   const unsigned char *s;
     487             :   size_t n;
     488             :   unsigned int c;
     489             :   int i;
     490             : 
     491           0 :   if (length>1 && !value[0] && (value[1] == ' ' || value[1] == '#'))
     492             :     {
     493           0 :       tmp[0] = '\\';
     494           0 :       tmp[1] = *value;
     495           0 :       put_stringbuf_mem (sb, tmp, 2);
     496           0 :       value += 2;
     497           0 :       length -= 2;
     498             :     }
     499           0 :   if (length>1 && !value[0] && value[1] == ' ')
     500             :     {
     501           0 :       tmp[0] = '\\';
     502           0 :       tmp[1] = ' ';
     503           0 :       put_stringbuf_mem (sb, tmp, 2);
     504           0 :       length -=2;
     505             :     }
     506             : 
     507           0 :   for (s=value, n=0;;)
     508             :     {
     509           0 :       for (value = s; n+1 < length && !s[0] && !(s[1] & 0x80); n += 2, s += 2)
     510             :         ;
     511           0 :       if (s != value)
     512           0 :         append_quoted (sb, value, s-value, 1);
     513           0 :       if (n>=length)
     514           0 :         return; /* ready */
     515           0 :       if (n < 2)
     516             :         { /* This is an invalid encoding - better stop after adding
     517             :              one impossible characater */
     518           0 :           put_stringbuf_mem (sb, "\xff", 1);
     519           0 :           return;
     520             :         }
     521           0 :       c  = *s++ << 8;
     522           0 :       c |= *s++;
     523           0 :       n += 2;
     524           0 :       i=0;
     525           0 :       if (c < (1<<11))
     526             :         {
     527           0 :           tmp[i++] = 0xc0 | ( c >>  6);
     528           0 :           tmp[i++] = 0x80 | ( c        & 0x3f);
     529             :         }
     530             :       else
     531             :         {
     532           0 :           tmp[i++] = 0xe0 | ( c >> 12);
     533           0 :           tmp[i++] = 0x80 | ((c >>  6) & 0x3f);
     534           0 :           tmp[i++] = 0x80 | ( c        & 0x3f);
     535             :         }
     536           0 :       put_stringbuf_mem (sb, tmp, i);
     537             :     }
     538             : }
     539             : 
     540             : 
     541             : /* Append attribute and value.  ROOT is the sequence */
     542             : static gpg_error_t
     543           0 : append_atv (const unsigned char *image, AsnNode root, struct stringbuf *sb)
     544             : {
     545           0 :   AsnNode node = root->down;
     546             :   const char *name;
     547           0 :   int use_hex = 0;
     548             :   int i;
     549             : 
     550           0 :   if (!node || node->type != TYPE_OBJECT_ID)
     551           0 :     return gpg_error (GPG_ERR_UNEXPECTED_TAG);
     552           0 :   if (node->off == -1)
     553           0 :     return gpg_error (GPG_ERR_NO_VALUE); /* Hmmm, this might lead to misunderstandings */
     554             : 
     555           0 :   name = NULL;
     556           0 :   for (i=0; oid_name_tbl[i].name; i++)
     557             :     {
     558           0 :       if (oid_name_tbl[i].source == 1
     559           0 :           && node->len == oid_name_tbl[i].oidlen
     560           0 :           && !memcmp (image+node->off+node->nhdr,
     561           0 :                       oid_name_tbl[i].oid, node->len))
     562             :         {
     563           0 :           name = oid_name_tbl[i].name;
     564           0 :           break;
     565             :         }
     566             :     }
     567           0 :   if (name)
     568           0 :     put_stringbuf (sb, name);
     569             :   else
     570             :     { /* No name for the OID in the table; at least not DER encoded.
     571             :          Now convert the OID to a string, try to find it in the table
     572             :          again and use the string as last resort.  */
     573             :       char *p;
     574             : 
     575           0 :       p = ksba_oid_to_str (image+node->off+node->nhdr, node->len);
     576           0 :       if (!p)
     577           0 :         return gpg_error (GPG_ERR_ENOMEM);
     578             : 
     579           0 :       for (i=0; *p && oid_name_tbl[i].name; i++)
     580             :         {
     581           0 :           if (oid_name_tbl[i].source == 1
     582           0 :               && !strcmp (p, oid_name_tbl[i].oidstr))
     583             :             {
     584           0 :               name = oid_name_tbl[i].name;
     585           0 :               break;
     586             :             }
     587             :         }
     588           0 :       if (name)
     589           0 :         put_stringbuf (sb, name);
     590             :       else
     591             :         {
     592           0 :           put_stringbuf (sb, p);
     593           0 :           use_hex = 1;
     594             :         }
     595           0 :       xfree (p);
     596             :     }
     597           0 :   put_stringbuf (sb, "=");
     598           0 :   node = node->right;
     599           0 :   if (!node || node->off == -1)
     600           0 :     return gpg_error (GPG_ERR_NO_VALUE);
     601             : 
     602           0 :   switch (use_hex? 0 : node->type)
     603             :     {
     604             :     case TYPE_UTF8_STRING:
     605           0 :       append_utf8_value (image+node->off+node->nhdr, node->len, sb);
     606           0 :       break;
     607             :     case TYPE_PRINTABLE_STRING:
     608             :     case TYPE_IA5_STRING:
     609             :       /* we assume that wrong encodings are latin-1 */
     610             :     case TYPE_TELETEX_STRING: /* Not correct, but mostly used as latin-1 */
     611           0 :       append_latin1_value (image+node->off+node->nhdr, node->len, sb);
     612           0 :       break;
     613             : 
     614             :     case TYPE_UNIVERSAL_STRING:
     615           0 :       append_ucs4_value (image+node->off+node->nhdr, node->len, sb);
     616           0 :       break;
     617             : 
     618             :     case TYPE_BMP_STRING:
     619           0 :       append_ucs2_value (image+node->off+node->nhdr, node->len, sb);
     620           0 :       break;
     621             : 
     622             :     case 0: /* forced usage of hex */
     623             :     default:
     624           0 :       put_stringbuf (sb, "#");
     625           0 :       for (i=0; i < node->len; i++)
     626             :         {
     627             :           char tmp[3];
     628           0 :           snprintf (tmp, sizeof tmp, "%02X", image[node->off+node->nhdr+i]);
     629           0 :           put_stringbuf (sb, tmp);
     630             :         }
     631           0 :       break;
     632             :     }
     633             : 
     634           0 :   return 0;
     635             : }
     636             : 
     637             : static gpg_error_t
     638           0 : dn_to_str (const unsigned char *image, AsnNode root, struct stringbuf *sb)
     639             : {
     640             :   gpg_error_t err;
     641             :   AsnNode nset;
     642             : 
     643           0 :   if (!root )
     644           0 :     return 0; /* empty DN */
     645           0 :   nset = root->down;
     646           0 :   if (!nset)
     647           0 :     return 0; /* consider this as empty */
     648           0 :   if (nset->type != TYPE_SET_OF)
     649           0 :     return gpg_error (GPG_ERR_UNEXPECTED_TAG);
     650             : 
     651             :   /* output in reverse order */
     652           0 :   while (nset->right)
     653           0 :     nset = nset->right;
     654             : 
     655             :   for (;;)
     656           0 :     {
     657             :       AsnNode nseq;
     658             : 
     659           0 :       if (nset->type != TYPE_SET_OF)
     660           0 :         return gpg_error (GPG_ERR_UNEXPECTED_TAG);
     661           0 :       for (nseq = nset->down; nseq; nseq = nseq->right)
     662             :         {
     663           0 :           if (nseq->type != TYPE_SEQUENCE)
     664           0 :             return gpg_error (GPG_ERR_UNEXPECTED_TAG);
     665           0 :           if (nseq != nset->down)
     666           0 :             put_stringbuf (sb, "+");
     667           0 :           err = append_atv (image, nseq, sb);
     668           0 :           if (err)
     669           0 :             return err;
     670             :         }
     671           0 :       if (nset == root->down)
     672           0 :         break;
     673           0 :       put_stringbuf (sb, ",");
     674           0 :       nset = nset->left;
     675             :     }
     676             : 
     677           0 :   return 0;
     678             : }
     679             : 
     680             : 
     681             : gpg_error_t
     682           0 : _ksba_dn_to_str (const unsigned char *image, AsnNode node, char **r_string)
     683             : {
     684             :   gpg_error_t err;
     685             :   struct stringbuf sb;
     686             : 
     687           0 :   *r_string = NULL;
     688           0 :   if (!node || node->type != TYPE_SEQUENCE_OF)
     689           0 :     return gpg_error (GPG_ERR_INV_VALUE);
     690             : 
     691           0 :   init_stringbuf (&sb, 100);
     692           0 :   err = dn_to_str (image, node, &sb);
     693           0 :   if (!err)
     694             :     {
     695           0 :       *r_string = get_stringbuf (&sb);
     696           0 :       if (!*r_string)
     697           0 :         err = gpg_error (GPG_ERR_ENOMEM);
     698             :     }
     699           0 :   deinit_stringbuf (&sb);
     700             : 
     701           0 :   return err;
     702             : }
     703             : 
     704             : 
     705             : /* Create a new decoder and run it for the given element */
     706             : /* Fixme: this code is duplicated from cms-parser.c */
     707             : static gpg_error_t
     708           0 : create_and_run_decoder (ksba_reader_t reader, const char *elem_name,
     709             :                         AsnNode *r_root,
     710             :                         unsigned char **r_image, size_t *r_imagelen)
     711             : {
     712             :   gpg_error_t err;
     713             :   ksba_asn_tree_t crl_tree;
     714             :   BerDecoder decoder;
     715             : 
     716           0 :   err = ksba_asn_create_tree ("tmttv2", &crl_tree);
     717           0 :   if (err)
     718           0 :     return err;
     719             : 
     720           0 :   decoder = _ksba_ber_decoder_new ();
     721           0 :   if (!decoder)
     722             :     {
     723           0 :       ksba_asn_tree_release (crl_tree);
     724           0 :       return gpg_error (GPG_ERR_ENOMEM);
     725             :     }
     726             : 
     727           0 :   err = _ksba_ber_decoder_set_reader (decoder, reader);
     728           0 :   if (err)
     729             :     {
     730           0 :       ksba_asn_tree_release (crl_tree);
     731           0 :       _ksba_ber_decoder_release (decoder);
     732           0 :       return err;
     733             :     }
     734             : 
     735           0 :   err = _ksba_ber_decoder_set_module (decoder, crl_tree);
     736           0 :   if (err)
     737             :     {
     738           0 :       ksba_asn_tree_release (crl_tree);
     739           0 :       _ksba_ber_decoder_release (decoder);
     740           0 :       return err;
     741             :     }
     742             : 
     743           0 :   err = _ksba_ber_decoder_decode (decoder, elem_name, 0,
     744             :                                   r_root, r_image, r_imagelen);
     745             : 
     746           0 :   _ksba_ber_decoder_release (decoder);
     747           0 :   ksba_asn_tree_release (crl_tree);
     748           0 :   return err;
     749             : }
     750             : 
     751             : 
     752             : gpg_error_t
     753           0 : _ksba_derdn_to_str (const unsigned char *der, size_t derlen, char **r_string)
     754             : {
     755             :   gpg_error_t err;
     756             :   AsnNode root;
     757             :   unsigned char *image;
     758             :   size_t imagelen;
     759             :   ksba_reader_t reader;
     760             : 
     761           0 :   err = ksba_reader_new (&reader);
     762           0 :   if (err)
     763           0 :     return err;
     764           0 :   err = ksba_reader_set_mem (reader, der, derlen);
     765           0 :   if (err)
     766             :     {
     767           0 :       ksba_reader_release (reader);
     768           0 :       return err;
     769             :     }
     770           0 :   err = create_and_run_decoder (reader,
     771             :                                 "TMTTv2.CertificateList.tbsCertList.issuer",
     772             :                                 &root, &image, &imagelen);
     773           0 :   ksba_reader_release (reader);
     774           0 :   if (!err)
     775             :     {
     776           0 :       err = _ksba_dn_to_str (image, root->down, r_string);
     777           0 :       _ksba_asn_release_nodes (root);
     778           0 :       xfree (image);
     779             :     }
     780           0 :   return err;
     781             : }
     782             : 
     783             : 
     784             : /*
     785             :    Convert a string back to DN
     786             : */
     787             : 
     788             : /* Count the number of bytes in a quoted string, return a pointer to
     789             :    the character after the string or NULL in case of an paring error.
     790             :    The number of bytes needed to store the string verbatim will be
     791             :    return as RESULT.  With V2COMAP true, the string is assumed to be
     792             :    in v2 quoting (but w/o the leading quote character)
     793             :  */
     794             : static const char *
     795           0 : count_quoted_string (const char *string, size_t *result,
     796             :                      int v2compat, int *stringtype)
     797             : {
     798             :   const unsigned char *s;
     799           0 :   int nbytes = 0;
     800           0 :   int highbit = 0;
     801           0 :   int nonprint = 0;
     802           0 :   int atsign = 0;
     803             : 
     804           0 :   *stringtype = 0;
     805           0 :   for (s=string; *s; s++)
     806             :     {
     807           0 :       if (*s == '\\')
     808             :         { /* pair */
     809           0 :           s++;
     810           0 :           if (*s == ',' || *s == '=' || *s == '+'
     811           0 :               || *s == '<' || *s == '>' || *s == '#' || *s == ';'
     812           0 :               || *s == '\\' || *s == '\"' || *s == ' ')
     813             :             {
     814           0 :               if (!charclasses[*s])
     815           0 :                 nonprint = 1;
     816           0 :               nbytes++;
     817             :             }
     818           0 :           else if (hexdigitp (s) && hexdigitp (s+1))
     819           0 :             {
     820           0 :               int c = xtoi_2 (s);
     821           0 :               if ((c & 0x80))
     822           0 :                 highbit = 1;
     823           0 :               else if (c == '@')
     824           0 :                 atsign = 1;
     825           0 :               else if (!charclasses[c])
     826           0 :                 nonprint = 1;
     827             : 
     828           0 :               s++;
     829           0 :               nbytes++;
     830             :             }
     831             :           else
     832           0 :             return NULL; /* invalid escape sequence */
     833             :         }
     834           0 :       else if (*s == '\"')
     835             :         {
     836           0 :           if (v2compat)
     837           0 :             break;
     838           0 :           return NULL; /* invalid encoding */
     839             :         }
     840           0 :       else if (!v2compat
     841           0 :                && (*s == ',' || *s == '=' || *s == '+'
     842           0 :                    || *s == '<' || *s == '>' || *s == '#' || *s == ';') )
     843             :         {
     844             :           break;
     845             :         }
     846             :       else
     847             :         {
     848           0 :           nbytes++;
     849           0 :           if ((*s & 0x80))
     850           0 :             highbit = 1;
     851           0 :           else if (*s == '@')
     852           0 :             atsign = 1;
     853           0 :           else if (!charclasses[*s])
     854           0 :             nonprint = 1;
     855             :         }
     856             :     }
     857             : 
     858             :   /* Fixme: Should be remove spaces or white spces from the end unless
     859             :      they are not escaped or we are in v2compat mode?  See TODO */
     860             : 
     861           0 :   if (highbit || nonprint)
     862           0 :     *stringtype = TYPE_UTF8_STRING;
     863           0 :   else if (atsign)
     864           0 :     *stringtype = TYPE_IA5_STRING;
     865             :   else
     866           0 :     *stringtype = TYPE_PRINTABLE_STRING;
     867             : 
     868           0 :   *result = nbytes;
     869           0 :   return s;
     870             : }
     871             : 
     872             : 
     873             : /* Write out the data to W and do the required escaping.  Note that
     874             :    NBYTES is the number of bytes actually to be written, i.e. it is
     875             :    the result from count_quoted_string */
     876             : static gpg_error_t
     877           0 : write_escaped (ksba_writer_t w, const unsigned char *buffer, size_t nbytes)
     878             : {
     879             :   const unsigned char *s;
     880             :   gpg_error_t err;
     881             : 
     882           0 :   for (s=buffer; nbytes; s++)
     883             :     {
     884           0 :       if (*s == '\\')
     885             :         {
     886           0 :           s++;
     887           0 :           if (hexdigitp (s) && hexdigitp (s+1))
     888           0 :             {
     889           0 :               unsigned char buf = xtoi_2 (s);
     890           0 :               err = ksba_writer_write (w, &buf, 1);
     891           0 :               if (err)
     892           0 :                 return err;
     893           0 :               s++;
     894           0 :               nbytes--;
     895             :             }
     896             :           else
     897             :             {
     898           0 :               err = ksba_writer_write (w, s, 1);
     899           0 :               if (err)
     900           0 :                 return err;
     901           0 :               nbytes--;
     902             :             }
     903             :         }
     904             :       else
     905             :         {
     906           0 :           err = ksba_writer_write (w, s, 1);
     907           0 :           if (err)
     908           0 :             return err;
     909           0 :           nbytes--;
     910             :         }
     911             :     }
     912             : 
     913           0 :   return 0;
     914             : }
     915             : 
     916             : 
     917             : /* Parse one RDN, and write it to WRITER.  Returns a pointer to the
     918             :    next RDN part where the comma has already been skipped or NULL in
     919             :    case of an error.  When NULL is passed as WRITER, the function does
     920             :    not allocate any memory but just parses the string and returns the
     921             :    ENDP. If ROFF or RLEN are not NULL, they will receive informaion
     922             :    useful for error reporting. */
     923             : static gpg_error_t
     924           0 : parse_rdn (const unsigned char *string, const char **endp,
     925             :            ksba_writer_t writer, size_t *roff, size_t *rlen)
     926             : {
     927           0 :   const unsigned char *orig_string = string;
     928             :   const unsigned char *s, *s1;
     929             :   size_t n, n1;
     930             :   int i;
     931             :   unsigned char *p;
     932           0 :   unsigned char *oidbuf = NULL;
     933           0 :   unsigned char *valuebuf = NULL;
     934           0 :   const unsigned char *oid = NULL;
     935             :   size_t oidlen;
     936           0 :   const unsigned char *value = NULL;
     937             :   int valuelen;
     938             :   int valuetype;
     939           0 :   int need_escaping = 0;
     940           0 :   gpg_error_t err = 0;
     941             :   size_t dummy_roff, dummy_rlen;
     942             : 
     943           0 :   if (!roff)
     944           0 :     roff = &dummy_roff;
     945           0 :   if (!rlen)
     946           0 :     rlen = &dummy_rlen;
     947             : 
     948           0 :   *roff = *rlen = 0;
     949             : 
     950           0 :   if (!string)
     951           0 :     return gpg_error (GPG_ERR_INV_VALUE);
     952           0 :   while (*string == ' ')
     953           0 :     string++;
     954           0 :   *roff = string - orig_string;
     955           0 :   if (!*string)
     956           0 :     return gpg_error (GPG_ERR_SYNTAX); /* empty elements are not allowed */
     957           0 :   s = string;
     958             : 
     959           0 :   if ( ((*s == 'o' && s[1] == 'i' && s[2] == 'd')
     960           0 :         ||(*s == 'O' && s[1] == 'I' && s[2] == 'D'))
     961           0 :        && s[3] == '.' && digitp (s+4))
     962           0 :     s += 3; /* skip a prefixed oid */
     963             : 
     964             :   /* parse attributeType */
     965           0 :   string = s;
     966           0 :   *roff = string - orig_string;
     967           0 :   if (digitp (s))
     968             :     { /* oid */
     969           0 :       for (s++; digitp (s) || (*s == '.' && s[1] != '.'); s++)
     970             :         ;
     971           0 :       n = s - string;
     972           0 :       while (*s == ' ')
     973           0 :         s++;
     974           0 :       if (*s != '=')
     975           0 :         return gpg_error (GPG_ERR_SYNTAX);
     976             : 
     977           0 :       if (writer)
     978             :         {
     979           0 :           p = xtrymalloc (n+1);
     980           0 :           if (!p)
     981           0 :             return gpg_error (GPG_ERR_ENOMEM);
     982           0 :           memcpy (p, string, n);
     983           0 :           p[n] = 0;
     984           0 :           err = ksba_oid_from_str (p, &oidbuf, &oidlen);
     985           0 :           xfree (p);
     986           0 :           if (err)
     987           0 :             return err;
     988           0 :           oid = oidbuf;
     989             :         }
     990             :     }
     991           0 :   else if ((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z') )
     992             :     { /* name */
     993           0 :       for (s++; ((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z')
     994           0 :                  || digitp (s) || *s == '-'); s++)
     995             :         ;
     996           0 :       n = s - string;
     997           0 :       while (*s == ' ')
     998           0 :         s++;
     999           0 :       if (*s != '=')
    1000           0 :         return gpg_error (GPG_ERR_SYNTAX);
    1001             : 
    1002           0 :       for (i=0; oid_name_tbl[i].name; i++)
    1003             :         {
    1004           0 :           if ( n == strlen (oid_name_tbl[i].name)
    1005           0 :                && !ascii_memcasecmp (string, oid_name_tbl[i].name, n))
    1006           0 :             break;
    1007             :         }
    1008           0 :       if (!oid_name_tbl[i].name)
    1009             :         {
    1010           0 :           *roff = string - orig_string;
    1011           0 :           *rlen = n;
    1012           0 :           return gpg_error (GPG_ERR_UNKNOWN_NAME);
    1013             :         }
    1014           0 :       oid = oid_name_tbl[i].oid;
    1015           0 :       oidlen = oid_name_tbl[i].oidlen;
    1016             :     }
    1017             :   else
    1018           0 :     return gpg_error (GPG_ERR_INV_NAME);
    1019             : 
    1020           0 :   s++;
    1021           0 :   while (*s == ' ')
    1022           0 :     s++;
    1023           0 :   string = s;
    1024             : 
    1025           0 :   *roff = string - orig_string;
    1026             : 
    1027             :   /* parse attributeValue */
    1028           0 :   if (!*s)
    1029             :     {
    1030           0 :       err = gpg_error (GPG_ERR_SYNTAX); /* missing value */
    1031           0 :       goto leave;
    1032             :     }
    1033             : 
    1034           0 :   if (*s == '#')
    1035             :     { /* hexstring */
    1036           0 :       int need_utf8 = 0;
    1037           0 :       int need_ia5 = 0;
    1038             : 
    1039           0 :       string = ++s;
    1040           0 :       for (; hexdigitp (s); s++)
    1041           0 :         s++;
    1042           0 :       n = s - string;
    1043           0 :       if (!n || (n & 1))
    1044             :         {
    1045           0 :           *rlen = n;
    1046           0 :           err = gpg_error (GPG_ERR_SYNTAX); /* no hex digits or odd number */
    1047           0 :           goto leave;
    1048             :         }
    1049           0 :       while (*s == ' ')
    1050           0 :         s++;
    1051           0 :       n /= 2;
    1052           0 :       valuelen = n;
    1053           0 :       if (writer)
    1054             :         {
    1055           0 :           valuebuf = xtrymalloc (valuelen);
    1056           0 :           if (!valuebuf)
    1057             :             {
    1058           0 :               err = gpg_error (GPG_ERR_ENOMEM);
    1059           0 :               goto leave;
    1060             :             }
    1061           0 :           for (p=valuebuf, s1=string; n; p++, s1 += 2, n--)
    1062             :             {
    1063           0 :               *p = xtoi_2 (s1);
    1064           0 :               if (*p == '@')
    1065           0 :                 need_ia5 = 1;
    1066           0 :               else if ((*p & 0x80) || !charclasses[*p])
    1067           0 :                 need_utf8 = 1;
    1068             :             }
    1069           0 :           value = valuebuf;
    1070             :         }
    1071             :       else
    1072             :         {
    1073           0 :           for (s1=string; n; s1 += 2, n--)
    1074             :             {
    1075             :               unsigned int c;
    1076             : 
    1077           0 :               c = xtoi_2 (s1);
    1078           0 :               if (c == '@')
    1079           0 :                 need_ia5 = 1;
    1080           0 :               else if ((c & 0x80) || !charclasses[c])
    1081           0 :                 need_utf8 = 1;
    1082             :             }
    1083             :         }
    1084           0 :       valuetype = need_utf8? TYPE_UTF8_STRING :
    1085           0 :                   need_ia5 ? TYPE_IA5_STRING : TYPE_PRINTABLE_STRING;
    1086             :     }
    1087           0 :   else if (*s == '\"')
    1088             :     { /* old style quotation */
    1089           0 :       string = s+1;
    1090           0 :       s = count_quoted_string (string, &n, 1, &valuetype);
    1091           0 :       if (!s || *s != '\"')
    1092             :         {
    1093           0 :           *rlen = s - orig_string;
    1094           0 :           err = gpg_error (GPG_ERR_SYNTAX); /* error or quote not closed */
    1095           0 :           goto leave;
    1096             :         }
    1097           0 :       s++;
    1098           0 :       while (*s == ' ')
    1099           0 :         s++;
    1100           0 :       value = string;
    1101           0 :       valuelen = n;
    1102           0 :       need_escaping = 1;
    1103             :     }
    1104             :   else
    1105             :     { /* regular v3 quoted string */
    1106           0 :       s = count_quoted_string (string, &n, 0, &valuetype);
    1107           0 :       if (!s)
    1108             :         {
    1109           0 :           err = gpg_error (GPG_ERR_SYNTAX); /* error */
    1110           0 :           goto leave;
    1111             :         }
    1112           0 :       while (*s == ' ')
    1113           0 :         s++;
    1114           0 :       value = string;
    1115           0 :       valuelen = n;
    1116           0 :       need_escaping = 1;
    1117             :     }
    1118             : 
    1119           0 :   if (!valuelen)
    1120             :     {
    1121           0 :       err = gpg_error (GPG_ERR_SYNTAX); /* empty elements are not allowed */
    1122           0 :       goto leave;
    1123             :     }
    1124           0 :   if ( *s && *s != ',' && *s != ';' && *s != '+')
    1125             :     {
    1126           0 :       *roff = s - orig_string;
    1127           0 :       err = gpg_error (GPG_ERR_SYNTAX); /* invalid delimiter */
    1128           0 :       goto leave;
    1129             :     }
    1130           0 :   if (*s == '+') /* fixme: implement this */
    1131             :     {
    1132           0 :       *roff = s - orig_string;
    1133           0 :       *rlen = 1;
    1134           0 :       err = gpg_error (GPG_ERR_NOT_IMPLEMENTED);
    1135           0 :       goto leave;
    1136             :     }
    1137           0 :   *endp = *s? (s+1):s;
    1138             : 
    1139           0 :   if (writer)
    1140             :     { /* write out the data */
    1141             : 
    1142             :       /* need to calculate the length in advance */
    1143           0 :       n1  = _ksba_ber_count_tl (TYPE_OBJECT_ID, CLASS_UNIVERSAL, 0, oidlen);
    1144           0 :       n1 += oidlen;
    1145           0 :       n1 += _ksba_ber_count_tl (valuetype, CLASS_UNIVERSAL, 0, valuelen);
    1146           0 :       n1 += valuelen;
    1147             : 
    1148             :       /* The SET tag */
    1149           0 :       n  = _ksba_ber_count_tl (TYPE_SET, CLASS_UNIVERSAL, 1, n);
    1150           0 :       n += n1;
    1151           0 :       err = _ksba_ber_write_tl (writer, TYPE_SET, CLASS_UNIVERSAL, 1, n);
    1152             : 
    1153             :       /* The sequence tag */
    1154           0 :       n = n1;
    1155           0 :       err = _ksba_ber_write_tl (writer, TYPE_SEQUENCE, CLASS_UNIVERSAL, 1, n);
    1156             : 
    1157             :       /* the OBJECT ID */
    1158           0 :       err = _ksba_ber_write_tl (writer, TYPE_OBJECT_ID, CLASS_UNIVERSAL,
    1159             :                                 0, oidlen);
    1160           0 :       if (!err)
    1161           0 :         err = ksba_writer_write (writer, oid, oidlen);
    1162           0 :       if (err)
    1163           0 :         goto leave;
    1164             : 
    1165             :       /* the value.  Note that we don't need any conversion to the target
    1166             :          characters set because the input is expected to be utf8 and the
    1167             :          target type is either utf8, IA5 or printable string where the last
    1168             :          two are subsets of utf8 */
    1169           0 :       err = _ksba_ber_write_tl (writer, valuetype,
    1170             :                                 CLASS_UNIVERSAL, 0, valuelen);
    1171           0 :       if (!err)
    1172           0 :         err = need_escaping? write_escaped (writer, value, valuelen)
    1173           0 :           : ksba_writer_write (writer, value, valuelen);
    1174             :     }
    1175             : 
    1176             :  leave:
    1177           0 :   xfree (oidbuf);
    1178           0 :   xfree (valuebuf);
    1179           0 :   return err;
    1180             : }
    1181             : 
    1182             : 
    1183             : 
    1184             : gpg_error_t
    1185           0 : _ksba_dn_from_str (const char *string, char **rbuf, size_t *rlength)
    1186             : {
    1187             :   gpg_error_t err;
    1188             :   ksba_writer_t writer;
    1189             :   const char *s, *endp;
    1190           0 :   void *buf = NULL;
    1191             :   size_t buflen;
    1192           0 :   char const **part_array = NULL;
    1193             :   int part_array_size, nparts;
    1194             : 
    1195           0 :   *rbuf = NULL; *rlength = 0;
    1196             :   /* We are going to build the object using a writer object.  */
    1197           0 :   err = ksba_writer_new (&writer);
    1198           0 :   if (!err)
    1199           0 :     err = ksba_writer_set_mem (writer, 1024);
    1200           0 :   if (err)
    1201           0 :     return err;
    1202             : 
    1203             :   /* We must assign it in reverse order so we do it in 2 passes. */
    1204           0 :   part_array_size = 0;
    1205           0 :   for (nparts=0, s=string; s && *s;)
    1206             :     {
    1207           0 :       err = parse_rdn (s, &endp, NULL, NULL, NULL);
    1208           0 :       if (err)
    1209           0 :         goto leave;
    1210           0 :       if (nparts >= part_array_size)
    1211             :         {
    1212             :           char const **tmp;
    1213             : 
    1214           0 :           part_array_size += 2;
    1215           0 :           tmp = part_array_size? xtryrealloc (part_array,
    1216             :                                               part_array_size * sizeof *tmp)
    1217           0 :                                 : xtrymalloc (part_array_size * sizeof *tmp);
    1218           0 :           if (!tmp)
    1219             :             {
    1220           0 :               err = gpg_error (GPG_ERR_ENOMEM);
    1221           0 :               goto leave;
    1222             :             }
    1223           0 :           part_array = tmp;
    1224             :         }
    1225           0 :       part_array[nparts++] = s;
    1226           0 :       s = endp;
    1227             :     }
    1228           0 :   if (!nparts)
    1229             :     {
    1230           0 :       err = gpg_error (GPG_ERR_SYNTAX);
    1231           0 :       goto leave;
    1232             :     }
    1233             : 
    1234           0 :   while (--nparts >= 0)
    1235             :     {
    1236           0 :       err = parse_rdn (part_array[nparts], &endp, writer, NULL, NULL);
    1237           0 :       if (err)
    1238           0 :         goto leave;
    1239             :     }
    1240             : 
    1241             :   /* Now get the memory.  */
    1242           0 :   buf = ksba_writer_snatch_mem (writer, &buflen);
    1243           0 :   if (!buf)
    1244             :     {
    1245           0 :       err = gpg_error (GPG_ERR_ENOMEM);
    1246           0 :       goto leave;
    1247             :     }
    1248             :   /* Reinitialize the buffer to create the outer sequence.  */
    1249           0 :   err = ksba_writer_set_mem (writer, buflen + 10);
    1250           0 :   if (err)
    1251           0 :     goto leave;
    1252             : 
    1253             :   /* write the outer sequence */
    1254           0 :   err = _ksba_ber_write_tl (writer, TYPE_SEQUENCE,
    1255             :                             CLASS_UNIVERSAL, 1, buflen);
    1256           0 :   if (err)
    1257           0 :     goto leave;
    1258             :   /* write the collected sets */
    1259           0 :   err = ksba_writer_write (writer, buf, buflen);
    1260           0 :   if (err)
    1261           0 :     goto leave;
    1262             : 
    1263             :   /* and get the result */
    1264           0 :   *rbuf = ksba_writer_snatch_mem (writer, rlength);
    1265           0 :   if (!*rbuf)
    1266             :     {
    1267           0 :       err = gpg_error (GPG_ERR_ENOMEM);
    1268           0 :       goto leave;
    1269             :     }
    1270             : 
    1271             :  leave:
    1272           0 :   xfree (part_array);
    1273           0 :   ksba_writer_release (writer);
    1274           0 :   xfree (buf);
    1275           0 :   return err;
    1276             : }
    1277             : 
    1278             : 
    1279             : 
    1280             : gpg_error_t
    1281           0 : ksba_dn_der2str (const void *der, size_t derlen, char **rstring)
    1282             : {
    1283           0 :   return _ksba_derdn_to_str (der, derlen, rstring);
    1284             : }
    1285             : 
    1286             : 
    1287             : gpg_error_t
    1288           0 : ksba_dn_str2der (const char *string, unsigned char **rder, size_t *rderlen)
    1289             : {
    1290           0 :   return _ksba_dn_from_str (string, (char**)rder, rderlen);
    1291             : }
    1292             : 
    1293             : 
    1294             : 
    1295             : /* Assuming that STRING contains an rfc2253 encoded string, test
    1296             :    whether this string may be passed as a valid DN to libksba.  On
    1297             :    success the functions returns 0.  On error the function returns an
    1298             :    error code and stores the offset within STRING of the erroneous
    1299             :    part at RERROFF. RERRLEN will then receive the length of the
    1300             :    erroneous part.  This function is most useful to test whether a
    1301             :    symbolic name (like SN) is supported. SEQ should be passed as 0 for
    1302             :    now.  RERROFF and RERRLEN may be passed as NULL if the caller is
    1303             :    not interested at this value. */
    1304             : gpg_error_t
    1305           0 : ksba_dn_teststr (const char *string, int seq,
    1306             :                  size_t *rerroff, size_t *rerrlen)
    1307             : {
    1308             :   size_t dummy_erroff, dummy_errlen;
    1309             :   gpg_error_t err;
    1310             :   int nparts;
    1311             :   const char *s, *endp;
    1312             :   size_t off, len;
    1313             : 
    1314           0 :   if (!rerroff)
    1315           0 :     rerroff = &dummy_erroff;
    1316           0 :   if (!rerrlen)
    1317           0 :     rerrlen = &dummy_errlen;
    1318             : 
    1319           0 :   *rerrlen = *rerroff = 0;
    1320             : 
    1321           0 :   for (nparts=0, s=string; s && *s; nparts++)
    1322             :     {
    1323           0 :       err = parse_rdn (s, &endp, NULL, &off, &len);
    1324           0 :       if (err && !seq--)
    1325             :         {
    1326           0 :           *rerroff = s - string + off;
    1327           0 :           *rerrlen = len? len : strlen (s);
    1328           0 :           return err;
    1329             :         }
    1330           0 :       s = endp;
    1331             :     }
    1332           0 :   if (!nparts)
    1333           0 :     return gpg_error (GPG_ERR_SYNTAX);
    1334           0 :   return 0;
    1335             : }

Generated by: LCOV version 1.12