xref: /openbsd/lib/libkeynote/keynote-sigver.c (revision 404b540a)
1 /* $OpenBSD: keynote-sigver.c,v 1.15 2004/06/29 11:35:56 msf 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 with or 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 #include <sys/types.h>
23 #include <sys/stat.h>
24 
25 #include <ctype.h>
26 #include <fcntl.h>
27 #include <regex.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32 
33 #include "header.h"
34 #include "keynote.h"
35 
36 void	sigverusage(void);
37 
38 void
39 sigverusage(void)
40 {
41     fprintf(stderr, "Arguments:\n");
42     fprintf(stderr, "\t<AssertionFile>\n");
43 }
44 
45 void
46 keynote_sigver(int argc, char *argv[])
47 {
48     char *buf, **assertlist;
49     int fd, i, n, j;
50     struct stat sb;
51 
52     if (argc != 2)
53     {
54 	sigverusage();
55 	exit(0);
56     }
57 
58     /* Open and read assertion file */
59     fd = open(argv[1], O_RDONLY, 0);
60     if (fd < 0)
61     {
62 	perror(argv[1]);
63 	exit(1);
64     }
65 
66     if (fstat(fd, &sb) < 0)
67     {
68 	perror("fstat()");
69 	exit(1);
70     }
71 
72     if (sb.st_size == 0) /* Paranoid */
73     {
74 	fprintf(stderr, "Illegal assertion-file size 0\n");
75 	exit(1);
76     }
77 
78     buf = (char *) calloc(sb.st_size + 1, sizeof(char));
79     if (buf == (char *) NULL)
80     {
81 	perror("calloc()");
82 	exit(1);
83     }
84 
85     if (read(fd, buf, sb.st_size) < 0)
86     {
87 	perror("read()");
88 	exit(1);
89     }
90 
91     close(fd);
92 
93     assertlist = kn_read_asserts(buf, sb.st_size, &n);
94     if (assertlist == NULL)
95     {
96       	fprintf(stderr, "Out of memory while allocating memory for "
97 		"assertions.\n");
98 	exit(1);
99     }
100 
101     if (n == 0)
102     {
103 	fprintf(stderr, "No assertions found in %s.\n", argv[1]);
104 	free(assertlist);
105 	exit(1);
106     }
107 
108     free(buf);
109 
110     for (j = 0; j < n; j++)
111     {
112 	i = kn_verify_assertion(assertlist[j], strlen(assertlist[j]));
113 	if (i == -1)
114 	{
115 	    switch (keynote_errno)
116 	    {
117 		case ERROR_MEMORY:
118 		    fprintf(stderr,
119 			    "Out of memory while parsing assertion %d.\n", j);
120 		    break;
121 
122 		case ERROR_SYNTAX:
123 		    fprintf(stderr,
124 			    "Syntax error while parsing assertion %d.\n", j);
125 		    break;
126 
127 		default:
128 		    fprintf(stderr,
129 			    "Unknown error while parsing assertion %d.\n", j);
130 	    }
131 	}
132 	else
133 	{
134 	    if (i == SIGRESULT_TRUE)
135 	      fprintf(stdout, "Signature on assertion %d verified.\n", j);
136 	    else
137 	    {
138 		if (keynote_errno != 0)
139 		  fprintf(stdout,
140 			  "Signature on assertion %d could not be verified "
141 			  "(keynote_errno = %d).\n", j, keynote_errno);
142 		else
143 		  fprintf(stdout,
144 			  "Signature on assertion %d did not verify!\n", j);
145 	    }
146 	}
147 
148 	free(assertlist[j]);
149     }
150 
151     free(assertlist);
152 
153     exit(0);
154 }
155