summaryrefslogtreecommitdiff
path: root/lib/libkeynote/keynote-sigver.c
blob: 5fa864bcfa736d0970ffa9c129aed4f5c14a02bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/* $OpenBSD: keynote-sigver.c,v 1.11 2001/03/08 21:50:12 angelos Exp $ */
/*
 * The author of this code is Angelos D. Keromytis (angelos@dsl.cis.upenn.edu)
 *
 * This code was written by Angelos D. Keromytis in Philadelphia, PA, USA,
 * in April-May 1998
 *
 * Copyright (C) 1998, 1999 by Angelos D. Keromytis.
 *	
 * Permission to use, copy, and modify this software without fee
 * is hereby granted, provided that this entire notice is included in
 * all copies of any software which is or includes a copy or
 * modification of this software. 
 *
 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTY. IN PARTICULAR, THE AUTHORS MAKES NO
 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE
 * MERCHANTABILITY OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR
 * PURPOSE.
 */

#if HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

#if STDC_HEADERS
#include <string.h>
#endif /* STDC_HEADERS */

#if HAVE_FCNTL_H
#include <fcntl.h>
#endif /* HAVE_FCNTL_H */

#if HAVE_IO_H
#include <io.h>
#elif HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_IO_H */

#include "header.h"
#include "keynote.h"

void
sigverusage(void)
{
    fprintf(stderr, "Arguments:\n");
    fprintf(stderr, "\t<AssertionFile>\n");
}

void
keynote_sigver(int argc, char *argv[])
{
    char *buf, **assertlist;
    int fd, i, n, j;
    struct stat sb;

    if (argc != 2)
    {
	sigverusage();
	exit(0);
    }

    /* Open and read assertion file */
    fd = open(argv[1], O_RDONLY, 0);
    if (fd < 0)
    {
	perror(argv[1]);
	exit(1);
    }

    if (fstat(fd, &sb) < 0)
    {
	perror("fstat()");
	exit(1);
    }

    if (sb.st_size == 0) /* Paranoid */
    {
	fprintf(stderr, "Illegal assertion-file size 0\n");
	exit(1);
    }

    buf = (char *) calloc(sb.st_size + 1, sizeof(char));
    if (buf == (char *) NULL)
    {
	perror("calloc()");
	exit(1);
    }

    if (read(fd, buf, sb.st_size) < 0)
    {
	perror("read()");
	exit(1);
    }

    close(fd);

    assertlist = kn_read_asserts(buf, sb.st_size, &n);
    if (assertlist == NULL)
    {
      	fprintf(stderr, "Out of memory while allocating memory for "
		"assertions.\n");
	exit(1);
    }

    if (n == 0)
    {
	fprintf(stderr, "No assertions found in %s.\n", argv[1]);
	free(assertlist);
	exit(1);
    }

    free(buf);

    for (j = 0; j < n; j++)
    {
	i = kn_verify_assertion(assertlist[j], strlen(assertlist[j]));
	if (i == -1)
	{
	    switch (keynote_errno)
	    {
		case ERROR_MEMORY:
		    fprintf(stderr,
			    "Out of memory while parsing assertion %d.\n", j);
		    break;

		case ERROR_SYNTAX:
		    fprintf(stderr,
			    "Syntax error while parsing assertion %d.\n", j);
		    break;

		default:
		    fprintf(stderr,
			    "Unknown error while parsing assertion %d.\n", j);
	    }
	}
	else
	{
	    if (i == SIGRESULT_TRUE)
	      fprintf(stdout, "Signature on assertion %d verified.\n", j);
	    else
	    {
		if (keynote_errno != 0)
		  fprintf(stdout,
			  "Signature on assertion %d could not be verified "
			  "(keynote_errno = %d).\n", j, keynote_errno);
		else
		  fprintf(stdout,
			  "Signature on assertion %d did not verify!\n", j);
	    }
	}

	free(assertlist[j]);
    }

    free(assertlist);

    exit(0);
}