summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEdwin Mons <edwin.mons@isode.com>2018-10-29 14:31:18 (GMT)
committerEdwin Mons <edwin.mons@isode.com>2018-11-14 11:02:05 (GMT)
commitc0615a472f8d23ce449fd59bbb1cdf7071082a43 (patch)
treeb2815062ce279824446febd403c0029f04c29f33
parent30639ed8dbb419890eab5a0b46d3a78896c7f22d (diff)
downloadswift-c0615a472f8d23ce449fd59bbb1cdf7071082a43.zip
swift-c0615a472f8d23ce449fd59bbb1cdf7071082a43.tar.bz2
Catch bad_numeric_casts in crypto code
Exceptions thrown by boost::numeric_cast are now caught and an assert explicitly triggered. Test-Information: Unit tests pass on macOS 10.13 Change-Id: I9a1cbe5ae2765e4275bf35473a871ef8468fd729
-rw-r--r--Swiften/Crypto/CommonCryptoCryptoProvider.cpp23
-rw-r--r--Swiften/Crypto/OpenSSLCryptoProvider.cpp7
2 files changed, 25 insertions, 5 deletions
diff --git a/Swiften/Crypto/CommonCryptoCryptoProvider.cpp b/Swiften/Crypto/CommonCryptoCryptoProvider.cpp
index d4257e0..3cc69b0 100644
--- a/Swiften/Crypto/CommonCryptoCryptoProvider.cpp
+++ b/Swiften/Crypto/CommonCryptoCryptoProvider.cpp
@@ -1,5 +1,5 @@
1/* 1/*
2 * Copyright (c) 2013-2017 Isode Limited. 2 * Copyright (c) 2013-2018 Isode Limited.
3 * All rights reserved. 3 * All rights reserved.
4 * See the COPYING file for more information. 4 * See the COPYING file for more information.
5 */ 5 */
@@ -49,7 +49,12 @@ namespace {
49 template<typename ContainerType> 49 template<typename ContainerType>
50 Hash& updateInternal(const ContainerType& data) { 50 Hash& updateInternal(const ContainerType& data) {
51 assert(!finalized); 51 assert(!finalized);
52 if (!CC_SHA1_Update(&context, vecptr(data), boost::numeric_cast<CC_LONG>(data.size()))) { 52 try {
53 if (!CC_SHA1_Update(&context, vecptr(data), boost::numeric_cast<CC_LONG>(data.size()))) {
54 assert(false);
55 }
56 }
57 catch (const boost::numeric::bad_numeric_cast&) {
53 assert(false); 58 assert(false);
54 } 59 }
55 return *this; 60 return *this;
@@ -90,7 +95,12 @@ namespace {
90 template<typename ContainerType> 95 template<typename ContainerType>
91 Hash& updateInternal(const ContainerType& data) { 96 Hash& updateInternal(const ContainerType& data) {
92 assert(!finalized); 97 assert(!finalized);
93 if (!CC_MD5_Update(&context, vecptr(data), boost::numeric_cast<CC_LONG>(data.size()))) { 98 try {
99 if (!CC_MD5_Update(&context, vecptr(data), boost::numeric_cast<CC_LONG>(data.size()))) {
100 assert(false);
101 }
102 }
103 catch (const boost::numeric::bad_numeric_cast&) {
94 assert(false); 104 assert(false);
95 } 105 }
96 return *this; 106 return *this;
@@ -104,7 +114,12 @@ namespace {
104 template<typename T> 114 template<typename T>
105 ByteArray getHMACSHA1Internal(const T& key, const ByteArray& data) { 115 ByteArray getHMACSHA1Internal(const T& key, const ByteArray& data) {
106 std::vector<unsigned char> result(CC_SHA1_DIGEST_LENGTH); 116 std::vector<unsigned char> result(CC_SHA1_DIGEST_LENGTH);
107 CCHmac(kCCHmacAlgSHA1, vecptr(key), key.size(), vecptr(data), boost::numeric_cast<CC_LONG>(data.size()), vecptr(result)); 117 try {
118 CCHmac(kCCHmacAlgSHA1, vecptr(key), key.size(), vecptr(data), boost::numeric_cast<CC_LONG>(data.size()), vecptr(result));
119 }
120 catch (const boost::numeric::bad_numeric_cast&) {
121 assert(false);
122 }
108 return result; 123 return result;
109 } 124 }
110} 125}
diff --git a/Swiften/Crypto/OpenSSLCryptoProvider.cpp b/Swiften/Crypto/OpenSSLCryptoProvider.cpp
index e8c1c73..73f46a6 100644
--- a/Swiften/Crypto/OpenSSLCryptoProvider.cpp
+++ b/Swiften/Crypto/OpenSSLCryptoProvider.cpp
@@ -107,7 +107,12 @@ namespace {
107 ByteArray getHMACSHA1Internal(const T& key, const ByteArray& data) { 107 ByteArray getHMACSHA1Internal(const T& key, const ByteArray& data) {
108 unsigned int len = SHA_DIGEST_LENGTH; 108 unsigned int len = SHA_DIGEST_LENGTH;
109 std::vector<unsigned char> result(len); 109 std::vector<unsigned char> result(len);
110 HMAC(EVP_sha1(), vecptr(key), boost::numeric_cast<int>(key.size()), vecptr(data), data.size(), vecptr(result), &len); 110 try {
111 HMAC(EVP_sha1(), vecptr(key), boost::numeric_cast<int>(key.size()), vecptr(data), boost::numeric_cast<int>(data.size()), vecptr(result), &len);
112 }
113 catch (const boost::numeric::bad_numeric_cast&) {
114 assert(false);
115 }
111 return result; 116 return result;
112 } 117 }
113} 118}