2008-08-06 18:36:30 +00:00
|
|
|
// This file is part of BOINC.
|
2005-01-20 23:22:22 +00:00
|
|
|
// http://boinc.berkeley.edu
|
2008-08-06 18:36:30 +00:00
|
|
|
// Copyright (C) 2008 University of California
|
2004-07-13 13:54:09 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is free software; you can redistribute it and/or modify it
|
|
|
|
// under the terms of the GNU Lesser General Public License
|
|
|
|
// as published by the Free Software Foundation,
|
|
|
|
// either version 3 of the License, or (at your option) any later version.
|
2004-07-13 13:54:09 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// BOINC is distributed in the hope that it will be useful,
|
2005-01-20 23:22:22 +00:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
// See the GNU Lesser General Public License for more details.
|
2002-09-26 18:11:06 +00:00
|
|
|
//
|
2008-08-06 18:36:30 +00:00
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
|
2002-09-26 18:11:06 +00:00
|
|
|
|
2020-06-04 07:48:21 +00:00
|
|
|
#if defined(_WIN32)
|
2004-06-16 23:16:08 +00:00
|
|
|
#include "boinc_win.h"
|
2007-07-02 20:45:16 +00:00
|
|
|
#else
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2008-09-09 19:10:42 +00:00
|
|
|
#ifdef _USING_FCGI_
|
|
|
|
#include "boinc_fcgi.h"
|
|
|
|
#else
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
2004-03-04 11:41:43 +00:00
|
|
|
#endif
|
2008-09-09 19:10:42 +00:00
|
|
|
#endif
|
2002-04-30 22:22:54 +00:00
|
|
|
|
2010-05-11 19:10:29 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <wincrypt.h>
|
|
|
|
#endif
|
|
|
|
|
2012-08-04 00:56:08 +00:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
|
2002-07-11 01:09:53 +00:00
|
|
|
#include "error_numbers.h"
|
2012-08-01 20:04:05 +00:00
|
|
|
#include "md5.h"
|
2004-07-03 21:38:15 +00:00
|
|
|
|
2012-08-11 05:47:18 +00:00
|
|
|
#include "md5_file.h"
|
|
|
|
|
2014-03-20 19:38:29 +00:00
|
|
|
int md5_file(const char* path, char* output, double& nbytes, bool is_gzip) {
|
2002-04-30 22:22:54 +00:00
|
|
|
unsigned char buf[4096];
|
|
|
|
unsigned char binout[16];
|
|
|
|
md5_state_t state;
|
|
|
|
int i, n;
|
2002-08-30 20:56:02 +00:00
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
nbytes = 0;
|
2008-09-09 19:10:42 +00:00
|
|
|
#ifndef _USING_FCGI_
|
|
|
|
FILE *f = fopen(path, "rb");
|
|
|
|
#else
|
|
|
|
FILE *f = FCGI::fopen(path, "rb");
|
|
|
|
#endif
|
2002-05-24 04:29:10 +00:00
|
|
|
if (!f) {
|
2002-10-03 18:33:46 +00:00
|
|
|
fprintf(stderr, "md5_file: can't open %s\n", path);
|
2009-02-26 00:23:23 +00:00
|
|
|
#ifndef _USING_FCGI_
|
|
|
|
std::perror("md5_file");
|
|
|
|
#else
|
|
|
|
FCGI::perror("md5_file");
|
|
|
|
#endif
|
|
|
|
|
2003-10-21 04:06:55 +00:00
|
|
|
return ERR_FOPEN;
|
2002-05-24 04:29:10 +00:00
|
|
|
}
|
2002-04-30 22:22:54 +00:00
|
|
|
md5_init(&state);
|
2014-03-20 19:38:29 +00:00
|
|
|
|
|
|
|
// check and skip gzip header if needed
|
|
|
|
//
|
|
|
|
if (is_gzip) {
|
|
|
|
n = (int)fread(buf, 1, 10, f);
|
|
|
|
if (n != 10) {
|
2014-06-27 20:38:16 +00:00
|
|
|
fclose(f);
|
2014-03-20 19:38:29 +00:00
|
|
|
return ERR_BAD_FORMAT;
|
|
|
|
}
|
|
|
|
if (buf[0] != 0x1f || buf[1] != 0x8b || buf[2] != 0x08) {
|
2014-06-27 20:38:16 +00:00
|
|
|
fclose(f);
|
2014-03-20 19:38:29 +00:00
|
|
|
return ERR_BAD_FORMAT;
|
|
|
|
}
|
|
|
|
nbytes = 10;
|
|
|
|
}
|
|
|
|
|
2002-04-30 22:22:54 +00:00
|
|
|
while (1) {
|
2005-07-23 07:24:51 +00:00
|
|
|
n = (int)fread(buf, 1, 4096, f);
|
2002-04-30 22:22:54 +00:00
|
|
|
if (n<=0) break;
|
|
|
|
nbytes += n;
|
|
|
|
md5_append(&state, buf, n);
|
|
|
|
}
|
|
|
|
md5_finish(&state, binout);
|
|
|
|
for (i=0; i<16; i++) {
|
|
|
|
sprintf(output+2*i, "%02x", binout[i]);
|
|
|
|
}
|
|
|
|
output[32] = 0;
|
|
|
|
fclose(f);
|
|
|
|
return 0;
|
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
|
2019-06-26 06:43:07 +00:00
|
|
|
int md5_block(const unsigned char* data, int nbytes, char* output,
|
|
|
|
const unsigned char* data2, int nbytes2 // optional 2nd block
|
|
|
|
) {
|
2002-07-05 05:33:40 +00:00
|
|
|
unsigned char binout[16];
|
|
|
|
int i;
|
2002-08-30 20:56:02 +00:00
|
|
|
|
2002-07-05 05:33:40 +00:00
|
|
|
md5_state_t state;
|
|
|
|
md5_init(&state);
|
|
|
|
md5_append(&state, data, nbytes);
|
2019-06-26 06:43:07 +00:00
|
|
|
if (data2) {
|
|
|
|
md5_append(&state, data2, nbytes2);
|
|
|
|
}
|
2002-07-05 05:33:40 +00:00
|
|
|
md5_finish(&state, binout);
|
|
|
|
for (i=0; i<16; i++) {
|
|
|
|
sprintf(output+2*i, "%02x", binout[i]);
|
|
|
|
}
|
|
|
|
output[32] = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
2004-07-13 14:18:47 +00:00
|
|
|
|
2005-01-08 06:54:03 +00:00
|
|
|
std::string md5_string(const unsigned char* data, int nbytes) {
|
|
|
|
char output[MD5_LEN];
|
2004-07-13 14:18:47 +00:00
|
|
|
md5_block(data, nbytes, output);
|
2004-07-20 10:38:14 +00:00
|
|
|
return std::string(output);
|
2004-07-13 14:18:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-28 08:50:31 +00:00
|
|
|
// make a secure (i.e. hard to guess)
|
|
|
|
// 32-char string using OS-supplied random bits
|
2012-08-04 00:56:08 +00:00
|
|
|
//
|
2019-11-28 08:50:31 +00:00
|
|
|
int make_secure_random_string_os(char* out) {
|
2005-10-27 20:03:51 +00:00
|
|
|
char buf[256];
|
2007-07-02 20:45:16 +00:00
|
|
|
#ifdef _WIN32
|
2005-10-27 20:03:51 +00:00
|
|
|
HCRYPTPROV hCryptProv;
|
|
|
|
|
|
|
|
if(! CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)) {
|
2021-03-21 23:40:36 +00:00
|
|
|
if (GetLastError() == NTE_BAD_KEYSET) {
|
|
|
|
if (!CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return -2;
|
|
|
|
}
|
2005-10-27 20:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(! CryptGenRandom(hCryptProv, (DWORD) 32, (BYTE *) buf)) {
|
|
|
|
CryptReleaseContext(hCryptProv, 0);
|
2021-03-21 23:40:36 +00:00
|
|
|
return -3;
|
2005-10-27 20:03:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CryptReleaseContext(hCryptProv, 0);
|
2012-08-04 00:56:08 +00:00
|
|
|
#elif defined ANDROID
|
2019-11-28 08:50:31 +00:00
|
|
|
return -1;
|
2005-10-27 20:03:51 +00:00
|
|
|
#else
|
2008-09-09 19:10:42 +00:00
|
|
|
#ifndef _USING_FCGI_
|
2005-10-27 20:03:51 +00:00
|
|
|
FILE* f = fopen("/dev/random", "r");
|
2008-09-09 19:10:42 +00:00
|
|
|
#else
|
|
|
|
FILE* f = FCGI::fopen("/dev/random", "r");
|
|
|
|
#endif
|
2005-10-27 20:03:51 +00:00
|
|
|
if (!f) {
|
|
|
|
return -1;
|
|
|
|
}
|
2008-01-31 18:34:51 +00:00
|
|
|
size_t n = fread(buf, 32, 1, f);
|
2005-10-27 20:03:51 +00:00
|
|
|
fclose(f);
|
2021-03-21 23:40:36 +00:00
|
|
|
if (n != 1) return -2;
|
2005-10-27 20:03:51 +00:00
|
|
|
#endif
|
|
|
|
md5_block((const unsigned char*)buf, 32, out);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|