Context.h
1 /*
2  * Copyright (C) 2010-2010 by Aloysius Indrayanto
3  * Copyright (C) 2010-2013 by Marc Duerner
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * As a special exception, you may use this file as part of a free
11  * software library without restriction. Specifically, if other files
12  * instantiate templates or use macros or inline functions from this
13  * file, or you compile this file and link it with other files to
14  * produce an executable, this file does not by itself cause the
15  * resulting executable to be covered by the GNU General Public
16  * License. This exception does not however invalidate any other
17  * reasons why the executable file might be covered by the GNU Library
18  * General Public License.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Lesser General Public License for more details.
24  *
25  * You should have received a copy of the GNU Lesser General Public
26  * License along with this library; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28  */
29 
30 #ifndef PT_SSL_CONTEXT_H
31 #define PT_SSL_CONTEXT_H
32 
33 #include <Pt/Ssl/Api.h>
34 #include <Pt/NonCopyable.h>
35 #include <string>
36 
37 namespace Pt {
38 
39 namespace Ssl {
40 
41 class Certificate;
42 class ContextImpl;
43 
45 static struct PT_SSL_API SSLInit
46 {
47  SSLInit();
48  ~SSLInit();
49 } ssl_init;
50 
53 {
54  Connect = 1,
55  Accept = 2
56 };
57 
59 enum Protocol
60 {
61  SSLv2 = 0,
62  SSLv3or2 = 1,
63  SSLv3 = 2,
65  TLSv1 = 3,
66  TLSv1_1 = 4,
67  TLSv1_2 = 5
68 };
69 
72 {
73  NoVerify = 0,
74  TryVerify = 1,
75  AlwaysVerify = 2
76 };
77 
79 class PT_SSL_API Context : public NonCopyable
80 {
81  public:
84 
86  Context(Protocol protocol);
87 
90 
92  void assign(const Context& ctx);
93 
95  Protocol protocol() const;
96 
98  void setProtocol(Protocol protocol);
99 
101  void setVerifyDepth(int n);
102 
105 
108 
115  void addCACertificate(const Certificate& trustedCert);
116 
122  void setIdentity(const Certificate& cert);
123 
129  void addCertificate(const Certificate& cert);
130 
132  ContextImpl* impl();
133 
135  const ContextImpl* impl() const;
136 
137  private:
138  ContextImpl* _impl;
139 };
140 
141 } // namespace Ssl
142 
143 } // namespace Pt
144 
145 #endif // PT_SSL_CONTEXT_H
@ TLSv1
TLS version 1.0.
Definition: Context.h:65
Core module.
Definition: Allocator.h:33
void assign(const Context &ctx)
Assigns the certificates, verify mode and protocol.
@ TLSv1_2
TLS version 1.2.
Definition: Context.h:67
Protocol protocol() const
Returns the current protocol.
@ NoVerify
No verification.
Definition: Context.h:73
@ SSLv3or2
highest possible protocol version
Definition: Context.h:62
OpenMode
Open mode for ssl I/O.
Definition: Context.h:53
@ TryVerify
Verify if certificate is presented.
Definition: Context.h:74
@ Accept
Accept client.
Definition: Context.h:55
@ SSLv3
SSL version 3.
Definition: Context.h:63
Protocol
Communication protocol.
Definition: Context.h:60
void setProtocol(Protocol protocol)
Sets the current protocol.
@ TLSv1_1
TLS version 1.1.
Definition: Context.h:66
void setVerifyMode(VerifyMode mode)
Sets the current validation mode.
@ AlwaysVerify
Require tp present certificate.
Definition: Context.h:75
VerifyMode
Verification mode.
Definition: Context.h:72
void setIdentity(const Certificate &cert)
Set the main certificate of this context.
@ SSLv2
SSL version 2.
Definition: Context.h:61
void addCACertificate(const Certificate &trustedCert)
Add a certificate to the trusted CA certificates.
@ TLS
highest possible TLS protocol version
Definition: Context.h:64
X509 certificate.
Definition: Certificate.h:45
void addCertificate(const Certificate &cert)
Builds certificate chain.
~Context()
Destructor.
void setVerifyDepth(int n)
Limits the number of certificates checked in the peer's certificate chain.
VerifyMode verifyMode() const
Returns the current verify mode.
Context(Protocol protocol)
Construct with specific protocol.
Protects derived classes from being copied.
Definition: NonCopyable.h:54
Context()
Construct with defaults.
@ Connect
Connect to server.
Definition: Context.h:54
Context for SSL connections.
Definition: Context.h:80