Skip to content

Commit

Permalink
Merge pull request #141 from freelan-developers/openssl1.1.0
Browse files Browse the repository at this point in the history
Merge OpenSSL 1.1.0 branch into master.
  • Loading branch information
s-vincent authored Aug 10, 2017
2 parents 7da7e14 + db178b0 commit 0127fc9
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 67 deletions.
12 changes: 8 additions & 4 deletions libs/cryptoplus/include/cryptoplus/asn1/string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ namespace cryptoplus
* \brief Get the string data.
* \return The string data.
*/
const unsigned char* data() const;
const unsigned char* data();

/**
* \brief Set the internal data.
Expand Down Expand Up @@ -175,7 +175,7 @@ namespace cryptoplus
* \brief Build a string from data() and size().
* \return A string built from data() and that will be size() bytes long.
*/
std::string str() const;
std::string str();

/**
* \brief Get the content as an UTF-8 string.
Expand Down Expand Up @@ -254,9 +254,13 @@ namespace cryptoplus
{
return ASN1_STRING_length(ptr().get());
}
inline const unsigned char* string::data() const
inline const unsigned char* string::data()
{
#if OPENSSL_VERSION_NUMBER < 0x10100000L
return ASN1_STRING_data(ptr().get());
#else
return ASN1_STRING_get0_data(ptr().get());
#endif
}
inline void string::set_data(const void* _data, size_t data_len) const
{
Expand All @@ -278,7 +282,7 @@ namespace cryptoplus
{
return ASN1_STRING_type(ptr().get());
}
inline std::string string::str() const
inline std::string string::str()
{
return std::string(reinterpret_cast<const char*>(data()), size());
}
Expand Down
10 changes: 10 additions & 0 deletions libs/cryptoplus/include/cryptoplus/bio/bio_chain.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ namespace cryptoplus
{
public:

/**
* \brief Create a new bio_chain from a BIO_METHOD.
* \param type The type.
*/
explicit bio_chain(const BIO_METHOD* type);

/**
* \brief Create a new bio_chain from a BIO_METHOD.
* \param type The type.
Expand All @@ -93,7 +99,11 @@ namespace cryptoplus
boost::shared_ptr<BIO> m_bio;
};

#if OPENSSL_VERSION_NUMBER < 0x10100000L
inline bio_chain::bio_chain(BIO_METHOD* _type) : m_bio(BIO_new(_type), BIO_free_all)
#else
inline bio_chain::bio_chain(const BIO_METHOD* _type) : m_bio(BIO_new(_type), BIO_free_all)
#endif
{
throw_error_if_not(m_bio != NULL);
}
Expand Down
2 changes: 2 additions & 0 deletions libs/cryptoplus/include/cryptoplus/bio/bio_ptr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,12 @@ namespace cryptoplus
{
return m_bio;
}
#if OPENSSL_VERSION_NUMBER < 0x10100000L
inline bool bio_ptr::set_method(BIO_METHOD* _type) const
{
return BIO_set(m_bio, _type) != 0;
}
#endif
inline bio_ptr bio_ptr::push(bio_ptr bio) const
{
return bio_ptr(BIO_push(m_bio, bio.raw()));
Expand Down
29 changes: 13 additions & 16 deletions libs/cryptoplus/include/cryptoplus/cipher/cipher_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,17 +342,14 @@ namespace cryptoplus

private:

EVP_CIPHER_CTX m_ctx;
EVP_CIPHER_CTX* m_ctx;
};

inline cipher_context::cipher_context()
{
EVP_CIPHER_CTX_init(&m_ctx);
}
inline cipher_context::cipher_context() : m_ctx(EVP_CIPHER_CTX_new()) {}

inline cipher_context::~cipher_context()
{
EVP_CIPHER_CTX_cleanup(&m_ctx);
EVP_CIPHER_CTX_free(m_ctx);
}

template <typename T>
Expand All @@ -379,7 +376,7 @@ namespace cryptoplus
pubk.push_back(pkey->raw());
}

throw_error_if_not(EVP_SealInit(&m_ctx, _algorithm.raw(), &ek[0], &ekl[0], static_cast<unsigned char*>(iv), &pubk[0], static_cast<int>(pkeys_count)) != 0);
throw_error_if_not(EVP_SealInit(m_ctx, _algorithm.raw(), &ek[0], &ekl[0], static_cast<unsigned char*>(iv), &pubk[0], static_cast<int>(pkeys_count)) != 0);

for (std::vector<unsigned char*>::iterator p = ek.begin(); p != ek.end(); ++p)
{
Expand Down Expand Up @@ -408,7 +405,7 @@ namespace cryptoplus
inline void cipher_context::set_padding(bool enabled)
{
// The call always returns 1 so testing its return value is useless.
EVP_CIPHER_CTX_set_padding(&m_ctx, static_cast<int>(enabled));
EVP_CIPHER_CTX_set_padding(m_ctx, static_cast<int>(enabled));
}

inline size_t cipher_context::get_iso_10126_padding_size(size_t len) const
Expand All @@ -427,28 +424,28 @@ namespace cryptoplus

inline size_t cipher_context::key_length() const
{
return EVP_CIPHER_CTX_key_length(&m_ctx);
return EVP_CIPHER_CTX_key_length(m_ctx);
}

inline void cipher_context::set_key_length(size_t len)
{
throw_error_if_not(EVP_CIPHER_CTX_set_key_length(&m_ctx, static_cast<int>(len)) != 0);
throw_error_if_not(EVP_CIPHER_CTX_set_key_length(m_ctx, static_cast<int>(len)) != 0);
}

inline void cipher_context::ctrl(int type, int set_value, void* get_value)
{
throw_error_if_not(EVP_CIPHER_CTX_ctrl(&m_ctx, type, set_value, get_value) != 0);
throw_error_if_not(EVP_CIPHER_CTX_ctrl(m_ctx, type, set_value, get_value) != 0);
}

template <typename T>
inline void cipher_context::ctrl_get(int type, T& value)
{
throw_error_if_not(EVP_CIPHER_CTX_ctrl(&m_ctx, type, 0, &value) != 0);
throw_error_if_not(EVP_CIPHER_CTX_ctrl(m_ctx, type, 0, &value) != 0);
}

inline void cipher_context::ctrl_set(int type, int value)
{
throw_error_if_not(EVP_CIPHER_CTX_ctrl(&m_ctx, type, value, NULL) != 0);
throw_error_if_not(EVP_CIPHER_CTX_ctrl(m_ctx, type, value, NULL) != 0);
}

inline size_t cipher_context::update(void* out, size_t out_len, const buffer& in)
Expand All @@ -468,17 +465,17 @@ namespace cryptoplus

inline const EVP_CIPHER_CTX& cipher_context::raw() const
{
return m_ctx;
return *m_ctx;
}

inline EVP_CIPHER_CTX& cipher_context::raw()
{
return m_ctx;
return *m_ctx;
}

inline cipher_algorithm cipher_context::algorithm() const
{
return cipher_algorithm(EVP_CIPHER_CTX_cipher(&m_ctx));
return cipher_algorithm(EVP_CIPHER_CTX_cipher(m_ctx));
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions libs/cryptoplus/include/cryptoplus/cryptoplus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,22 @@ namespace cryptoplus
*
* Only one instance of this class should be created. When an instance exists, the library can proceed to name resolutions.
*/
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
typedef initializer<_OpenSSL_add_all_algorithms, _null_function> algorithms_initializer;
#else
typedef initializer<_OpenSSL_add_all_algorithms, EVP_cleanup> algorithms_initializer;
#endif

