1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/tsrc/topenssl/src/asn1pars.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,448 @@
1.4 +/* apps/asn1pars.c */
1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
1.6 + * All rights reserved.
1.7 + *
1.8 + * This package is an SSL implementation written
1.9 + * by Eric Young (eay@cryptsoft.com).
1.10 + * The implementation was written so as to conform with Netscapes SSL.
1.11 + *
1.12 + * This library is free for commercial and non-commercial use as long as
1.13 + * the following conditions are aheared to. The following conditions
1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
1.15 + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
1.16 + * included with this distribution is covered by the same copyright terms
1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1.18 + *
1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
1.20 + * the code are not to be removed.
1.21 + * If this package is used in a product, Eric Young should be given attribution
1.22 + * as the author of the parts of the library used.
1.23 + * This can be in the form of a textual message at program startup or
1.24 + * in documentation (online or textual) provided with the package.
1.25 + *
1.26 + * Redistribution and use in source and binary forms, with or without
1.27 + * modification, are permitted provided that the following conditions
1.28 + * are met:
1.29 + * 1. Redistributions of source code must retain the copyright
1.30 + * notice, this list of conditions and the following disclaimer.
1.31 + * 2. Redistributions in binary form must reproduce the above copyright
1.32 + * notice, this list of conditions and the following disclaimer in the
1.33 + * documentation and/or other materials provided with the distribution.
1.34 + * 3. All advertising materials mentioning features or use of this software
1.35 + * must display the following acknowledgement:
1.36 + * "This product includes cryptographic software written by
1.37 + * Eric Young (eay@cryptsoft.com)"
1.38 + * The word 'cryptographic' can be left out if the rouines from the library
1.39 + * being used are not cryptographic related :-).
1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from
1.41 + * the apps directory (application code) you must include an acknowledgement:
1.42 + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
1.43 + *
1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.47 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.54 + * SUCH DAMAGE.
1.55 + *
1.56 + * The licence and distribution terms for any publically available version or
1.57 + * derivative of this code cannot be changed. i.e. this code cannot simply be
1.58 + * copied and put under another distribution licence
1.59 + * [including the GNU Public Licence.]
1.60 + */
1.61 +
1.62 +/* A nice addition from Dr Stephen Henson <shenson@bigfoot.com> to
1.63 + * add the -strparse option which parses nested binary structures
1.64 + */
1.65 +
1.66 +#include <stdio.h>
1.67 +#include <stdlib.h>
1.68 +#include <string.h>
1.69 +#include "apps.h"
1.70 +#include <openssl/err.h>
1.71 +#include <openssl/evp.h>
1.72 +#include <openssl/x509.h>
1.73 +#include <openssl/pem.h>
1.74 +
1.75 +/* -inform arg - input format - default PEM (DER or PEM)
1.76 + * -in arg - input file - default stdin
1.77 + * -i - indent the details by depth
1.78 + * -offset - where in the file to start
1.79 + * -length - how many bytes to use
1.80 + * -oid file - extra oid description file
1.81 + */
1.82 +
1.83 +#undef PROG
1.84 +#define PROG asn1parse_main
1.85 +
1.86 +
1.87 +
1.88 +int MAIN(int, char **);
1.89 +
1.90 +static int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf);
1.91 +
1.92 +int MAIN(int argc, char **argv)
1.93 + {
1.94 + int i,badops=0,offset=0,ret=1,j;
1.95 + unsigned int length=0;
1.96 + long num,tmplen;
1.97 + BIO *in=NULL,*out=NULL,*b64=NULL, *derout = NULL;
1.98 + int informat,indent=0, noout = 0, dump = 0;
1.99 + char *infile=NULL,*str=NULL,*prog,*oidfile=NULL, *derfile=NULL;
1.100 + char *genstr=NULL, *genconf=NULL;
1.101 + unsigned char *tmpbuf;
1.102 + const unsigned char *ctmpbuf;
1.103 + BUF_MEM *buf=NULL;
1.104 + STACK *osk=NULL;
1.105 + ASN1_TYPE *at=NULL;
1.106 +
1.107 + informat=FORMAT_PEM;
1.108 +
1.109 + apps_startup();
1.110 +
1.111 + if (bio_err == NULL)
1.112 + if ((bio_err=BIO_new(BIO_s_file())) != NULL)
1.113 + BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
1.114 +
1.115 + if (!load_config(bio_err, NULL))
1.116 + goto end;
1.117 +
1.118 + prog=argv[0];
1.119 + argc--;
1.120 + argv++;
1.121 + if ((osk=sk_new_null()) == NULL)
1.122 + {
1.123 + BIO_printf(bio_err,"Memory allocation failure\n");
1.124 + goto end;
1.125 + }
1.126 + while (argc >= 1)
1.127 + {
1.128 + if (strcmp(*argv,"-inform") == 0)
1.129 + {
1.130 + if (--argc < 1) goto bad;
1.131 + informat=str2fmt(*(++argv));
1.132 + }
1.133 + else if (strcmp(*argv,"-in") == 0)
1.134 + {
1.135 + if (--argc < 1) goto bad;
1.136 + infile= *(++argv);
1.137 + }
1.138 + else if (strcmp(*argv,"-out") == 0)
1.139 + {
1.140 + if (--argc < 1) goto bad;
1.141 + derfile= *(++argv);
1.142 + }
1.143 + else if (strcmp(*argv,"-i") == 0)
1.144 + {
1.145 + indent=1;
1.146 + }
1.147 + else if (strcmp(*argv,"-noout") == 0) noout = 1;
1.148 + else if (strcmp(*argv,"-oid") == 0)
1.149 + {
1.150 + if (--argc < 1) goto bad;
1.151 + oidfile= *(++argv);
1.152 + }
1.153 + else if (strcmp(*argv,"-offset") == 0)
1.154 + {
1.155 + if (--argc < 1) goto bad;
1.156 + offset= atoi(*(++argv));
1.157 + }
1.158 + else if (strcmp(*argv,"-length") == 0)
1.159 + {
1.160 + if (--argc < 1) goto bad;
1.161 + length= atoi(*(++argv));
1.162 + if (length == 0) goto bad;
1.163 + }
1.164 + else if (strcmp(*argv,"-dump") == 0)
1.165 + {
1.166 + dump= -1;
1.167 + }
1.168 + else if (strcmp(*argv,"-dlimit") == 0)
1.169 + {
1.170 + if (--argc < 1) goto bad;
1.171 + dump= atoi(*(++argv));
1.172 + if (dump <= 0) goto bad;
1.173 + }
1.174 + else if (strcmp(*argv,"-strparse") == 0)
1.175 + {
1.176 + if (--argc < 1) goto bad;
1.177 + sk_push(osk,*(++argv));
1.178 + }
1.179 + else if (strcmp(*argv,"-genstr") == 0)
1.180 + {
1.181 + if (--argc < 1) goto bad;
1.182 + genstr= *(++argv);
1.183 + }
1.184 + else if (strcmp(*argv,"-genconf") == 0)
1.185 + {
1.186 + if (--argc < 1) goto bad;
1.187 + genconf= *(++argv);
1.188 + }
1.189 + else
1.190 + {
1.191 + BIO_printf(bio_err,"unknown option %s\n",*argv);
1.192 + badops=1;
1.193 + break;
1.194 + }
1.195 + argc--;
1.196 + argv++;
1.197 + }
1.198 +
1.199 + if (badops)
1.200 + {
1.201 +bad:
1.202 + BIO_printf(bio_err,"%s [options] <infile\n",prog);
1.203 + BIO_printf(bio_err,"where options are\n");
1.204 + BIO_printf(bio_err," -inform arg input format - one of DER PEM\n");
1.205 + BIO_printf(bio_err," -in arg input file\n");
1.206 + BIO_printf(bio_err," -out arg output file (output format is always DER\n");
1.207 + BIO_printf(bio_err," -noout arg don't produce any output\n");
1.208 + BIO_printf(bio_err," -offset arg offset into file\n");
1.209 + BIO_printf(bio_err," -length arg length of section in file\n");
1.210 + BIO_printf(bio_err," -i indent entries\n");
1.211 + BIO_printf(bio_err," -dump dump unknown data in hex form\n");
1.212 + BIO_printf(bio_err," -dlimit arg dump the first arg bytes of unknown data in hex form\n");
1.213 + BIO_printf(bio_err," -oid file file of extra oid definitions\n");
1.214 + BIO_printf(bio_err," -strparse offset\n");
1.215 + BIO_printf(bio_err," a series of these can be used to 'dig' into multiple\n");
1.216 + BIO_printf(bio_err," ASN1 blob wrappings\n");
1.217 + BIO_printf(bio_err," -genstr str string to generate ASN1 structure from\n");
1.218 + BIO_printf(bio_err," -genconf file file to generate ASN1 structure from\n");
1.219 + goto end;
1.220 + }
1.221 +
1.222 + ERR_load_crypto_strings();
1.223 +
1.224 + in=BIO_new(BIO_s_file());
1.225 + out=BIO_new(BIO_s_file());
1.226 + if ((in == NULL) || (out == NULL))
1.227 + {
1.228 + ERR_print_errors(bio_err);
1.229 + goto end;
1.230 + }
1.231 + BIO_set_fp(out,stdout,BIO_NOCLOSE|BIO_FP_TEXT);
1.232 +
1.233 +#ifdef OPENSSL_SYS_VMS
1.234 + {
1.235 + BIO *tmpbio = BIO_new(BIO_f_linebuffer());
1.236 + out = BIO_push(tmpbio, out);
1.237 + }
1.238 +#endif
1.239 +
1.240 + if (oidfile != NULL)
1.241 + {
1.242 + if (BIO_read_filename(in,oidfile) <= 0)
1.243 + {
1.244 + BIO_printf(bio_err,"problems opening %s\n",oidfile);
1.245 + ERR_print_errors(bio_err);
1.246 + goto end;
1.247 + }
1.248 + OBJ_create_objects(in);
1.249 + }
1.250 +
1.251 + if (infile == NULL)
1.252 + BIO_set_fp(in,stdin,BIO_NOCLOSE);
1.253 +
1.254 + else
1.255 + {
1.256 + if (BIO_read_filename(in,infile) <= 0)
1.257 + {
1.258 + perror(infile);
1.259 + goto end;
1.260 + }
1.261 + }
1.262 +
1.263 + if (derfile) {
1.264 + if(!(derout = BIO_new_file(derfile, "wb"))) {
1.265 + BIO_printf(bio_err,"problems opening %s\n",derfile);
1.266 + ERR_print_errors(bio_err);
1.267 + goto end;
1.268 + }
1.269 + }
1.270 +
1.271 + if ((buf=BUF_MEM_new()) == NULL) goto end;
1.272 + if (!BUF_MEM_grow(buf,BUFSIZ*8)) goto end; /* Pre-allocate :-) */
1.273 +
1.274 + if (genstr || genconf)
1.275 + {
1.276 + num = do_generate(bio_err, genstr, genconf, buf);
1.277 + if (num < 0)
1.278 + {
1.279 + ERR_print_errors(bio_err);
1.280 + goto end;
1.281 + }
1.282 + }
1.283 +
1.284 + else
1.285 + {
1.286 +
1.287 + if (informat == FORMAT_PEM)
1.288 + {
1.289 + BIO *tmp;
1.290 +
1.291 + if ((b64=BIO_new(BIO_f_base64())) == NULL)
1.292 + goto end;
1.293 + BIO_push(b64,in);
1.294 + tmp=in;
1.295 + in=b64;
1.296 + b64=tmp;
1.297 + }
1.298 +
1.299 + num=0;
1.300 + for (;;)
1.301 + {
1.302 + if (!BUF_MEM_grow(buf,(int)num+BUFSIZ)) goto end;
1.303 + i=BIO_read(in,&(buf->data[num]),BUFSIZ);
1.304 + if (i <= 0) break;
1.305 + num+=i;
1.306 + }
1.307 + }
1.308 + str=buf->data;
1.309 +
1.310 + /* If any structs to parse go through in sequence */
1.311 +
1.312 + if (sk_num(osk))
1.313 + {
1.314 + tmpbuf=(unsigned char *)str;
1.315 + tmplen=num;
1.316 + for (i=0; i<sk_num(osk); i++)
1.317 + {
1.318 + ASN1_TYPE *atmp;
1.319 + int typ;
1.320 + j=atoi(sk_value(osk,i));
1.321 + if (j == 0)
1.322 + {
1.323 + BIO_printf(bio_err,"'%s' is an invalid number\n",sk_value(osk,i));
1.324 + continue;
1.325 + }
1.326 + tmpbuf+=j;
1.327 + tmplen-=j;
1.328 + atmp = at;
1.329 + ctmpbuf = tmpbuf;
1.330 + at = d2i_ASN1_TYPE(NULL,&ctmpbuf,tmplen);
1.331 + ASN1_TYPE_free(atmp);
1.332 + if(!at)
1.333 + {
1.334 + BIO_printf(bio_err,"Error parsing structure\n");
1.335 + ERR_print_errors(bio_err);
1.336 + goto end;
1.337 + }
1.338 + typ = ASN1_TYPE_get(at);
1.339 + if ((typ == V_ASN1_OBJECT)
1.340 + || (typ == V_ASN1_NULL))
1.341 + {
1.342 + BIO_printf(bio_err, "Can't parse %s type\n",
1.343 + typ == V_ASN1_NULL ? "NULL" : "OBJECT");
1.344 + ERR_print_errors(bio_err);
1.345 + goto end;
1.346 + }
1.347 + /* hmm... this is a little evil but it works */
1.348 + tmpbuf=at->value.asn1_string->data;
1.349 + tmplen=at->value.asn1_string->length;
1.350 + }
1.351 + str=(char *)tmpbuf;
1.352 + num=tmplen;
1.353 + }
1.354 +
1.355 + if (offset >= num)
1.356 + {
1.357 + BIO_printf(bio_err, "Error: offset too large\n");
1.358 + goto end;
1.359 + }
1.360 +
1.361 + num -= offset;
1.362 +
1.363 + if ((length == 0) || ((long)length > num)) length=(unsigned int)num;
1.364 + if(derout) {
1.365 + if(BIO_write(derout, str + offset, length) != (int)length) {
1.366 + BIO_printf(bio_err, "Error writing output\n");
1.367 + ERR_print_errors(bio_err);
1.368 + goto end;
1.369 + }
1.370 + }
1.371 + if (!noout &&
1.372 + !ASN1_parse_dump(out,(unsigned char *)&(str[offset]),length,
1.373 + indent,dump))
1.374 + {
1.375 + ERR_print_errors(bio_err);
1.376 + goto end;
1.377 + }
1.378 + ret=0;
1.379 +end:
1.380 + BIO_free(derout);
1.381 + if (in != NULL) BIO_free(in);
1.382 + if (out != NULL) BIO_free_all(out);
1.383 + if (b64 != NULL) BIO_free(b64);
1.384 + if (ret != 0)
1.385 + ERR_print_errors(bio_err);
1.386 + if (buf != NULL) BUF_MEM_free(buf);
1.387 + if (at != NULL) ASN1_TYPE_free(at);
1.388 + if (osk != NULL) sk_free(osk);
1.389 + OBJ_cleanup();
1.390 + apps_shutdown();
1.391 + OPENSSL_EXIT(ret);
1.392 + }
1.393 +
1.394 +static int do_generate(BIO *bio, char *genstr, char *genconf, BUF_MEM *buf)
1.395 + {
1.396 + CONF *cnf = NULL;
1.397 + int len;
1.398 + long errline;
1.399 + unsigned char *p;
1.400 + ASN1_TYPE *atyp = NULL;
1.401 +
1.402 + if (genconf)
1.403 + {
1.404 + cnf = NCONF_new(NULL);
1.405 + if (!NCONF_load(cnf, genconf, &errline))
1.406 + goto conferr;
1.407 + if (!genstr)
1.408 + genstr = NCONF_get_string(cnf, "default", "asn1");
1.409 + if (!genstr)
1.410 + {
1.411 + BIO_printf(bio, "Can't find 'asn1' in '%s'\n", genconf);
1.412 + goto err;
1.413 + }
1.414 + }
1.415 +
1.416 + atyp = ASN1_generate_nconf(genstr, cnf);
1.417 + NCONF_free(cnf);
1.418 +
1.419 + if (!atyp)
1.420 + return -1;
1.421 +
1.422 + len = i2d_ASN1_TYPE(atyp, NULL);
1.423 +
1.424 + if (len <= 0)
1.425 + goto err;
1.426 +
1.427 + if (!BUF_MEM_grow(buf,len))
1.428 + goto err;
1.429 +
1.430 + p=(unsigned char *)buf->data;
1.431 +
1.432 + i2d_ASN1_TYPE(atyp, &p);
1.433 +
1.434 + ASN1_TYPE_free(atyp);
1.435 + return len;
1.436 +
1.437 + conferr:
1.438 +
1.439 + if (errline > 0)
1.440 + BIO_printf(bio, "Error on line %ld of config file '%s'\n",
1.441 + errline, genconf);
1.442 + else
1.443 + BIO_printf(bio, "Error loading config file '%s'\n", genconf);
1.444 +
1.445 + err:
1.446 + NCONF_free(cnf);
1.447 + ASN1_TYPE_free(atyp);
1.448 +
1.449 + return -1;
1.450 +
1.451 + }