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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2003 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <libintl.h>
#include <locale.h>
#include <string.h>
#include <errno.h>
#include <wanbootutil.h>
#include <sys/wanboot_impl.h>
/* Return codes */
#define HMAC_SUCCESS 0
#define HMAC_NOKEY 1
#define HMAC_ERROR 2
/* Private buffer length */
#define HMAC_BUF_LEN 1024
/*
* This routine is used to compute a hash digest for the file represented
* by the file descirptor, 'fd'. The key, 'hmac_key', and key type, 'ka',
* will be provided by the caller. The resulting hash digest will be
* written to stdout.
*
* Returns:
* HMAC_SUCCESS or HMAC_ERROR.
*/
static int
hash_gen(int in_fd, const wbku_key_attr_t *ka, const uint8_t *hmac_key)
{
SHA1_CTX ctx;
uint8_t buf[HMAC_BUF_LEN];
ssize_t i;
uint8_t digest[HMAC_DIGEST_LEN];
/*
* Initialize the computation.
*/
HMACInit(&ctx, hmac_key, ka->ka_len);
/*
* Read the data to hash.
*/
while ((i = read(in_fd, buf, HMAC_BUF_LEN)) > 0) {
HMACUpdate(&ctx, buf, i);
}
if (i < 0) {
wbku_printerr("Cannot read input_file");
return (HMAC_ERROR);
}
/*
* Finalize the digest.
*/
HMACFinal(&ctx, hmac_key, ka->ka_len, digest);
/*
* Write the digest to stdout.
*/
if (wbio_nwrite(STDOUT_FILENO, digest, sizeof (digest)) != 0) {
wbku_printerr("Cannot output digest");
return (HMAC_ERROR);
}
/*
* Success.
*/
return (HMAC_SUCCESS);
}
/*
* Prints usage().
*/
static void
usage(const char *cmd)
{
(void) fprintf(stderr,
gettext("Usage: %s [-i input_file] -k key_file\n"), cmd);
}
/*
* This program is used to compute a hash digest for data read in from
* stdin or optionally, a file. The resulting hash digest will be written
* to stdout.
*
* Returns:
* HMAC_SUCCESS, HMAC_ERROR or HMAC_NOKEY.
*/
int
main(int argc, char **argv)
{
uint8_t hmac_key[WANBOOT_HMAC_KEY_SIZE];
int c;
char *infile_name = NULL;
char *keyfile_name = NULL;
int in_fd = -1;
FILE *key_fp = NULL;
wbku_key_attr_t ka;
wbku_retcode_t wbkuret;
int ret = HMAC_ERROR;
/*
* Do the necessary magic for localization support.
*/
(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
/*
* Initialize program name for use by wbku_printerr().
*/
wbku_errinit(argv[0]);
/*
* Should be at least three arguments.
*/
if (argc < 3) {
usage(argv[0]);
return (HMAC_ERROR);
}
/*
* Parse the options.
*/
while ((c = getopt(argc, argv, "i:k:")) != EOF) {
switch (c) {
case 'i':
/*
* Optional input file.
*/
infile_name = optarg;
break;
case 'k':
/*
* Path to key file.
*/
keyfile_name = optarg;
break;
default:
usage(argv[0]);
return (HMAC_ERROR);
}
}
/*
* A key file must be defined.
*/
if (keyfile_name == NULL) {
wbku_printerr("Must specify the key_file\n");
return (HMAC_ERROR);
}
/*
* If the user did not provide an input file for the data,
* then use stdin as the source.
*/
if (infile_name == NULL) {
in_fd = STDIN_FILENO;
} else {
in_fd = open(infile_name, O_RDONLY);
if (in_fd < 0) {
wbku_printerr("Cannot open input_file");
return (HMAC_ERROR);
}
}
/*
* Open the key file for reading.
*/
if ((key_fp = fopen(keyfile_name, "r")) == NULL) {
wbku_printerr("Cannot open %s", keyfile_name);
goto out;
}
/*
* Create a SHA1 key attribute structure. It's the only hash
* type we support.
*/
wbkuret = wbku_str_to_keyattr(WBKU_KW_HMAC_SHA1, &ka, WBKU_HASH_KEY);
if (wbkuret != WBKU_SUCCESS) {
wbku_printerr("%s\n", wbku_retmsg(wbkuret));
goto out;
}
/*
* Find the client key, if it exists.
*/
wbkuret = wbku_find_key(key_fp, NULL, &ka, hmac_key, B_FALSE);
if (wbkuret != WBKU_SUCCESS) {
wbku_printerr("%s\n", wbku_retmsg(wbkuret));
ret = (wbkuret == WBKU_NOKEY) ? HMAC_NOKEY : HMAC_ERROR;
} else {
ret = hash_gen(in_fd, &ka, hmac_key);
}
out:
/*
* Cleanup.
*/
if (in_fd != -1) {
(void) close(in_fd);
}
if (key_fp != NULL) {
(void) fclose(key_fp);
}
return (ret);
}
|