os/ossrv/ssl/libcrypto/src/crypto/asn1/asn_pack.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* asn_pack.c */
sl@0
     2
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
sl@0
     3
 * project 1999.
sl@0
     4
 */
sl@0
     5
/* ====================================================================
sl@0
     6
 * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
sl@0
     7
 *
sl@0
     8
 * Redistribution and use in source and binary forms, with or without
sl@0
     9
 * modification, are permitted provided that the following conditions
sl@0
    10
 * are met:
sl@0
    11
 *
sl@0
    12
 * 1. Redistributions of source code must retain the above copyright
sl@0
    13
 *    notice, this list of conditions and the following disclaimer. 
sl@0
    14
 *
sl@0
    15
 * 2. Redistributions in binary form must reproduce the above copyright
sl@0
    16
 *    notice, this list of conditions and the following disclaimer in
sl@0
    17
 *    the documentation and/or other materials provided with the
sl@0
    18
 *    distribution.
sl@0
    19
 *
sl@0
    20
 * 3. All advertising materials mentioning features or use of this
sl@0
    21
 *    software must display the following acknowledgment:
sl@0
    22
 *    "This product includes software developed by the OpenSSL Project
sl@0
    23
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
sl@0
    24
 *
sl@0
    25
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
sl@0
    26
 *    endorse or promote products derived from this software without
sl@0
    27
 *    prior written permission. For written permission, please contact
sl@0
    28
 *    licensing@OpenSSL.org.
sl@0
    29
 *
sl@0
    30
 * 5. Products derived from this software may not be called "OpenSSL"
sl@0
    31
 *    nor may "OpenSSL" appear in their names without prior written
sl@0
    32
 *    permission of the OpenSSL Project.
sl@0
    33
 *
sl@0
    34
 * 6. Redistributions of any form whatsoever must retain the following
sl@0
    35
 *    acknowledgment:
sl@0
    36
 *    "This product includes software developed by the OpenSSL Project
sl@0
    37
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
sl@0
    38
 *
sl@0
    39
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
sl@0
    40
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
sl@0
    41
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
sl@0
    42
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
sl@0
    43
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
sl@0
    44
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
sl@0
    45
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
sl@0
    46
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sl@0
    47
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sl@0
    48
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
sl@0
    49
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
sl@0
    50
 * OF THE POSSIBILITY OF SUCH DAMAGE.
sl@0
    51
 * ====================================================================
sl@0
    52
 *
sl@0
    53
 * This product includes cryptographic software written by Eric Young
sl@0
    54
 * (eay@cryptsoft.com).  This product includes software written by Tim
sl@0
    55
 * Hudson (tjh@cryptsoft.com).
sl@0
    56
 *
sl@0
    57
 */
sl@0
    58
sl@0
    59
#include <stdio.h>
sl@0
    60
#include "cryptlib.h"
sl@0
    61
#include <openssl/asn1.h>
sl@0
    62
sl@0
    63
#ifndef NO_ASN1_OLD
sl@0
    64
sl@0
    65
/* ASN1 packing and unpacking functions */
sl@0
    66
sl@0
    67
/* Turn an ASN1 encoded SEQUENCE OF into a STACK of structures */
sl@0
    68
sl@0
    69
EXPORT_C STACK *ASN1_seq_unpack(const unsigned char *buf, int len,
sl@0
    70
		       d2i_of_void *d2i,void (*free_func)(void *))
sl@0
    71
{
sl@0
    72
    STACK *sk;
sl@0
    73
    const unsigned char *pbuf;
sl@0
    74
    pbuf =  buf;
sl@0
    75
    if (!(sk = d2i_ASN1_SET(NULL, &pbuf, len, d2i, free_func,
sl@0
    76
					V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL)))
sl@0
    77
		 ASN1err(ASN1_F_ASN1_SEQ_UNPACK,ASN1_R_DECODE_ERROR);
sl@0
    78
    return sk;
sl@0
    79
}
sl@0
    80
sl@0
    81
/* Turn a STACK structures into an ASN1 encoded SEQUENCE OF structure in a
sl@0
    82
 * OPENSSL_malloc'ed buffer
sl@0
    83
 */
sl@0
    84
sl@0
    85
EXPORT_C unsigned char *ASN1_seq_pack(STACK *safes, i2d_of_void *i2d,
sl@0
    86
			     unsigned char **buf, int *len)
sl@0
    87
{
sl@0
    88
	int safelen;
sl@0
    89
	unsigned char *safe, *p;
sl@0
    90
	if (!(safelen = i2d_ASN1_SET(safes, NULL, i2d, V_ASN1_SEQUENCE,
sl@0
    91
					      V_ASN1_UNIVERSAL, IS_SEQUENCE))) {
sl@0
    92
		ASN1err(ASN1_F_ASN1_SEQ_PACK,ASN1_R_ENCODE_ERROR);
sl@0
    93
		return NULL;
sl@0
    94
	}
sl@0
    95
	if (!(safe = OPENSSL_malloc (safelen))) {
sl@0
    96
		ASN1err(ASN1_F_ASN1_SEQ_PACK,ERR_R_MALLOC_FAILURE);
sl@0
    97
		return NULL;
sl@0
    98
	}
sl@0
    99
	p = safe;
sl@0
   100
	i2d_ASN1_SET(safes, &p, i2d, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL,
sl@0
   101
								 IS_SEQUENCE);
sl@0
   102
	if (len) *len = safelen;
sl@0
   103
	if (buf) *buf = safe;
sl@0
   104
	return safe;
sl@0
   105
}
sl@0
   106
