1 | /* |
---|---|
2 | * IXSocketTLSOptions.h |
3 | * Author: Matt DeBoer |
4 | * Copyright (c) 2019 Machine Zone, Inc. All rights reserved. |
5 | */ |
6 | |
7 | #include "IXSocketTLSOptions.h" |
8 | |
9 | #include <assert.h> |
10 | #include <fstream> |
11 | #include <sstream> |
12 | |
13 | namespace ix |
14 | { |
15 | const char* kTLSCAFileUseSystemDefaults = "SYSTEM"; |
16 | const char* kTLSCAFileDisableVerify = "NONE"; |
17 | const char* kTLSCiphersUseDefault = "DEFAULT"; |
18 | const char* kTLSInMemoryMarker = "-----BEGIN CERTIFICATE-----"; |
19 | |
20 | bool SocketTLSOptions::isValid() const |
21 | { |
22 | if (!_validated) |
23 | { |
24 | if (!certFile.empty() && !std::ifstream(certFile)) |
25 | { |
26 | _errMsg = "certFile not found: "+ certFile; |
27 | return false; |
28 | } |
29 | if (!keyFile.empty() && !std::ifstream(keyFile)) |
30 | { |
31 | _errMsg = "keyFile not found: "+ keyFile; |
32 | return false; |
33 | } |
34 | if (!caFile.empty() && caFile != kTLSCAFileDisableVerify && |
35 | caFile != kTLSCAFileUseSystemDefaults && !std::ifstream(caFile)) |
36 | { |
37 | _errMsg = "caFile not found: "+ caFile; |
38 | return false; |
39 | } |
40 | |
41 | if (certFile.empty() != keyFile.empty()) |
42 | { |
43 | _errMsg = "certFile and keyFile must be both present, or both absent"; |
44 | return false; |
45 | } |
46 | |
47 | _validated = true; |
48 | } |
49 | return true; |
50 | } |
51 | |
52 | bool SocketTLSOptions::hasCertAndKey() const |
53 | { |
54 | return !certFile.empty() && !keyFile.empty(); |
55 | } |
56 | |
57 | bool SocketTLSOptions::isUsingSystemDefaults() const |
58 | { |
59 | return caFile == kTLSCAFileUseSystemDefaults; |
60 | } |
61 | |
62 | bool SocketTLSOptions::isUsingInMemoryCAs() const |
63 | { |
64 | return caFile.find(kTLSInMemoryMarker) != std::string::npos; |
65 | } |
66 | |
67 | bool SocketTLSOptions::isPeerVerifyDisabled() const |
68 | { |
69 | return caFile == kTLSCAFileDisableVerify; |
70 | } |
71 | |
72 | bool SocketTLSOptions::isUsingDefaultCiphers() const |
73 | { |
74 | return ciphers.empty() || ciphers == kTLSCiphersUseDefault; |
75 | } |
76 | |
77 | const std::string& SocketTLSOptions::getErrorMsg() const |
78 | { |
79 | return _errMsg; |
80 | } |
81 | |
82 | std::string SocketTLSOptions::getDescription() const |
83 | { |
84 | std::stringstream ss; |
85 | ss << "TLS Options:"<< std::endl; |
86 | ss << " certFile = "<< certFile << std::endl; |
87 | ss << " keyFile = "<< keyFile << std::endl; |
88 | ss << " caFile = "<< caFile << std::endl; |
89 | ss << " ciphers = "<< ciphers << std::endl; |
90 | ss << " tls = "<< tls << std::endl; |
91 | return ss.str(); |
92 | } |
93 | } // namespace ix |
94 |