summaryrefslogtreecommitdiff
path: root/lib/libkeynote/keynote-sigver.c
blob: a7eafdec2aaab2a59651a7274bc00beac236ff8d (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
/* $OpenBSD: keynote-sigver.c,v 1.2 1999/05/24 01:29:22 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.
 */

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

#ifdef WIN32
#include <ctype.h>
#include <io.h>
#else /* WIN32 */
#include <unistd.h>
#endif /* WIN32 */

#include "assertion.h"
#include "signature.h"

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

#ifdef WIN32
void
#else /* WIN32 */
int
#endif /* WIN32 */
keynote_sigver(int argc, char *argv[])
{
    struct stat sb;
    int fd, i;
    char *buf;

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

    /* Open and read assertion file */
    fd = open(argv[1], O_RDONLY, 0);
    if (fd < 0)
    {
	perror("open()");
	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);

    i = kn_verify_assertion(buf, sb.st_size);
    if (i == -1)
    {
	switch (keynote_errno)
	{
	    case ERROR_MEMORY:
		fprintf(stderr,
			"Out of memory while parsing the assertion.\n");
		break;

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

	    default:
		fprintf(stderr,
			"Unknown error while parsing the assertion.\n");
	}

	exit(-1);
    }

    free(buf);

    if (i == SIGRESULT_TRUE)
      fprintf(stdout, "Signature verified.\n");
    else
    {
	if (keynote_errno != 0)
	  fprintf(stdout, "Signature could not be verified "
		  "(keynote_errno = %d).\n", keynote_errno);
	else
	  fprintf(stdout, "Signature did not verify!\n");
    }

    exit(0);
}