Add type parameter to "get" commands 95/296795/1
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Mon, 24 Jul 2023 10:44:00 +0000 (12:44 +0200)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 4 Aug 2023 08:10:49 +0000 (10:10 +0200)
CMD_GET_DATA
CMD_GET_DATA_SIZE
CMD_EXPORT_WRAPPED_KEY

This may be necessary if data is encrypted.

Change-Id: Ie34d33c11e9c55958cb44adcb0bf5371c36e8a68

src/manager/crypto/tz-backend/internals.cpp
src/manager/crypto/tz-backend/internals.h
src/manager/crypto/tz-backend/obj.cpp
src/manager/crypto/tz-backend/obj.h
src/manager/crypto/tz-backend/store.cpp
src/manager/crypto/tz-backend/tz-context.cpp
src/manager/crypto/tz-backend/tz-context.h

index efad483..dd4aed4 100644 (file)
@@ -402,7 +402,8 @@ RawBuffer exportWrappedKey(const RawBuffer &wrappingKeyId,
                                                   const Pwd &wrappingKeyPwd,
                                                   const CryptoAlgorithm &alg,
                                                   const RawBuffer &keyToWrapId,
-                                                  const Pwd &keyToWrapPwd)
+                                                  const Pwd &keyToWrapPwd,
+                                                  const DataType &keyToWrapType)
 {
        AlgoType algo;
        uint32_t ctrLenOrTagSizeBits = 0;
@@ -418,15 +419,18 @@ RawBuffer exportWrappedKey(const RawBuffer &wrappingKeyId,
                                                                                                                 ctrLenOrTagSizeBits,
                                                                                                                 aad,
                                                                                                                 keyToWrapId,
-                                                                                                                keyToWrapPwd);
+                                                                                                                keyToWrapPwd,
+                                                                                                                toTzDataType(keyToWrapType));
 }
 
 RawBuffer getData(const RawBuffer &dataId,
-                                 const Pwd &pwd)
+                                 const Pwd &pwd,
+                                 const DataType &type)
 {
        RawBuffer result;
        TrustZoneContext::Instance().getData(dataId,
                                 pwd,
+                                toTzDataType(type),
                                 result);
        return result;
 }
index 98171a5..00fb25f 100644 (file)
@@ -75,10 +75,12 @@ RawBuffer exportWrappedKey(const RawBuffer &wrappingKeyId,
                                                   const Pwd &wrappingKeyPwd,
                                                   const CryptoAlgorithm &alg,
                                                   const RawBuffer &keyToWrapId,
-                                                  const Pwd &keyToWrapPwd);
+                                                  const Pwd &keyToWrapPwd,
+                                                  const DataType &keyToWrapType);
 
 RawBuffer getData(const RawBuffer &dataId,
-                                 const Pwd &pwd);
+                                 const Pwd &pwd,
+                                 const DataType &type);
 
 void destroyData(const RawBuffer &dataId);
 
index 5a8153e..a811557 100644 (file)
@@ -119,7 +119,8 @@ RawBuffer Key::wrap(const CryptoAlgorithm &alg,
                                                                           getPassword(),
                                                                           alg,
                                                                           keyToWrapId,
-                                                                          Pwd(keyToWrapPass, keyToWrapIV, keyToWrapTag));
+                                                                          Pwd(keyToWrapPass, keyToWrapIV, keyToWrapTag),
+                                                                          m_type);
 }
 
 RawBuffer SKey::encrypt(const CryptoAlgorithm &alg, const RawBuffer &data)
@@ -180,7 +181,7 @@ GCtxShPtr AKey::initContext(const CryptoAlgorithm &, bool)
 RawBuffer AKey::getBinary() const
 {
        if (m_type.isKeyPublic() && m_raw.empty())
-               m_raw = Internals::getData(getId(), getPassword());
+               m_raw = Internals::getData(getId(), getPassword(), m_type);
 
        return m_raw;
 }
