diff options
-rw-r--r-- | Sluift/Lua/Check.cpp | 12 | ||||
-rw-r--r-- | Sluift/Lua/Check.h | 5 | ||||
-rw-r--r-- | Sluift/Lua/LuaUtils.cpp | 8 | ||||
-rw-r--r-- | Sluift/Lua/LuaUtils.h | 2 | ||||
-rw-r--r-- | Sluift/SluiftGlobals.h | 2 | ||||
-rw-r--r-- | Sluift/core.lua | 10 | ||||
-rw-r--r-- | Sluift/sluift.cpp | 44 | ||||
-rw-r--r-- | Swift/Controllers/Storages/CertificateFileStorage.cpp | 2 | ||||
-rw-r--r-- | Swiften/QA/TLSTest/CertificateTest.cpp | 14 | ||||
-rw-r--r-- | Swiften/TLS/CertificateFactory.h | 2 | ||||
-rw-r--r-- | Swiften/TLS/OpenSSL/OpenSSLCertificateFactory.h | 6 | ||||
-rw-r--r-- | Swiften/TLS/Schannel/SchannelCertificateFactory.h | 4 |
12 files changed, 92 insertions, 19 deletions
diff --git a/Sluift/Lua/Check.cpp b/Sluift/Lua/Check.cpp index 65ada7b..a9b8f02 100644 --- a/Sluift/Lua/Check.cpp +++ b/Sluift/Lua/Check.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Remko Tronçon + * Copyright (c) 2013-2014 Remko Tronçon * Licensed under the GNU General Public License. * See the COPYING file for more information. */ @@ -12,6 +12,7 @@ #include <lua.hpp> #include <Sluift/Lua/Exception.h> +#include <Swiften/Base/ByteArray.h> using namespace Swift; @@ -43,6 +44,15 @@ std::string Lua::checkString(lua_State* L, int arg) { return std::string(s); } +ByteArray Lua::checkByteArray(lua_State* L, int arg) { + size_t len; + const char *s = lua_tolstring(L, arg, &len); + if (!s) { + throw Lua::Exception(getArgTypeError(L, arg, LUA_TSTRING)); + } + return createByteArray(s, len); +} + void* Lua::checkUserDataRaw(lua_State* L, int arg) { void* userData = lua_touserdata(L, arg); if (!userData) { diff --git a/Sluift/Lua/Check.h b/Sluift/Lua/Check.h index 8a8b64a..c22751b 100644 --- a/Sluift/Lua/Check.h +++ b/Sluift/Lua/Check.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2013 Remko Tronçon + * Copyright (c) 2013-2014 Remko Tronçon * Licensed under the GNU General Public License. * See the COPYING file for more information. */ @@ -8,6 +8,8 @@ #include <string> +#include <Swiften/Base/ByteArray.h> + struct lua_State; namespace Swift { @@ -15,6 +17,7 @@ namespace Swift { void checkType(lua_State* L, int arg, int type); int checkIntNumber(lua_State* L, int arg); std::string checkString(lua_State* L, int arg); + ByteArray checkByteArray(lua_State* L, int arg); void* checkUserDataRaw(lua_State* L, int arg); diff --git a/Sluift/Lua/LuaUtils.cpp b/Sluift/Lua/LuaUtils.cpp index 915f3cc..dbadaab 100644 --- a/Sluift/Lua/LuaUtils.cpp +++ b/Sluift/Lua/LuaUtils.cpp @@ -189,3 +189,11 @@ void Swift::Lua::registerExtraHelp(lua_State* L, int index, const std::string& n } lua_pop(L, 3); } + +void Swift::Lua::pushStringArray(lua_State* L, const std::vector<std::string>& strings) { + lua_createtable(L, strings.size(), 0); + for (size_t i = 0; i < strings.size(); ++i) { + lua_pushstring(L, strings[i].c_str()); + lua_rawseti(L, -2, boost::numeric_cast<int>(i+1)); + } +} diff --git a/Sluift/Lua/LuaUtils.h b/Sluift/Lua/LuaUtils.h index 105f249..ed4fcc1 100644 --- a/Sluift/Lua/LuaUtils.h +++ b/Sluift/Lua/LuaUtils.h @@ -37,5 +37,7 @@ namespace Swift { boost::optional<std::string> getStringField(lua_State* L, int index, const std::string&); boost::optional<bool> getBooleanField(lua_State* L, int index, const std::string&); boost::optional<int> getIntField(lua_State* L, int index, const std::string&); + + void pushStringArray(lua_State* L, const std::vector<std::string>& strings); } } diff --git a/Sluift/SluiftGlobals.h b/Sluift/SluiftGlobals.h index e89f495..9d9cc51 100644 --- a/Sluift/SluiftGlobals.h +++ b/Sluift/SluiftGlobals.h @@ -9,6 +9,7 @@ #include <Sluift/LuaElementConvertors.h> #include <Swiften/EventLoop/SimpleEventLoop.h> #include <Swiften/Network/BoostNetworkFactories.h> +#include <Swiften/TLS/PlatformTLSFactories.h> #ifdef HAVE_ITUNES #include <Sluift/ITunesInterface.h> #endif @@ -25,6 +26,7 @@ namespace Swift { LuaElementConvertors elementConvertor; SimpleEventLoop eventLoop; BoostNetworkFactories networkFactories; + PlatformTLSFactories tlsFactories; int coreLibIndex; int moduleLibIndex; sig_atomic_t interruptRequested; diff --git a/Sluift/core.lua b/Sluift/core.lua index 9e81575..48b8a97 100644 --- a/Sluift/core.lua +++ b/Sluift/core.lua @@ -6,7 +6,7 @@ local sluift = select(1, ...) local _G = _G -local pairs, ipairs, print, tostring, type, error, assert, next, rawset, xpcall, unpack = pairs, ipairs, print, tostring, type, error, assert, next, rawset, xpcall, unpack +local pairs, ipairs, print, tostring, type, error, assert, next, rawset, xpcall, unpack, io = pairs, ipairs, print, tostring, type, error, assert, next, rawset, xpcall, unpack, io local setmetatable, getmetatable = setmetatable, getmetatable local string = require "string" local table = require "table" @@ -435,6 +435,13 @@ local function call(options) end end +local function read_file(file) + local f = io.open(file, 'rb') + local result = f:read('*all') + f:close() + return result +end + -------------------------------------------------------------------------------- -- Metatables -------------------------------------------------------------------------------- @@ -1005,6 +1012,7 @@ return { register_get_by_type_index = register_get_by_type_index, process_pubsub_event = process_pubsub_event, tprint = tprint, + read_file = read_file, disco = disco, get_help = get_help, help = help, diff --git a/Sluift/sluift.cpp b/Sluift/sluift.cpp index 37cb4f2..858e634 100644 --- a/Sluift/sluift.cpp +++ b/Sluift/sluift.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011-2013 Remko Tronçon + * Copyright (c) 2011-2014 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ @@ -26,6 +26,8 @@ #include <Swiften/Parser/PayloadParsers/UnitTest/PayloadsParserTester.h> #include <Swiften/Serializer/PayloadSerializers/FullPayloadSerializerCollection.h> #include <Swiften/Serializer/PayloadSerializer.h> +#include <Swiften/TLS/Certificate.h> +#include <Swiften/TLS/CertificateFactory.h> #include <Sluift/LuaElementConvertor.h> #include <Sluift/Lua/Debug.h> #include <Swiften/StringCodecs/Base64.h> @@ -178,6 +180,42 @@ SLUIFT_LUA_FUNCTION_WITH_HELP( } /******************************************************************************* + * Crypto functions + ******************************************************************************/ + +SLUIFT_LUA_FUNCTION_WITH_HELP( + Crypto, new_certificate, + "Creates a new X.509 certificate from DER data.\n", + + "der the DER-encoded certificate data", + + "") { + ByteArray certData(Lua::checkByteArray(L, 1)); + Certificate::ref cert(Sluift::globals.tlsFactories.getCertificateFactory()->createCertificateFromDER(certData)); + lua_createtable(L, 0, 0); + lua_pushstring(L, cert->getSubjectName().c_str()); + lua_setfield(L, -2, "subject_name"); + lua_pushstring(L, Certificate::getSHA1Fingerprint(cert, Sluift::globals.networkFactories.getCryptoProvider()).c_str()); + lua_setfield(L, -2, "sha1_fingerprint"); + + Lua::pushStringArray(L, cert->getCommonNames()); + lua_setfield(L, -2, "common_names"); + + Lua::pushStringArray(L, cert->getSRVNames()); + lua_setfield(L, -2, "srv_names"); + + Lua::pushStringArray(L, cert->getDNSNames()); + lua_setfield(L, -2, "dns_names"); + + Lua::pushStringArray(L, cert->getXMPPAddresses()); + lua_setfield(L, -2, "xmpp_addresses"); + + Lua::registerTableToString(L, -1); + return 1; +} + + +/******************************************************************************* * JID Functions ******************************************************************************/ @@ -333,6 +371,8 @@ SLUIFT_API int luaopen_sluift(lua_State* L) { lua_setfield(L, -2, "base64"); Lua::FunctionRegistry::getInstance().createFunctionTable(L, "IDN"); lua_setfield(L, -2, "idn"); + Lua::FunctionRegistry::getInstance().createFunctionTable(L, "Crypto"); + lua_setfield(L, -2, "crypto"); #ifdef HAVE_ITUNES Lua::FunctionRegistry::getInstance().createFunctionTable(L, "iTunes"); lua_setfield(L, -2, "itunes"); @@ -341,7 +381,7 @@ SLUIFT_API int luaopen_sluift(lua_State* L) { // Register convenience functions lua_rawgeti(L, LUA_REGISTRYINDEX, Sluift::globals.coreLibIndex); std::vector<std::string> coreLibExports = boost::assign::list_of - ("tprint")("disco")("help")("get_help")("copy")("with"); + ("tprint")("disco")("help")("get_help")("copy")("with")("read_file"); foreach (const std::string& coreLibExport, coreLibExports) { lua_getfield(L, -1, coreLibExport.c_str()); lua_setfield(L, -3, coreLibExport.c_str()); diff --git a/Swift/Controllers/Storages/CertificateFileStorage.cpp b/Swift/Controllers/Storages/CertificateFileStorage.cpp index 34d1f76..2c242cd 100644 --- a/Swift/Controllers/Storages/CertificateFileStorage.cpp +++ b/Swift/Controllers/Storages/CertificateFileStorage.cpp @@ -25,7 +25,7 @@ bool CertificateFileStorage::hasCertificate(Certificate::ref certificate) const if (boost::filesystem::exists(certificatePath)) { ByteArray data; readByteArrayFromFile(data, certificatePath); - Certificate::ref storedCertificate = certificateFactory->createCertificateFromDER(data); + Certificate::ref storedCertificate(certificateFactory->createCertificateFromDER(data)); if (storedCertificate && storedCertificate->toDER() == certificate->toDER()) { return true; } diff --git a/Swiften/QA/TLSTest/CertificateTest.cpp b/Swiften/QA/TLSTest/CertificateTest.cpp index 2fa4c04..2089732 100644 --- a/Swiften/QA/TLSTest/CertificateTest.cpp +++ b/Swiften/QA/TLSTest/CertificateTest.cpp @@ -42,41 +42,41 @@ class CertificateTest : public CppUnit::TestFixture { } void testConstructFromDER() { - Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData); + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getCommonNames()[0]); } void testToDER() { - Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData); + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); CPPUNIT_ASSERT_EQUAL(certificateData, testling->toDER()); } /* void testGetSubjectName() { - Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData); + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData); CPPUNIT_ASSERT_EQUAL(std::string("/description=114072-VMk8pdi1aj5kTXxO/C=US/ST=Colorado/L=Denver/O=Peter Saint-Andre/OU=StartCom Trusted Certificate Member/CN=*.jabber.org/emailAddress=hostmaster@jabber.org"), testling->getSubjectName()); } */ void testGetCommonNames() { - Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData); + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getCommonNames().size())); CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getCommonNames()[0]); } void testGetSRVNames() { - Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData); + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getSRVNames().size())); CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getSRVNames()[0]); } void testGetDNSNames() { - Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData); + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); CPPUNIT_ASSERT_EQUAL(2, static_cast<int>(testling->getDNSNames().size())); CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getDNSNames()[0]); @@ -84,7 +84,7 @@ class CertificateTest : public CppUnit::TestFixture { } void testGetXMPPAddresses() { - Certificate::ref testling = certificateFactory->createCertificateFromDER(certificateData); + Certificate::ref testling = Certificate::ref(certificateFactory->createCertificateFromDER(certificateData)); CPPUNIT_ASSERT_EQUAL(1, static_cast<int>(testling->getXMPPAddresses().size())); CPPUNIT_ASSERT_EQUAL(std::string("*.jabber.org"), testling->getXMPPAddresses()[0]); diff --git a/Swiften/TLS/CertificateFactory.h b/Swiften/TLS/CertificateFactory.h index 3e94082..e8971c3 100644 --- a/Swiften/TLS/CertificateFactory.h +++ b/Swiften/TLS/CertificateFactory.h @@ -13,6 +13,6 @@ namespace Swift { public: virtual ~CertificateFactory(); - virtual Certificate::ref createCertificateFromDER(const ByteArray& der) = 0; + virtual Certificate* createCertificateFromDER(const ByteArray& der) = 0; }; } diff --git a/Swiften/TLS/OpenSSL/OpenSSLCertificateFactory.h b/Swiften/TLS/OpenSSL/OpenSSLCertificateFactory.h index 52f134c..98376ae 100644 --- a/Swiften/TLS/OpenSSL/OpenSSLCertificateFactory.h +++ b/Swiften/TLS/OpenSSL/OpenSSLCertificateFactory.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2010 Remko Tronçon + * Copyright (c) 2010-2014 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ @@ -12,8 +12,8 @@ namespace Swift { class OpenSSLCertificateFactory : public CertificateFactory { public: - virtual Certificate::ref createCertificateFromDER(const ByteArray& der) { - return Certificate::ref(new OpenSSLCertificate(der)); + virtual Certificate* createCertificateFromDER(const ByteArray& der) { + return new OpenSSLCertificate(der); } }; } diff --git a/Swiften/TLS/Schannel/SchannelCertificateFactory.h b/Swiften/TLS/Schannel/SchannelCertificateFactory.h index d09bb54..5a2b208 100644 --- a/Swiften/TLS/Schannel/SchannelCertificateFactory.h +++ b/Swiften/TLS/Schannel/SchannelCertificateFactory.h @@ -12,8 +12,8 @@ namespace Swift { class SchannelCertificateFactory : public CertificateFactory { public: - virtual Certificate::ref createCertificateFromDER(const ByteArray& der) { - return Certificate::ref(new SchannelCertificate(der)); + virtual Certificate* createCertificateFromDER(const ByteArray& der) { + return new SchannelCertificate(der); } }; } |