ucommon  7.0.0
About: GNU uCommon C++ is a portable and optimized class framework for writing C++ applications that need to use threads and support concurrent synchronization, and that use sockets, XML parsing, object serialization, thread-optimized string and data structure classes, etc..
  Fossies Dox: ucommon-7.0.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

Loading...
Searching...
No Matches
secure.cpp
Go to the documentation of this file.
1// Copyright (C) 2010-2014 David Sugar, Tycho Softworks.
2// Copyright (C) 2015 Cherokees of Idaho.
3//
4// This file is part of GNU uCommon C++.
5//
6// GNU uCommon C++ is free software: you can redistribute it and/or modify
7// it under the terms of the GNU Lesser General Public License as published
8// by the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10//
11// GNU uCommon C++ 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
14// GNU Lesser General Public License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18
19#include "local.h"
20
21extern "C" {
22 static void secure_shutdown(void)
23 {
24 gnutls_global_deinit();
25 }
26}
27
28namespace ucommon {
29
30gnutls_priority_t __context::priority_cache;
31
32bool secure::fips(void)
33{
34 return false;
35}
36
37bool secure::init(void)
38{
39 static bool initialized = false;
40
41 if(!initialized) {
44
45 gnutls_global_init();
46 gnutls_priority_init (&__context::priority_cache, "NORMAL", NULL);
47 atexit(secure_shutdown);
48 initialized = true;
49 }
50 return true;
51}
52
53secure::server_t secure::server(const char *certfile, const char *ca)
54{
55 __context *ctx = new __context;
56
57 if(!ctx)
58 return NULL;
59
60 ctx->error = secure::OK;
61 ctx->connect = GNUTLS_SERVER;
62 ctx->xtype = GNUTLS_CRD_CERTIFICATE;
63 ctx->xcred = NULL;
64 ctx->dh = NULL;
65 gnutls_certificate_allocate_credentials(&ctx->xcred);
66
67 gnutls_certificate_set_x509_key_file(ctx->xcred, certfile, certfile, GNUTLS_X509_FMT_PEM);
68
69 if(!ca)
70 ca = oscerts();
71
72 gnutls_certificate_set_x509_trust_file (ctx->xcred, ca, GNUTLS_X509_FMT_PEM);
73
74 return ctx;
75}
76
77secure::client_t secure::client(const char *ca, const char *paths)
78{
79 __context *ctx = new __context;
80
81 if(!ctx)
82 return NULL;
83
84 ctx->error = secure::OK;
85 ctx->connect = GNUTLS_CLIENT;
86 ctx->xtype = GNUTLS_CRD_CERTIFICATE;
87 ctx->xcred = NULL;
88 ctx->dh = NULL;
89 gnutls_certificate_allocate_credentials(&ctx->xcred);
90
91 if(!ca && !paths)
92 return ctx;
93
94 if(!ca && paths)
95 ca = paths;
96 else if(!ca)
97 ca = oscerts();
98
99 gnutls_certificate_set_x509_trust_file (ctx->xcred, ca, GNUTLS_X509_FMT_PEM);
100
101 return ctx;
102}
103
105{
106 if(dh)
107 gnutls_dh_params_deinit(dh);
108
109 if(!xcred)
110 return;
111
112 switch(xtype) {
113 case GNUTLS_CRD_ANON:
114 gnutls_anon_free_client_credentials((gnutls_anon_client_credentials_t)xcred);
115 break;
116 case GNUTLS_CRD_CERTIFICATE:
117 gnutls_certificate_free_credentials(xcred);
118 break;
119 default:
120 break;
121 }
122}
123
125{
126}
127
128gnutls_session_t __context::session(__context *ctx)
129{
130 SSL ssl = NULL;
131 if(ctx && ctx->xcred && ctx->err() == secure::OK) {
132 gnutls_init(&ssl, (gnutls_connection_end_t)(ctx->connect));
133 switch(ctx->connect) {
134 case GNUTLS_CLIENT:
135 gnutls_priority_set_direct(ssl, "PERFORMANCE", NULL);
136 break;
137 case GNUTLS_SERVER:
138 gnutls_priority_set(ssl, __context::priority_cache);
139 gnutls_certificate_server_set_request(ssl, GNUTLS_CERT_REQUEST);
140 gnutls_session_enable_compatibility_mode(ssl);
141 default:
142 break;
143 }
144 gnutls_credentials_set(ssl, ctx->xtype, ctx->xcred);
145 }
146 return ssl;
147}
148
149} // namespace ucommon
static void init(void)
Initialize socket subsystem.
Definition: socket.cpp:531
static void init(void)
Used to initialize threading library.
Definition: thread.cpp:1274
static gnutls_priority_t priority_cache
Definition: local.h:42
unsigned int connect
Definition: local.h:37
gnutls_credentials_type_t xtype
Definition: local.h:38
gnutls_certificate_credentials_t xcred
Definition: local.h:39
SSL_CTX * ctx
Definition: local.h:42
static gnutls_session_t session(__context *ctx)
Definition: secure.cpp:128
gnutls_dh_params_t dh
Definition: local.h:40
Common secure socket support.
Definition: secure.h:129
error_t error
Last error flagged for this context.
Definition: secure.h:149
static server_t server(const char *keyfile=NULL, const char *authority=NULL)
Create a sever context.
Definition: secure.cpp:53
static bool init(void)
Initialize secure stack for first use, and report if SSL support is compiled in.
Definition: secure.cpp:37
static bool fips(void)
Initialize secure stack with fips support.
Definition: secure.cpp:32
static const char * oscerts(void)
Get path to system certificates.
Definition: common.cpp:301
static client_t client(const char *authority=NULL, const char *paths=NULL)
Create an anonymous client context with an optional authority to validate.
Definition: secure.cpp:77
error_t err(void) const
Get last error code associated with the security context.
Definition: secure.h:257
virtual ~secure()
This is derived in different back-end libraries, and will be used to clear certificate credentials.
Definition: secure.cpp:124
static void secure_shutdown(void)
Definition: secure.cpp:22
Common namespace for all ucommon objects.
Definition: access.cpp:23
gnutls_session_t SSL
Definition: local.h:51