/**
* \brief The crypto initializer.
*
* Only one instance of this class should be created. When an instance exists, it will prevent memory leaks related to the libcrypto's internals.
*/
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
typedef initializer<_null_function, _null_function> crypto_initializer;
#else
typedef initializer<_null_function, CRYPTO_cleanup_all_ex_data> crypto_initializer;
#endif
}

#endif /* CRYPTOPLUS_CRYPTOPLUS_HPP */
9 changes: 9 additions & 0 deletions libs/cryptoplus/include/cryptoplus/error/error_strings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ namespace cryptoplus
*
* Only one instance of this class should be created. When an instance exists, the library can provide more informative error strings.
*/

#if OPENSSL_VERSION_NUMBER >= 0x10100000L
inline void _null_function()
{
}

typedef initializer<_null_function, _null_function> error_strings_initializer;
#else
typedef initializer<ERR_load_crypto_strings, ERR_free_strings> error_strings_initializer;
#endif

/**
* \brief Get the error string associated with a specified error.
Expand Down
32 changes: 23 additions & 9 deletions libs/cryptoplus/include/cryptoplus/hash/hmac_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,35 @@ namespace cryptoplus

private:

HMAC_CTX m_ctx;
HMAC_CTX* m_ctx;
};

inline hmac_context::hmac_context()
{
HMAC_CTX_init(&m_ctx);
#if OPENSSL_VERSION_NUMBER < 0x10100000L
m_ctx = new HMAC_CTX;
HMAC_CTX_init(m_ctx);
#else
m_ctx = HMAC_CTX_new();
#endif
}

inline hmac_context::~hmac_context()
{
HMAC_CTX_cleanup(&m_ctx);
#if OPENSSL_VERSION_NUMBER < 0x10100000L
HMAC_CTX_cleanup(m_ctx);
delete m_ctx;
#else
HMAC_CTX_free(m_ctx);
#endif
}

inline void hmac_context::update(const void* data, size_t len)
{
#if OPENSSL_VERSION_NUMBER < 0x01000000
HMAC_Update(&m_ctx, static_cast<const unsigned char*>(data), static_cast<int>(len));
HMAC_Update(m_ctx, static_cast<const unsigned char*>(data), static_cast<int>(len));
#else
throw_error_if_not(HMAC_Update(&m_ctx, static_cast<const unsigned char*>(data), static_cast<int>(len)) != 0);
throw_error_if_not(HMAC_Update(m_ctx, static_cast<const unsigned char*>(data), static_cast<int>(len)) != 0);
#endif
}

Expand All @@ -172,18 +182,22 @@ namespace cryptoplus

inline const HMAC_CTX& hmac_context::raw() const
{
return m_ctx;
return *m_ctx;
}

inline HMAC_CTX& hmac_context::raw()
{
return m_ctx;
return *m_ctx;
}

inline message_digest_algorithm hmac_context::algorithm() const
{
//WARNING: Here we directly use the undocumented HMAC_CTX.md field. This is unlikely to change, but if it ever does, we'll have to find a better way of doing things nicely.
return message_digest_algorithm(m_ctx.md);
#if OPENSSL_VERSION_NUMBER < 0x10100000L
//WARNING: Here we directly use the undocumented HMAC_CTX.md field.
return message_digest_algorithm(m_ctx->md);
#else
return HMAC_CTX_get_md(m_ctx);
#endif
}
}
}
Expand Down
Loading

0 comments on commit 0127fc9

Please sign in to comment.