• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the License); you may
5  *  not use this file except in compliance with the License.
6  *
7  *  http://www.apache.org/licenses/LICENSE-2.0
8  */
9 
10 
11 #include <stdio.h>
12 #include <errno.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <sys/stat.h>
16 #include <gmssl/x509.h>
17 #include <gmssl/x509_crl.h>
18 
19 
20 static const char *options = "-in file [-out file]";
21 
crlparse_main(int argc,char ** argv)22 int crlparse_main(int argc, char **argv)
23 {
24 	int ret = 1;
25 	char *prog = argv[0];
26 	char *infile = NULL;
27 	char *outfile = NULL;
28 	FILE *infp = stdin;
29 	FILE *outfp = stdout;
30 	struct stat st;
31 	uint8_t *in = NULL;
32 	size_t inlen;
33 	const uint8_t *pin;
34 	const uint8_t *crl = NULL;
35 	size_t crllen;
36 
37 	argc--;
38 	argv++;
39 
40 	if (argc < 1) {
41 		fprintf(stderr, "usage: %s %s\n", prog, options);
42 		return 1;
43 	}
44 
45 	while (argc > 0) {
46 		if (!strcmp(*argv, "-help")) {
47 			printf("usage: %s %s\n", prog, options);
48 			goto end;
49 		} else if (!strcmp(*argv, "-in")) {
50 			if (--argc < 1) goto bad;
51 			infile = *(++argv);
52 			if (!(infp = fopen(infile, "r"))) {
53 				fprintf(stderr, "%s: open '%s' failure : %s\n", prog, infile, strerror(errno));
54 				goto end;
55 			}
56 		} else if (!strcmp(*argv, "-out")) {
57 			if (--argc < 1) goto bad;
58 			outfile = *(++argv);
59 			if (!(outfp = fopen(outfile, "w"))) {
60 				fprintf(stderr, "%s: open '%s' failure : %s\n", prog, outfile, strerror(errno));
61 				goto end;
62 			}
63 		} else {
64 			fprintf(stderr, "%s: illegal option '%s'\n", prog, *argv);
65 			goto end;
66 bad:
67 			fprintf(stderr, "%s: '%s' option value missing\n", prog, *argv);
68 			goto end;
69 		}
70 
71 		argc--;
72 		argv++;
73 	}
74 
75 	if (!infile) {
76 		fprintf(stderr, "%s: '-in' option required\n", prog);
77 		goto end;
78 	}
79 	if (fstat(fileno(infp), &st) < 0) {
80 		fprintf(stderr, "%s: access file error : %s\n", prog, strerror(errno));
81 		goto end;
82 	}
83 	if ((inlen = st.st_size) <= 0) {
84 		fprintf(stderr, "%s: invalid input length\n", prog);
85 		goto end;
86 	}
87 	if (!(in = malloc(inlen))) {
88 		fprintf(stderr, "%s: malloc failure\n", prog);
89 		goto end;
90 	}
91 	if (fread(in, 1, inlen, infp) != inlen) {
92 		fprintf(stderr, "%s: read file error : %s\n",  prog, strerror(errno));
93 		goto end;
94 	}
95 	pin = in;
96 	if (x509_crl_from_der(&crl, &crllen, &pin, &inlen) != 1
97 		|| asn1_length_is_zero(inlen) != 1) {
98 		fprintf(stderr, "%s: read CRL failure\n", prog);
99 		goto end;
100 	}
101 	x509_crl_print(outfp, 0, 0, "CRL", crl, crllen);
102 
103 end:
104 	if (infile && infp) fclose(infp);
105 	if (outfile && outfp) fclose(outfp);
106 	if (in) free(in);
107 	return ret;
108 }
109