1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
|
/*
* Copyright 1999-2006 University of Chicago
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*********************************************************************
rfc1779.c
Description:
Routines to parse subjects names that are in RFC 1779 format
and convert them to the format used internally by SSLeay
CVS Information:
$Source$
$Date$
$Revision$
$Author$
**********************************************************************/
/**********************************************************************
Include header files
**********************************************************************/
#include <string.h>
#include <ctype.h> /* for isxdigit() */
#include <stdlib.h> /* for realloc() */
#include <errno.h>
/**********************************************************************
Type definitions
**********************************************************************/
/**********************************************************************
Definitions
**********************************************************************/
/* End of line characters */
#define END_OF_LINE_CHARS "\n"
/* Escaping characters */
#define ESCAPING_CHARS "\\"
/* Hex representation character */
#define HEX_CHARS "x"
/* String terminator character */
#ifndef NUL
#define NUL '\0'
#endif /* NUL */
/* Chunk size at which to grow buffer (arbitrary value) */
#define BUFFER_CHUNK_SIZE 16
/**********************************************************************
Module specific prototypes
**********************************************************************/
static void handle_error(char **errstring,
const char * const message);
static int xdigit_to_value(char xdigit);
/**********************************************************************
Define module specific variables
**********************************************************************/
/**********************************************************************
Exported Functions
**********************************************************************/
/******************************************************************************
Function: gridmap_ssleay_rfc1779_name_parse
Description:
Function that takes a string representing a distinguished name
and parse all the escaped characters and hexcodes as layed out
in RFC 1779.
Parameters:
rfc1779_string, the string with escaped characters
imported_name, pointer to a pointer that will be set to
point at an allocated string with the parsed name.
errstring, pointer to a pointer that will be set to point
at an allocated string with a description of an error, if
an error occurs.
Returns:
0 on success, non-zero on error.
******************************************************************************/
int
oldgaa_rfc1779_name_parse(
char * rfc1779_string,
char ** imported_name,
char ** errstring)
{
/* Is the current character escaped? (Previous char was backslash) */
int escaped = 0;
/* Buffer we are putting resulting name into */
char * buffer = NULL;
/* Buffer's length in bytes */
int buffer_len = 0;
/* And our current position in buffer */
int buffer_index = 0;
/* Character we're currently looking at */
char rfc1779_char;
/*
* Check input parameters for legality
*/
if (!rfc1779_string)
{
handle_error(errstring, "bad input string parameter");
errno = EINVAL;
goto error_return;
}
if (!imported_name)
{
handle_error(errstring, "bad output string parameter");
errno = EINVAL;
goto error_return;
}
buffer_len = strlen(rfc1779_string);
buffer = malloc(buffer_len);
if (buffer == NULL)
{
handle_error(errstring, "out of memory");
goto error_return;
}
/*
* Walk through the name, parsing as we go
*/
while ((rfc1779_char = *(rfc1779_string++)) != NUL)
{
/* Unescaped backslash */
if (strchr(ESCAPING_CHARS, rfc1779_char) && !escaped)
{
escaped = 1;
continue;
}
/* Unescaped newline */
if (strchr(END_OF_LINE_CHARS, rfc1779_char) && !escaped)
{
handle_error(errstring, "closing double quote delimitor missing");
goto error_return;
}
/* Escaped hex character - e.g. '\xfe' */
if (strchr(HEX_CHARS, rfc1779_char) && escaped)
{
if (isxdigit(*rfc1779_string) &&
isxdigit(*(rfc1779_string + 1)))
{
/* Set rfc1779_char to value represented by hex value */
rfc1779_char =
(xdigit_to_value(*rfc1779_string) << 4) +
xdigit_to_value(*(rfc1779_string + 1));
rfc1779_string += 2;
}
else
{
handle_error(errstring, "bad hex character format");
goto error_return;
}
}
/*
* Ok, we now have the character in rfc1779_char to be appended
* to our output string.
*
* First, make sure we have enough room in our output buffer.
*/
if ((buffer_index + 1 /* for NUL */) >= buffer_len)
{
/* Grow buffer */
char *tmp_buffer;
buffer_len += BUFFER_CHUNK_SIZE;
tmp_buffer = realloc(buffer, buffer_len);
if (tmp_buffer == NULL)
{
handle_error(errstring, "out of memory");
goto error_return;
}
buffer = tmp_buffer;
}
buffer[buffer_index++] = rfc1779_char;
buffer[buffer_index] = NUL;
escaped = 0;
}
/* XXX What if escaped == 1 here? */
/* Success */
*imported_name = buffer;
return 0;
error_return:
if (buffer)
free(buffer);
return -1;
} /* globus_ssleay_rfc1779_name_parse() */
/**********************************************************************
Internal Functions
**********************************************************************/
/******************************************************************************
Function: handle_error
Description:
Given and error message and a pointer to a pointer to be
allocated handle the allocation and setting of the pointer.
Parameters:
errstring, pointer to a pointer to be set to the allocated
error message. May be NULL.
message, the error message.
Returns:
Nothing
******************************************************************************/
static void
handle_error(
char ** errstring,
const char * const message)
{
if (errstring)
{
/* If this fails we're hoses so don't bother checking */
*errstring = strdup(message);
}
} /* handle_error() */
/******************************************************************************
Function: xdigit_to_value
Description:
Convert a ascii character representing a hexadecimal digit
into a integer.
Parameters:
xdigit, character contain the hex digit.
Returns:
value contained in xdigit, or -1 on error.
******************************************************************************/
static int
xdigit_to_value(
char xdigit)
{
if ((xdigit >= '0') && (xdigit <= '9'))
return (xdigit - '0');
if ((xdigit >= 'a') && (xdigit <= 'f'))
return (xdigit - 'a' + 0xa);
if ((xdigit >= 'A') && (xdigit <= 'F'))
return (xdigit - 'A' + 0xa);
/* Illegal digit */
return -1;
} /* xdigit_to_value() */
|