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.
8280304Sjkim *
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).
15280304Sjkim *
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.
22280304Sjkim *
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 :-).
37280304Sjkim * 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)"
40280304Sjkim *
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.
52280304Sjkim *
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)
65280304Sjkim{
66280304Sjkim    ASN1_STRING *os;
6755714Skris
68280304Sjkim    if ((os = M_ASN1_OCTET_STRING_new()) == NULL)
69280304Sjkim        return (0);
70280304Sjkim    if (!M_ASN1_OCTET_STRING_set(os, data, len)) {
71280304Sjkim        M_ASN1_OCTET_STRING_free(os);
72280304Sjkim        return 0;
73280304Sjkim    }
74280304Sjkim    ASN1_TYPE_set(a, V_ASN1_OCTET_STRING, os);
75280304Sjkim    return (1);
76280304Sjkim}
7755714Skris
7855714Skris/* int max_len:  for returned value    */
79280304Sjkimint ASN1_TYPE_get_octetstring(ASN1_TYPE *a, unsigned char *data, int max_len)
80280304Sjkim{
81280304Sjkim    int ret, num;
82280304Sjkim    unsigned char *p;
8355714Skris
84280304Sjkim    if ((a->type != V_ASN1_OCTET_STRING) || (a->value.octet_string == NULL)) {
85280304Sjkim        ASN1err(ASN1_F_ASN1_TYPE_GET_OCTETSTRING, ASN1_R_DATA_IS_WRONG);
86280304Sjkim        return (-1);
87280304Sjkim    }
88280304Sjkim    p = M_ASN1_STRING_data(a->value.octet_string);
89280304Sjkim    ret = M_ASN1_STRING_length(a->value.octet_string);
90280304Sjkim    if (ret < max_len)
91280304Sjkim        num = ret;
92280304Sjkim    else
93280304Sjkim        num = max_len;
94280304Sjkim    memcpy(data, p, num);
95280304Sjkim    return (ret);
96280304Sjkim}
9755714Skris
9855714Skrisint ASN1_TYPE_set_int_octetstring(ASN1_TYPE *a, long num, unsigned char *data,
99280304Sjkim                                  int len)
100280304Sjkim{
101280304Sjkim    int n, size;
102280304Sjkim    ASN1_OCTET_STRING os, *osp;
103280304Sjkim    ASN1_INTEGER in;
104280304Sjkim    unsigned char *p;
105280304Sjkim    unsigned char buf[32];      /* when they have 256bit longs, I'll be in
106280304Sjkim                                 * trouble */
107280304Sjkim    in.data = buf;
108280304Sjkim    in.length = 32;
109280304Sjkim    os.data = data;
110280304Sjkim    os.type = V_ASN1_OCTET_STRING;
111280304Sjkim    os.length = len;
112280304Sjkim    ASN1_INTEGER_set(&in, num);
113280304Sjkim    n = i2d_ASN1_INTEGER(&in, NULL);
114280304Sjkim    n += M_i2d_ASN1_OCTET_STRING(&os, NULL);
11555714Skris
116280304Sjkim    size = ASN1_object_size(1, n, V_ASN1_SEQUENCE);
11755714Skris
118280304Sjkim    if ((osp = ASN1_STRING_new()) == NULL)
119280304Sjkim        return (0);
120280304Sjkim    /* Grow the 'string' */
121280304Sjkim    if (!ASN1_STRING_set(osp, NULL, size)) {
122280304Sjkim        ASN1_STRING_free(osp);
123280304Sjkim        return (0);
124280304Sjkim    }
12555714Skris
126280304Sjkim    M_ASN1_STRING_length_set(osp, size);
127280304Sjkim    p = M_ASN1_STRING_data(osp);
12855714Skris
129280304Sjkim    ASN1_put_object(&p, 1, n, V_ASN1_SEQUENCE, V_ASN1_UNIVERSAL);
130280304Sjkim    i2d_ASN1_INTEGER(&in, &p);
131280304Sjkim    M_i2d_ASN1_OCTET_STRING(&os, &p);
13255714Skris
133280304Sjkim    ASN1_TYPE_set(a, V_ASN1_SEQUENCE, osp);
134280304Sjkim    return (1);
135280304Sjkim}
13655714Skris
137280304Sjkim/*
138280304Sjkim * we return the actual length..., num may be missing, in which case, set it
139280304Sjkim * to zero
140280304Sjkim */
14155714Skris/* int max_len:  for returned value    */
142280304Sjkimint ASN1_TYPE_get_int_octetstring(ASN1_TYPE *a, long *num,
143280304Sjkim                                  unsigned char *data, int max_len)
144280304Sjkim{
145280304Sjkim    int ret = -1, n;
146280304Sjkim    ASN1_INTEGER *ai = NULL;
147280304Sjkim    ASN1_OCTET_STRING *os = NULL;
148280304Sjkim    const unsigned char *p;
149280304Sjkim    long length;
150280304Sjkim    ASN1_const_CTX c;
15155714Skris
152280304Sjkim    if ((a->type != V_ASN1_SEQUENCE) || (a->value.sequence == NULL)) {
153280304Sjkim        goto err;
154280304Sjkim    }
155280304Sjkim    p = M_ASN1_STRING_data(a->value.sequence);
156280304Sjkim    length = M_ASN1_STRING_length(a->value.sequence);
15755714Skris
158280304Sjkim    c.pp = &p;
159280304Sjkim    c.p = p;
160280304Sjkim    c.max = p + length;
161280304Sjkim    c.error = ASN1_R_DATA_IS_WRONG;
16255714Skris
163280304Sjkim    M_ASN1_D2I_start_sequence();
164280304Sjkim    c.q = c.p;
165280304Sjkim    if ((ai = d2i_ASN1_INTEGER(NULL, &c.p, c.slen)) == NULL)
166280304Sjkim        goto err;
167280304Sjkim    c.slen -= (c.p - c.q);
168280304Sjkim    c.q = c.p;
169280304Sjkim    if ((os = d2i_ASN1_OCTET_STRING(NULL, &c.p, c.slen)) == NULL)
170280304Sjkim        goto err;
171280304Sjkim    c.slen -= (c.p - c.q);
172280304Sjkim    if (!M_ASN1_D2I_end_sequence())
173280304Sjkim        goto err;
17455714Skris
175280304Sjkim    if (num != NULL)
176280304Sjkim        *num = ASN1_INTEGER_get(ai);
17755714Skris
178280304Sjkim    ret = M_ASN1_STRING_length(os);
179280304Sjkim    if (max_len > ret)
180280304Sjkim        n = ret;
181280304Sjkim    else
182280304Sjkim        n = max_len;
18355714Skris
184280304Sjkim    if (data != NULL)
185280304Sjkim        memcpy(data, M_ASN1_STRING_data(os), n);
186280304Sjkim    if (0) {
187280304Sjkim err:
188280304Sjkim        ASN1err(ASN1_F_ASN1_TYPE_GET_INT_OCTETSTRING, ASN1_R_DATA_IS_WRONG);
189280304Sjkim    }
190280304Sjkim    if (os != NULL)
191280304Sjkim        M_ASN1_OCTET_STRING_free(os);
192280304Sjkim    if (ai != NULL)
193280304Sjkim        M_ASN1_INTEGER_free(ai);
194280304Sjkim    return (ret);
195280304Sjkim}
196