xref: /openbsd-src/lib/libkeynote/keynote-sigver.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /* $OpenBSD: keynote-sigver.c,v 1.11 2001/03/08 21:50:12 angelos Exp $ */
2 /*
3  * The author of this code is Angelos D. Keromytis (angelos@dsl.cis.upenn.edu)
4  *
5  * This code was written by Angelos D. Keromytis in Philadelphia, PA, USA,
6  * in April-May 1998
7  *
8  * Copyright (C) 1998, 1999 by Angelos D. Keromytis.
9  *
10  * Permission to use, copy, and modify this software without fee
11  * is hereby granted, provided that this entire notice is included in
12  * all copies of any software which is or includes a copy or
13  * modification of this software.
14  *
15  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTY. IN PARTICULAR, THE AUTHORS MAKES NO
17  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
18  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
19  * PURPOSE.
20  */
21 
22 #if HAVE_CONFIG_H
23 #include "config.h"
24 #endif /* HAVE_CONFIG_H */
25 
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <ctype.h>
31 
32 #if STDC_HEADERS
33 #include <string.h>
34 #endif /* STDC_HEADERS */
35 
36 #if HAVE_FCNTL_H
37 #include <fcntl.h>
38 #endif /* HAVE_FCNTL_H */
39 
40 #if HAVE_IO_H
41 #include <io.h>
42 #elif HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif /* HAVE_IO_H */
45 
46 #include "header.h"
47 #include "keynote.h"
48 
49 void
50 sigverusage(void)
51 {
52     fprintf(stderr, "Arguments:\n");
53     fprintf(stderr, "\t<AssertionFile>\n");
54 }
55 
56 void
57 keynote_sigver(int argc, char *argv[])
58 {
59     char *buf, **assertlist;
60     int fd, i, n, j;
61     struct stat sb;
62 
63     if (argc != 2)
64     {
65 	sigverusage();
66 	exit(0);
67     }
68 
69     /* Open and read assertion file */
70     fd = open(argv[1], O_RDONLY, 0);
71     if (fd < 0)
72     {
73 	perror(argv[1]);
74 	exit(1);
75     }
76 
77     if (fstat(fd, &sb) < 0)
78     {
79 	perror("fstat()");
80 	exit(1);
81     }
82 
83     if (sb.st_size == 0) /* Paranoid */
84     {
85 	fprintf(stderr, "Illegal assertion-file size 0\n");
86 	exit(1);
87     }
88 
89     buf = (char *) calloc(sb.st_size + 1, sizeof(char));
90     if (buf == (char *) NULL)
91     {
92 	perror("calloc()");
93 	exit(1);
94     }
95 
96     if (read(fd, buf, sb.st_size) < 0)
97     {
98 	perror("read()");
99 	exit(1);
100     }
101 
102     close(fd);
103 
104     assertlist = kn_read_asserts(buf, sb.st_size, &n);
105     if (assertlist == NULL)
106     {
107       	fprintf(stderr, "Out of memory while allocating memory for "
108 		"assertions.\n");
109 	exit(1);
110     }
111 
112     if (n == 0)
113     {
114 	fprintf(stderr, "No assertions found in %s.\n", argv[1]);
115 	free(assertlist);
116 	exit(1);
117     }
118 
119     free(buf);
120 
121     for (j = 0; j < n; j++)
122     {
123 	i = kn_verify_assertion(assertlist[j], strlen(assertlist[j]));
124 	if (i == -1)
125 	{
126 	    switch (keynote_errno)
127 	    {
128 		case ERROR_MEMORY:
129 		    fprintf(stderr,
130 			    "Out of memory while parsing assertion %d.\n", j);
131 		    break;
132 
133 		case ERROR_SYNTAX:
134 		    fprintf(stderr,
135 			    "Syntax error while parsing assertion %d.\n", j);
136 		    break;
137 
138 		default:
139 		    fprintf(stderr,
140 			    "Unknown error while parsing assertion %d.\n", j);
141 	    }
142 	}
143 	else
144 	{
145 	    if (i == SIGRESULT_TRUE)
146 	      fprintf(stdout, "Signature on assertion %d verified.\n", j);
147 	    else
148 	    {
149 		if (keynote_errno != 0)
150 		  fprintf(stdout,
151 			  "Signature on assertion %d could not be verified "
152 			  "(keynote_errno = %d).\n", j, keynote_errno);
153 		else
154 		  fprintf(stdout,
155 			  "Signature on assertion %d did not verify!\n", j);
156 	    }
157 	}
158 
159 	free(assertlist[j]);
160     }
161 
162     free(assertlist);
163 
164     exit(0);
165 }
166