xref: /openbsd-src/lib/libkeynote/keynote-sigver.c (revision b7041c0781c8668129da8084451ded41b0c43954)
1*b7041c07Sderaadt /* $OpenBSD: keynote-sigver.c,v 1.18 2021/10/24 21:24:20 deraadt Exp $ */
2983e9580Sangelos /*
3983e9580Sangelos  * The author of this code is Angelos D. Keromytis (angelos@dsl.cis.upenn.edu)
4983e9580Sangelos  *
5983e9580Sangelos  * This code was written by Angelos D. Keromytis in Philadelphia, PA, USA,
6983e9580Sangelos  * in April-May 1998
7983e9580Sangelos  *
8983e9580Sangelos  * Copyright (C) 1998, 1999 by Angelos D. Keromytis.
9983e9580Sangelos  *
105e4ac158Sderaadt  * Permission to use, copy, and modify this software with or without fee
11983e9580Sangelos  * is hereby granted, provided that this entire notice is included in
12983e9580Sangelos  * all copies of any software which is or includes a copy or
13983e9580Sangelos  * modification of this software.
14983e9580Sangelos  *
15983e9580Sangelos  * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
16983e9580Sangelos  * IMPLIED WARRANTY. IN PARTICULAR, THE AUTHORS MAKES NO
17983e9580Sangelos  * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
18983e9580Sangelos  * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
19983e9580Sangelos  * PURPOSE.
20983e9580Sangelos  */
21983e9580Sangelos 
22983e9580Sangelos #include <sys/types.h>
23983e9580Sangelos #include <sys/stat.h>
24e0758482Smsf 
25983e9580Sangelos #include <ctype.h>
269186b70cSangelos #include <fcntl.h>
2785616838Smsf #include <regex.h>
28e0758482Smsf #include <stdio.h>
29e0758482Smsf #include <stdlib.h>
30e0758482Smsf #include <string.h>
31983e9580Sangelos #include <unistd.h>
32983e9580Sangelos 
33dd34a27eSangelos #include "header.h"
341c338448Sangelos #include "keynote.h"
35983e9580Sangelos 
36aa31cba2Sderaadt void	sigverusage(void);
37aa31cba2Sderaadt 
38983e9580Sangelos void
sigverusage(void)39e482c692Sangelos sigverusage(void)
40983e9580Sangelos {
41983e9580Sangelos     fprintf(stderr, "Arguments:\n");
42983e9580Sangelos     fprintf(stderr, "\t<AssertionFile>\n");
43983e9580Sangelos }
44983e9580Sangelos 
45983e9580Sangelos void
keynote_sigver(int argc,char * argv[])46e482c692Sangelos keynote_sigver(int argc, char *argv[])
47983e9580Sangelos {
48204e95a6Sangelos     char *buf, **assertlist;
49204e95a6Sangelos     int fd, i, n, j;
50983e9580Sangelos     struct stat sb;
51983e9580Sangelos 
52983e9580Sangelos     if (argc != 2)
53983e9580Sangelos     {
54e482c692Sangelos 	sigverusage();
55983e9580Sangelos 	exit(0);
56983e9580Sangelos     }
57983e9580Sangelos 
58983e9580Sangelos     /* Open and read assertion file */
59*b7041c07Sderaadt     fd = open(argv[1], O_RDONLY);
60df69c215Sderaadt     if (fd == -1)
61983e9580Sangelos     {
62ca0ac210Sangelos 	perror(argv[1]);
633cb7a939Sangelos 	exit(1);
64983e9580Sangelos     }
65983e9580Sangelos 
66df69c215Sderaadt     if (fstat(fd, &sb) == -1)
67983e9580Sangelos     {
68983e9580Sangelos 	perror("fstat()");
693cb7a939Sangelos 	exit(1);
70983e9580Sangelos     }
71983e9580Sangelos 
72983e9580Sangelos     if (sb.st_size == 0) /* Paranoid */
73983e9580Sangelos     {
74983e9580Sangelos 	fprintf(stderr, "Illegal assertion-file size 0\n");
753cb7a939Sangelos 	exit(1);
76983e9580Sangelos     }
77983e9580Sangelos 
78313d0fe7Smmcc     buf = calloc(sb.st_size + 1, sizeof(char));
79313d0fe7Smmcc     if (buf == NULL)
80983e9580Sangelos     {
81983e9580Sangelos 	perror("calloc()");
823cb7a939Sangelos 	exit(1);
83983e9580Sangelos     }
84983e9580Sangelos 
85df69c215Sderaadt     if (read(fd, buf, sb.st_size) == -1)
86983e9580Sangelos     {
87983e9580Sangelos 	perror("read()");
883cb7a939Sangelos 	exit(1);
89983e9580Sangelos     }
90983e9580Sangelos 
91983e9580Sangelos     close(fd);
92983e9580Sangelos 
93204e95a6Sangelos     assertlist = kn_read_asserts(buf, sb.st_size, &n);
94805e681cSangelos     if (assertlist == NULL)
95204e95a6Sangelos     {
96204e95a6Sangelos       	fprintf(stderr, "Out of memory while allocating memory for "
97204e95a6Sangelos 		"assertions.\n");
983cb7a939Sangelos 	exit(1);
99805e681cSangelos     }
100805e681cSangelos 
101805e681cSangelos     if (n == 0)
102805e681cSangelos     {
103805e681cSangelos 	fprintf(stderr, "No assertions found in %s.\n", argv[1]);
104805e681cSangelos 	free(assertlist);
1053cb7a939Sangelos 	exit(1);
106204e95a6Sangelos     }
107204e95a6Sangelos 
108204e95a6Sangelos     free(buf);
109204e95a6Sangelos 
110204e95a6Sangelos     for (j = 0; j < n; j++)
111204e95a6Sangelos     {
112204e95a6Sangelos 	i = kn_verify_assertion(assertlist[j], strlen(assertlist[j]));
113983e9580Sangelos 	if (i == -1)
114983e9580Sangelos 	{
115983e9580Sangelos 	    switch (keynote_errno)
116983e9580Sangelos 	    {
117983e9580Sangelos 		case ERROR_MEMORY:
118983e9580Sangelos 		    fprintf(stderr,
119204e95a6Sangelos 			    "Out of memory while parsing assertion %d.\n", j);
120983e9580Sangelos 		    break;
121983e9580Sangelos 
122983e9580Sangelos 		case ERROR_SYNTAX:
123983e9580Sangelos 		    fprintf(stderr,
124204e95a6Sangelos 			    "Syntax error while parsing assertion %d.\n", j);
125983e9580Sangelos 		    break;
126983e9580Sangelos 
127983e9580Sangelos 		default:
128983e9580Sangelos 		    fprintf(stderr,
129204e95a6Sangelos 			    "Unknown error while parsing assertion %d.\n", j);
130983e9580Sangelos 	    }
131983e9580Sangelos 	}
132204e95a6Sangelos 	else
133204e95a6Sangelos 	{
134983e9580Sangelos 	    if (i == SIGRESULT_TRUE)
135204e95a6Sangelos 	      fprintf(stdout, "Signature on assertion %d verified.\n", j);
136983e9580Sangelos 	    else
137983e9580Sangelos 	    {
138983e9580Sangelos 		if (keynote_errno != 0)
139204e95a6Sangelos 		  fprintf(stdout,
140204e95a6Sangelos 			  "Signature on assertion %d could not be verified "
141204e95a6Sangelos 			  "(keynote_errno = %d).\n", j, keynote_errno);
142983e9580Sangelos 		else
143204e95a6Sangelos 		  fprintf(stdout,
144204e95a6Sangelos 			  "Signature on assertion %d did not verify!\n", j);
145983e9580Sangelos 	    }
146204e95a6Sangelos 	}
147204e95a6Sangelos 
148204e95a6Sangelos 	free(assertlist[j]);
149204e95a6Sangelos     }
150204e95a6Sangelos 
151204e95a6Sangelos     free(assertlist);
152983e9580Sangelos 
153983e9580Sangelos     exit(0);
154983e9580Sangelos }
155