Home | History | Annotate | Line # | Download | only in asn1
extra.c revision 1.1.1.1.4.1
      1  1.1.1.1.4.1   yamt /*	$NetBSD: extra.c,v 1.1.1.1.4.1 2014/05/22 13:21:25 yamt Exp $	*/
      2          1.1  elric 
      3          1.1  elric /*
      4          1.1  elric  * Copyright (c) 2003 - 2005 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 #include <krb5/heim_asn1.h>
     40          1.1  elric 
     41  1.1.1.1.4.1   yamt __RCSID("NetBSD");
     42          1.1  elric 
     43          1.1  elric int
     44          1.1  elric encode_heim_any(unsigned char *p, size_t len,
     45          1.1  elric 		const heim_any *data, size_t *size)
     46          1.1  elric {
     47          1.1  elric     return der_put_octet_string (p, len, data, size);
     48          1.1  elric }
     49          1.1  elric 
     50          1.1  elric int
     51          1.1  elric decode_heim_any(const unsigned char *p, size_t len,
     52          1.1  elric 		heim_any *data, size_t *size)
     53          1.1  elric {
     54          1.1  elric     size_t len_len, length, l;
     55          1.1  elric     Der_class thisclass;
     56          1.1  elric     Der_type thistype;
     57          1.1  elric     unsigned int thistag;
     58          1.1  elric     int e;
     59          1.1  elric 
     60          1.1  elric     memset(data, 0, sizeof(*data));
     61          1.1  elric 
     62          1.1  elric     e = der_get_tag (p, len, &thisclass, &thistype, &thistag, &l);
     63          1.1  elric     if (e) return e;
     64          1.1  elric     if (l > len)
     65          1.1  elric 	return ASN1_OVERFLOW;
     66          1.1  elric     e = der_get_length(p + l, len - l, &length, &len_len);
     67          1.1  elric     if (e) return e;
     68          1.1  elric     if (length == ASN1_INDEFINITE) {
     69          1.1  elric         if (len < len_len + l)
     70          1.1  elric 	    return ASN1_OVERFLOW;
     71          1.1  elric 	length = len - (len_len + l);
     72          1.1  elric     } else {
     73          1.1  elric 	if (len < length + len_len + l)
     74          1.1  elric 	    return ASN1_OVERFLOW;
     75          1.1  elric     }
     76  1.1.1.1.4.1   yamt 
     77          1.1  elric     data->data = malloc(length + len_len + l);
     78          1.1  elric     if (data->data == NULL)
     79          1.1  elric 	return ENOMEM;
     80          1.1  elric     data->length = length + len_len + l;
     81          1.1  elric     memcpy(data->data, p, length + len_len + l);
     82  1.1.1.1.4.1   yamt 
     83          1.1  elric     if (size)
     84          1.1  elric 	*size = length + len_len + l;
     85          1.1  elric 
     86          1.1  elric     return 0;
     87          1.1  elric }
     88          1.1  elric 
     89          1.1  elric void
     90          1.1  elric free_heim_any(heim_any *data)
     91          1.1  elric {
     92          1.1  elric     der_free_octet_string(data);
     93          1.1  elric }
     94          1.1  elric 
     95          1.1  elric size_t
     96          1.1  elric length_heim_any(const heim_any *data)
     97          1.1  elric {
     98          1.1  elric     return data->length;
     99          1.1  elric }
    100          1.1  elric 
    101          1.1  elric int
    102          1.1  elric copy_heim_any(const heim_any *from, heim_any *to)
    103          1.1  elric {
    104          1.1  elric     return der_copy_octet_string(from, to);
    105          1.1  elric }
    106          1.1  elric 
    107          1.1  elric int
    108          1.1  elric encode_heim_any_set(unsigned char *p, size_t len,
    109          1.1  elric 		    const heim_any_set *data, size_t *size)
    110          1.1  elric {
    111          1.1  elric     return der_put_octet_string (p, len, data, size);
    112          1.1  elric }
    113          1.1  elric 
    114          1.1  elric int
    115          1.1  elric decode_heim_any_set(const unsigned char *p, size_t len,
    116          1.1  elric 		heim_any_set *data, size_t *size)
    117          1.1  elric {
    118          1.1  elric     return der_get_octet_string(p, len, data, size);
    119          1.1  elric }
    120          1.1  elric 
    121          1.1  elric void
    122          1.1  elric free_heim_any_set(heim_any_set *data)
    123          1.1  elric {
    124          1.1  elric     der_free_octet_string(data);
    125          1.1  elric }
    126          1.1  elric 
    127          1.1  elric size_t
    128          1.1  elric length_heim_any_set(const heim_any *data)
    129          1.1  elric {
    130          1.1  elric     return data->length;
    131          1.1  elric }
    132          1.1  elric 
    133          1.1  elric int
    134          1.1  elric copy_heim_any_set(const heim_any_set *from, heim_any_set *to)
    135          1.1  elric {
    136          1.1  elric     return der_copy_octet_string(from, to);
    137          1.1  elric }
    138          1.1  elric 
    139          1.1  elric int
    140          1.1  elric heim_any_cmp(const heim_any_set *p, const heim_any_set *q)
    141          1.1  elric {
    142          1.1  elric     return der_heim_octet_string_cmp(p, q);
    143          1.1  elric }
    144