155714Skris/* crypto/asn1/evp_asn1.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 855714Skris * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1555714Skris * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 2255714Skris * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 4055714Skris * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 5255714Skris * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include "cryptlib.h" 6155714Skris#include <openssl/asn1.h> 6255714Skris#include <openssl/asn1_mac.h> 6355714Skris 6455714Skrisint ASN1_TYPE_set_octetstring(ASN1_TYPE *a, unsigned char *data, int len) 6555714Skris { 6655714Skris ASN1_STRING *os; 6755714Skris 6859191Skris if ((os=M_ASN1_OCTET_STRING_new()) == NULL) return(0); 69279264Sdelphij if (!M_ASN1_OCTET_STRING_set(os,data,len)) 70279264Sdelphij { 71279264Sdelphij M_ASN1_OCTET_STRING_free(os); 72279264Sdelphij return 0; 73279264Sdelphij } 7455714Skris ASN1_TYPE_set(a,V_ASN1_OCTET_STRING,os); 7555714Skris return(1); 7655714Skris } 7755714Skris 7855714Skris/* int max_len: for returned value */ 7955714Skrisint ASN1_TYPE_get_octetstring(ASN1_TYPE *a, unsigned char *data, 8055714Skris int max_len) 8155714Skris { 8255714Skris int ret,num; 8355714Skris unsigned char *p; 8455714Skris 8555714Skris if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) 8655714Skris { 8755714Skris ASN1err(ASN1_F_ASN1_TYPE_GET_OCTETSTRING,ASN1_R_DATA_IS_WRONG); 8855714Skris return(-1); 8955714Skris } 9059191Skris p=M_ASN1_STRING_data(a->value.octet_string); 9159191Skris ret=M_ASN1_STRING_length(a->value.octet_string); 9255714Skris if (ret < max_len) 9355714Skris num=ret; 9455714Skris else 9555714Skris num=max_len; 9655714Skris memcpy(data,p,num); 9755714Skris return(ret); 9855714Skris } 9955714Skris 10055714Skrisint ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data, 10155714Skris int len) 10255714Skris { 10355714Skris int n,size; 10455714Skris ASN1_OCTET_STRING os,*osp; 10555714Skris ASN1_INTEGER in; 10655714Skris unsigned char *p; 10755714Skris unsigned char buf[32]; /* when they have 256bit longs, 10855714Skris * I'll be in trouble */ 10955714Skris in.data=buf; 11055714Skris in.length=32; 11155714Skris os.data=data; 11255714Skris os.type=V_ASN1_OCTET_STRING; 11355714Skris os.length=len; 11455714Skris ASN1_INTEGER_set(&in,num); 11555714Skris n = i2d_ASN1_INTEGER(&in,NULL); 11655714Skris n+=M_i2d_ASN1_OCTET_STRING(&os,NULL); 11755714Skris 11855714Skris size=ASN1_object_size(1,n,V_ASN1_SEQUENCE); 11955714Skris 12055714Skris if ((osp=ASN1_STRING_new()) == NULL) return(0); 12155714Skris /* Grow the 'string' */ 122160814Ssimon if (!ASN1_STRING_set(osp,NULL,size)) 123160814Ssimon { 124160814Ssimon ASN1_STRING_free(osp); 125160814Ssimon return(0); 126160814Ssimon } 12755714Skris 12859191Skris M_ASN1_STRING_length_set(osp, size); 12959191Skris p=M_ASN1_STRING_data(osp); 13055714Skris 13155714Skris ASN1_put_object(&p,1,n,V_ASN1_SEQUENCE,V_ASN1_UNIVERSAL); 13255714Skris i2d_ASN1_INTEGER(&in,&p); 13355714Skris M_i2d_ASN1_OCTET_STRING(&os,&p); 13455714Skris 13555714Skris ASN1_TYPE_set(a,V_ASN1_SEQUENCE,osp); 13655714Skris return(1); 13755714Skris } 13855714Skris 13955714Skris/* we return the actual length..., num may be missing, in which 14055714Skris * case, set it to zero */ 14155714Skris/* int max_len: for returned value */ 14255714Skrisint ASN1_TYPE_get_int_octetstring(ASN1_TYPE *a, long *num, unsigned char *data, 14355714Skris int max_len) 14455714Skris { 14555714Skris int ret= -1,n; 14655714Skris ASN1_INTEGER *ai=NULL; 14755714Skris ASN1_OCTET_STRING *os=NULL; 148160814Ssimon const unsigned char *p; 14955714Skris long length; 150160814Ssimon ASN1_const_CTX c; 15155714Skris 15255714Skris if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) 15355714Skris { 15455714Skris goto err; 15555714Skris } 15659191Skris p=M_ASN1_STRING_data(a->value.sequence); 15759191Skris length=M_ASN1_STRING_length(a->value.sequence); 15855714Skris 15955714Skris c.pp= &p; 16055714Skris c.p=p; 16155714Skris c.max=p+length; 16255714Skris c.error=ASN1_R_DATA_IS_WRONG; 16355714Skris 16455714Skris M_ASN1_D2I_start_sequence(); 16555714Skris c.q=c.p; 16655714Skris if ((ai=d2i_ASN1_INTEGER(NULL,&c.p,c.slen)) == NULL) goto err; 16755714Skris c.slen-=(c.p-c.q); 16855714Skris c.q=c.p; 16955714Skris if ((os=d2i_ASN1_OCTET_STRING(NULL,&c.p,c.slen)) == NULL) goto err; 17055714Skris c.slen-=(c.p-c.q); 17155714Skris if (!M_ASN1_D2I_end_sequence()) goto err; 17255714Skris 17355714Skris if (num != NULL) 17455714Skris *num=ASN1_INTEGER_get(ai); 17555714Skris 17659191Skris ret=M_ASN1_STRING_length(os); 17755714Skris if (max_len > ret) 17855714Skris n=ret; 17955714Skris else 18055714Skris n=max_len; 18155714Skris 18255714Skris if (data != NULL) 18359191Skris memcpy(data,M_ASN1_STRING_data(os),n); 18455714Skris if (0) 18555714Skris { 18655714Skriserr: 18755714Skris ASN1err(ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING,ASN1_R_DATA_IS_WRONG); 18855714Skris } 18959191Skris if (os != NULL) M_ASN1_OCTET_STRING_free(os); 19059191Skris if (ai != NULL) M_ASN1_INTEGER_free(ai); 19155714Skris return(ret); 19255714Skris } 19355714Skris 194