2020-03-04 18:05:57 +03:00
/* -------------------------------------------------------------------------- */
2022-04-07 20:49:58 +03:00
/* Copyright 2002-2022, OpenNebula Project, OpenNebula Systems */
2020-03-04 18:05:57 +03:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
2020-06-29 13:14:00 +03:00
# ifndef SSL_UTIL_H
# define SSL_UTIL_H
2020-03-04 18:05:57 +03:00
2020-06-29 13:14:00 +03:00
# include <string>
2020-07-05 23:01:32 +03:00
# include <vector>
2020-09-10 14:32:52 +03:00
# include <mutex>
# include <memory>
2020-03-04 18:05:57 +03:00
2020-06-29 13:14:00 +03:00
namespace ssl_util
2020-03-04 18:05:57 +03:00
{
2020-07-05 23:01:32 +03:00
/**
* Base 64 decoding
* @ param in the string to decode
* @ param out the decoded string
*/
2020-06-29 13:14:00 +03:00
void base64_decode ( const std : : string & in , std : : string & out ) ;
2020-07-05 23:01:32 +03:00
/**
* Base 64 encoding
* @ param in the string to encode
* @ param out the encoded string
* @ return 0 on success , - 1 on error
*/
2020-06-29 13:14:00 +03:00
int base64_encode ( const std : : string & in , std : : string & out ) ;
2020-07-05 23:01:32 +03:00
/**
* Decompress the input string unsing zlib
* @ param in input string
* @ param out decompressed string
* @ return 0 on success , - 1 on error
*/
2020-06-29 13:14:00 +03:00
int zlib_decompress ( const std : : string & in , std : : string & out ) ;
2020-07-05 23:01:32 +03:00
/**
* Decompress the input string unsing zlib and base64 decodes the result
* @ param in input string
* @ param out decompressed string
* @ return 0 on success , - 1 on error
*/
int zlib_decompress64 ( const std : : string & in , std : : string & out ) ;
/**
* Compress the input string unsing zlib
* @ param in input string
* @ param out compressed string true to base64 encode output
* @ return 0 on success , - 1 on error
*/
2020-06-29 13:14:00 +03:00
int zlib_compress ( const std : : string & in , std : : string & out ) ;
2020-07-05 23:01:32 +03:00
/**
* Compress the input string unsing zlib and base64 encodes the ouput
* @ param in input string
* @ param out compressed string true to base64 encode output
* @ return 0 on success , - 1 on error
*/
int zlib_compress64 ( const std : : string & in , std : : string & out ) ;
2020-06-29 13:14:00 +03:00
/**
* Set path to public and private rsa keys
*/
void init_rsa_keys ( const std : : string & pub_key , const std : : string & pri_key ) ;
2020-07-05 23:01:32 +03:00
/**
* Is rsa initialized and private key set ?
*/
2020-06-29 13:14:00 +03:00
bool is_rsa_set ( ) ;
2020-07-05 23:01:32 +03:00
/**
* Encrypt message with public key
*/
2020-06-29 13:14:00 +03:00
int rsa_public_encrypt ( const std : : string & in , std : : string & out ) ;
2020-07-05 23:01:32 +03:00
/**
* Decrypt message with private key
*/
2020-06-29 13:14:00 +03:00
int rsa_private_decrypt ( const std : : string & in , std : : string & out ) ;
2020-07-05 23:01:32 +03:00
extern " C " void sslmutex_lock_callback ( int mode , int type , char * file ,
2020-09-10 14:32:52 +03:00
int line ) ;
2020-07-05 23:01:32 +03:00
extern " C " unsigned long sslmutex_id_callback ( ) ;
2020-09-10 14:32:52 +03:00
// Needed for openssl version < 1.1.0
2020-07-05 23:01:32 +03:00
class SSLMutex
{
public :
static void initialize ( ) ;
static void finalize ( ) ;
private :
friend void sslmutex_lock_callback ( int mode , int type , char * file ,
int line ) ;
SSLMutex ( ) ;
~ SSLMutex ( ) ;
static SSLMutex * ssl_mutex ;
2020-09-10 14:32:52 +03:00
static std : : vector < std : : unique_ptr < std : : mutex > > vmutex ;
2020-07-05 23:01:32 +03:00
} ;
2020-06-29 13:14:00 +03:00
} // namespace ssl_util
# endif /*SSL_UTIL_H_*/