bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2012 bigbiff/Dees_Troy TeamWin |
| 3 | This file is part of TWRP/TeamWin Recovery Project. |
| 4 | |
| 5 | TWRP is free software: you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation, either version 3 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | TWRP is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | GNU General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with TWRP. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 19 | extern "C" |
| 20 | { |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 21 | #include "digest/md5.h" |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 22 | #include "gui/gui.h" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 23 | #include "libcrecovery/common.h" |
| 24 | } |
Vojtech Bocek | fafb0c5 | 2013-07-25 22:53:02 +0200 | [diff] [blame] | 25 | |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 26 | #include <vector> |
| 27 | #include <string> |
| 28 | #include <sstream> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <sys/wait.h> |
| 32 | #include <string.h> |
| 33 | #include <libgen.h> |
| 34 | #include <errno.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <fstream> |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 37 | #include <sys/mman.h> |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 38 | #include "twcommon.h" |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 39 | #include "data.hpp" |
| 40 | #include "variables.h" |
| 41 | #include "twrp-functions.hpp" |
| 42 | #include "twrpDigest.hpp" |
| 43 | |
| 44 | using namespace std; |
| 45 | |
| 46 | void twrpDigest::setfn(string fn) { |
| 47 | md5fn = fn; |
| 48 | } |
| 49 | |
| 50 | int twrpDigest::computeMD5(void) { |
| 51 | string line; |
| 52 | struct MD5Context md5c; |
| 53 | FILE *file; |
| 54 | int len; |
| 55 | unsigned char buf[1024]; |
| 56 | MD5Init(&md5c); |
| 57 | file = fopen(md5fn.c_str(), "rb"); |
| 58 | if (file == NULL) |
| 59 | return -1; |
| 60 | while ((len = fread(buf, 1, sizeof(buf), file)) > 0) { |
| 61 | MD5Update(&md5c, buf, len); |
| 62 | } |
Dees_Troy | 2673cec | 2013-04-02 20:22:16 +0000 | [diff] [blame] | 63 | fclose(file); |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 64 | MD5Final(md5sum, &md5c); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
| 68 | int twrpDigest::write_md5digest(void) { |
| 69 | int i; |
| 70 | string md5string, md5file; |
| 71 | char hex[3]; |
| 72 | md5file = md5fn + ".md5"; |
| 73 | |
| 74 | for (i = 0; i < 16; ++i) { |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 75 | snprintf(hex, 3, "%02x", md5sum[i]); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 76 | md5string += hex; |
| 77 | } |
| 78 | md5string += " "; |
| 79 | md5string += basename((char*) md5fn.c_str()); |
| 80 | md5string += + "\n"; |
| 81 | TWFunc::write_file(md5file, md5string); |
bigbiff bigbiff | 6b05954 | 2013-09-10 10:28:26 -0400 | [diff] [blame] | 82 | LOGINFO("MD5 for %s: %s\n", md5fn.c_str(), md5string.c_str()); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | int twrpDigest::read_md5digest(void) { |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 87 | int i = 0; |
| 88 | bool foundMd5File = false; |
| 89 | string md5file = ""; |
| 90 | vector<string> md5ext; |
| 91 | md5ext.push_back(".md5"); |
| 92 | md5ext.push_back(".md5sum"); |
| 93 | |
| 94 | while (i < md5ext.size()) { |
| 95 | md5file = md5fn + md5ext[i]; |
| 96 | if (TWFunc::Path_Exists(md5file)) { |
| 97 | foundMd5File = true; |
| 98 | break; |
| 99 | } |
| 100 | i++; |
| 101 | } |
| 102 | |
| 103 | if (!foundMd5File) { |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 104 | gui_print("Skipping MD5 check: no MD5 file found\n"); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 105 | return -1; |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 106 | } else if (TWFunc::read_file(md5file, line) != 0) { |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 107 | gui_print("Skipping MD5 check: MD5 file unreadable\n"); |
| 108 | return 1; |
Matt Mower | 0251abc | 2014-04-08 21:33:08 -0500 | [diff] [blame] | 109 | } |
| 110 | |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 114 | /* verify_md5digest return codes: |
| 115 | -2: md5 did not match |
| 116 | -1: no md5 file found |
| 117 | 0: md5 matches |
| 118 | 1: md5 file unreadable |
| 119 | */ |
| 120 | |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 121 | int twrpDigest::verify_md5digest(void) { |
| 122 | string buf; |
| 123 | char hex[3]; |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 124 | int i, ret; |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 125 | string md5string; |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 126 | |
| 127 | ret = read_md5digest(); |
| 128 | if (ret != 0) |
| 129 | return ret; |
bigbiff bigbiff | 65a4c73 | 2013-03-15 15:17:50 -0400 | [diff] [blame] | 130 | stringstream ss(line); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 131 | vector<string> tokens; |
| 132 | while (ss >> buf) |
| 133 | tokens.push_back(buf); |
| 134 | computeMD5(); |
| 135 | for (i = 0; i < 16; ++i) { |
| 136 | snprintf(hex, 3, "%02x", md5sum[i]); |
| 137 | md5string += hex; |
| 138 | } |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 139 | if (tokens.at(0) != md5string) { |
| 140 | LOGERR("MD5 does not match\n"); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 141 | return -2; |
Matt Mower | d5c1a92 | 2014-04-15 12:50:58 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | gui_print("MD5 matched\n"); |
bigbiff bigbiff | cdcfee4 | 2013-02-27 21:11:26 -0500 | [diff] [blame] | 145 | return 0; |
| 146 | } |