2008-11-09 11:52:14 -05:00
|
|
|
/*
|
|
|
|
* libretroshare/src/pqi: authssl.h
|
|
|
|
*
|
|
|
|
* 3P/PQI network interface for RetroShare.
|
|
|
|
*
|
|
|
|
* Copyright 2004-2008 by Robert Fernie.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License Version 2 as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
* USA.
|
|
|
|
*
|
|
|
|
* Please report all bugs and problems to "retroshare@lunamutt.com".
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MRK_AUTH_SSL_HEADER
|
|
|
|
#define MRK_AUTH_SSL_HEADER
|
|
|
|
|
2009-05-23 11:07:35 -04:00
|
|
|
/*
|
2010-01-13 16:22:52 -05:00
|
|
|
* This is an implementation of SSL certificate authentication, which is
|
2009-05-23 11:07:35 -04:00
|
|
|
* overloaded with pgp style signatures, and web-of-trust authentication.
|
2008-11-09 11:52:14 -05:00
|
|
|
*
|
2010-01-13 16:22:52 -05:00
|
|
|
* only the owner ssl cert is store, the rest is jeus callback verification
|
2009-05-23 11:07:35 -04:00
|
|
|
*
|
|
|
|
* To use as an SSL authentication system, you must use a common CA certificate.
|
|
|
|
* * The pqissl stuff doesn't need to differentiate between SSL, SSL + PGP,
|
|
|
|
* as its X509 certs.
|
|
|
|
* * The rsserver stuff has to distinguish between all three types ;(
|
|
|
|
*
|
2008-11-09 11:52:14 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "util/rsthreads.h"
|
|
|
|
|
|
|
|
#include "pqi/pqi_base.h"
|
|
|
|
#include "pqi/pqinetwork.h"
|
2010-01-13 16:22:52 -05:00
|
|
|
#include "rsiface/rspeers.h"
|
2008-11-09 11:52:14 -05:00
|
|
|
|
2010-01-13 15:52:31 -05:00
|
|
|
typedef std::string SSL_id;
|
|
|
|
|
2009-04-22 19:29:16 -04:00
|
|
|
class AuthSSL;
|
|
|
|
|
2010-01-13 16:16:56 -05:00
|
|
|
class p3ConnectMgr;
|
|
|
|
|
2009-04-22 19:29:16 -04:00
|
|
|
class sslcert
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
sslcert(X509 *x509, std::string id);
|
2010-01-13 15:56:55 -05:00
|
|
|
sslcert();
|
2009-04-22 19:29:16 -04:00
|
|
|
|
|
|
|
/* certificate parameters */
|
|
|
|
std::string id;
|
|
|
|
std::string name;
|
|
|
|
std::string location;
|
|
|
|
std::string org;
|
|
|
|
std::string email;
|
|
|
|
|
2009-05-23 11:07:35 -04:00
|
|
|
std::string issuer;
|
|
|
|
|
2009-04-22 19:29:16 -04:00
|
|
|
std::string fpr;
|
2010-01-13 16:22:52 -05:00
|
|
|
//std::list<std::string> signers;
|
2009-04-22 19:29:16 -04:00
|
|
|
|
|
|
|
/* Auth settings */
|
|
|
|
bool authed;
|
|
|
|
|
|
|
|
/* INTERNAL Parameters */
|
|
|
|
X509 *certificate;
|
|
|
|
};
|
|
|
|
|
2008-11-09 11:52:14 -05:00
|
|
|
|
2010-01-13 15:56:55 -05:00
|
|
|
class AuthSSL
|
2008-11-09 11:52:14 -05:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2010-01-13 15:58:58 -05:00
|
|
|
/* Initialisation Functions (Unique) */
|
2008-11-09 11:52:14 -05:00
|
|
|
AuthSSL();
|
2009-05-23 11:07:35 -04:00
|
|
|
bool validateOwnCertificate(X509 *x509, EVP_PKEY *pkey);
|
|
|
|
|
2008-11-09 11:52:14 -05:00
|
|
|
virtual bool active();
|
|
|
|
virtual int InitAuth(const char *srvr_cert, const char *priv_key,
|
|
|
|
const char *passwd);
|
|
|
|
virtual bool CloseAuth();
|
|
|
|
virtual int setConfigDirectories(std::string confFile, std::string neighDir);
|
2010-01-13 16:16:56 -05:00
|
|
|
SSL_CTX * getNewSslCtx();
|
2008-11-09 11:52:14 -05:00
|
|
|
|
2009-05-23 11:07:35 -04:00
|
|
|
|
2008-11-09 11:52:14 -05:00
|
|
|
/*********** Overloaded Functions from p3AuthMgr **********/
|
|
|
|
|
2010-01-13 16:22:52 -05:00
|
|
|
/* get Certificate Id */
|
2008-11-09 11:52:14 -05:00
|
|
|
virtual std::string OwnId();
|
2010-01-13 16:29:21 -05:00
|
|
|
virtual std::string getOwnLocation();
|
2010-01-13 16:22:52 -05:00
|
|
|
//virtual bool getAllList(std::list<std::string> &ids);
|
|
|
|
//virtual bool getAuthenticatedList(std::list<std::string> &ids);
|
|
|
|
//virtual bool getUnknownList(std::list<std::string> &ids);
|
|
|
|
//virtual bool getSSLChildListOfGPGId(std::string gpg_id, std::list<std::string> &ids);
|
2010-01-13 16:16:18 -05:00
|
|
|
|
2008-11-09 11:52:14 -05:00
|
|
|
/* get Details from the Certificates */
|
2010-01-13 16:22:52 -05:00
|
|
|
//virtual bool isAuthenticated(std::string id);
|
|
|
|
//virtual std::string getName(std::string id);
|
|
|
|
//virtual std::string getIssuerName(std::string id);
|
|
|
|
//virtual std::string getGPGId(SSL_id id);
|
|
|
|
//virtual bool getCertDetails(std::string id, sslcert &cert);
|
2009-04-22 19:29:16 -04:00
|
|
|
|
2008-11-09 11:52:14 -05:00
|
|
|
/* High Level Load/Save Configuration */
|
2010-01-13 16:22:52 -05:00
|
|
|
//virtual bool FinalSaveCertificates();
|
|
|
|
//virtual bool CheckSaveCertificates();
|
|
|
|
//virtual bool saveCertificates();
|
|
|
|
//virtual bool loadCertificates();
|
2008-11-09 11:52:14 -05:00
|
|
|
|
|
|
|
/* Load/Save certificates */
|
2010-01-13 16:22:52 -05:00
|
|
|
|
|
|
|
virtual bool LoadDetailsFromStringCert(std::string pem, RsPeerDetails &pd);
|
|
|
|
virtual std::string SaveOwnCertificateToString();
|
|
|
|
//virtual bool LoadCertificateFromFile(std::string filename, std::string &id);
|
|
|
|
//virtual bool SaveCertificateToFile(std::string id, std::string filename);
|
|
|
|
//bool ProcessX509(X509 *x509, std::string &id);
|
|
|
|
//
|
|
|
|
//virtual bool LoadCertificateFromBinary(const uint8_t *ptr, uint32_t len, std::string &id);
|
|
|
|
//virtual bool SaveCertificateToBinary(std::string id, uint8_t **ptr, uint32_t *len);
|
2008-11-09 11:52:14 -05:00
|
|
|
|
|
|
|
/* Sign / Encrypt / Verify Data (TODO) */
|
|
|
|
virtual bool SignData(std::string input, std::string &sign);
|
|
|
|
virtual bool SignData(const void *data, const uint32_t len, std::string &sign);
|
2009-04-22 19:29:16 -04:00
|
|
|
virtual bool SignDataBin(std::string, unsigned char*, unsigned int*);
|
|
|
|
virtual bool SignDataBin(const void*, uint32_t, unsigned char*, unsigned int*);
|
2010-02-03 16:21:04 -05:00
|
|
|
virtual bool VerifySignBin(const void*, uint32_t, unsigned char*, unsigned int, sslcert* cert);
|
|
|
|
virtual bool VerifyOwnSignBin(const void*, uint32_t, unsigned char*, unsigned int);
|
|
|
|
virtual bool VerifyOtherSignBin(const void*, uint32_t, unsigned char*, unsigned int, std::string sslCert);
|
2009-04-22 19:29:16 -04:00
|
|
|
|
2009-12-13 16:59:26 -05:00
|
|
|
// return : false if encrypt failed
|
2010-01-13 16:16:18 -05:00
|
|
|
bool encrypt(void *&out, int &outlen, const void *in, int inlen, std::string peerId);
|
2009-12-13 16:59:26 -05:00
|
|
|
|
|
|
|
// return : false if decrypt fails
|
2010-01-13 16:16:18 -05:00
|
|
|
bool decrypt(void *&out, int &outlen, const void *in, int inlen);
|
2009-12-13 16:59:26 -05:00
|
|
|
|
2008-11-09 11:52:14 -05:00
|
|
|
|
|
|
|
/*********** Overloaded Functions from p3AuthMgr **********/
|
|
|
|
|
2009-05-23 11:07:35 -04:00
|
|
|
/************* Virtual Functions from AuthSSL *************/
|
2010-01-13 15:52:31 -05:00
|
|
|
X509* SignX509Req(X509_REQ *req, long days);
|
|
|
|
bool AuthX509(X509 *x509);
|
|
|
|
|
2009-05-23 11:07:35 -04:00
|
|
|
|
|
|
|
virtual int VerifyX509Callback(int preverify_ok, X509_STORE_CTX *ctx);
|
|
|
|
virtual bool ValidateCertificate(X509 *x509, std::string &peerId); /* validate + get id */
|
|
|
|
|
|
|
|
/************* Virtual Functions from AuthSSL *************/
|
|
|
|
|
|
|
|
|
2008-11-09 11:52:14 -05:00
|
|
|
public: /* SSL specific functions used in pqissl/pqissllistener */
|
|
|
|
SSL_CTX *getCTX();
|
2010-01-13 16:16:56 -05:00
|
|
|
static int ex_data_ctx_index; //used to pass the peer id in the ssl context
|
2008-11-09 11:52:14 -05:00
|
|
|
|
2009-05-23 11:07:35 -04:00
|
|
|
|
2010-01-13 16:22:52 -05:00
|
|
|
//bool FailedCertificate(X509 *x509, bool incoming); /* store for discovery */
|
|
|
|
//bool CheckCertificate(std::string peerId, X509 *x509); /* check that they are exact match */
|
2008-11-09 11:52:14 -05:00
|
|
|
|
|
|
|
/* Special Config Loading (backwards compatibility) */
|
2010-01-13 16:22:52 -05:00
|
|
|
//bool loadCertificates(bool &oldFormat, std::map<std::string, std::string> &keyValueMap);
|
2008-11-09 11:52:14 -05:00
|
|
|
|
2010-01-13 16:05:38 -05:00
|
|
|
static AuthSSL *getAuthSSL() throw() // pour obtenir l'instance
|
|
|
|
{ return instance_ssl; }
|
2009-05-23 11:07:35 -04:00
|
|
|
|
2010-01-13 16:16:56 -05:00
|
|
|
p3ConnectMgr *mConnMgr;
|
|
|
|
|
|
|
|
private:
|
2008-11-09 11:52:14 -05:00
|
|
|
|
2010-01-13 16:05:38 -05:00
|
|
|
// the single instance of this
|
|
|
|
static AuthSSL *instance_ssl;
|
|
|
|
|
2008-11-09 11:52:14 -05:00
|
|
|
/* Helper Functions */
|
2009-04-22 19:29:16 -04:00
|
|
|
X509 * loadX509FromPEM(std::string pem);
|
|
|
|
X509 * loadX509FromFile(std::string fname, std::string hash);
|
|
|
|
bool saveX509ToFile(X509 *x509, std::string fname, std::string &hash);
|
2008-11-09 11:52:14 -05:00
|
|
|
|
2009-04-22 19:29:16 -04:00
|
|
|
X509 * loadX509FromDER(const uint8_t *ptr, uint32_t len);
|
|
|
|
bool saveX509ToDER(X509 *x509, uint8_t **ptr, uint32_t *len);
|
2008-11-09 11:52:14 -05:00
|
|
|
|
|
|
|
/*********** LOCKED Functions ******/
|
2010-01-13 16:22:52 -05:00
|
|
|
//bool locked_FindCert(std::string id, sslcert **cert);
|
2008-11-09 11:52:14 -05:00
|
|
|
|
|
|
|
|
|
|
|
/* Data */
|
2010-01-13 15:58:58 -05:00
|
|
|
RsMutex sslMtx; /**** LOCKING */
|
2008-11-09 11:52:14 -05:00
|
|
|
|
|
|
|
int init;
|
|
|
|
std::string mCertConfigFile;
|
|
|
|
std::string mNeighDir;
|
|
|
|
|
|
|
|
SSL_CTX *sslctx;
|
|
|
|
|
|
|
|
std::string mOwnId;
|
2009-04-22 19:29:16 -04:00
|
|
|
sslcert *mOwnCert;
|
2008-11-09 11:52:14 -05:00
|
|
|
EVP_PKEY *pkey;
|
|
|
|
|
|
|
|
bool mToSaveCerts;
|
|
|
|
bool mConfigSaveActive;
|
2010-01-13 16:22:52 -05:00
|
|
|
//std::map<std::string, sslcert *> mCerts;
|
2008-11-09 11:52:14 -05:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2009-05-23 11:07:35 -04:00
|
|
|
X509_REQ *GenerateX509Req(
|
|
|
|
std::string pkey_file, std::string passwd,
|
|
|
|
std::string name, std::string email, std::string org,
|
|
|
|
std::string loc, std::string state, std::string country,
|
|
|
|
int nbits_in, std::string &errString);
|
|
|
|
|
|
|
|
X509 *SignX509Certificate(X509_NAME *issuer, EVP_PKEY *privkey, X509_REQ *req, long days);
|
|
|
|
|
|
|
|
|
2008-11-09 11:52:14 -05:00
|
|
|
/* Helper Functions */
|
|
|
|
int printSSLError(SSL *ssl, int retval, int err, unsigned long err2, std::ostream &out);
|
|
|
|
std::string getX509NameString(X509_NAME *name);
|
|
|
|
std::string getX509CNString(X509_NAME *name);
|
|
|
|
|
|
|
|
std::string getX509OrgString(X509_NAME *name);
|
|
|
|
std::string getX509LocString(X509_NAME *name);
|
|
|
|
std::string getX509CountryString(X509_NAME *name);
|
|
|
|
|
2009-05-23 11:07:35 -04:00
|
|
|
std::string getX509Info(X509 *cert);
|
2009-04-22 19:29:16 -04:00
|
|
|
bool getX509id(X509 *x509, std::string &xid);
|
2008-11-09 11:52:14 -05:00
|
|
|
|
2009-07-30 17:27:47 -04:00
|
|
|
int LoadCheckX509andGetIssuerName(const char *cert_file,
|
|
|
|
std::string &issuerName, std::string &userId);
|
2010-01-18 17:44:09 -05:00
|
|
|
int LoadCheckX509andGetLocation(const char *cert_file,
|
|
|
|
std::string &location, std::string &userId);
|
2009-05-23 11:07:35 -04:00
|
|
|
|
2009-04-22 19:29:16 -04:00
|
|
|
#endif // MRK_AUTH_SSL_HEADER
|