Path: blob/main/crypto/heimdal/lib/asn1/der_copy.c
34889 views
/*1* Copyright (c) 1997 - 2006 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Portions Copyright (c) 2009 Apple Inc. All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13*14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17*18* 3. Neither the name of the Institute nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include "der_locl.h"3637RCSID("$Id$");3839int40der_copy_general_string (const heim_general_string *from,41heim_general_string *to)42{43*to = strdup(*from);44if(*to == NULL)45return ENOMEM;46return 0;47}4849int50der_copy_integer (const int *from, int *to)51{52*to = *from;53return 0;54}5556int57der_copy_integer64 (const int64_t *from, int64_t *to)58{59*to = *from;60return 0;61}6263int64der_copy_unsigned (const unsigned *from, unsigned *to)65{66*to = *from;67return 0;68}6970int71der_copy_unsigned64 (const uint64_t *from, uint64_t *to)72{73*to = *from;74return 0;75}7677int78der_copy_generalized_time (const time_t *from, time_t *to)79{80*to = *from;81return 0;82}8384int85der_copy_utctime (const time_t *from, time_t *to)86{87*to = *from;88return 0;89}9091int92der_copy_utf8string (const heim_utf8_string *from, heim_utf8_string *to)93{94return der_copy_general_string(from, to);95}9697int98der_copy_printable_string (const heim_printable_string *from,99heim_printable_string *to)100{101to->length = from->length;102to->data = malloc(to->length + 1);103if(to->data == NULL)104return ENOMEM;105memcpy(to->data, from->data, to->length);106((char *)to->data)[to->length] = '\0';107return 0;108}109110int111der_copy_ia5_string (const heim_ia5_string *from,112heim_ia5_string *to)113{114return der_copy_printable_string(from, to);115}116117int118der_copy_bmp_string (const heim_bmp_string *from, heim_bmp_string *to)119{120to->length = from->length;121to->data = malloc(to->length * sizeof(to->data[0]));122if(to->length != 0 && to->data == NULL)123return ENOMEM;124memcpy(to->data, from->data, to->length * sizeof(to->data[0]));125return 0;126}127128int129der_copy_universal_string (const heim_universal_string *from,130heim_universal_string *to)131{132to->length = from->length;133to->data = malloc(to->length * sizeof(to->data[0]));134if(to->length != 0 && to->data == NULL)135return ENOMEM;136memcpy(to->data, from->data, to->length * sizeof(to->data[0]));137return 0;138}139140int141der_copy_visible_string (const heim_visible_string *from,142heim_visible_string *to)143{144return der_copy_general_string(from, to);145}146147int148der_copy_octet_string (const heim_octet_string *from, heim_octet_string *to)149{150to->length = from->length;151if (from->data == NULL) {152to->data = NULL;153return 0;154}155to->data = malloc(to->length);156if (to->length != 0 && to->data == NULL)157return ENOMEM;158memcpy(to->data, from->data, to->length);159return 0;160}161162int163der_copy_heim_integer (const heim_integer *from, heim_integer *to)164{165to->length = from->length;166to->data = malloc(to->length);167if(to->length != 0 && to->data == NULL)168return ENOMEM;169memcpy(to->data, from->data, to->length);170to->negative = from->negative;171return 0;172}173174int175der_copy_oid (const heim_oid *from, heim_oid *to)176{177to->length = from->length;178to->components = malloc(to->length * sizeof(*to->components));179if (to->length != 0 && to->components == NULL)180return ENOMEM;181memcpy(to->components, from->components,182to->length * sizeof(*to->components));183return 0;184}185186int187der_copy_bit_string (const heim_bit_string *from, heim_bit_string *to)188{189size_t len;190191len = (from->length + 7) / 8;192to->length = from->length;193to->data = malloc(len);194if(len != 0 && to->data == NULL)195return ENOMEM;196memcpy(to->data, from->data, len);197return 0;198}199200201