SocketTLSOptions: more methods (contributed by Matt DeBoer)

This commit is contained in:
Benjamin Sergeant
2019-09-29 17:35:18 -07:00
parent d1fc31b894
commit d1fb34694c
2 changed files with 65 additions and 10 deletions

View File

@ -1,35 +1,78 @@
/*
* IXSocketTLSOptions.h
* Author: Benjamin Sergeant
* Author: Matt DeBoer
* Copyright (c) 2017-2018 Machine Zone, Inc. All rights reserved.
*/
#include "IXSocketTLSOptions.h"
#include <assert.h>
#include <fstream>
namespace ix
{
SocketTLSOptions::SocketTLSOptions()
const char* kTLSCAFileUseSystemDefaults = "SYSTEM";
const char* kTLSCAFileDisableVerify = "NONE";
const char* kTLSCiphersUseDefault = "DEFAULT";
bool SocketTLSOptions::isValid() const
{
#ifndef IXWEBSOCKET_USE_TLS
assert(false && "To use TLS features the library must be compiled with USE_TLS");
_errMsg = "To use TLS features the library must be compiled with USE_TLS";
return false;
#endif
if (!_validated)
{
if (!certFile.empty() && !std::ifstream(certFile))
{
_errMsg = "certFile not found: " + certFile;
return false;
}
if (!keyFile.empty() && !std::ifstream(keyFile))
{
_errMsg = "keyFile not found: " + keyFile;
return false;
}
if (!caFile.empty() && caFile != kTLSCAFileDisableVerify &&
caFile != kTLSCAFileUseSystemDefaults && !std::ifstream(caFile))
{
_errMsg = "caFile not found: " + caFile;
return false;
}
if (certFile.empty() != keyFile.empty())
{
_errMsg = "certFile and keyFile must be both present, or both absent";
return false;
}
_validated = true;
}
return true;
}
bool SocketTLSOptions::isUsingClientCert() const
bool SocketTLSOptions::hasCertAndKey() const
{
return !certFile.empty() && !keyFile.empty();
}
bool SocketTLSOptions::isUsingSystemDefaults() const
{
return caFile == "SYSTEM";
return caFile == kTLSCAFileUseSystemDefaults;
}
bool SocketTLSOptions::isPeerVerifyDisabled() const
{
return caFile != "NONE";
return caFile == kTLSCAFileDisableVerify;
}
bool SocketTLSOptions::isUsingDefaultCiphers() const
{
return ciphers.empty() || ciphers == kTLSCiphersUseDefault;
}
const std::string& SocketTLSOptions::getErrorMsg() const
{
return _errMsg;
}
} // namespace ix