1*b4712960Sbeck /* $OpenBSD: asn1_old.c,v 1.6 2024/04/10 14:55:12 beck Exp $ */
20cd86ebdSjsing /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30cd86ebdSjsing * All rights reserved.
40cd86ebdSjsing *
50cd86ebdSjsing * This package is an SSL implementation written
60cd86ebdSjsing * by Eric Young (eay@cryptsoft.com).
70cd86ebdSjsing * The implementation was written so as to conform with Netscapes SSL.
80cd86ebdSjsing *
90cd86ebdSjsing * This library is free for commercial and non-commercial use as long as
100cd86ebdSjsing * the following conditions are aheared to. The following conditions
110cd86ebdSjsing * apply to all code found in this distribution, be it the RC4, RSA,
120cd86ebdSjsing * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130cd86ebdSjsing * included with this distribution is covered by the same copyright terms
140cd86ebdSjsing * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150cd86ebdSjsing *
160cd86ebdSjsing * Copyright remains Eric Young's, and as such any Copyright notices in
170cd86ebdSjsing * the code are not to be removed.
180cd86ebdSjsing * If this package is used in a product, Eric Young should be given attribution
190cd86ebdSjsing * as the author of the parts of the library used.
200cd86ebdSjsing * This can be in the form of a textual message at program startup or
210cd86ebdSjsing * in documentation (online or textual) provided with the package.
220cd86ebdSjsing *
230cd86ebdSjsing * Redistribution and use in source and binary forms, with or without
240cd86ebdSjsing * modification, are permitted provided that the following conditions
250cd86ebdSjsing * are met:
260cd86ebdSjsing * 1. Redistributions of source code must retain the copyright
270cd86ebdSjsing * notice, this list of conditions and the following disclaimer.
280cd86ebdSjsing * 2. Redistributions in binary form must reproduce the above copyright
290cd86ebdSjsing * notice, this list of conditions and the following disclaimer in the
300cd86ebdSjsing * documentation and/or other materials provided with the distribution.
310cd86ebdSjsing * 3. All advertising materials mentioning features or use of this software
320cd86ebdSjsing * must display the following acknowledgement:
330cd86ebdSjsing * "This product includes cryptographic software written by
340cd86ebdSjsing * Eric Young (eay@cryptsoft.com)"
350cd86ebdSjsing * The word 'cryptographic' can be left out if the rouines from the library
360cd86ebdSjsing * being used are not cryptographic related :-).
370cd86ebdSjsing * 4. If you include any Windows specific code (or a derivative thereof) from
380cd86ebdSjsing * the apps directory (application code) you must include an acknowledgement:
390cd86ebdSjsing * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400cd86ebdSjsing *
410cd86ebdSjsing * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420cd86ebdSjsing * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430cd86ebdSjsing * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440cd86ebdSjsing * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450cd86ebdSjsing * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460cd86ebdSjsing * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470cd86ebdSjsing * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480cd86ebdSjsing * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490cd86ebdSjsing * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500cd86ebdSjsing * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510cd86ebdSjsing * SUCH DAMAGE.
520cd86ebdSjsing *
530cd86ebdSjsing * The licence and distribution terms for any publically available version or
540cd86ebdSjsing * derivative of this code cannot be changed. i.e. this code cannot simply be
550cd86ebdSjsing * copied and put under another distribution licence
560cd86ebdSjsing * [including the GNU Public Licence.]
570cd86ebdSjsing */
580cd86ebdSjsing
590cd86ebdSjsing #include <limits.h>
600cd86ebdSjsing #include <stdio.h>
610cd86ebdSjsing
620cd86ebdSjsing #include <openssl/asn1.h>
630cd86ebdSjsing #include <openssl/buffer.h>
640cd86ebdSjsing #include <openssl/err.h>
650cd86ebdSjsing
66c9675a23Stb #include "asn1_local.h"
670cd86ebdSjsing
680cd86ebdSjsing #ifndef NO_OLD_ASN1
690cd86ebdSjsing
700cd86ebdSjsing void *
ASN1_dup(i2d_of_void * i2d,d2i_of_void * d2i,void * x)710cd86ebdSjsing ASN1_dup(i2d_of_void *i2d, d2i_of_void *d2i, void *x)
720cd86ebdSjsing {
730cd86ebdSjsing unsigned char *b, *p;
740cd86ebdSjsing const unsigned char *p2;
750cd86ebdSjsing int i;
760cd86ebdSjsing char *ret;
770cd86ebdSjsing
780cd86ebdSjsing if (x == NULL)
790cd86ebdSjsing return (NULL);
800cd86ebdSjsing
810cd86ebdSjsing i = i2d(x, NULL);
820cd86ebdSjsing b = malloc(i + 10);
830cd86ebdSjsing if (b == NULL) {
840cd86ebdSjsing ASN1error(ERR_R_MALLOC_FAILURE);
850cd86ebdSjsing return (NULL);
860cd86ebdSjsing }
870cd86ebdSjsing p = b;
880cd86ebdSjsing i = i2d(x, &p);
890cd86ebdSjsing p2 = b;
900cd86ebdSjsing ret = d2i(NULL, &p2, i);
910cd86ebdSjsing free(b);
920cd86ebdSjsing return (ret);
930cd86ebdSjsing }
94*b4712960Sbeck LCRYPTO_ALIAS(ASN1_dup);
950cd86ebdSjsing
960cd86ebdSjsing void *
ASN1_d2i_fp(void * (* xnew)(void),d2i_of_void * d2i,FILE * in,void ** x)970cd86ebdSjsing ASN1_d2i_fp(void *(*xnew)(void), d2i_of_void *d2i, FILE *in, void **x)
980cd86ebdSjsing {
990cd86ebdSjsing BIO *b;
1000cd86ebdSjsing void *ret;
1010cd86ebdSjsing
1020cd86ebdSjsing if ((b = BIO_new(BIO_s_file())) == NULL) {
1030cd86ebdSjsing ASN1error(ERR_R_BUF_LIB);
1040cd86ebdSjsing return (NULL);
1050cd86ebdSjsing }
1060cd86ebdSjsing BIO_set_fp(b, in, BIO_NOCLOSE);
1070cd86ebdSjsing ret = ASN1_d2i_bio(xnew, d2i, b, x);
1080cd86ebdSjsing BIO_free(b);
1090cd86ebdSjsing return (ret);
1100cd86ebdSjsing }
111acf64401Sbeck LCRYPTO_ALIAS(ASN1_d2i_fp);
1120cd86ebdSjsing
1130cd86ebdSjsing void *
ASN1_d2i_bio(void * (* xnew)(void),d2i_of_void * d2i,BIO * in,void ** x)1140cd86ebdSjsing ASN1_d2i_bio(void *(*xnew)(void), d2i_of_void *d2i, BIO *in, void **x)
1150cd86ebdSjsing {
1160cd86ebdSjsing BUF_MEM *b = NULL;
1170cd86ebdSjsing const unsigned char *p;
1180cd86ebdSjsing void *ret = NULL;
1190cd86ebdSjsing int len;
1200cd86ebdSjsing
1210cd86ebdSjsing len = asn1_d2i_read_bio(in, &b);
1220cd86ebdSjsing if (len < 0)
1230cd86ebdSjsing goto err;
1240cd86ebdSjsing
1250cd86ebdSjsing p = (unsigned char *)b->data;
1260cd86ebdSjsing ret = d2i(x, &p, len);
1270cd86ebdSjsing
1280cd86ebdSjsing err:
1290cd86ebdSjsing if (b != NULL)
1300cd86ebdSjsing BUF_MEM_free(b);
1310cd86ebdSjsing return (ret);
1320cd86ebdSjsing }
133acf64401Sbeck LCRYPTO_ALIAS(ASN1_d2i_bio);
1340cd86ebdSjsing
1350cd86ebdSjsing int
ASN1_i2d_fp(i2d_of_void * i2d,FILE * out,void * x)1360cd86ebdSjsing ASN1_i2d_fp(i2d_of_void *i2d, FILE *out, void *x)
1370cd86ebdSjsing {
1380cd86ebdSjsing BIO *b;
1390cd86ebdSjsing int ret;
1400cd86ebdSjsing
1410cd86ebdSjsing if ((b = BIO_new(BIO_s_file())) == NULL) {
1420cd86ebdSjsing ASN1error(ERR_R_BUF_LIB);
1430cd86ebdSjsing return (0);
1440cd86ebdSjsing }
1450cd86ebdSjsing BIO_set_fp(b, out, BIO_NOCLOSE);
1460cd86ebdSjsing ret = ASN1_i2d_bio(i2d, b, x);
1470cd86ebdSjsing BIO_free(b);
1480cd86ebdSjsing return (ret);
1490cd86ebdSjsing }
150acf64401Sbeck LCRYPTO_ALIAS(ASN1_i2d_fp);
1510cd86ebdSjsing
1520cd86ebdSjsing int
ASN1_i2d_bio(i2d_of_void * i2d,BIO * out,unsigned char * x)1530cd86ebdSjsing ASN1_i2d_bio(i2d_of_void *i2d, BIO *out, unsigned char *x)
1540cd86ebdSjsing {
1550cd86ebdSjsing char *b;
1560cd86ebdSjsing unsigned char *p;
1570cd86ebdSjsing int i, j = 0, n, ret = 1;
1580cd86ebdSjsing
1590cd86ebdSjsing n = i2d(x, NULL);
1600cd86ebdSjsing b = malloc(n);
1610cd86ebdSjsing if (b == NULL) {
1620cd86ebdSjsing ASN1error(ERR_R_MALLOC_FAILURE);
1630cd86ebdSjsing return (0);
1640cd86ebdSjsing }
1650cd86ebdSjsing
1660cd86ebdSjsing p = (unsigned char *)b;
1670cd86ebdSjsing i2d(x, &p);
1680cd86ebdSjsing
1690cd86ebdSjsing for (;;) {
1700cd86ebdSjsing i = BIO_write(out, &(b[j]), n);
1710cd86ebdSjsing if (i == n)
1720cd86ebdSjsing break;
1730cd86ebdSjsing if (i <= 0) {
1740cd86ebdSjsing ret = 0;
1750cd86ebdSjsing break;
1760cd86ebdSjsing }
1770cd86ebdSjsing j += i;
1780cd86ebdSjsing n -= i;
1790cd86ebdSjsing }
1800cd86ebdSjsing free(b);
1810cd86ebdSjsing return (ret);
1820cd86ebdSjsing }
183acf64401Sbeck LCRYPTO_ALIAS(ASN1_i2d_bio);
1840cd86ebdSjsing
1850cd86ebdSjsing #endif
186