Home | History | Annotate | Line # | Download | only in asn1
      1  1.1     elric /*	$NetBSD: der_copy.c,v 1.3 2023/06/19 21:41:42 christos Exp $	*/
      2  1.1     elric 
      3  1.1     elric /*
      4  1.1     elric  * Copyright (c) 1997 - 2006 Kungliga Tekniska Hgskolan
      5  1.1     elric  * (Royal Institute of Technology, Stockholm, Sweden).
      6  1.1     elric  * All rights reserved.
      7  1.1     elric  *
      8  1.1     elric  * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
      9  1.1     elric  *
     10  1.1     elric  * Redistribution and use in source and binary forms, with or without
     11  1.1     elric  * modification, are permitted provided that the following conditions
     12  1.1     elric  * are met:
     13  1.1     elric  *
     14  1.1     elric  * 1. Redistributions of source code must retain the above copyright
     15  1.1     elric  *    notice, this list of conditions and the following disclaimer.
     16  1.1     elric  *
     17  1.1     elric  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.1     elric  *    notice, this list of conditions and the following disclaimer in the
     19  1.1     elric  *    documentation and/or other materials provided with the distribution.
     20  1.1     elric  *
     21  1.1     elric  * 3. Neither the name of the Institute nor the names of its contributors
     22  1.1     elric  *    may be used to endorse or promote products derived from this software
     23  1.1     elric  *    without specific prior written permission.
     24  1.1     elric  *
     25  1.1     elric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
     26  1.1     elric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1     elric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1     elric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
     29  1.1     elric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1     elric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1     elric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1     elric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1     elric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1     elric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1     elric  * SUCH DAMAGE.
     36  1.1     elric  */
     37  1.1     elric 
     38  1.1     elric #include "der_locl.h"
     39  1.1     elric 
     40  1.1     elric __RCSID("$NetBSD: der_copy.c,v 1.3 2023/06/19 21:41:42 christos Exp $");
     41  1.1     elric 
     42  1.1     elric int
     43  1.1     elric der_copy_general_string (const heim_general_string *from,
     44  1.1     elric 			 heim_general_string *to)
     45  1.1     elric {
     46  1.1     elric     *to = strdup(*from);
     47  1.1     elric     if(*to == NULL)
     48  1.1     elric 	return ENOMEM;
     49  1.1     elric     return 0;
     50  1.1     elric }
     51  1.1     elric 
     52  1.1     elric int
     53  1.1     elric der_copy_integer (const int *from, int *to)
     54  1.1     elric {
     55  1.1     elric     *to = *from;
     56  1.1     elric     return 0;
     57  1.1     elric }
     58  1.1     elric 
     59  1.1     elric int
     60  1.2  christos der_copy_integer64 (const int64_t *from, int64_t *to)
     61  1.2  christos {
     62  1.2  christos     *to = *from;
     63  1.2  christos     return 0;
     64  1.2  christos }
     65  1.2  christos 
     66  1.2  christos int
     67  1.1     elric der_copy_unsigned (const unsigned *from, unsigned *to)
     68  1.1     elric {
     69  1.1     elric     *to = *from;
     70  1.1     elric     return 0;
     71  1.1     elric }
     72  1.1     elric 
     73  1.1     elric int
     74  1.2  christos der_copy_unsigned64 (const uint64_t *from, uint64_t *to)
     75  1.2  christos {
     76  1.2  christos     *to = *from;
     77  1.2  christos     return 0;
     78  1.2  christos }
     79  1.2  christos 
     80  1.2  christos int
     81  1.1     elric der_copy_generalized_time (const time_t *from, time_t *to)
     82  1.1     elric {
     83  1.1     elric     *to = *from;
     84  1.1     elric     return 0;
     85  1.1     elric }
     86  1.1     elric 
     87  1.1     elric int
     88  1.1     elric der_copy_utctime (const time_t *from, time_t *to)
     89  1.1     elric {
     90  1.1     elric     *to = *from;
     91  1.1     elric     return 0;
     92  1.1     elric }
     93  1.1     elric 
     94  1.1     elric int
     95  1.1     elric der_copy_utf8string (const heim_utf8_string *from, heim_utf8_string *to)
     96  1.1     elric {
     97  1.1     elric     return der_copy_general_string(from, to);
     98  1.1     elric }
     99  1.1     elric 
    100  1.1     elric int
    101  1.1     elric der_copy_printable_string (const heim_printable_string *from,
    102  1.1     elric 		       heim_printable_string *to)
    103  1.1     elric {
    104  1.1     elric     to->length = from->length;
    105  1.1     elric     to->data   = malloc(to->length + 1);
    106  1.1     elric     if(to->data == NULL)
    107  1.1     elric 	return ENOMEM;
    108  1.1     elric     memcpy(to->data, from->data, to->length);
    109  1.1     elric     ((char *)to->data)[to->length] = '\0';
    110  1.1     elric     return 0;
    111  1.1     elric }
    112  1.1     elric 
    113  1.1     elric int
    114  1.1     elric der_copy_ia5_string (const heim_ia5_string *from,
    115  1.1     elric 		     heim_ia5_string *to)
    116  1.1     elric {
    117  1.1     elric     return der_copy_printable_string(from, to);
    118  1.1     elric }
    119  1.1     elric 
    120  1.1     elric int
    121  1.1     elric der_copy_bmp_string (const heim_bmp_string *from, heim_bmp_string *to)
    122  1.1     elric {
    123  1.1     elric     to->length = from->length;
    124  1.1     elric     to->data   = malloc(to->length * sizeof(to->data[0]));
    125  1.1     elric     if(to->length != 0 && to->data == NULL)
    126  1.1     elric 	return ENOMEM;
    127  1.1     elric     memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
    128  1.1     elric     return 0;
    129  1.1     elric }
    130  1.1     elric 
    131  1.1     elric int
    132  1.1     elric der_copy_universal_string (const heim_universal_string *from,
    133  1.1     elric 			   heim_universal_string *to)
    134  1.1     elric {
    135  1.1     elric     to->length = from->length;
    136  1.1     elric     to->data   = malloc(to->length * sizeof(to->data[0]));
    137  1.1     elric     if(to->length != 0 && to->data == NULL)
    138  1.1     elric 	return ENOMEM;
    139  1.1     elric     memcpy(to->data, from->data, to->length * sizeof(to->data[0]));
    140  1.1     elric     return 0;
    141  1.1     elric }
    142  1.1     elric 
    143  1.1     elric int
    144  1.1     elric der_copy_visible_string (const heim_visible_string *from,
    145  1.1     elric 			 heim_visible_string *to)
    146  1.1     elric {
    147  1.1     elric     return der_copy_general_string(from, to);
    148  1.1     elric }
    149  1.1     elric 
    150  1.1     elric int
    151  1.1     elric der_copy_octet_string (const heim_octet_string *from, heim_octet_string *to)
    152  1.1     elric {
    153  1.1     elric     to->length = from->length;
    154  1.3  christos     if (from->data == NULL) {
    155  1.3  christos         to->data = NULL;
    156  1.3  christos         return 0;
    157  1.3  christos     }
    158  1.3  christos     to->data = malloc(to->length);
    159  1.3  christos     if (to->length != 0 && to->data == NULL)
    160  1.1     elric 	return ENOMEM;
    161  1.1     elric     memcpy(to->data, from->data, to->length);
    162  1.1     elric     return 0;
    163  1.1     elric }
    164  1.1     elric 
    165  1.1     elric int
    166  1.1     elric der_copy_heim_integer (const heim_integer *from, heim_integer *to)
    167  1.1     elric {
    168  1.1     elric     to->length = from->length;
    169  1.1     elric     to->data   = malloc(to->length);
    170  1.1     elric     if(to->length != 0 && to->data == NULL)
    171  1.1     elric 	return ENOMEM;
    172  1.1     elric     memcpy(to->data, from->data, to->length);
    173  1.1     elric     to->negative = from->negative;
    174  1.1     elric     return 0;
    175  1.1     elric }
    176  1.1     elric 
    177  1.1     elric int
    178  1.1     elric der_copy_oid (const heim_oid *from, heim_oid *to)
    179  1.1     elric {
    180  1.1     elric     to->length     = from->length;
    181  1.1     elric     to->components = malloc(to->length * sizeof(*to->components));
    182  1.1     elric     if (to->length != 0 && to->components == NULL)
    183  1.1     elric 	return ENOMEM;
    184  1.1     elric     memcpy(to->components, from->components,
    185  1.1     elric 	   to->length * sizeof(*to->components));
    186  1.1     elric     return 0;
    187  1.1     elric }
    188  1.1     elric 
    189  1.1     elric int
    190  1.1     elric der_copy_bit_string (const heim_bit_string *from, heim_bit_string *to)
    191  1.1     elric {
    192  1.1     elric     size_t len;
    193  1.1     elric 
    194  1.1     elric     len = (from->length + 7) / 8;
    195  1.1     elric     to->length = from->length;
    196  1.1     elric     to->data   = malloc(len);
    197  1.1     elric     if(len != 0 && to->data == NULL)
    198  1.1     elric 	return ENOMEM;
    199  1.1     elric     memcpy(to->data, from->data, len);
    200  1.1     elric     return 0;
    201  1.1     elric }
    202