-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaiosecurecontext.hpp
102 lines (77 loc) · 2.83 KB
/
aiosecurecontext.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// solid/frame/aio/openssl/aiosecurecontext.hpp
//
// Copyright (c) 2015 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#pragma once
#include "openssl/ssl.h"
#include "solid/system/error.hpp"
#include "solid/utility/function.hpp"
namespace solid {
namespace frame {
namespace aio {
namespace openssl {
enum struct PasswordPurpose {
Read,
Write
};
enum struct FileFormat {
Asn1,
Pem
};
class Socket;
class Context {
Context();
public:
using NativeContextT = SSL_CTX*;
static Context create(const SSL_METHOD* = nullptr);
Context(Context const&) = delete;
Context(Context&& _rctx) noexcept;
Context& operator=(Context const&) = delete;
Context& operator=(Context&& _rctx) noexcept;
~Context();
bool isValid() const;
bool empty() const;
// ErrorCodeT configure(const char *_filename = nullptr, const char *_appname = nullptr);
ErrorCodeT addVerifyAuthority(const unsigned char* _data, const size_t _data_size);
ErrorCodeT addVerifyAuthority(const std::string& _str);
ErrorCodeT loadDefaultVerifyPaths();
//! Use it on client side to load the certificates
ErrorCodeT loadVerifyFile(const char* _path);
//! Use it on client side to load the certificates
ErrorCodeT loadVerifyPath(const char* _path);
//! Use it on client/server side to load the certificates
ErrorCodeT loadCertificateFile(const char* _path, const FileFormat _fformat = FileFormat::Pem);
ErrorCodeT loadCertificate(const unsigned char* _data, const size_t _data_size, const FileFormat _fformat = FileFormat::Pem);
ErrorCodeT loadCertificate(const std::string& _str, const FileFormat _fformat = FileFormat::Pem);
//! Use it on client/server side to load the certificates
ErrorCodeT loadPrivateKeyFile(const char* _path, const FileFormat _fformat = FileFormat::Pem);
ErrorCodeT loadPrivateKey(const unsigned char* _data, const size_t _data_size, const FileFormat _fformat = FileFormat::Pem);
ErrorCodeT loadPrivateKey(const std::string& _str, const FileFormat _fformat = FileFormat::Pem);
template <typename F>
ErrorCodeT passwordCallback(F _f)
{
pwdfnc = _f;
return doSetPasswordCallback();
}
NativeContextT nativeContext() const
{
return pctx;
}
private:
static int on_password_cb(char* buf, int size, int rwflag, void* u);
ErrorCodeT doSetPasswordCallback();
private:
using PasswordFunctionT = solid_function_t(std::string(std::size_t, PasswordPurpose));
friend class Socket;
SSL_CTX* pctx;
PasswordFunctionT pwdfnc;
};
} // namespace openssl
} // namespace aio
} // namespace frame
} // namespace solid