sl@0
   107
/* Extract an ASN1 object from an ASN1_STRING */
sl@0
   108
sl@0
   109
EXPORT_C void *ASN1_unpack_string(ASN1_STRING *oct, d2i_of_void *d2i)
sl@0
   110
{
sl@0
   111
	const unsigned char *p;
sl@0
   112
	char *ret;
sl@0
   113
sl@0
   114
	p = oct->data;
sl@0
   115
	if(!(ret = d2i(NULL, &p, oct->length)))
sl@0
   116
		ASN1err(ASN1_F_ASN1_UNPACK_STRING,ASN1_R_DECODE_ERROR);
sl@0
   117
	return ret;
sl@0
   118
}
sl@0
   119
sl@0
   120
/* Pack an ASN1 object into an ASN1_STRING */
sl@0
   121
sl@0
   122
EXPORT_C ASN1_STRING *ASN1_pack_string(void *obj, i2d_of_void *i2d, ASN1_STRING **oct)
sl@0
   123
{
sl@0
   124
	unsigned char *p;
sl@0
   125
	ASN1_STRING *octmp;
sl@0
   126
sl@0
   127
	if (!oct || !*oct) {
sl@0
   128
		if (!(octmp = ASN1_STRING_new ())) {
sl@0
   129
			ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE);
sl@0
   130
			return NULL;
sl@0
   131
		}
sl@0
   132
		if (oct) *oct = octmp;
sl@0
   133
	} else octmp = *oct;
sl@0
   134
		
sl@0
   135
	if (!(octmp->length = i2d(obj, NULL))) {
sl@0
   136
		ASN1err(ASN1_F_ASN1_PACK_STRING,ASN1_R_ENCODE_ERROR);
sl@0
   137
		return NULL;
sl@0
   138
	}
sl@0
   139
	if (!(p = OPENSSL_malloc (octmp->length))) {
sl@0
   140
		ASN1err(ASN1_F_ASN1_PACK_STRING,ERR_R_MALLOC_FAILURE);
sl@0
   141
		return NULL;
sl@0
   142
	}
sl@0
   143
	octmp->data = p;
sl@0
   144
	i2d (obj, &p);
sl@0
   145
	return octmp;
sl@0
   146
}
sl@0
   147
sl@0
   148
#endif
sl@0
   149
sl@0
   150
/* ASN1_ITEM versions of the above */
sl@0
   151
sl@0
   152
EXPORT_C ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
sl@0
   153
{
sl@0
   154
	ASN1_STRING *octmp;
sl@0
   155
sl@0
   156
	if (!oct || !*oct) {
sl@0
   157
		if (!(octmp = ASN1_STRING_new ())) {
sl@0
   158
			ASN1err(ASN1_F_ASN1_ITEM_PACK,ERR_R_MALLOC_FAILURE);
sl@0
   159
			return NULL;
sl@0
   160
		}
sl@0
   161
		if (oct) *oct = octmp;
sl@0
   162
	} else octmp = *oct;
sl@0
   163
sl@0
   164
	if(octmp->data) {
sl@0
   165
		OPENSSL_free(octmp->data);
sl@0
   166
		octmp->data = NULL;
sl@0
   167
	}
sl@0
   168
		
sl@0
   169
	if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) {
sl@0
   170
		ASN1err(ASN1_F_ASN1_ITEM_PACK,ASN1_R_ENCODE_ERROR);
sl@0
   171
		return NULL;
sl@0
   172
	}
sl@0
   173
	if (!octmp->data) {
sl@0
   174
		ASN1err(ASN1_F_ASN1_ITEM_PACK,ERR_R_MALLOC_FAILURE);
sl@0
   175
		return NULL;
sl@0
   176
	}
sl@0
   177
	return octmp;
sl@0
   178
}
sl@0
   179
sl@0
   180
/* Extract an ASN1 object from an ASN1_STRING */
sl@0
   181
sl@0
   182
EXPORT_C void *ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it)
sl@0
   183
{
sl@0
   184
	const unsigned char *p;
sl@0
   185
	void *ret;
sl@0
   186
sl@0
   187
	p = oct->data;
sl@0
   188
	if(!(ret = ASN1_item_d2i(NULL, &p, oct->length, it)))
sl@0
   189
		ASN1err(ASN1_F_ASN1_ITEM_UNPACK,ASN1_R_DECODE_ERROR);
sl@0
   190
	return ret;
sl@0
   191
}