${CMAKE_CURRENT_SOURCE_DIR}/websockets.cpp
${CMAKE_CURRENT_SOURCE_DIR}/handshake.cpp
${CMAKE_CURRENT_SOURCE_DIR}/encrypted_tunnel.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/base64.cpp
)
SET(WEBAUTHN_BLE_SOURCES ${WEBAUTHN_BLE_SOURCES} PARENT_SCOPE)
--- /dev/null
+/*
+ * Copyright (c) 2024 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
+ */
+
+#include "base64.h"
+
+#include <cassert>
+#include <openssl/evp.h>
+
+std::string Base64UrlEncode(BufferView buff)
+{
+ size_t len = (buff.size() + 2) / 3 * 4;
+ std::string res(len, '\0');
+ auto outLen =
+ EVP_EncodeBlock(reinterpret_cast<uint8_t *>(res.data()), buff.data(), buff.size());
+ assert(len == static_cast<size_t>(outLen));
+ // Remove padding
+ res.resize((buff.size() * 4 + 2) / 3);
+ return res;
+}
--- /dev/null
+/*
+ * Copyright (c) 2024 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
+ */
+
+#pragma once
+
+#include "common.h"
+
+#include <string>
+
+std::string Base64UrlEncode(BufferView buff);
#include <array>
#include <cstddef>
+#include <string_view>
#include <utility> // for std::move
constexpr size_t QR_SECRET_LEN = 16;
private:
T m_cleanupFn;
};
+
+typedef std::basic_string_view<uint8_t> BufferView;
${CMAKE_CURRENT_SOURCE_DIR}/state_assisted_transaction_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/handshake_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/encrypted_tunnel_tests.cpp
+ ${CMAKE_CURRENT_SOURCE_DIR}/base64_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/crypto/hkdf_unittest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/crypto/hmac_unittest.cpp
${CMAKE_CURRENT_SOURCE_DIR}/crypto/sha2_unittest.cpp
--- /dev/null
+/*
+ * Copyright (c) 2024 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
+ */
+
+#include "base64.h"
+
+#include <gtest/gtest.h>
+
+TEST(base64, Base64UrlEncode)
+{
+ unsigned char data[] = "\x00\x01\x02\x03\x04\x05";
+ EXPECT_EQ(Base64UrlEncode({data, 0}), "");
+ EXPECT_EQ(Base64UrlEncode({data, 1}), "AA");
+ EXPECT_EQ(Base64UrlEncode({data, 2}), "AAE");
+ EXPECT_EQ(Base64UrlEncode({data, 3}), "AAEC");
+ EXPECT_EQ(Base64UrlEncode({data, 4}), "AAECAw");
+ EXPECT_EQ(Base64UrlEncode({data, 5}), "AAECAwQ");
+ EXPECT_EQ(Base64UrlEncode({data, 6}), "AAECAwQF");
+
+ unsigned char longData[256];
+ for (size_t i = 0; i < sizeof(longData); ++i)
+ longData[i] = i;
+
+ EXPECT_EQ(
+ Base64UrlEncode({longData, sizeof(longData)}),
+ "AAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+"
+ "P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+"
+ "AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/"
+ "wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/w");
+}