Path: blob/main/crypto/heimdal/lib/asn1/der_format.c
34889 views
/*1* Copyright (c) 2005 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "der_locl.h"34#include <hex.h>3536RCSID("$Id$");3738int39der_parse_hex_heim_integer (const char *p, heim_integer *data)40{41ssize_t len;4243data->length = 0;44data->negative = 0;45data->data = NULL;4647if (*p == '-') {48p++;49data->negative = 1;50}5152len = strlen(p);53if (len <= 0) {54data->data = NULL;55data->length = 0;56return EINVAL;57}5859data->length = (len / 2) + 1;60data->data = malloc(data->length);61if (data->data == NULL) {62data->length = 0;63return ENOMEM;64}6566len = hex_decode(p, data->data, data->length);67if (len < 0) {68free(data->data);69data->data = NULL;70data->length = 0;71return EINVAL;72}7374{75unsigned char *q = data->data;76while(len > 0 && *q == 0) {77q++;78len--;79}80data->length = len;81memmove(data->data, q, len);82}83return 0;84}8586int87der_print_hex_heim_integer (const heim_integer *data, char **p)88{89ssize_t len;90char *q;9192len = hex_encode(data->data, data->length, p);93if (len < 0)94return ENOMEM;9596if (data->negative) {97len = asprintf(&q, "-%s", *p);98free(*p);99if (len < 0)100return ENOMEM;101*p = q;102}103return 0;104}105106int107der_print_heim_oid (const heim_oid *oid, char delim, char **str)108{109struct rk_strpool *p = NULL;110size_t i;111112if (oid->length == 0)113return EINVAL;114115for (i = 0; i < oid->length ; i++) {116p = rk_strpoolprintf(p, "%d", oid->components[i]);117if (p && i < oid->length - 1)118p = rk_strpoolprintf(p, "%c", delim);119if (p == NULL) {120*str = NULL;121return ENOMEM;122}123}124125*str = rk_strpoolcollect(p);126if (*str == NULL)127return ENOMEM;128return 0;129}130131int132der_parse_heim_oid (const char *str, const char *sep, heim_oid *data)133{134char *s, *w, *brkt, *endptr;135unsigned int *c;136long l;137138data->length = 0;139data->components = NULL;140141if (sep == NULL)142sep = ".";143144s = strdup(str);145146for (w = strtok_r(s, sep, &brkt);147w != NULL;148w = strtok_r(NULL, sep, &brkt)) {149150c = realloc(data->components,151(data->length + 1) * sizeof(data->components[0]));152if (c == NULL) {153der_free_oid(data);154free(s);155return ENOMEM;156}157data->components = c;158159l = strtol(w, &endptr, 10);160if (*endptr != '\0' || l < 0 || l > INT_MAX) {161der_free_oid(data);162free(s);163return EINVAL;164}165data->components[data->length++] = l;166}167free(s);168return 0;169}170171172