Debian Codegraph @

src/auth/davixx509cred.cpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) CERN 2013
4 * Author: Adrien Devresse <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include <davix_internal.hpp>
23 #include <ne_ssl.h>
24
25 #include "davixx509cred_internal.hpp"
26
27
28
29 namespace Davix {
30
31 struct X509CredentialInternal{
32 X509CredentialInternal() :
33 _cred(NULL),
34 x509_ucert(),
35 x509_ukey(),
36 x509_passwd(),
37 pemLoaded(false)
38 {}
39
40 X509CredentialInternal(const X509CredentialInternal & orig) :
41 _cred((orig._cred)?ne_ssl_dup_client_cert(orig._cred):NULL),
42 x509_ucert(orig.x509_ucert), x509_ukey(orig.x509_ukey),
43 x509_passwd(orig.x509_passwd), pemLoaded(orig.pemLoaded)
44 {}
45
46 X509CredentialInternal & operator=(const X509CredentialInternal & orig){
47 _cred= (orig._cred)?ne_ssl_dup_client_cert(orig._cred):NULL;
48
49 x509_ucert.assign(orig.x509_ucert);
50 x509_ukey.assign(orig.x509_ukey);
51 x509_passwd.assign(orig.x509_passwd);
52 pemLoaded = orig.pemLoaded;
53
54 return *this;
55 }
56
57 ~X509CredentialInternal(){
58 clear_cert();
59 }
60
61 void clear_cert(){
62 if(_cred){
63 ne_ssl_clicert_free(_cred);
64 _cred = NULL;
65 }
66 pemLoaded = false;
67 x509_ucert.clear();
68 x509_ukey.clear();
69 x509_passwd.clear();
70 }
71
72 ne_ssl_client_cert * _cred;
73
74 // Remember pem location
75 std::string x509_ucert;
76 std::string x509_ukey;
77 std::string x509_passwd;
78 bool pemLoaded;
79 };
80
81 X509Credential::X509Credential() :
82 d_ptr(new X509CredentialInternal())
83 {
84 }
85
86 X509Credential::X509Credential(const X509Credential &orig) :
87 d_ptr(new X509CredentialInternal(*(orig.d_ptr)))
88 {
89
90 }
91
92 X509Credential & X509Credential::operator =( const X509Credential & orig){
93 if(this == & orig)
94 return *this;
95 delete d_ptr;
96 d_ptr = new X509CredentialInternal(*(orig.d_ptr));
97 return *this;
98 }
99
100 X509Credential::~X509Credential(){
101 delete d_ptr;
102 }
103
104
105 int X509Credential::loadFromFileP12(const std::string &p12_cred, const std::string & passwd, DavixError **err){
106 d_ptr->clear_cert();
107 if( (d_ptr->_cred = ne_ssl_clicert_read(p12_cred.c_str())) == NULL){
108 Davix::DavixError::setupError(err, davix_scope_x509cred(),StatusCode::CredentialNotFound, std::string("Impossible to load credential ").append(p12_cred));
109 return -1;
110 }
111
112 if( ne_ssl_clicert_encrypted(d_ptr->_cred) !=0
113 && ne_ssl_clicert_decrypt(d_ptr->_cred, passwd.c_str()) !=0){
114 Davix::DavixError::setupError(err, davix_scope_x509cred(), StatusCode::LoginPasswordError, std::string("Impossible to decrypt the credential ").append(p12_cred).append(" with the provided password"));
115 d_ptr->clear_cert();
116 return -1;
117 }
118 return 0;
119 }
120
121
122 int X509Credential::loadFromFilePEM(const std::string & filepath_priv_key, const std::string & filepath_cred,
123 const std::string & password, DavixError** err){
124 d_ptr->clear_cert();
125 if( (d_ptr->_cred = SSL_X509_Pem_Read(filepath_priv_key, filepath_cred, password, err)) != NULL){
126 d_ptr->x509_ucert = filepath_cred;
127 d_ptr->x509_ukey = filepath_priv_key;
128 d_ptr->x509_passwd = password;
129 d_ptr->pemLoaded = true;
130 return 0;
131 }
132 return -1;
133 }
134
135 bool X509Credential::hasCert() const{
136 return (d_ptr->_cred != NULL);
137 }
138
139 ne_ssl_client_cert* X509CredentialExtra::extract_ne_ssl_clicert(const X509Credential &cred)
140 {
141 return (cred.d_ptr->_cred);
142 }
143
144 bool X509CredentialExtra::get_x509_info(const X509Credential &cred,
145 std::string* ucert, std::string* ukey, std::string* passwd)
146 {
147 ucert->assign(cred.d_ptr->x509_ucert);
148 ukey->assign(cred.d_ptr->x509_ukey);
149 passwd->assign(cred.d_ptr->x509_passwd);
150 return cred.d_ptr->pemLoaded;
151 }
152
153 } // namespace DAvix
154
155
156