Make cbor_encoding.cpp use cbor wrapper
authorDaniel Kita <d.kita@samsung.com>
Thu, 28 Mar 2024 11:25:05 +0000 (12:25 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 12 Apr 2024 10:05:05 +0000 (12:05 +0200)
Change-Id: I79c1e4b8f3a74fcecd7984fe83f5e2c3408f880f

srcs/cbor_encoding.cpp
srcs/cbor_encoding.h

index 1e1e26619af469cfd8357235954cbe8f2f5ef320..0dbe3a7a79ed4ce249959aa7a67b32bb0aeb395b 100644 (file)
@@ -84,8 +84,6 @@ void Cbor::EncodeQRContents(const CryptoBuffer &publicKey,
 
     size_t buffSize = 73; // epoch is int64 which may take up to 8B (9B with key) in CBOR
     size_t mapElements = 6;
-    CborEncoder encoder, mapEncoder;
-    CborError err = CborNoError;
     CryptoBuffer buffer(buffSize, 0);
 
     assert(publicKey.size() <= 33);
@@ -104,100 +102,29 @@ void Cbor::EncodeQRContents(const CryptoBuffer &publicKey,
         buffer.resize(buffSize);
     }
 
-    cbor_encoder_init(&encoder, buffer.data(), buffer.size(), 0);
-    if ((err = cbor_encoder_create_map(&encoder, &mapEncoder, mapElements))) {
-        LogError("cbor_encoder_create_map error");
-        throw EncodingFailed{};
-    }
-
-    // key 0 - public key
-    if ((err = cbor_encode_int(&mapEncoder, 0))) {
-        LogError("cbor_encode_int error in public key encoding");
-        throw EncodingFailed{};
-    }
-
-    if ((err = cbor_encode_byte_string(&mapEncoder, publicKey.data(), publicKey.size()))) {
-        LogError("cbor_encode_byte_string error in public key encoding");
-        throw EncodingFailed{};
-    }
-
-    // key 1 - QR Secret
-    if ((err = cbor_encode_int(&mapEncoder, 1))) {
-        LogError("cbor_encode_int error in QR secret encoding");
-        throw EncodingFailed{};
-    }
-
-    if ((err = cbor_encode_byte_string(&mapEncoder, qrSecret.data(), qrSecret.size()))) {
-        LogError("cbor_encode_byte_string error in QR secret encoding");
-        throw EncodingFailed{};
-    }
-
-    // key 2 - assigned tunnel servers domains
-    if ((err = cbor_encode_int(&mapEncoder, 2))) {
-        LogError("cbor_encode_int error in tunnel servers domains encoding");
-        throw EncodingFailed{};
-    }
-
-    if ((err = cbor_encode_int(&mapEncoder, ASSIGNED_TUNNEL_SERVER_DOMAINS.size()))) {
-        LogError("cbor_encode_int error in tunnel servers domains encoding");
-        throw EncodingFailed{};
-    }
-
-    // key 3 - current time
-    if ((err = cbor_encode_int(&mapEncoder, 3))) {
-        LogError("cbor_encode_int error in time encoding");
-        throw EncodingFailed{};
-    }
-
-    if ((err = cbor_encode_int(&mapEncoder, getEpoch()))) {
-        LogError("cbor_encode_int error in time encoding");
-        throw EncodingFailed{};
+    auto encoder = CborEncoding::Encoder::Create(buffer.data(), buffer.size());
+    {
+        auto map = encoder.OpenMap(mapElements);
+        // key 0 - public key
+        map.AppendByteStringAt(0, publicKey);
+        // key 1 - QR Secret
+        map.AppendByteStringAt(1, qrSecret);
+        // key 2 - assigned tunnel servers domains
+        map.AppendInt64At(2, ASSIGNED_TUNNEL_SERVER_DOMAINS.size());
+        // key 3 - current time
+        map.AppendInt64At(3, getEpoch());
+        // key 4 - state assisted transactions
+        map.AppendBooleanAt(4, stateAssisted);
+        // key 5 - "ga" if get assertion and "mc" if make credential
+        // Here specification says byte string with which Android does not work. webauthn.io uses
+        // text
+        map.AppendTextStringAt(5, std::string_view(hint.data(), hint.size()));
+        // key 6 - when GREASE is true
+        if (extraKey)
+            map.AppendInt64At(65535, 0);
     }
 
-    // key 4 - state assisted transactions
-    if ((err = cbor_encode_int(&mapEncoder, 4))) {
-        LogError("cbor_encode_int error in state assisted transactions encoding");
-        throw EncodingFailed{};
-    }
-
-    if ((err = cbor_encode_boolean(&mapEncoder, stateAssisted))) {
-        LogError("cbor_encode_boolean error in state assisted transactions encoding");
-        throw EncodingFailed{};
-    }
-
-    // key 5 - "ga" if get assertion and "mc" if make credential
-    if ((err = cbor_encode_int(&mapEncoder, 5))) {
-        LogError("cbor_encode_int error in hint encoding");
-        throw EncodingFailed{};
-    }
-
-    // Here specification says byte string with which Android does not work. webauthn.io uses text
-    // string which works.
-    if ((err = cbor_encode_text_string(&mapEncoder, hint.data(), hint.size()))) {
-        LogError("cbor_encode_text_string error in hint encoding");
-        throw EncodingFailed{};
-    }
-
-    // key 6 - when GREASE is true
-    if (extraKey) {
-        if ((err = cbor_encode_int(&mapEncoder, 65535))) {
-            LogError("cbor_encode_int error in GREASE");
-            throw EncodingFailed{};
-        }
-        if ((err = cbor_encode_int(&mapEncoder, 0))) {
-            LogError("cbor_encode_int error in GREASE");
-            throw EncodingFailed{};
-        }
-    }
-
-    if ((err = cbor_encoder_close_container(&encoder, &mapEncoder))) {
-        LogError("Cbor encode error: cannot close container");
-        throw EncodingFailed{};
-    }
-
-    buffSize = cbor_encoder_get_buffer_size(&encoder, buffer.data());
-    buffer.resize(buffSize);
-
+    buffer.resize(encoder.GetBufferSize());
     fidoUri = "FIDO:/" + DigitEncode(buffer);
 }
 
@@ -222,12 +149,12 @@ void Container::AppendTextStringZ(const char *value)
         THROW_ENCODING("cbor_encode_text_stringz() failed with " << static_cast<int>(err));
 }
 
-void Container::AppendTextString(const std::string_view &text)
+void Container::AppendTextString(const std::string_view &value)
 {
     assert(!m_appendingToChild);
 
     CborError err;
-    err = cbor_encode_text_string(&m_encoder, text.data(), text.size());
+    err = cbor_encode_text_string(&m_encoder, value.data(), value.size());
     if (err != CborNoError)
         THROW_ENCODING("cbor_encode_text_string() failed with " << static_cast<int>(err));
 }
@@ -317,6 +244,12 @@ void SortedMap::AppendByteStringAt(const Key &key, const wauthn_const_buffer_s &
     AppendByteString(value);
 }
 
+void SortedMap::AppendTextStringAt(const Key &key, const std::string_view &value)
+{
+    AddKey(key);
+    AppendTextString(value);
+}
+
 void SortedMap::AppendTextStringZAt(const Key &key, const char *value)
 {
     assert(value);
index 8aa9c7a71a172357598c516cb163908d620a121c..26a678db7a83e501be72d411f7d1e5efc483818c 100644 (file)
@@ -62,7 +62,7 @@ public:
     Container &operator=(Container &&) = delete;
 
     void AppendTextStringZ(const char *value);
-    void AppendTextString(const std::string_view &text);
+    void AppendTextString(const std::string_view &value);
     void AppendInt64(int64_t value);
     void AppendByteString(const Buffer &value);
     void AppendByteString(const wauthn_const_buffer_s &value);
@@ -91,6 +91,7 @@ public:
 
     void AppendByteStringAt(const Key &key, const Buffer &value);
     void AppendByteStringAt(const Key &key, const wauthn_const_buffer_s &value);
+    void AppendTextStringAt(const Key &key, const std::string_view &value);
     void AppendTextStringZAt(const Key &key, const char *value);
     void AppendOptionalTextStringZAt(const Key &key, const char *value);
     void AppendInt64At(const Key &key, int64_t value);