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. 8296465Sdelphij * 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). 15296465Sdelphij * 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. 22296465Sdelphij * 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 :-). 37296465Sdelphij * 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)" 40296465Sdelphij * 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. 52296465Sdelphij * 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) 65296465Sdelphij{ 66296465Sdelphij ASN1_STRING *os; 6755714Skris 68296465Sdelphij if ((os = M_ASN1_OCTET_STRING_new()) == NULL) 69296465Sdelphij return (0); 70296465Sdelphij if (!M_ASN1_OCTET_STRING_set(os, data, len)) { 71296465Sdelphij M_ASN1_OCTET_STRING_free(os); 72296465Sdelphij return 0; 73296465Sdelphij } 74296465Sdelphij ASN1_TYPE_set(a, V_ASN1_OCTET_STRING, os); 75296465Sdelphij return (1); 76296465Sdelphij} 7755714Skris 7855714Skris/* int max_len: for returned value */ 79296465Sdelphijint ASN1_TYPE_get_octetstring(ASN1_TYPE *a, unsigned char *data, int max_len) 80296465Sdelphij{ 81296465Sdelphij int ret, num; 82296465Sdelphij unsigned char *p; 8355714Skris 84296465Sdelphij if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) { 85296465Sdelphij ASN1err(ASN1_F_ASN1_TYPE_GET_OCTETSTRING, ASN1_R_DATA_IS_WRONG); 86296465Sdelphij return (-1); 87296465Sdelphij } 88296465Sdelphij p = M_ASN1_STRING_data(a->value.octet_string); 89296465Sdelphij ret = M_ASN1_STRING_length(a->value.octet_string); 90296465Sdelphij if (ret < max_len) 91296465Sdelphij num = ret; 92296465Sdelphij else 93296465Sdelphij num = max_len; 94296465Sdelphij memcpy(data, p, num); 95296465Sdelphij return (ret); 96296465Sdelphij} 9755714Skris 9855714Skrisint ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data, 99296465Sdelphij int len) 100296465Sdelphij{ 101296465Sdelphij int n, size; 102296465Sdelphij ASN1_OCTET_STRING os, *osp; 103296465Sdelphij ASN1_INTEGER in; 104296465Sdelphij unsigned char *p; 105296465Sdelphij unsigned char buf[32]; /* when they have 256bit longs, I'll be in 106296465Sdelphij * trouble */ 107296465Sdelphij in.data = buf; 108296465Sdelphij in.length = 32; 109296465Sdelphij os.data = data; 110296465Sdelphij os.type = V_ASN1_OCTET_STRING; 111296465Sdelphij os.length = len; 112296465Sdelphij ASN1_INTEGER_set(&in, num); 113296465Sdelphij n = i2d_ASN1_INTEGER(&in, NULL); 114296465Sdelphij n += M_i2d_ASN1_OCTET_STRING(&os, NULL); 11555714Skris 116296465Sdelphij size = ASN1_object_size(1, n, V_ASN1_SEQUENCE); 11755714Skris 118296465Sdelphij if ((osp = ASN1_STRING_new()) == NULL) 119296465Sdelphij return (0); 120296465Sdelphij /* Grow the 'string' */ 121296465Sdelphij if (!ASN1_STRING_set(osp, NULL, size)) { 122296465Sdelphij ASN1_STRING_free(osp); 123296465Sdelphij return (0); 124296465Sdelphij } 12555714Skris 126296465Sdelphij M_ASN1_STRING_length_set(osp, size); 127296465Sdelphij p = M_ASN1_STRING_data(osp); 12855714Skris 129296465Sdelphij ASN1_put_object(&p, 1, n, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL); 130296465Sdelphij i2d_ASN1_INTEGER(&in, &p); 131296465Sdelphij M_i2d_ASN1_OCTET_STRING(&os, &p); 13255714Skris 133296465Sdelphij ASN1_TYPE_set(a, V_ASN1_SEQUENCE, osp); 134296465Sdelphij return (1); 135296465Sdelphij} 13655714Skris 137296465Sdelphij/* 138296465Sdelphij * we return the actual length..., num may be missing, in which case, set it 139296465Sdelphij * to zero 140296465Sdelphij */ 14155714Skris/* int max_len: for returned value */ 142296465Sdelphijint ASN1_TYPE_get_int_octetstring(ASN1_TYPE *a, long *num, 143296465Sdelphij unsigned char *data, int max_len) 144296465Sdelphij{ 145296465Sdelphij int ret = -1, n; 146296465Sdelphij ASN1_INTEGER *ai = NULL; 147296465Sdelphij ASN1_OCTET_STRING *os = NULL; 148296465Sdelphij const unsigned char *p; 149296465Sdelphij long length; 150296465Sdelphij ASN1_const_CTX c; 15155714Skris 152296465Sdelphij if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) { 153296465Sdelphij goto err; 154296465Sdelphij } 155296465Sdelphij p = M_ASN1_STRING_data(a->value.sequence); 156296465Sdelphij length = M_ASN1_STRING_length(a->value.sequence); 15755714Skris 158296465Sdelphij c.pp = &p; 159296465Sdelphij c.p = p; 160296465Sdelphij c.max = p + length; 161296465Sdelphij c.error = ASN1_R_DATA_IS_WRONG; 16255714Skris 163296465Sdelphij M_ASN1_D2I_start_sequence(); 164296465Sdelphij c.q = c.p; 165296465Sdelphij if ((ai = d2i_ASN1_INTEGER(NULL, &c.p, c.slen)) == NULL) 166296465Sdelphij goto err; 167296465Sdelphij c.slen -= (c.p - c.q); 168296465Sdelphij c.q = c.p; 169296465Sdelphij if ((os = d2i_ASN1_OCTET_STRING(NULL, &c.p, c.slen)) == NULL) 170296465Sdelphij goto err; 171296465Sdelphij c.slen -= (c.p - c.q); 172296465Sdelphij if (!M_ASN1_D2I_end_sequence()) 173296465Sdelphij goto err; 17455714Skris 175296465Sdelphij if (num != NULL) 176296465Sdelphij *num = ASN1_INTEGER_get(ai); 17755714Skris 178296465Sdelphij ret = M_ASN1_STRING_length(os); 179296465Sdelphij if (max_len > ret) 180296465Sdelphij n = ret; 181296465Sdelphij else 182296465Sdelphij n = max_len; 18355714Skris 184296465Sdelphij if (data != NULL) 185296465Sdelphij memcpy(data, M_ASN1_STRING_data(os), n); 186296465Sdelphij if (0) { 187296465Sdelphij err: 188296465Sdelphij ASN1err(ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING, ASN1_R_DATA_IS_WRONG); 189296465Sdelphij } 190296465Sdelphij if (os != NULL) 191296465Sdelphij M_ASN1_OCTET_STRING_free(os); 192296465Sdelphij if (ai != NULL) 193296465Sdelphij M_ASN1_INTEGER_free(ai); 194296465Sdelphij return (ret); 195296465Sdelphij} 196