index eec1e9f..3d406be 100644 (file)
@@ -98,8 +98,8 @@ protected:
 
 class Key : public BData {
 public:
-       Key(CryptoBackend backendId, int scheme, RawBuffer id, Pwd pwd) :
-               BData(backendId, scheme, std::move(id), std::move(pwd)) {}
+       Key(CryptoBackend backendId, int scheme, RawBuffer id, Pwd pwd, DataType dataType) :
+               BData(backendId, scheme, std::move(id), std::move(pwd)), m_type(dataType) {}
 
        Token unwrap(const CryptoAlgorithm &params,
                                 const Data &encryptedKey,
@@ -109,12 +109,15 @@ public:
        RawBuffer wrap(const CryptoAlgorithm &params,
                                   const Token &keyToWrap,
                                   const Password &keyToWrapPass) override;
+
+protected:
+       DataType m_type;
 };
 
 class SKey : public Key {
 public:
        SKey(CryptoBackend backendId, int scheme, RawBuffer id, Pwd pwd) :
-               Key(backendId, scheme, std::move(id), std::move(pwd)) {}
+               Key(backendId, scheme, std::move(id), std::move(pwd), DataType::KEY_AES) {}
 
        RawBuffer encrypt(const CryptoAlgorithm &, const RawBuffer &) override;
        RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &) override;
@@ -128,7 +131,7 @@ public:
                 RawBuffer id,
                 Pwd pwd,
                 DataType dataType) :
-               Key(backendId, scheme, std::move(id), std::move(pwd)), m_type(dataType) {}
+               Key(backendId, scheme, std::move(id), std::move(pwd)dataType) {}
 
        RawBuffer getBinary() const override;
        RawBuffer sign(const CryptoAlgorithm &alg, const RawBuffer &message) override;
@@ -138,9 +141,6 @@ public:
        RawBuffer decrypt(const CryptoAlgorithm &, const RawBuffer &) override;
        Token derive(const CryptoAlgorithm &, const Password &, const RawBuffer &) override;
        GCtxShPtr initContext(const CryptoAlgorithm &, bool) override;
-
-protected:
-       DataType m_type;
 };
 
 class Cert : public AKey {
index 23ba831..ca91c54 100644 (file)
@@ -78,7 +78,7 @@ GObjUPtr Store::getObject(const Token &token, const Password &pass)
                return make<Cert>(scheme, std::move(id), Pwd(pass, iv, tag), token.dataType);
 
        auto pwd = Pwd(pass, iv, tag);
-       RawBuffer raw = Internals::getData(id, pwd);
+       RawBuffer raw = Internals::getData(id, pwd, token.dataType);
 
        if (token.dataType.isBinaryData())
                return make<BData>(scheme, std::move(id), std::move(pwd), std::move(raw));
index 2c013ae..8710982 100644 (file)
@@ -746,7 +746,8 @@ RawBuffer TrustZoneContext::exportWrappedKey(const RawBuffer &wrappingKeyId,
                                                                                         const uint32_t ctrLenOrTagSizeBits,
                                                                                         const RawBuffer &aad,
                                                                                         const RawBuffer &keyToWrapId,
-                                                                                        const Pwd &keyToWrapPwd)
+                                                                                        const Pwd &keyToWrapPwd,
+                                                                                        tz_data_type keyToWrapType)
 {
        // command ID = CMD_EXPORT_WRAPPED_KEY
        LogDebug("TrustZoneContext::exportWrappedKey");
@@ -758,19 +759,20 @@ RawBuffer TrustZoneContext::exportWrappedKey(const RawBuffer &wrappingKeyId,
                                                          ctrLenOrTagSizeBits,
                                                          aad,
                                                          keyToWrapId,
-                                                         keyToWrapPwd);
+                                                         keyToWrapPwd,
+                                                         keyToWrapType);
 
        TrustZoneMemory inMemory(m_Context, sIn.GetSize(), TEEC_MEM_INPUT);
        sIn.Serialize(inMemory);
 
