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