michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include "secitem.h" michael@0: #include "secport.h" michael@0: #include "secerr.h" michael@0: michael@0: /* if to->data is not NULL, and to->len is large enough to hold the result, michael@0: * then the resultant OID will be copyed into to->data, and to->len will be michael@0: * changed to show the actual OID length. michael@0: * Otherwise, memory for the OID will be allocated (from the caller's michael@0: * PLArenaPool, if pool is non-NULL) and to->data will receive the address michael@0: * of the allocated data, and to->len will receive the OID length. michael@0: * The original value of to->data is not freed when a new buffer is allocated. michael@0: * michael@0: * The input string may begin with "OID." and this still be ignored. michael@0: * The length of the input string is given in len. If len == 0, then michael@0: * len will be computed as strlen(from), meaning it must be NUL terminated. michael@0: * It is an error if from == NULL, or if *from == '\0'. michael@0: */ michael@0: michael@0: SECStatus michael@0: SEC_StringToOID(PLArenaPool *pool, SECItem *to, const char *from, PRUint32 len) michael@0: { michael@0: PRUint32 decimal_numbers = 0; michael@0: PRUint32 result_bytes = 0; michael@0: SECStatus rv; michael@0: PRUint8 result[1024]; michael@0: michael@0: static const PRUint32 max_decimal = (0xffffffff / 10); michael@0: static const char OIDstring[] = {"OID."}; michael@0: michael@0: if (!from || !to) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: if (!len) { michael@0: len = PL_strlen(from); michael@0: } michael@0: if (len >= 4 && !PL_strncasecmp(from, OIDstring, 4)) { michael@0: from += 4; /* skip leading "OID." if present */ michael@0: len -= 4; michael@0: } michael@0: if (!len) { michael@0: bad_data: michael@0: PORT_SetError(SEC_ERROR_BAD_DATA); michael@0: return SECFailure; michael@0: } michael@0: do { michael@0: PRUint32 decimal = 0; michael@0: while (len > 0 && isdigit(*from)) { michael@0: PRUint32 addend = (*from++ - '0'); michael@0: --len; michael@0: if (decimal > max_decimal) /* overflow */ michael@0: goto bad_data; michael@0: decimal = (decimal * 10) + addend; michael@0: if (decimal < addend) /* overflow */ michael@0: goto bad_data; michael@0: } michael@0: if (len != 0 && *from != '.') { michael@0: goto bad_data; michael@0: } michael@0: if (decimal_numbers == 0) { michael@0: if (decimal > 2) michael@0: goto bad_data; michael@0: result[0] = decimal * 40; michael@0: result_bytes = 1; michael@0: } else if (decimal_numbers == 1) { michael@0: if (decimal > 40) michael@0: goto bad_data; michael@0: result[0] += decimal; michael@0: } else { michael@0: /* encode the decimal number, */ michael@0: PRUint8 * rp; michael@0: PRUint32 num_bytes = 0; michael@0: PRUint32 tmp = decimal; michael@0: while (tmp) { michael@0: num_bytes++; michael@0: tmp >>= 7; michael@0: } michael@0: if (!num_bytes ) michael@0: ++num_bytes; /* use one byte for a zero value */ michael@0: if (num_bytes + result_bytes > sizeof result) michael@0: goto bad_data; michael@0: tmp = num_bytes; michael@0: rp = result + result_bytes - 1; michael@0: rp[tmp] = (PRUint8)(decimal & 0x7f); michael@0: decimal >>= 7; michael@0: while (--tmp > 0) { michael@0: rp[tmp] = (PRUint8)(decimal | 0x80); michael@0: decimal >>= 7; michael@0: } michael@0: result_bytes += num_bytes; michael@0: } michael@0: ++decimal_numbers; michael@0: if (len > 0) { /* skip trailing '.' */ michael@0: ++from; michael@0: --len; michael@0: } michael@0: } while (len > 0); michael@0: /* now result contains result_bytes of data */ michael@0: if (to->data && to->len >= result_bytes) { michael@0: PORT_Memcpy(to->data, result, to->len = result_bytes); michael@0: rv = SECSuccess; michael@0: } else { michael@0: SECItem result_item = {siBuffer, NULL, 0 }; michael@0: result_item.data = result; michael@0: result_item.len = result_bytes; michael@0: rv = SECITEM_CopyItem(pool, to, &result_item); michael@0: } michael@0: return rv; michael@0: }