net/http/transport_security_state.cpp
src/tpkp_common.cpp
src/tpkp_parser.cpp
+ src/tpkp_client_cache.cpp
)
ADD_LIBRARY(${TARGET_TPKP_COMMON_LIB} SHARED ${TPKP_COMMON_SRCS})
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * @file tpkp_client_cache.h
+ * @author Kyungwook Tak (k.tak@samsung.com)
+ * @version 1.0
+ * @brief Tizen Https Public Key Pinning client cache declaration.
+ */
+#pragma once
+
+#include <sys/types.h>
+#include <string>
+#include <map>
+#include <mutex>
+
+#define EXPORT_API __attribute__((visibility("default")))
+
+namespace TPKP {
+
+class EXPORT_API ClientCache {
+public:
+ ClientCache();
+ virtual ~ClientCache();
+
+ /* thread-specific url mapped */
+ void setUrl(const std::string &url);
+ std::string getUrl(void);
+ void eraseUrl(void);
+ void eraseUrlAll(void);
+
+private:
+ std::map<pid_t, std::string> m_urls;
+ std::mutex m_url_mutex;
+};
+
+}
std::unique_ptr<Impl> pImpl;
};
-EXPORT_API
-pid_t getThreadId(void);
-
}
#define TPKP_THROW_EXCEPTION(code, message) \
--- /dev/null
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+/*
+ * @file tpkp_client_cache.cpp
+ * @author Kyungwook Tak (k.tak@samsung.com)
+ * @version 1.0
+ * @brief Https Public Key Pinning client cache implementation.
+ */
+#include "tpkp_client_cache.h"
+
+#include <string>
+#include <map>
+#include <sys/syscall.h>
+#include <unistd.h>
+
+#include "tpkp_logger.h"
+
+namespace {
+
+pid_t _getThreadId()
+{
+ return syscall(SYS_gettid);
+}
+
+}
+
+namespace TPKP {
+
+ClientCache::ClientCache() {}
+
+ClientCache::~ClientCache() {}
+
+void ClientCache::setUrl(const std::string &url)
+{
+ auto tid = _getThreadId();
+ {
+ std::lock_guard<std::mutex> lock(m_url_mutex);
+ m_urls[tid] = url;
+ }
+
+ SLOGD("set url[%s] of thread id[%u]", url.c_str(), tid);
+}
+
+std::string ClientCache::getUrl(void)
+{
+ std::string url;
+
+ auto tid = _getThreadId();
+ {
+ std::lock_guard<std::mutex> lock(m_url_mutex);
+ url = m_urls[tid];
+ }
+
+ SLOGD("get url[%s] from thread id[%u]", url.c_str(), tid);
+
+ return url;
+}
+
+void ClientCache::eraseUrl(void)
+{
+ auto tid = _getThreadId();
+ {
+ std::lock_guard<std::mutex> lock(m_url_mutex);
+ m_urls.erase(tid);
+ }
+
+ SLOGD("erase url of mapped by thread id[%u]", tid);
+}
+
+void ClientCache::eraseUrlAll(void)
+{
+ m_urls.clear();
+
+ SLOGD("erase all urls saved of client");
+}
+
+}
*/
#include "tpkp_common.h"
-#include <sys/syscall.h>
-#include <unistd.h>
-
#include <cstring>
#include <cctype>
#include <new>
namespace TPKP {
-pid_t getThreadId()
-{
- return syscall(SYS_gettid);
-}
-
Exception::Exception(tpkp_e code, const std::string &message)
: m_code(code)
, m_message(message)
* @version 1.0
* @brief Tizen Https Public Key Pinning implementation for libcurl.
*/
+#include "tpkp_curl.h"
+
#include <string>
#include <memory>
#include <map>
#include <curl/curl.h>
#include "tpkp_common.h"
-#include "tpkp_curl.h"
+#include "tpkp_client_cache.h"
namespace {
-std::map<pid_t, std::string> s_urlmap;
-std::mutex s_mutex;
+TPKP::ClientCache g_cache;
inline CURLcode err_tpkp_to_curle(tpkp_e err) noexcept
{
TPKP_CHECK_THROW_EXCEPTION(preverify_ok != 0,
TPKP_E_INTERNAL, "verify callback already failed before enter tpkp_curl callback");
- auto tid = TPKP::getThreadId();
- std::string url;
-
- {
- std::lock_guard<std::mutex> lock(s_mutex);
- url = s_urlmap[tid];
- }
+ std::string url = g_cache.getUrl();
TPKP_CHECK_THROW_EXCEPTION(!url.empty(),
- TPKP_E_NO_URL_DATA, "No url for thread id[" << tid << "] in map");
-
- SLOGD("get url[%s] of thread id[%u]", url.c_str(), tid);
+ TPKP_E_NO_URL_DATA, "No url in client cache!!");
TPKP::Context ctx(url);
if (!ctx.hasPins()) {
char *url = nullptr;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
- auto tid = TPKP::getThreadId();
-
- {
- std::lock_guard<std::mutex> lock(s_mutex);
- s_urlmap[tid] = url;
- }
-
- SLOGD("set url[%s] of thread id[%u]", url, tid);
+ g_cache.setUrl(url);
});
}
void tpkp_curl_cleanup(void)
{
tpkp_e res = TPKP::ExceptionSafe([&]{
- auto tid = TPKP::getThreadId();
-
- {
- std::lock_guard<std::mutex> lock(s_mutex);
- s_urlmap.erase(tid);
- }
-
- SLOGD("cleanup url data for thread id[%u]", tid);
+ g_cache.eraseUrl();
});
(void) res;
EXPORT_API
void tpkp_curl_cleanup_all(void)
{
- s_urlmap.clear();
+ g_cache.eraseUrlAll();
}
* @version 1.0
* @brief Tizen Https Public Key Pinning implementation for gnutls.
*/
+#include "tpkp_gnutls.h"
+
#include <string>
#include <memory>
#include <map>
#include <gnutls/x509.h>
#include "tpkp_common.h"
-#include "tpkp_gnutls.h"
+#include "tpkp_client_cache.h"
namespace {
-std::map<pid_t, std::string> s_urlmap;
-std::mutex s_mutex;
+TPKP::ClientCache g_cache;
inline int err_tpkp_to_gnutlse(tpkp_e err) noexcept
{
TPKP_E_CERT_VERIFICATION_FAILED,
"Peer certificate verification failed!! status: " << status);
- auto tid = TPKP::getThreadId();
- std::string url;
-
- {
- std::lock_guard<std::mutex> lock(s_mutex);
- url = s_urlmap[tid];
- }
+ std::string url = g_cache.getUrl();
TPKP_CHECK_THROW_EXCEPTION(
!url.empty(),
TPKP_E_NO_URL_DATA,
- "No url of thread id[" << tid << "]");
-
- SLOGD("get url[%s] of thread id[%u]", url.c_str(), tid);
+ "No url of found in client cache!!");
TPKP::Context ctx(url);
if (!ctx.hasPins()) {
tpkp_e tpkp_gnutls_set_url_data(const char *url)
{
return TPKP::ExceptionSafe([&]{
- pid_t tid = TPKP::getThreadId();
-
- {
- std::lock_guard<std::mutex> lock(s_mutex);
- s_urlmap[tid] = url;
- }
-
- SLOGD("set url[%s] of thread id[%u]", url, tid);
+ g_cache.setUrl(url);
});
}
void tpkp_gnutls_cleanup(void)
{
tpkp_e res = TPKP::ExceptionSafe([&]{
- auto tid = TPKP::getThreadId();
-
- {
- std::lock_guard<std::mutex> lock(s_mutex);
- s_urlmap.erase(tid);
- }
-
- SLOGD("cleanup url data from thread id[%u]", tid);
+ g_cache.eraseUrl();
});
(void) res;
EXPORT_API
void tpkp_gnutls_cleanup_all(void)
{
- s_urlmap.clear();
+ g_cache.eraseUrlAll();
}