-       uint32_t data_size = 0;
-       GetDataSize(keyToWrapId, data_size);
+       uint32_t dataSize = 0;
+       GetDataSize(keyToWrapId, keyToWrapType, dataSize);
 
-       LogDebug("GetData data_size = [" << data_size << "]");
+       LogDebug("GetData data_size = [" << dataSize << "]");
 
        // encrypted data may be longer
        TZSerializer sOut;
-       sOut.Push(new TZSerializableBinary(data_size + KM_ENCRYPTION_OVERHEAD));
+       sOut.Push(new TZSerializableBinary(dataSize + KM_ENCRYPTION_OVERHEAD));
        TrustZoneMemory outMemory(m_Context, sOut.GetSize(), TEEC_MEM_OUTPUT);
        sOut.Serialize(outMemory);
 
@@ -786,12 +788,14 @@ RawBuffer TrustZoneContext::exportWrappedKey(const RawBuffer &wrappingKeyId,
        return wrappedKey;
 }
 
-void TrustZoneContext::GetDataSize(const RawBuffer &dataId, uint32_t &dataSize)
+void TrustZoneContext::GetDataSize(const RawBuffer &dataId,
+                                                                  const tz_data_type type,
+                                                                  uint32_t &dataSize)
 {
        // command ID = CMD_GET_DATA_SIZE
        LogDebug("Object ID (passed to CMD_GET_DATA_SIZE) is (hex): " << rawToHexString(dataId));
 
-       auto sIn = makeSerializer(dataId);
+       auto sIn = makeSerializer(dataId, type);
        TrustZoneMemory inMemory(m_Context, sIn.GetSize(), TEEC_MEM_INPUT);
        sIn.Serialize(inMemory);
 
@@ -803,17 +807,18 @@ void TrustZoneContext::GetDataSize(const RawBuffer &dataId, uint32_t &dataSize)
 
 void TrustZoneContext::getData(const RawBuffer &dataId,
                         const Pwd &pwd,
+                        const tz_data_type type,
                         RawBuffer &data)
 {
        // command ID = CMD_GET_DATA
        LogDebug("Object ID (passed to CMD_GET_DATA) is (hex): " << rawToHexString(dataId));
 
-       auto sIn = makeSerializer(dataId, pwd);
+       auto sIn = makeSerializer(dataId, pwd, type);
        TrustZoneMemory inMemory(m_Context, sIn.GetSize(), TEEC_MEM_INPUT);
        sIn.Serialize(inMemory);
 
        uint32_t data_size = 0;
-       GetDataSize(dataId, data_size);
+       GetDataSize(dataId, type, data_size);
 
        LogDebug("GetData data_size = [" << data_size << "]");
 
index 9fd3a1e..342fdfe 100644 (file)
@@ -109,7 +109,8 @@ public:
                                                           const uint32_t ctrLenOrTagSizeBits,
                                                           const RawBuffer &aad,
                                                           const RawBuffer &keyToWrapId,
-                                                          const Pwd &keyToWrapPwd);
+                                                          const Pwd &keyToWrapPwd,
+                                                          tz_data_type keyToWrapType);
 
        void executeCrypt(tz_command cmd,
                                        tz_algo_type algo,
@@ -169,6 +170,7 @@ public:
 
        void getData(const RawBuffer &dataId,
                                 const Pwd &pwd,
+                                const tz_data_type type,
                                 RawBuffer &data);
 
        void destroyData(const RawBuffer &dataId);
@@ -211,7 +213,7 @@ private:
        void Destroy();
        void Reload();
 
-       void GetDataSize(const RawBuffer &dataId, uint32_t &dataSize);
+       void GetDataSize(const RawBuffer &dataId, const tz_data_type type, uint32_t &dataSize);
 
        void Execute(tz_command commandID, TEEC_Operation* op);