~ubuntu-branches/ubuntu/quantal/poco/quantal

« back to all changes in this revision

Viewing changes to NetSSL_OpenSSL/src/X509Certificate.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Krzysztof Burghardt
  • Date: 2008-11-15 11:39:15 UTC
  • mfrom: (3.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20081115113915-7kauhm2c3m2i7oid
Tags: 1.3.3p1-2
* Fixed FTBFS with GCC 4.4 due to missing #include (Closes: #505619)
* Renamed 20_gcc43-missing-include.dpatch to 20_gcc44-missing-include.dpatch
* Downgraded dependencies on -dbg packages (Closes: #504342)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
//
2
2
// X509Certificate.cpp
3
3
//
4
 
// $Id: //poco/1.3/NetSSL_OpenSSL/src/X509Certificate.cpp#1 $
 
4
// $Id: //poco/1.3/NetSSL_OpenSSL/src/X509Certificate.cpp#3 $
5
5
//
6
6
// Library: NetSSL_OpenSSL
7
7
// Package: SSLCore
35
35
 
36
36
 
37
37
#include "Poco/Net/X509Certificate.h"
 
38
#include "Poco/Net/SSLException.h"
 
39
#include "Poco/Net/SSLManager.h"
 
40
#include "Poco/Net/SecureSocketImpl.h"
 
41
#include "Poco/TemporaryFile.h"
 
42
#include "Poco/FileStream.h"
 
43
#include "Poco/StreamCopier.h"
 
44
#include <openssl/pem.h>
38
45
 
39
46
 
40
47
namespace Poco {
41
48
namespace Net {
42
49
 
43
 
 
44
 
X509Certificate::X509Certificate(X509* pCert):_pCert(pCert)
 
50
X509Certificate::X509Certificate(std::istream& str):
 
51
        _issuerName(),
 
52
        _subjectName(),
 
53
        _pCert(0),
 
54
        _file()
 
55
{
 
56
        Poco::TemporaryFile certFile;
 
57
        
 
58
        if (!certFile.createFile())
 
59
                throw Poco::FileException("No temporary file could be created for X509Certificate!");
 
60
        _file = certFile.path();
 
61
        Poco::FileOutputStream fout(_file);
 
62
        Poco::StreamCopier::copyStream(str, fout);
 
63
        fout.close();
 
64
        
 
65
        BIO *fp=BIO_new(BIO_s_file());
 
66
        const char* pFN = _file.c_str();
 
67
        BIO_read_filename(fp, (void*)pFN);
 
68
        if (!fp)
 
69
                throw Poco::PathNotFoundException("Failed to open temporary file for X509Certificate");
 
70
        try
 
71
        {
 
72
                _pCert = PEM_read_bio_X509(fp,0,0,0);
 
73
        }
 
74
        catch(...)
 
75
        {
 
76
                BIO_free(fp);
 
77
                throw;
 
78
        }
 
79
        if (!_pCert)
 
80
                throw SSLException("Faild to load certificate from " + _file);
 
81
        initialize();
 
82
}
 
83
 
 
84
X509Certificate::X509Certificate(const std::string& file):
 
85
        _issuerName(),
 
86
        _subjectName(),
 
87
        _pCert(0),
 
88
        _file(file)
 
89
{
 
90
        BIO *fp=BIO_new(BIO_s_file());
 
91
        const char* pFN = file.c_str();
 
92
        BIO_read_filename(fp, (void*)pFN);
 
93
        if (!fp)
 
94
                throw Poco::PathNotFoundException("Failed to open " + file);
 
95
        try
 
96
        {
 
97
                _pCert = PEM_read_bio_X509(fp,0,0,0);
 
98
        }
 
99
        catch(...)
 
100
        {
 
101
                BIO_free(fp);
 
102
                throw;
 
103
        }
 
104
        if (!_pCert)
 
105
                throw SSLException("Faild to load certificate from " + file);
 
106
        initialize();
 
107
}
 
108
 
 
109
 
 
110
X509Certificate::X509Certificate(X509* pCert):
 
111
        _issuerName(),
 
112
        _subjectName(),
 
113
        _pCert(pCert),
 
114
        _file()
45
115
{
46
116
        poco_check_ptr(_pCert);
47
117
        initialize();
48
118
}
49
119
 
50
120
 
 
121
X509Certificate::X509Certificate(const X509Certificate& cert):
 
122
        _issuerName(cert._issuerName),
 
123
        _subjectName(cert._subjectName),
 
124
        _pCert(cert._pCert),
 
125
        _file(cert._file)
 
126
{
 
127
        if (!_file.empty())
 
128
                _pCert = X509_dup(_pCert);
 
129
}
 
130
 
 
131
 
 
132
X509Certificate& X509Certificate::operator=(const X509Certificate& cert)
 
133
{
 
134
        if (this != &cert)
 
135
        {
 
136
                X509Certificate c(cert);
 
137
                swap(c);
 
138
        }
 
139
        return *this;
 
140
}
 
141
 
 
142
 
 
143
void X509Certificate::swap(X509Certificate& cert)
 
144
{
 
145
        using std::swap;
 
146
        swap(cert._file, _file);
 
147
        swap(cert._issuerName, _issuerName);
 
148
        swap(cert._subjectName, _subjectName);
 
149
        swap(cert._pCert, _pCert);
 
150
}
 
151
 
 
152
 
51
153
X509Certificate::~X509Certificate()
52
154
{
 
155
        if (!_file.empty() && _pCert)
 
156
                X509_free(_pCert);
53
157
}
54
158
 
55
159
 
63
167
}
64
168
 
65
169
 
 
170
bool X509Certificate::verify(const std::string& hostName, Poco::SharedPtr<Context> ptr)
 
171
{
 
172
        X509* pCert = X509_dup(_pCert);
 
173
        return (X509_V_OK == SecureSocketImpl::postConnectionCheck(ptr, pCert, hostName));
 
174
}
 
175
 
 
176
 
66
177
} } // namespace Poco::Net