xref: /openbsd-src/lib/libkeynote/keynote-sign.c (revision b7041c0781c8668129da8084451ded41b0c43954)
1*b7041c07Sderaadt /* $OpenBSD: keynote-sign.c,v 1.20 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>
2685616838Smsf #include <regex.h>
27e0758482Smsf #include <stdio.h>
28e0758482Smsf #include <stdlib.h>
299186b70cSangelos #include <string.h>
309186b70cSangelos #include <fcntl.h>
31983e9580Sangelos #include <unistd.h>
32983e9580Sangelos 
33dd34a27eSangelos #include "header.h"
341c338448Sangelos #include "keynote.h"
35e482c692Sangelos 
36aa31cba2Sderaadt void	signusage(void);
37aa31cba2Sderaadt 
38983e9580Sangelos void
signusage(void)39e482c692Sangelos signusage(void)
40983e9580Sangelos {
41983e9580Sangelos     fprintf(stderr, "Arguments:\n");
42983e9580Sangelos     fprintf(stderr, "\t[-v] <AlgorithmName> <AssertionFile> "
43a1d1ca57Sangelos 	    "<PrivateKeyFile> [<print-offset>] [<print-length>]\n");
44983e9580Sangelos }
45983e9580Sangelos 
46983e9580Sangelos void
keynote_sign(int argc,char * argv[])47e482c692Sangelos keynote_sign(int argc, char *argv[])
48983e9580Sangelos {
49983e9580Sangelos     int begin = SIG_PRINT_OFFSET, prlen = SIG_PRINT_LENGTH;
50983e9580Sangelos     char *buf, *buf2, *sig, *algname;
51983e9580Sangelos     int fd, flg = 0, buflen;
52983e9580Sangelos     struct stat sb;
53983e9580Sangelos 
54983e9580Sangelos     if ((argc != 4) &&
55a1d1ca57Sangelos 	(argc != 5) &&
56a1d1ca57Sangelos 	(argc != 6) &&
57a1d1ca57Sangelos 	(argc != 7))
58983e9580Sangelos     {
59e482c692Sangelos 	signusage();
603cb7a939Sangelos 	exit(1);
61983e9580Sangelos     }
62983e9580Sangelos 
63983e9580Sangelos     if (!strcmp("-v", argv[1]))
64983e9580Sangelos       flg = 1;
65a1d1ca57Sangelos 
66a1d1ca57Sangelos     if (argc > 4 + flg)
67983e9580Sangelos     {
68a1d1ca57Sangelos         begin = atoi(argv[4 + flg]);
69a1d1ca57Sangelos         if (begin <= -1)
70a1d1ca57Sangelos         {
71a1d1ca57Sangelos             fprintf(stderr, "Erroneous value for print-offset parameter.\n");
723cb7a939Sangelos             exit(1);
73a1d1ca57Sangelos         }
74a1d1ca57Sangelos     }
75a1d1ca57Sangelos 
76a1d1ca57Sangelos     if (argc > 5 + flg)
77a1d1ca57Sangelos     {
78a1d1ca57Sangelos         prlen = atoi(argv[5 + flg]);
79a1d1ca57Sangelos         if (prlen <= 0)
80a1d1ca57Sangelos         {
81a1d1ca57Sangelos             fprintf(stderr, "Erroneous value for print-length parameter.\n");
823cb7a939Sangelos             exit(1);
83983e9580Sangelos         }
84983e9580Sangelos     }
85983e9580Sangelos 
86983e9580Sangelos     /* Fix algorithm name */
87983e9580Sangelos     if (argv[1 + flg][strlen(argv[1 + flg]) - 1] != ':')
88983e9580Sangelos     {
89f91597c4Sderaadt 	int len = strlen(argv[1 + flg]) + 2;
90983e9580Sangelos         fprintf(stderr, "Algorithm name [%s] should be terminated with a "
91983e9580Sangelos 		"colon, fixing.\n", argv[1 + flg]);
920eb1fdc9Sguenther 	algname = calloc(len, sizeof(char));
93313d0fe7Smmcc 	if (algname == NULL)
94983e9580Sangelos 	{
95983e9580Sangelos 	    perror("calloc()");
963cb7a939Sangelos 	    exit(1);
97983e9580Sangelos 	}
98983e9580Sangelos 
99f91597c4Sderaadt 	strlcpy(algname, argv[1 + flg], len);
100983e9580Sangelos 	algname[strlen(algname)] = ':';
101983e9580Sangelos     }
102983e9580Sangelos     else
103983e9580Sangelos 	algname = argv[1 + flg];
104983e9580Sangelos 
105983e9580Sangelos     /* Read assertion */
106*b7041c07Sderaadt     fd = open(argv[2 + flg], O_RDONLY);
107df69c215Sderaadt     if (fd == -1)
108983e9580Sangelos     {
109983e9580Sangelos 	perror(argv[2 + flg]);
1103cb7a939Sangelos 	exit(1);
111983e9580Sangelos     }
112983e9580Sangelos 
113df69c215Sderaadt     if (fstat(fd, &sb) == -1)
114983e9580Sangelos     {
115983e9580Sangelos 	perror("fstat()");
1163cb7a939Sangelos 	exit(1);
117983e9580Sangelos     }
118983e9580Sangelos 
119983e9580Sangelos     if (sb.st_size == 0) /* Paranoid */
120983e9580Sangelos     {
121983e9580Sangelos 	fprintf(stderr, "Error: zero-sized assertion-file.\n");
1223cb7a939Sangelos 	exit(1);
123983e9580Sangelos     }
124983e9580Sangelos 
125983e9580Sangelos     buflen = sb.st_size + 1;
126313d0fe7Smmcc     buf = calloc(buflen, sizeof(char));
127313d0fe7Smmcc     if (buf == NULL)
128983e9580Sangelos     {
129983e9580Sangelos 	perror("calloc()");
1303cb7a939Sangelos 	exit(1);
131983e9580Sangelos     }
132983e9580Sangelos 
133df69c215Sderaadt     if (read(fd, buf, buflen - 1) == -1)
134983e9580Sangelos     {
135983e9580Sangelos 	perror("read()");
1363cb7a939Sangelos 	exit(1);
137983e9580Sangelos     }
138983e9580Sangelos 
139983e9580Sangelos     close(fd);
140983e9580Sangelos 
141983e9580Sangelos     /* Read private key file */
142*b7041c07Sderaadt     fd = open(argv[3 + flg], O_RDONLY);
143df69c215Sderaadt     if (fd == -1)
144983e9580Sangelos     {
145983e9580Sangelos 	perror(argv[3 + flg]);
1463cb7a939Sangelos 	exit(1);
147983e9580Sangelos     }
148983e9580Sangelos 
149df69c215Sderaadt     if (fstat(fd, &sb) == -1)
150983e9580Sangelos     {
151983e9580Sangelos 	perror("fstat()");
1523cb7a939Sangelos 	exit(1);
153983e9580Sangelos     }
154983e9580Sangelos 
155983e9580Sangelos     if (sb.st_size == 0) /* Paranoid */
156983e9580Sangelos     {
157983e9580Sangelos 	fprintf(stderr, "Illegal key-file size 0\n");
1583cb7a939Sangelos 	exit(1);
159983e9580Sangelos     }
160983e9580Sangelos 
161313d0fe7Smmcc     buf2 = calloc(sb.st_size + 1, sizeof(char));
162313d0fe7Smmcc     if (buf2 == NULL)
163983e9580Sangelos     {
164983e9580Sangelos 	perror("calloc()");
1653cb7a939Sangelos 	exit(1);
166983e9580Sangelos     }
167983e9580Sangelos 
168df69c215Sderaadt     if (read(fd, buf2, sb.st_size) == -1)
169983e9580Sangelos     {
170983e9580Sangelos 	perror("read()");
1713cb7a939Sangelos 	exit(1);
172983e9580Sangelos     }
173983e9580Sangelos 
174983e9580Sangelos     close(fd);
175983e9580Sangelos 
176983e9580Sangelos     sig = kn_sign_assertion(buf, buflen, buf2, algname, flg);
177983e9580Sangelos 
178983e9580Sangelos     /* Free buffers */
179983e9580Sangelos     free(buf);
180983e9580Sangelos     free(buf2);
181983e9580Sangelos 
182313d0fe7Smmcc     if (sig == NULL)
183983e9580Sangelos     {
184983e9580Sangelos 	switch (keynote_errno)
185983e9580Sangelos 	{
186983e9580Sangelos 	    case ERROR_MEMORY:
187983e9580Sangelos 		fprintf(stderr, "Out of memory while creating signature.\n");
188983e9580Sangelos 		break;
189983e9580Sangelos 
190983e9580Sangelos 	    case ERROR_SYNTAX:
191983e9580Sangelos 		fprintf(stderr, "Bad assertion or algorithm format, or "
192983e9580Sangelos 			"unsupported algorithm while creating signature.\n");
193983e9580Sangelos 		break;
194983e9580Sangelos 
195983e9580Sangelos 	    default:
196983e9580Sangelos 		fprintf(stderr, "Unknown error while creating signature.\n");
197983e9580Sangelos 	}
198983e9580Sangelos 
1993cb7a939Sangelos 	exit(1);
200983e9580Sangelos     }
201983e9580Sangelos 
202983e9580Sangelos     /* Print signature string */
203ea561257Sangelos     print_key(stdout, "", sig, begin, prlen);
204983e9580Sangelos 
205983e9580Sangelos     free(sig);   /* Just a reminder that the result is malloc'ed */
206983e9580Sangelos 
207983e9580Sangelos     exit(0);
208983e9580Sangelos }
209