sl@0: /* apps/asn1pars.c */ sl@0: /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) sl@0: * All rights reserved. sl@0: * sl@0: * This package is an SSL implementation written sl@0: * by Eric Young (eay@cryptsoft.com). sl@0: * The implementation was written so as to conform with Netscapes SSL. sl@0: * sl@0: * This library is free for commercial and non-commercial use as long as sl@0: * the following conditions are aheared to. The following conditions sl@0: * apply to all code found in this distribution, be it the RC4, RSA, sl@0: * lhash, DES, etc., code; not just the SSL code. The SSL documentation sl@0: * included with this distribution is covered by the same copyright terms sl@0: * except that the holder is Tim Hudson (tjh@cryptsoft.com). sl@0: * sl@0: * Copyright remains Eric Young's, and as such any Copyright notices in sl@0: * the code are not to be removed. sl@0: * If this package is used in a product, Eric Young should be given attribution sl@0: * as the author of the parts of the library used. sl@0: * This can be in the form of a textual message at program startup or sl@0: * in documentation (online or textual) provided with the package. sl@0: * sl@0: * Redistribution and use in source and binary forms, with or without sl@0: * modification, are permitted provided that the following conditions sl@0: * are met: sl@0: * 1. Redistributions of source code must retain the copyright sl@0: * notice, this list of conditions and the following disclaimer. sl@0: * 2. Redistributions in binary form must reproduce the above copyright sl@0: * notice, this list of conditions and the following disclaimer in the sl@0: * documentation and/or other materials provided with the distribution. sl@0: * 3. All advertising materials mentioning features or use of this software sl@0: * must display the following acknowledgement: sl@0: * "This product includes cryptographic software written by sl@0: * Eric Young (eay@cryptsoft.com)" sl@0: * The word 'cryptographic' can be left out if the rouines from the library sl@0: * being used are not cryptographic related :-). sl@0: * 4. If you include any Windows specific code (or a derivative thereof) from sl@0: * the apps directory (application code) you must include an acknowledgement: sl@0: * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" sl@0: * sl@0: * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND sl@0: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE sl@0: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE sl@0: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE sl@0: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL sl@0: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS sl@0: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) sl@0: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT sl@0: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY sl@0: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF sl@0: * SUCH DAMAGE. sl@0: * sl@0: * The licence and distribution terms for any publically available version or sl@0: * derivative of this code cannot be changed. i.e. this code cannot simply be sl@0: * copied and put under another distribution licence sl@0: * [including the GNU Public Licence.] sl@0: */ sl@0: sl@0: /* A nice addition from Dr Stephen Henson to sl@0: * add the -strparse option which parses nested binary structures sl@0: */ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include "apps.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: /* -inform arg - input format - default PEM (DER or PEM) sl@0: * -in arg - input file - default stdin sl@0: * -i - indent the details by depth sl@0: * -offset - where in the file to start sl@0: * -length - how many bytes to use sl@0: * -oid file - extra oid description file sl@0: */ sl@0: sl@0: #undef PROG sl@0: #define PROG asn1parse_main sl@0: sl@0: sl@0: sl@0: int MAIN(int, char **); sl@0: sl@0: static int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf); sl@0: sl@0: int MAIN(int argc, char **argv) sl@0: { sl@0: int i,badops=0,offset=0,ret=1,j; sl@0: unsigned int length=0; sl@0: long num,tmplen; sl@0: BIO *in=NULL,*out=NULL,*b64=NULL, *derout = NULL; sl@0: int informat,indent=0, noout = 0, dump = 0; sl@0: char *infile=NULL,*str=NULL,*prog,*oidfile=NULL, *derfile=NULL; sl@0: char *genstr=NULL, *genconf=NULL; sl@0: unsigned char *tmpbuf; sl@0: const unsigned char *ctmpbuf; sl@0: BUF_MEM *buf=NULL; sl@0: STACK *osk=NULL; sl@0: ASN1_TYPE *at=NULL; sl@0: sl@0: informat=FORMAT_PEM; sl@0: sl@0: apps_startup(); sl@0: sl@0: if (bio_err == NULL) sl@0: if ((bio_err=BIO_new(BIO_s_file())) != NULL) sl@0: BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT); sl@0: sl@0: if (!load_config(bio_err, NULL)) sl@0: goto end; sl@0: sl@0: prog=argv[0]; sl@0: argc--; sl@0: argv++; sl@0: if ((osk=sk_new_null()) == NULL) sl@0: { sl@0: BIO_printf(bio_err,"Memory allocation failure\n"); sl@0: goto end; sl@0: } sl@0: while (argc >= 1) sl@0: { sl@0: if (strcmp(*argv,"-inform") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: informat=str2fmt(*(++argv)); sl@0: } sl@0: else if (strcmp(*argv,"-in") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: infile= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-out") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: derfile= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-i") == 0) sl@0: { sl@0: indent=1; sl@0: } sl@0: else if (strcmp(*argv,"-noout") == 0) noout = 1; sl@0: else if (strcmp(*argv,"-oid") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: oidfile= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-offset") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: offset= atoi(*(++argv)); sl@0: } sl@0: else if (strcmp(*argv,"-length") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: length= atoi(*(++argv)); sl@0: if (length == 0) goto bad; sl@0: } sl@0: else if (strcmp(*argv,"-dump") == 0) sl@0: { sl@0: dump= -1; sl@0: } sl@0: else if (strcmp(*argv,"-dlimit") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: dump= atoi(*(++argv)); sl@0: if (dump <= 0) goto bad; sl@0: } sl@0: else if (strcmp(*argv,"-strparse") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: sk_push(osk,*(++argv)); sl@0: } sl@0: else if (strcmp(*argv,"-genstr") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: genstr= *(++argv); sl@0: } sl@0: else if (strcmp(*argv,"-genconf") == 0) sl@0: { sl@0: if (--argc < 1) goto bad; sl@0: genconf= *(++argv); sl@0: } sl@0: else sl@0: { sl@0: BIO_printf(bio_err,"unknown option %s\n",*argv); sl@0: badops=1; sl@0: break; sl@0: } sl@0: argc--; sl@0: argv++; sl@0: } sl@0: sl@0: if (badops) sl@0: { sl@0: bad: sl@0: BIO_printf(bio_err,"%s [options] data[num]),BUFSIZ); sl@0: if (i <= 0) break; sl@0: num+=i; sl@0: } sl@0: } sl@0: str=buf->data; sl@0: sl@0: /* If any structs to parse go through in sequence */ sl@0: sl@0: if (sk_num(osk)) sl@0: { sl@0: tmpbuf=(unsigned char *)str; sl@0: tmplen=num; sl@0: for (i=0; ivalue.asn1_string->data; sl@0: tmplen=at->value.asn1_string->length; sl@0: } sl@0: str=(char *)tmpbuf; sl@0: num=tmplen; sl@0: } sl@0: sl@0: if (offset >= num) sl@0: { sl@0: BIO_printf(bio_err, "Error: offset too large\n"); sl@0: goto end; sl@0: } sl@0: sl@0: num -= offset; sl@0: sl@0: if ((length == 0) || ((long)length > num)) length=(unsigned int)num; sl@0: if(derout) { sl@0: if(BIO_write(derout, str + offset, length) != (int)length) { sl@0: BIO_printf(bio_err, "Error writing output\n"); sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: } sl@0: if (!noout && sl@0: !ASN1_parse_dump(out,(unsigned char *)&(str[offset]),length, sl@0: indent,dump)) sl@0: { sl@0: ERR_print_errors(bio_err); sl@0: goto end; sl@0: } sl@0: ret=0; sl@0: end: sl@0: BIO_free(derout); sl@0: if (in != NULL) BIO_free(in); sl@0: if (out != NULL) BIO_free_all(out); sl@0: if (b64 != NULL) BIO_free(b64); sl@0: if (ret != 0) sl@0: ERR_print_errors(bio_err); sl@0: if (buf != NULL) BUF_MEM_free(buf); sl@0: if (at != NULL) ASN1_TYPE_free(at); sl@0: if (osk != NULL) sk_free(osk); sl@0: OBJ_cleanup(); sl@0: apps_shutdown(); sl@0: OPENSSL_EXIT(ret); sl@0: } sl@0: sl@0: static int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf) sl@0: { sl@0: CONF *cnf = NULL; sl@0: int len; sl@0: long errline; sl@0: unsigned char *p; sl@0: ASN1_TYPE *atyp = NULL; sl@0: sl@0: if (genconf) sl@0: { sl@0: cnf = NCONF_new(NULL); sl@0: if (!NCONF_load(cnf, genconf, &errline)) sl@0: goto conferr; sl@0: if (!genstr) sl@0: genstr = NCONF_get_string(cnf, "default", "asn1"); sl@0: if (!genstr) sl@0: { sl@0: BIO_printf(bio, "Can't find 'asn1' in '%s'\n", genconf); sl@0: goto err; sl@0: } sl@0: } sl@0: sl@0: atyp = ASN1_generate_nconf(genstr, cnf); sl@0: NCONF_free(cnf); sl@0: sl@0: if (!atyp) sl@0: return -1; sl@0: sl@0: len = i2d_ASN1_TYPE(atyp, NULL); sl@0: sl@0: if (len <= 0) sl@0: goto err; sl@0: sl@0: if (!BUF_MEM_grow(buf,len)) sl@0: goto err; sl@0: sl@0: p=(unsigned char *)buf->data; sl@0: sl@0: i2d_ASN1_TYPE(atyp, &p); sl@0: sl@0: ASN1_TYPE_free(atyp); sl@0: return len; sl@0: sl@0: conferr: sl@0: sl@0: if (errline > 0) sl@0: BIO_printf(bio, "Error on line %ld of config file '%s'\n", sl@0: errline, genconf); sl@0: else sl@0: BIO_printf(bio, "Error loading config file '%s'\n", genconf); sl@0: sl@0: err: sl@0: NCONF_free(cnf); sl@0: ASN1_TYPE_free(atyp); sl@0: sl@0: return -1; sl@0: sl@0: }