| 1 |
/* oid.c - Object identifier helper functions |
| 2 |
* Copyright (C) 2001, 2009, 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 |
#include <config.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <assert.h> |
| 36 |
|
| 37 |
#include "util.h" |
| 38 |
#include "asn1-func.h" |
| 39 |
#include "convert.h" |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
/** |
| 44 |
* ksba_oid_to_str: |
| 45 |
* @buffer: A BER encoded OID |
| 46 |
* @length: The length of this OID |
| 47 |
* |
| 48 |
* Take a buffer with an object identifier in BER encoding and return |
| 49 |
* a string representing this OID. We do not use the ASN.1 syntax |
| 50 |
* here but delimit the arcs with dots, so it is easier to parse in |
| 51 |
* most cases. This dotted-decimal notation is also known as LDAPOID |
| 52 |
* string and described in RFC-2251. |
| 53 |
* |
| 54 |
* The function returns an empty string for an empty buffer and does |
| 55 |
* no interpretation of the OID. The caller must free the returned |
| 56 |
* string using ksba_free() or the function he has registered as a |
| 57 |
* replacement. |
| 58 |
* |
| 59 |
* |
| 60 |
* Return value: A allocated string or NULL in case of memory problem. |
| 61 |
**/ |
| 62 |
char * |
| 63 |
ksba_oid_to_str (const char *buffer, size_t length) |
| 64 |
{ |
| 65 |
const unsigned char *buf = buffer; |
| 66 |
char *string, *p; |
| 67 |
int n = 0; |
| 68 |
unsigned long val, valmask; |
| 69 |
|
| 70 |
valmask = (unsigned long)0xfe << (8 * (sizeof (valmask) - 1)); |
| 71 |
|
| 72 |
/* To calculate the length of the string we can safely assume an |
| 73 |
upper limit of 3 decimal characters per byte. Two extra bytes |
| 74 |
account for the special first octect */ |
| 75 |
string = p = xtrymalloc (length*(1+3)+2+1); |
| 76 |
if (!string) |
| 77 |
return NULL; |
| 78 |
if (!length) |
| 79 |
{ |
| 80 |
*p = 0; |
| 81 |
return string; |
| 82 |
} |
| 83 |
|
| 84 |
if (buf[0] < 40) |
| 85 |
p += sprintf (p, "0.%d", buf[n]); |
| 86 |
else if (buf[0] < 80) |
| 87 |
p += sprintf (p, "1.%d", buf[n]-40); |
| 88 |
else { |
| 89 |
val = buf[n] & 0x7f; |
| 90 |
while ( (buf[n]&0x80) && ++n < length ) |
| 91 |
{ |
| 92 |
if ( (val & valmask) ) |
| 93 |
goto badoid; /* Overflow. */ |
| 94 |
val <<= 7; |
| 95 |
val |= buf[n] & 0x7f; |
| 96 |
} |
| 97 |
if (val < 80) |
| 98 |
goto badoid; |
| 99 |
val -= 80; |
| 100 |
sprintf (p, "2.%lu", val); |
| 101 |
p += strlen (p); |
| 102 |
} |
| 103 |
for (n++; n < length; n++) |
| 104 |
{ |
| 105 |
val = buf[n] & 0x7f; |
| 106 |
while ( (buf[n]&0x80) && ++n < length ) |
| 107 |
{ |
| 108 |
if ( (val & valmask) ) |
| 109 |
goto badoid; /* Overflow. */ |
| 110 |
val <<= 7; |
| 111 |
val |= buf[n] & 0x7f; |
| 112 |
} |
| 113 |
sprintf (p, ".%lu", val); |
| 114 |
p += strlen (p); |
| 115 |
} |
| 116 |
|
| 117 |
*p = 0; |
| 118 |
return string; |
| 119 |
|
| 120 |
badoid: |
| 121 |
/* Return a special OID (gnu.gnupg.badoid) to indicate the error |
| 122 |
case. The OID is broken and thus we return one which can't do |
| 123 |
any harm. Formally this does not need to be a bad OID but an OID |
| 124 |
with an arc that can't be represented in a 32 bit word is more |
| 125 |
than likely corrupt. */ |
| 126 |
xfree (string); |
| 127 |
return xtrystrdup ("1.3.6.1.4.1.11591.2.12242973"); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
/* Take the OID at NODE and return it in string format */ |
| 132 |
char * |
| 133 |
_ksba_oid_node_to_str (const unsigned char *image, AsnNode node) |
| 134 |
{ |
| 135 |
if (!node || node->type != TYPE_OBJECT_ID || node->off == -1) |
| 136 |
return NULL; |
| 137 |
return ksba_oid_to_str (image+node->off + node->nhdr, node->len); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
static size_t |
| 143 |
make_flagged_int (unsigned long value, char *buf, size_t buflen) |
| 144 |
{ |
| 145 |
int more = 0; |
| 146 |
int shift; |
| 147 |
|
| 148 |
/* fixme: figure out the number of bits in an ulong and start with |
| 149 |
that value as shift (after making it a multiple of 7) a more |
| 150 |
straigtforward implementation is to do it in reverse order using |
| 151 |
a temporary buffer - saves a lot of compares */ |
| 152 |
for (more=0, shift=28; shift > 0; shift -= 7) |
| 153 |
{ |
| 154 |
if (more || value >= (1<<shift)) |
| 155 |
{ |
| 156 |
buf[buflen++] = 0x80 | (value >> shift); |
| 157 |
value -= (value >> shift) << shift; |
| 158 |
more = 1; |
| 159 |
} |
| 160 |
} |
| 161 |
buf[buflen++] = value; |
| 162 |
return buflen; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
/* Converts an object-identifier given in dotted decimal form in |
| 167 |
* STRING to an DER encoding and returns this at the allocated buffer |
| 168 |
* RBUF with its length at RLENGTH. RBUF is set to NULL in case an |
| 169 |
* error is returned. Scanning stops at the first white space. |
| 170 |
* |
| 171 |
* The caller must free the returned buffer using ksba_free or the |
| 172 |
* function which has been registered as a replacement. |
| 173 |
* |
| 174 |
* Return value: 0 on success or an error value |
| 175 |
**/ |
| 176 |
gpg_error_t |
| 177 |
ksba_oid_from_str (const char *string, unsigned char **rbuf, size_t *rlength) |
| 178 |
{ |
| 179 |
unsigned char *buf; |
| 180 |
size_t buflen; |
| 181 |
unsigned long val1, val; |
| 182 |
const char *endp; |
| 183 |
int arcno; |
| 184 |
|
| 185 |
if (!string || !rbuf || !rlength) |
| 186 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 187 |
*rbuf = NULL; |
| 188 |
*rlength = 0; |
| 189 |
|
| 190 |
/* we allow the OID to be prefixed with either "oid." or "OID." */ |
| 191 |
if ( !strncmp (string, "oid.", 4) || !strncmp (string, "OID.", 4)) |
| 192 |
string += 4; |
| 193 |
|
| 194 |
if (!*string) |
| 195 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 196 |
|
| 197 |
/* we can safely assume that the encoded OID is shorter than the string */ |
| 198 |
buf = xtrymalloc ( strlen(string) + 2); |
| 199 |
if (!buf) |
| 200 |
return gpg_error (GPG_ERR_ENOMEM); |
| 201 |
buflen = 0; |
| 202 |
|
| 203 |
val1 = 0; /* avoid compiler warnings */ |
| 204 |
arcno = 0; |
| 205 |
do { |
| 206 |
arcno++; |
| 207 |
val = strtoul (string, (char**)&endp, 10); |
| 208 |
if (!digitp (string) || !(*endp == '.' || !*endp)) |
| 209 |
{ |
| 210 |
xfree (buf); |
| 211 |
return gpg_error (GPG_ERR_INV_OID_STRING); |
| 212 |
} |
| 213 |
if (*endp == '.') |
| 214 |
string = endp+1; |
| 215 |
|
| 216 |
if (arcno == 1) |
| 217 |
{ |
| 218 |
if (val > 2) |
| 219 |
break; /* not allowed, error catched below */ |
| 220 |
val1 = val; |
| 221 |
} |
| 222 |
else if (arcno == 2) |
| 223 |
{ /* need to combine the first to arcs in one octet */ |
| 224 |
if (val1 < 2) |
| 225 |
{ |
| 226 |
if (val > 39) |
| 227 |
{ |
| 228 |
xfree (buf); |
| 229 |
return gpg_error (GPG_ERR_INV_OID_STRING); |
| 230 |
} |
| 231 |
buf[buflen++] = val1*40 + val; |
| 232 |
} |
| 233 |
else |
| 234 |
{ |
| 235 |
val += 80; |
| 236 |
buflen = make_flagged_int (val, buf, buflen); |
| 237 |
} |
| 238 |
} |
| 239 |
else |
| 240 |
{ |
| 241 |
buflen = make_flagged_int (val, buf, buflen); |
| 242 |
} |
| 243 |
} while (*endp == '.'); |
| 244 |
|
| 245 |
if (arcno == 1) |
| 246 |
{ /* it is not possible to encode only the first arc */ |
| 247 |
xfree (buf); |
| 248 |
return gpg_error (GPG_ERR_INV_OID_STRING); |
| 249 |
} |
| 250 |
|
| 251 |
*rbuf = buf; |
| 252 |
*rlength = buflen; |
| 253 |
return 0; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
/* Convert the string in BUFFER which is of length BUFLEN to its DER |
| 258 |
encoding and returns it in a new allocated buffer RBUF and its |
| 259 |
length in RLENGTH. RBUF is set to NULL if an error is returned. |
| 260 |
The caller must free the returned buffer using ksba_free() or the |
| 261 |
function he has registered as a replacement. */ |
| 262 |
gpg_error_t |
| 263 |
_ksba_oid_from_buf (const void *buffer, size_t buflen, |
| 264 |
unsigned char **rbuf, size_t *rlength) |
| 265 |
{ |
| 266 |
gpg_error_t err; |
| 267 |
char *string; |
| 268 |
|
| 269 |
string = xtrymalloc (buflen+1); |
| 270 |
if (!string) |
| 271 |
{ |
| 272 |
*rbuf = NULL; |
| 273 |
*rlength = 0; |
| 274 |
return gpg_error_from_syserror (); |
| 275 |
} |
| 276 |
memcpy (string, buffer, buflen); |
| 277 |
string[buflen] = 0; |
| 278 |
err = ksba_oid_from_str (string, rbuf, rlength); |
| 279 |
xfree (string); |
| 280 |
return err; |
| 281 |
} |