| 1 |
/* |
| 2 |
* Copyright (c) 2008-2012 Zmanda, Inc. All Rights Reserved. |
| 3 |
* Copyright (c) 2013-2016 Carbonite, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU General Public License |
| 7 |
* as published by the Free Software Foundation; either version 2 |
| 8 |
* of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 12 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 13 |
* for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 17 |
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
* |
| 19 |
* Contact information: Carbonite Inc., 756 N Pastoria Ave |
| 20 |
* Sunnyvale, CA 94085, or: http://www.zmanda.com |
| 21 |
*/ |
| 22 |
|
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
/* use a relative path here to avoid conflicting with Perl's config.h. */ |
| 26 |
#include "../config/config.h" |
| 27 |
#endif |
| 28 |
#ifdef HAVE_REGEX_H |
| 29 |
#include <sys/types.h> |
| 30 |
#include <regex.h> |
| 31 |
#endif |
| 32 |
#ifdef HAVE_AMANDA_H |
| 33 |
#include "amanda.h" |
| 34 |
#endif |
| 35 |
|
| 36 |
#include <glib.h> |
| 37 |
#include <openssl/hmac.h> |
| 38 |
#include <openssl/sha.h> |
| 39 |
#include <openssl/md5.h> |
| 40 |
#include <openssl/bio.h> |
| 41 |
#include <openssl/evp.h> |
| 42 |
#include <openssl/bn.h> |
| 43 |
#include "s3-util.h" |
| 44 |
|
| 45 |
#ifdef HAVE_REGEX_H |
| 46 |
int |
| 47 |
s3_regexec_wrap(regex_t *regex, |
| 48 |
const char *str, |
| 49 |
size_t nmatch, |
| 50 |
regmatch_t pmatch[], |
| 51 |
int eflags) |
| 52 |
{ |
| 53 |
char *message; |
| 54 |
int size; |
| 55 |
int reg_result; |
| 56 |
|
| 57 |
reg_result = regexec(regex, str, nmatch, pmatch, eflags); |
| 58 |
if (reg_result != 0 && reg_result != REG_NOMATCH) { |
| 59 |
size = regerror(reg_result, regex, NULL, 0); |
| 60 |
message = g_malloc(size); |
| 61 |
regerror(reg_result, regex, message, size); |
| 62 |
|
| 63 |
/* this is programmer error (bad regexp), so just log |
| 64 |
* and abort(). There's no good way to signal a |
| 65 |
* permanaent error from interpret_response. */ |
| 66 |
g_critical(_("Regex error: %s"), message); |
| 67 |
} |
| 68 |
|
| 69 |
return reg_result; |
| 70 |
} |
| 71 |
#else |
| 72 |
|
| 73 |
int |
| 74 |
s3_regexec_wrap(regex_t *regex, |
| 75 |
const char *str, |
| 76 |
size_t nmatch, |
| 77 |
regmatch_t pmatch[], |
| 78 |
int eflags) |
| 79 |
{ |
| 80 |
GMatchInfo *match_info; |
| 81 |
int ret = REG_NOERROR; |
| 82 |
guint i; |
| 83 |
|
| 84 |
g_assert(regex && *regex); |
| 85 |
g_regex_match(*regex, str, eflags, &match_info); |
| 86 |
if (g_match_info_matches(match_info)) { |
| 87 |
g_assert(g_match_info_get_match_count(match_info) <= (glong) nmatch); |
| 88 |
for (i = 0; i < nmatch; i++) { |
| 89 |
pmatch[i].rm_eo = pmatch[i].rm_so = -1; |
| 90 |
g_match_info_fetch_pos(match_info, i, &pmatch[i].rm_so, &pmatch[i].rm_eo); |
| 91 |
} |
| 92 |
} else { |
| 93 |
ret = REG_NOMATCH; |
| 94 |
} |
| 95 |
g_match_info_free(match_info); |
| 96 |
return ret; |
| 97 |
} |
| 98 |
#endif |
| 99 |
|
| 100 |
#ifndef HAVE_AMANDA_H |
| 101 |
char* |
| 102 |
find_regex_substring(const char* base_string, const regmatch_t match) |
| 103 |
{ |
| 104 |
g_assert(match.rm_eo >= match.rm_so); |
| 105 |
return g_strndup(base_string+match.rm_so, match.rm_eo - match.rm_so); |
| 106 |
} |
| 107 |
#endif |
| 108 |
|
| 109 |
gchar* |
| 110 |
s3_base64_encode(const GByteArray *to_enc) { |
| 111 |
BIO *bio_b64 = NULL, *bio_buff = NULL; |
| 112 |
long bio_b64_len; |
| 113 |
char *bio_b64_data = NULL, *ret = NULL; |
| 114 |
if (!to_enc) return NULL; |
| 115 |
|
| 116 |
/* Initialize base64 encoding filter */ |
| 117 |
bio_b64 = BIO_new(BIO_f_base64()); |
| 118 |
g_assert(bio_b64); |
| 119 |
BIO_set_flags(bio_b64, BIO_FLAGS_BASE64_NO_NL); |
| 120 |
|
| 121 |
/* Initialize memory buffer for the base64 encoding */ |
| 122 |
bio_buff = BIO_new(BIO_s_mem()); |
| 123 |
g_assert(bio_buff); |
| 124 |
bio_buff = BIO_push(bio_b64, bio_buff); |
| 125 |
|
| 126 |
/* Write the MD5 hash into the buffer to encode it in base64 */ |
| 127 |
BIO_write(bio_buff, to_enc->data, to_enc->len); |
| 128 |
/* BIO_flush is a macro and GCC 4.1.2 complains without this cast*/ |
| 129 |
(void) BIO_flush(bio_buff); |
| 130 |
|
| 131 |
/* Pull out the base64 encoding of the MD5 hash */ |
| 132 |
bio_b64_len = BIO_get_mem_data(bio_buff, &bio_b64_data); |
| 133 |
g_assert(bio_b64_data); |
| 134 |
ret = g_strndup(bio_b64_data, bio_b64_len); |
| 135 |
|
| 136 |
/* If bio_b64 is freed separately, freeing bio_buff will |
| 137 |
* invalidly free memory and potentially segfault. |
| 138 |
*/ |
| 139 |
BIO_free_all(bio_buff); |
| 140 |
return ret; |
| 141 |
} |
| 142 |
|
| 143 |
gchar* |
| 144 |
s3_hex_encode(const GByteArray *to_enc) { |
| 145 |
guint i; |
| 146 |
gchar *ret = NULL, table[] = "0123456789abcdef"; |
| 147 |
if (!to_enc) return NULL; |
| 148 |
|
| 149 |
ret = g_new(gchar, to_enc->len*2 + 1); |
| 150 |
for (i = 0; i < to_enc->len; i++) { |
| 151 |
/* most significant 4 bits */ |
| 152 |
ret[i*2] = table[to_enc->data[i] >> 4]; |
| 153 |
/* least significant 4 bits */ |
| 154 |
ret[i*2 + 1] = table[to_enc->data[i] & 0xf]; |
| 155 |
} |
| 156 |
ret[to_enc->len*2] = '\0'; |
| 157 |
|
| 158 |
return ret; |
| 159 |
} |
| 160 |
|
| 161 |
GByteArray* |
| 162 |
s3_compute_md5_hash(const GByteArray *to_hash) { |
| 163 |
MD5_CTX md5_ctx; |
| 164 |
GByteArray *ret; |
| 165 |
if (!to_hash) return NULL; |
| 166 |
|
| 167 |
ret = g_byte_array_sized_new(S3_MD5_HASH_BYTE_LEN); |
| 168 |
g_byte_array_set_size(ret, S3_MD5_HASH_BYTE_LEN); |
| 169 |
|
| 170 |
MD5_Init(&md5_ctx); |
| 171 |
MD5_Update(&md5_ctx, to_hash->data, to_hash->len); |
| 172 |
MD5_Final(ret->data, &md5_ctx); |
| 173 |
|
| 174 |
return ret; |
| 175 |
} |
| 176 |
|
| 177 |
char * |
| 178 |
s3_compute_sha256_hash_ba( |
| 179 |
const GByteArray *to_hash) |
| 180 |
{ |
| 181 |
return s3_compute_sha256_hash(to_hash->data, to_hash->len); |
| 182 |
} |
| 183 |
|
| 184 |
char * |
| 185 |
s3_compute_sha256_hash( |
| 186 |
const unsigned char *to_hash, |
| 187 |
int len) |
| 188 |
{ |
| 189 |
unsigned char hash[SHA256_DIGEST_LENGTH]; |
| 190 |
char *ret = malloc(SHA256_DIGEST_LENGTH*2+1); |
| 191 |
int i; |
| 192 |
|
| 193 |
SHA256_CTX sha256; |
| 194 |
SHA256_Init(&sha256); |
| 195 |
SHA256_Update(&sha256, to_hash, len); |
| 196 |
SHA256_Final(hash, &sha256); |
| 197 |
for (i = 0; i < SHA256_DIGEST_LENGTH; i++) { |
| 198 |
sprintf(ret + (i * 2), "%02x", hash[i]); |
| 199 |
} |
| 200 |
ret[64] = 0; |
| 201 |
return ret; |
| 202 |
} |
| 203 |
|
| 204 |
char * |
| 205 |
s3_uri_encode( |
| 206 |
const char *s, |
| 207 |
gboolean encodeSlash) |
| 208 |
{ |
| 209 |
GString *ret = g_string_new(""); |
| 210 |
int i; |
| 211 |
int len_s = strlen(s); |
| 212 |
|
| 213 |
for (i = 0; i < len_s; i++) { |
| 214 |
unsigned char ch = s[i]; |
| 215 |
if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') || ch == '_' || ch == '-' || ch == '~' || ch == '.') { |
| 216 |
g_string_append_c(ret, ch); |
| 217 |
} else if (ch == '/') { |
| 218 |
if (encodeSlash) { |
| 219 |
g_string_append(ret, "%2F"); |
| 220 |
} else { |
| 221 |
g_string_append_c(ret, ch); |
| 222 |
} |
| 223 |
} else { |
| 224 |
g_string_append_printf(ret, "%%%02X", ch); |
| 225 |
} |
| 226 |
} |
| 227 |
return g_string_free(ret, FALSE); |
| 228 |
} |
| 229 |
|
| 230 |
unsigned char * |
| 231 |
EncodeHMACSHA256( |
| 232 |
unsigned char* key, |
| 233 |
int keylen, |
| 234 |
const char* data, |
| 235 |
int datalen) |
| 236 |
{ |
| 237 |
unsigned char *hmachash = malloc(32); |
| 238 |
const unsigned char *datatohash = (unsigned char *)data; |
| 239 |
unsigned char tk[SHA256_DIGEST_LENGTH]; |
| 240 |
|
| 241 |
// Initialise HMACh |
| 242 |
#if (defined OPENSSL_VERSION_NUMBER && OPENSSL_VERSION_NUMBER < 0x10100000L) \ |
| 243 |
|| defined LIBRESSL_VERSION_NUMBER |
| 244 |
|
| 245 |
HMAC_CTX HMAC; |
| 246 |
#else |
| 247 |
HMAC_CTX *HMAC; |
| 248 |
#endif |
| 249 |
unsigned int hmaclength = 32; |
| 250 |
memset(hmachash, 0, hmaclength); |
| 251 |
|
| 252 |
if (keylen > 64 ) { |
| 253 |
SHA256(key, keylen, tk); |
| 254 |
key = tk; |
| 255 |
keylen = SHA256_DIGEST_LENGTH; |
| 256 |
} |
| 257 |
|
| 258 |
// Digest the key and message using SHA256 |
| 259 |
#if (defined OPENSSL_VERSION_NUMBER && OPENSSL_VERSION_NUMBER < 0x10100000L) \ |
| 260 |
|| defined LIBRESSL_VERSION_NUMBER |
| 261 |
HMAC_CTX_init(&HMAC); |
| 262 |
HMAC_Init_ex(&HMAC, key, keylen, EVP_sha256(),NULL); |
| 263 |
HMAC_Update(&HMAC, datatohash, datalen); |
| 264 |
HMAC_Final(&HMAC, hmachash, &hmaclength); |
| 265 |
HMAC_CTX_cleanup(&HMAC); |
| 266 |
#else |
| 267 |
HMAC = HMAC_CTX_new(); |
| 268 |
HMAC_CTX_reset(HMAC); |
| 269 |
HMAC_Init_ex(HMAC, key, keylen, EVP_sha256(),NULL); |
| 270 |
HMAC_Update(HMAC, datatohash, datalen); |
| 271 |
HMAC_Final(HMAC, hmachash, &hmaclength); |
| 272 |
HMAC_CTX_free(HMAC); |
| 273 |
#endif |
| 274 |
|
| 275 |
return hmachash; |
| 276 |
} |
| 277 |
|
| 278 |
unsigned char * |
| 279 |
s3_tohex( |
| 280 |
unsigned char *s, |
| 281 |
int len_s) |
| 282 |
{ |
| 283 |
unsigned char *r = malloc(len_s*2+1); |
| 284 |
unsigned char *t = r; |
| 285 |
int i; |
| 286 |
gchar table[] = "0123456789abcdef"; |
| 287 |
|
| 288 |
for (i = 0; i < len_s; i++) { |
| 289 |
/* most significant 4 bits */ |
| 290 |
*t++ = table[s[i] >> 4]; |
| 291 |
/* least significant 4 bits */ |
| 292 |
*t++ = table[s[i] & 0xf]; |
| 293 |
} |
| 294 |
*t = '\0'; |
| 295 |
return r; |
| 296 |
} |
| 297 |
|