From 6ec622c0cfd7564c9c16e20dc1bf600157c84d3b Mon Sep 17 00:00:00 2001 From: Yehuda Sadeh Date: Fri, 3 Dec 2010 12:48:26 -0800 Subject: [PATCH] common: use ceph_armor instead of openssl based functions also modify ceph_[un]armor to get dest buffer length --- src/Makefile.am | 2 -- src/common/armor.c | 49 ++++++++++++++++++++++++++++++-------------- src/common/armor.h | 6 ++++-- src/common/base64.c | 22 +++++++++++++++++++- src/common/buffer.cc | 4 ++-- src/rgw/rgw_admin.cc | 7 ++++--- src/rgw/rgw_main.cc | 6 +++--- src/rgw/rgw_op.cc | 8 ++++---- 8 files changed, 72 insertions(+), 32 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index ee423f52409b9..87bfd94378b60 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -430,7 +430,6 @@ libcommon_files = \ common/ConfUtils.cc \ common/MemoryModel.cc \ common/armor.c \ - common/base64.c \ common/str_list.cc \ common/errno.cc \ mon/MonMap.cc \ @@ -560,7 +559,6 @@ noinst_HEADERS = \ cm.txt\ common/arch.h\ common/armor.h\ - common/base64.h\ common/debug.h\ common/errno.h\ common/lockdep.h\ diff --git a/src/common/armor.c b/src/common/armor.c index 73cc74e5670b0..067ed70414033 100644 --- a/src/common/armor.c +++ b/src/common/armor.c @@ -29,44 +29,63 @@ static int decode_bits(char c) return -EINVAL; } -int ceph_armor(char *dst, const char *src, const char *end) +static int set_str_val(char **pdst, const char *end, char c) +{ + if (*pdst < end) { + char *p = *pdst; + *p = c; + (*pdst)++; + } else + return -ERANGE; + + return 0; +} + +int ceph_armor(char *dst, const char *dst_end, const char *src, const char *end) { int olen = 0; int line = 0; +#define SET_DST(c) do { \ + int __ret = set_str_val(&dst, dst_end, c); \ + if (__ret < 0) \ + return __ret; \ +} while (0); + while (src < end) { unsigned char a, b, c; a = *src++; - *dst++ = encode_bits(a >> 2); + SET_DST(encode_bits(a >> 2)); if (src < end) { b = *src++; - *dst++ = encode_bits(((a & 3) << 4) | (b >> 4)); + SET_DST(encode_bits(((a & 3) << 4) | (b >> 4))); if (src < end) { c = *src++; - *dst++ = encode_bits(((b & 15) << 2) | (c >> 6)); - *dst++ = encode_bits(c & 63); + SET_DST(encode_bits(((b & 15) << 2) | + (c >> 6))); + SET_DST(encode_bits(c & 63)); } else { - *dst++ = encode_bits((b & 15) << 2); - *dst++ = '='; + SET_DST(encode_bits((b & 15) << 2)); + SET_DST('='); } } else { - *dst++ = encode_bits(((a & 3) << 4)); - *dst++ = '='; - *dst++ = '='; + SET_DST(encode_bits(((a & 3) << 4))); + SET_DST('='); + SET_DST('='); } olen += 4; line += 4; if (line == 64) { line = 0; - *(dst++) = '\n'; + SET_DST('\n'); olen++; } } return olen; } -int ceph_unarmor(char *dst, const char *src, const char *end) +int ceph_unarmor(char *dst, const char *dst_end, const char *src, const char *end) { int olen = 0; @@ -84,13 +103,13 @@ int ceph_unarmor(char *dst, const char *src, const char *end) if (a < 0 || b < 0 || c < 0 || d < 0) return -EINVAL; - *dst++ = (a << 2) | (b >> 4); + SET_DST((a << 2) | (b >> 4)); if (src[2] == '=') return olen + 1; - *dst++ = ((b & 15) << 4) | (c >> 2); + SET_DST(((b & 15) << 4) | (c >> 2)); if (src[3] == '=') return olen + 2; - *dst++ = ((c & 3) << 6) | d; + SET_DST(((c & 3) << 6) | d); olen += 3; src += 4; } diff --git a/src/common/armor.h b/src/common/armor.h index aa9407dad5e4b..0ed3b21a49921 100644 --- a/src/common/armor.h +++ b/src/common/armor.h @@ -2,8 +2,10 @@ #define CEPH_ARMOR_H extern "C" { -int ceph_armor(char *dst, const char *src, const char *end); -int ceph_unarmor(char *dst, const char *src, const char *end); +int ceph_armor(char *dst, const char *dst_end, + const char *src, const char *end); +int ceph_unarmor(char *dst, const char *dst_end, + const char *src, const char *end); } #endif diff --git a/src/common/base64.c b/src/common/base64.c index c1245325237f1..996ffc4ca753e 100644 --- a/src/common/base64.c +++ b/src/common/base64.c @@ -1,11 +1,21 @@ +#define CRYPTOPP +#ifdef CRYPTOPP +#include +#include +#else #include #include #include +#endif -#include +#include + +using namespace std; int encode_base64(const char *in, int in_len, char *out, int out_len) { +#ifdef CRYPTOPP +#else BIO *bmem, *b64; BUF_MEM *bptr; @@ -26,12 +36,21 @@ int encode_base64(const char *in, int in_len, char *out, int out_len) out[len - 1] = '\0'; BIO_free_all(b64); +#endif return 0; } int decode_base64(const char *in, int in_len, char *out, int out_len) { +#ifdef CRYPTOPP + string digest; + + CryptoPP::StringSource foo("CryptoPP is cool", true, +/* new CryptoPP::HashFilter(hash, */ + new CryptoPP::Base64Encoder ( + new CryptoPP::StringSink(digest))); +#else BIO *b64, *bmem; int ret; char in_eol[in_len + 2]; @@ -48,4 +67,5 @@ int decode_base64(const char *in, int in_len, char *out, int out_len) BIO_free_all(bmem); return ret; +#endif } diff --git a/src/common/buffer.cc b/src/common/buffer.cc index 06abc47da3bb0..4af9ec798d294 100644 --- a/src/common/buffer.cc +++ b/src/common/buffer.cc @@ -31,7 +31,7 @@ atomic_t buffer_total_alloc; void buffer::list::encode_base64(buffer::list& o) { bufferptr bp(length() * 4 / 3 + 3); - int l = ceph_armor(bp.c_str(), c_str(), c_str() + length()); + int l = ceph_armor(bp.c_str(), bp.c_str() + bp.length(), c_str(), c_str() + length()); bp.set_length(l); o.push_back(bp); } @@ -39,7 +39,7 @@ void buffer::list::encode_base64(buffer::list& o) void buffer::list::decode_base64(buffer::list& e) { bufferptr bp(e.length() * 3 / 4 + 4); - int l = ceph_unarmor(bp.c_str(), e.c_str(), e.c_str() + e.length()); + int l = ceph_unarmor(bp.c_str(), bp.c_str() + bp.length(), e.c_str(), e.c_str() + e.length()); assert(l <= (int)bp.length()); bp.set_length(l); push_back(bp); diff --git a/src/rgw/rgw_admin.cc b/src/rgw/rgw_admin.cc index 19d3003fb7aaa..e24403142f31f 100644 --- a/src/rgw/rgw_admin.cc +++ b/src/rgw/rgw_admin.cc @@ -10,7 +10,7 @@ using namespace std; #include #include "common/common_init.h" -#include "common/base64.h" +#include "common/armor.h" #include "rgw_user.h" #include "rgw_access.h" #include "rgw_acl.h" @@ -45,9 +45,10 @@ int gen_rand_base64(char *dest, int size) /* size should be the required string return -1; } - ret = encode_base64((const char *)buf, ((size - 1) * 3 + 4 - 1) / 4, tmp_dest, sizeof(tmp_dest)); + ret = ceph_armor(tmp_dest, &tmp_dest[sizeof(tmp_dest)], + (const char *)buf, ((const char *)buf) + ((size - 1) * 3 + 4 - 1) / 4); if (ret < 0) { - cerr << "encode_base64 failed" << std::endl; + cerr << "ceph_armor failed" << std::endl; return -1; } memcpy(dest, tmp_dest, size); diff --git a/src/rgw/rgw_main.cc b/src/rgw/rgw_main.cc index ff8699d249c59..92aefd2789452 100644 --- a/src/rgw/rgw_main.cc +++ b/src/rgw/rgw_main.cc @@ -28,7 +28,7 @@ #include #include "include/types.h" -#include "common/base64.h" +#include "common/armor.h" #include "common/BackTrace.h" using namespace std; @@ -191,9 +191,9 @@ static bool verify_signature(struct req_state *s) calc_hmac_sha1(key, key_len, auth_hdr.c_str(), auth_hdr.size(), hmac_sha1, &len); char b64[64]; /* 64 is really enough */ - int ret = encode_base64(hmac_sha1, len, b64, sizeof(b64)); + int ret = ceph_armor(b64, &b64[sizeof(b64)], hmac_sha1, &hmac_sha1[len]); if (ret < 0) { - RGW_LOG(10) << "encode_base64 failed" << endl; + RGW_LOG(10) << "ceph_armor failed" << endl; return false; } diff --git a/src/rgw/rgw_op.cc b/src/rgw/rgw_op.cc index 56cd5bfd2fc83..c5f1f5109c898 100644 --- a/src/rgw/rgw_op.cc +++ b/src/rgw/rgw_op.cc @@ -4,7 +4,7 @@ #include -#include "common/base64.h" +#include "common/armor.h" #include "rgw_access.h" #include "rgw_op.h" @@ -333,9 +333,9 @@ void RGWPutObj::execute() if (supplied_md5_b64) { RGW_LOG(15) << "supplied_md5_b64=" << supplied_md5_b64 << endl; - int ret = decode_base64(supplied_md5_b64, strlen(supplied_md5_b64), - supplied_md5_bin, sizeof(supplied_md5_bin)); - RGW_LOG(15) << "decode_base64 ret=" << ret << endl; + int ret = ceph_unarmor(supplied_md5_bin, &supplied_md5_bin[MD5_DIGEST_LENGTH + 1], + supplied_md5_b64, supplied_md5_b64 + strlen(supplied_md5_b64)); + RGW_LOG(15) << "ceph_armor ret=" << ret << endl; if (ret != MD5_DIGEST_LENGTH) { err.code = "InvalidDigest"; ret = -EINVAL; -- 2.39.5