blob: f95f4120d96875bddce0aedbda730819e6c944a0 [file] [log] [blame]
Adam Langleycca4d592015-01-12 12:01:23 -08001/* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <openssl/base.h>
16
Adam Langleycca4d592015-01-12 12:01:23 -080017#include <memory>
18#include <string>
19#include <vector>
20
21#include <errno.h>
22#include <fcntl.h>
23#include <limits.h>
24#include <stdio.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080025#include <string.h>
Adam Langleycca4d592015-01-12 12:01:23 -080026#include <sys/stat.h>
27#include <sys/types.h>
Brian Smithafdaeee2015-01-26 19:54:32 -080028
29#if !defined(OPENSSL_WINDOWS)
Adam Langleycca4d592015-01-12 12:01:23 -080030#include <unistd.h>
Brian Smithafdaeee2015-01-26 19:54:32 -080031#if !defined(O_BINARY)
32#define O_BINARY 0
33#endif
34#else
35#define NOMINMAX
Brian Smithefed2212015-01-28 16:20:02 -080036#pragma warning(push, 3)
Brian Smithafdaeee2015-01-26 19:54:32 -080037#include <windows.h>
Brian Smithefed2212015-01-28 16:20:02 -080038#pragma warning(pop)
Brian Smithafdaeee2015-01-26 19:54:32 -080039#include <io.h>
40#define PATH_MAX MAX_PATH
Brian Smith33970e62015-01-27 22:32:08 -080041typedef int ssize_t;
Brian Smithafdaeee2015-01-26 19:54:32 -080042#endif
Adam Langleycca4d592015-01-12 12:01:23 -080043
44#include <openssl/digest.h>
45
Adam Langley2b2d66d2015-01-30 17:08:37 -080046
Adam Langleycca4d592015-01-12 12:01:23 -080047struct close_delete {
48 void operator()(int *fd) {
49 close(*fd);
50 }
51};
52
53template<typename T, typename R, R (*func) (T*)>
54struct func_delete {
55 void operator()(T* obj) {
56 func(obj);
57 }
58};
59
60// Source is an awkward expression of a union type in C++: Stdin | File filename.
61struct Source {
62 enum Type {
63 STDIN,
64 };
65
66 Source() : is_stdin_(false) {}
67 Source(Type) : is_stdin_(true) {}
68 Source(const std::string &name) : is_stdin_(false), filename_(name) {}
69
70 bool is_stdin() const { return is_stdin_; }
71 const std::string &filename() const { return filename_; }
72
73 private:
74 bool is_stdin_;
75 std::string filename_;
76};
77
78static const char kStdinName[] = "standard input";
79
80// OpenFile opens the regular file named |filename| and sets |*out_fd| to be a
81// file descriptor to it. Returns true on sucess or prints an error to stderr
82// and returns false on error.
83static bool OpenFile(int *out_fd, const std::string &filename) {
84 *out_fd = -1;
85
Brian Smithafdaeee2015-01-26 19:54:32 -080086 int fd = open(filename.c_str(), O_RDONLY | O_BINARY);
Adam Langleycca4d592015-01-12 12:01:23 -080087 if (fd < 0) {
88 fprintf(stderr, "Failed to open input file '%s': %s\n", filename.c_str(),
89 strerror(errno));
90 return false;
91 }
92
Brian Smithafdaeee2015-01-26 19:54:32 -080093#if !defined(OPENSSL_WINDOWS)
Adam Langleycca4d592015-01-12 12:01:23 -080094 struct stat st;
95 if (fstat(fd, &st)) {
96 fprintf(stderr, "Failed to stat input file '%s': %s\n", filename.c_str(),
97 strerror(errno));
98 goto err;
99 }
100
101 if (!S_ISREG(st.st_mode)) {
102 fprintf(stderr, "%s: not a regular file\n", filename.c_str());
103 goto err;
104 }
Brian Smithafdaeee2015-01-26 19:54:32 -0800105#endif
Adam Langleycca4d592015-01-12 12:01:23 -0800106
107 *out_fd = fd;
108 return true;
109
Brian Smithefed2212015-01-28 16:20:02 -0800110#if !defined(OPENSSL_WINDOWS)
Adam Langleycca4d592015-01-12 12:01:23 -0800111err:
112 close(fd);
113 return false;
Brian Smithefed2212015-01-28 16:20:02 -0800114#endif
Adam Langleycca4d592015-01-12 12:01:23 -0800115}
116
117// SumFile hashes the contents of |source| with |md| and sets |*out_hex| to the
118// hex-encoded result.
119//
120// It returns true on success or prints an error to stderr and returns false on
121// error.
122static bool SumFile(std::string *out_hex, const EVP_MD *md,
123 const Source &source) {
124 std::unique_ptr<int, close_delete> scoped_fd;
125 int fd;
126
127 if (source.is_stdin()) {
128 fd = 0;
129 } else {
130 if (!OpenFile(&fd, source.filename())) {
131 return false;
132 }
133 scoped_fd.reset(&fd);
134 }
135
136 static const size_t kBufSize = 8192;
137 std::unique_ptr<uint8_t[]> buf(new uint8_t[kBufSize]);
138
139 EVP_MD_CTX ctx;
140 EVP_MD_CTX_init(&ctx);
141 std::unique_ptr<EVP_MD_CTX, func_delete<EVP_MD_CTX, int, EVP_MD_CTX_cleanup>>
142 scoped_ctx(&ctx);
143
144 if (!EVP_DigestInit_ex(&ctx, md, NULL)) {
145 fprintf(stderr, "Failed to initialize EVP_MD_CTX.\n");
146 return false;
147 }
148
149 for (;;) {
150 ssize_t n;
151
152 do {
153 n = read(fd, buf.get(), kBufSize);
154 } while (n == -1 && errno == EINTR);
155
156 if (n == 0) {
157 break;
158 } else if (n < 0) {
159 fprintf(stderr, "Failed to read from %s: %s\n",
160 source.is_stdin() ? kStdinName : source.filename().c_str(),
161 strerror(errno));
162 return false;
163 }
164
165 if (!EVP_DigestUpdate(&ctx, buf.get(), n)) {
166 fprintf(stderr, "Failed to update hash.\n");
167 return false;
168 }
169 }
170
171 uint8_t digest[EVP_MAX_MD_SIZE];
172 unsigned digest_len;
173 if (!EVP_DigestFinal_ex(&ctx, digest, &digest_len)) {
174 fprintf(stderr, "Failed to finish hash.\n");
175 return false;
176 }
177
178 char hex_digest[EVP_MAX_MD_SIZE * 2];
179 static const char kHextable[] = "0123456789abcdef";
180 for (unsigned i = 0; i < digest_len; i++) {
181 const uint8_t b = digest[i];
182 hex_digest[i * 2] = kHextable[b >> 4];
183 hex_digest[i * 2 + 1] = kHextable[b & 0xf];
184 }
185 *out_hex = std::string(hex_digest, digest_len * 2);
186
187 return true;
188}
189
190// PrintFileSum hashes |source| with |md| and prints a line to stdout in the
191// format of the coreutils *sum utilities. It returns true on success or prints
192// an error to stderr and returns false on error.
193static bool PrintFileSum(const EVP_MD *md, const Source &source) {
194 std::string hex_digest;
195 if (!SumFile(&hex_digest, md, source)) {
196 return false;
197 }
198
Brian Smithafdaeee2015-01-26 19:54:32 -0800199 // TODO: When given "--binary" or "-b", we should print " *" instead of " "
200 // between the digest and the filename.
201 //
202 // MSYS and Cygwin md5sum default to binary mode by default, whereas other
203 // platforms' tools default to text mode by default. We default to text mode
204 // by default and consider text mode equivalent to binary mode (i.e. we
205 // always use Unix semantics, even on Windows), which means that our default
206 // output will differ from the MSYS and Cygwin tools' default output.
Adam Langleycca4d592015-01-12 12:01:23 -0800207 printf("%s %s\n", hex_digest.c_str(),
208 source.is_stdin() ? "-" : source.filename().c_str());
209 return true;
210}
211
212// CheckModeArguments contains arguments for the check mode. See the
213// sha256sum(1) man page for details.
214struct CheckModeArguments {
215 bool quiet = false;
216 bool status = false;
217 bool warn = false;
218 bool strict = false;
219};
220
221// Check reads lines from |source| where each line is in the format of the
222// coreutils *sum utilities. It attempts to verify each hash by reading the
223// file named in the line.
224//
225// It returns true if all files were verified and, if |args.strict|, no input
226// lines had formatting errors. Otherwise it prints errors to stderr and
227// returns false.
228static bool Check(const CheckModeArguments &args, const EVP_MD *md,
229 const Source &source) {
230 std::unique_ptr<FILE, func_delete<FILE, int, fclose>> scoped_file;
231 FILE *file;
232
233 if (source.is_stdin()) {
234 file = stdin;
235 } else {
236 int fd;
237 if (!OpenFile(&fd, source.filename())) {
238 return false;
239 }
240
Brian Smithafdaeee2015-01-26 19:54:32 -0800241 file = fdopen(fd, "rb");
Adam Langleycca4d592015-01-12 12:01:23 -0800242 if (!file) {
243 perror("fdopen");
244 close(fd);
245 return false;
246 }
247
248 scoped_file = std::unique_ptr<FILE, func_delete<FILE, int, fclose>>(file);
249 }
250
251 const size_t hex_size = EVP_MD_size(md) * 2;
252 char line[EVP_MAX_MD_SIZE * 2 + 2 /* spaces */ + PATH_MAX + 1 /* newline */ +
253 1 /* NUL */];
254 unsigned bad_lines = 0;
255 unsigned parsed_lines = 0;
256 unsigned error_lines = 0;
257 unsigned bad_hash_lines = 0;
258 unsigned line_no = 0;
259 bool ok = true;
260 bool draining_overlong_line = false;
261
262 for (;;) {
263 line_no++;
264
265 if (fgets(line, sizeof(line), file) == nullptr) {
266 if (feof(file)) {
267 break;
268 }
269 fprintf(stderr, "Error reading from input.\n");
270 return false;
271 }
272
273 size_t len = strlen(line);
274
275 if (draining_overlong_line) {
276 if (line[len - 1] == '\n') {
277 draining_overlong_line = false;
278 }
279 continue;
280 }
281
282 const bool overlong = line[len - 1] != '\n' && !feof(file);
283
284 if (len < hex_size + 2 /* spaces */ + 1 /* filename */ ||
285 line[hex_size] != ' ' ||
286 line[hex_size + 1] != ' ' ||
287 overlong) {
288 bad_lines++;
289 if (args.warn) {
290 fprintf(stderr, "%s: %u: improperly formatted line\n",
291 source.is_stdin() ? kStdinName : source.filename().c_str(), line_no);
292 }
293 if (args.strict) {
294 ok = false;
295 }
296 if (overlong) {
297 draining_overlong_line = true;
298 }
299 continue;
300 }
301
302 if (line[len - 1] == '\n') {
303 line[len - 1] = 0;
304 len--;
305 }
306
307 parsed_lines++;
308
309 // coreutils does not attempt to restrict relative or absolute paths in the
310 // input so nor does this code.
311 std::string calculated_hex_digest;
312 const std::string target_filename(&line[hex_size + 2]);
313 Source target_source;
314 if (target_filename == "-") {
315 // coreutils reads from stdin if the filename is "-".
316 target_source = Source(Source::STDIN);
317 } else {
318 target_source = Source(target_filename);
319 }
320
321 if (!SumFile(&calculated_hex_digest, md, target_source)) {
322 error_lines++;
323 ok = false;
324 continue;
325 }
326
327 if (calculated_hex_digest != std::string(line, hex_size)) {
328 bad_hash_lines++;
329 if (!args.status) {
330 printf("%s: FAILED\n", target_filename.c_str());
331 }
332 ok = false;
333 continue;
334 }
335
336 if (!args.quiet) {
337 printf("%s: OK\n", target_filename.c_str());
338 }
339 }
340
341 if (!args.status) {
342 if (bad_lines > 0 && parsed_lines > 0) {
343 fprintf(stderr, "WARNING: %u line%s improperly formatted\n", bad_lines,
344 bad_lines == 1 ? " is" : "s are");
345 }
346 if (error_lines > 0) {
347 fprintf(stderr, "WARNING: %u computed checksum(s) did NOT match\n",
348 error_lines);
349 }
350 }
351
352 if (parsed_lines == 0) {
353 fprintf(stderr, "%s: no properly formatted checksum lines found.\n",
354 source.is_stdin() ? kStdinName : source.filename().c_str());
355 ok = false;
356 }
357
358 return ok;
359}
360
361// DigestSum acts like the coreutils *sum utilites, with the given hash
362// function.
363static bool DigestSum(const EVP_MD *md,
364 const std::vector<std::string> &args) {
365 bool check_mode = false;
366 CheckModeArguments check_args;
367 bool check_mode_args_given = false;
368 std::vector<Source> sources;
369
370 auto it = args.begin();
371 while (it != args.end()) {
372 const std::string &arg = *it;
373 if (!arg.empty() && arg[0] != '-') {
374 break;
375 }
376
377 it++;
378
379 if (arg == "--") {
380 break;
381 }
382
Adam Langleybed8f732015-01-26 16:34:37 -0800383 if (arg == "-") {
Adam Langleycca4d592015-01-12 12:01:23 -0800384 // "-" ends the argument list and indicates that stdin should be used.
385 sources.push_back(Source(Source::STDIN));
386 break;
387 }
388
389 if (arg.size() >= 2 && arg[0] == '-' && arg[1] != '-') {
390 for (size_t i = 1; i < arg.size(); i++) {
391 switch (arg[i]) {
392 case 'b':
393 case 't':
Brian Smithafdaeee2015-01-26 19:54:32 -0800394 // Binary/text mode – irrelevent, even on Windows.
Adam Langleycca4d592015-01-12 12:01:23 -0800395 break;
396 case 'c':
397 check_mode = true;
398 break;
399 case 'w':
400 check_mode_args_given = true;
401 check_args.warn = true;
402 break;
403 default:
404 fprintf(stderr, "Unknown option '%c'.\n", arg[i]);
405 return false;
406 }
407 }
408 } else if (arg == "--binary" || arg == "--text") {
Brian Smithafdaeee2015-01-26 19:54:32 -0800409 // Binary/text mode – irrelevent, even on Windows.
Adam Langleycca4d592015-01-12 12:01:23 -0800410 } else if (arg == "--check") {
411 check_mode = true;
412 } else if (arg == "--quiet") {
413 check_mode_args_given = true;
414 check_args.quiet = true;
415 } else if (arg == "--status") {
416 check_mode_args_given = true;
417 check_args.status = true;
418 } else if (arg == "--warn") {
419 check_mode_args_given = true;
420 check_args.warn = true;
421 } else if (arg == "--strict") {
422 check_mode_args_given = true;
423 check_args.strict = true;
424 } else {
425 fprintf(stderr, "Unknown option '%s'.\n", arg.c_str());
426 return false;
427 }
428 }
429
430 if (check_mode_args_given && !check_mode) {
431 fprintf(
432 stderr,
433 "Check mode arguments are only meaningful when verifying checksums.\n");
434 return false;
435 }
436
437 for (; it != args.end(); it++) {
438 sources.push_back(Source(*it));
439 }
440
441 if (sources.empty()) {
442 sources.push_back(Source(Source::STDIN));
443 }
444
445 bool ok = true;
446
447 if (check_mode) {
448 for (auto &source : sources) {
449 ok &= Check(check_args, md, source);
450 }
451 } else {
452 for (auto &source : sources) {
453 ok &= PrintFileSum(md, source);
454 }
455 }
456
457 return ok;
458}
459
460bool MD5Sum(const std::vector<std::string> &args) {
461 return DigestSum(EVP_md5(), args);
462}
463
464bool SHA1Sum(const std::vector<std::string> &args) {
465 return DigestSum(EVP_sha1(), args);
466}
467
468bool SHA224Sum(const std::vector<std::string> &args) {
469 return DigestSum(EVP_sha224(), args);
470}
471
472bool SHA256Sum(const std::vector<std::string> &args) {
473 return DigestSum(EVP_sha256(), args);
474}
475
476bool SHA384Sum(const std::vector<std::string> &args) {
477 return DigestSum(EVP_sha384(), args);
478}
479
480bool SHA512Sum(const std::vector<std::string> &args) {
481 return DigestSum(EVP_sha512(), args);
482}