Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unpackedmessage.as_value() does not provide decrypted version of the message #94

Open
vongohren opened this issue Nov 16, 2022 · 10 comments

Comments

@vongohren
Copy link

vongohren commented Nov 16, 2022

What

When I receive a message that is encrypted, and run it through unpack with the right secrets and resolvers. I expect that I get an object I can work with that is decrypted and possible to read the datat itself.

But the object that is provided is an object with a ciphertext. Since I was not the one encrypting the initial ciphertext, I expect that the library will provide me with a way to get the clear text message out.

Here is an example of the unpacked.as_value() message

{
    "id": "36f2a86e-5b25-4baf-9f7f-40d978590316",
    "typ": "application/didcomm-plain+json",
    "type": "https://didcomm.org/routing/2.0/forward",
    "body": {
        "next": "did:peer:2.Vz6MkhMTSwCfytUNmrEQ4We5i6B2ywM8hAcHvRDD293QGrqSS.Ez6LSm3LPosAtT2qUtFNU9Y2y9P9wQUfJjGgs98uchVJtZjfS.SeyJpZCI6IiNkaWRjb21tIiwidCI6ImRtIiwicyI6Imh0dHBzOi8vZGV2LW9wZW4tbWVzc2FnZXMtYXBpLWx0aDRnb3dkeXEtZXcuYS5ydW4uYXBwL21lc3NhZ2VzIiwiYSI6WyJkaWRjb21tL3YyIl19"
    },
    "attachments": [
        {
            "data": {
                "json": {
                    "ciphertext": "EvyxDoHnIYCc0iFhenovIaiJfnIDWlE1brmoqLlNaVoHJ6oGuSWp_UaiqT2yGCZiKKP4E0mptTM14TD9dYpiAKajcjhIKFIeDCMyyaKdusdd6V6C7On98D9uEW9vqW6VDn-EygIv1ZTQInJ3P0ec_my-x1L2G6CReM7p5uDyfI0xIYzr_Lv8cTNRJIna5HOXEPq04fSf-BQZDX3DiL-wcdO-HCy4qL_udevp9hUla7Xam1xhnU_7pttu7ygHnSrtjJMyzNymNSsb1mNgz3qio-_d_MW2xyhoMtQzig7TVnB9CIYpvMRg-HSwMLwkxGDHx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lpLdNW0pHof5_LlaUk--XdUM88XchSK-dcrck1kq1AE3HxMVcLNm9QKoga3f8EcgfQiD2B4230nutEcUP2dRXaC9rfTHC7cuzQmtMb1_ktLutM8f7U0bce4SJyenxYCaqduxhzwylJBo5e2kDWTJqK0NUUF47yYDD9ybroyWvfY_cjuUNbqMxVEerz5ZtSO9sqBE6TeHWPDsSJVjr73vQb5YEUWSba0u_irqBls_sgha7e3MP9NXp6SSO1xf9uNgct7CJVgNvYgQDZNdQgpDx-iTBDLeCkhapB3XNJFMpdzdrdJqxu6-oW8qpKrP6NlGuNSl7G3LwIfhFTV9auFOGRkDCzUC4RJ8e8Do-2DNJCBY_fbclxnLD3y2SZLI5bfH3yHQhN_Ayfn0uePVA2m-pbK6QafohqtdngFn63NrS19QIfw29py2LTP4iVEspzIfDhwJnSgIdk_jS9FbMbDvjLTrrPmvUf5If3cCTeHgH-lZmlruIqhhASgNpjLppHrjwg-1S0KxuoWJxSpdHvQ36G2_hFM8z5jrbow4h9X6QDq8wNV-cT6GS5zJGaQRO61nHOg-0XpRUHUYpxukIHritIjTIqiF_VgSq0X7COodZeM1h54rb37FS7i-v337r5slBUl9x8D7EXraGuu6xPuDG6fMmefYiMfNmwY07l8gR6fWdSLj_vTdPlxIMaAXYdjppaNVqPyYa2DvV4gu_Mk9rhzbB0rw3sx5a9SNOPEHvXinc-5U2RSMrivo8OM7FcM3jZw__gkgCsRgCqy8PXrkwg5wmPJU9vQRhf_VPvArRPfPjhTzJsu-9prZILnnMkvBQx8K-4yD8-Co9jCbhBXSTjyPDeGF59Yr_Qum5yxfBVcrF2nquTvi4FzRd_ehXbIeSAT3Ue9IzBt96xHl4giVTW_yrda_1efCAURDc",
                    "iv": "ppQiLheseVBGRfdsXVxnhQzQF1kavfZF",
                    "protected": "eyJ0eXAiOiJhcHBsaWNhdGlvbi9kaWRjb21tLWVuY3J5cHRlZCtqc29uIiwiYWxnIjoiRUNESC1FUytBMjU2S1ciLCJlbmMiOiJYQzIwUCIsImFwdiI6IkJCLW9GVjBKemZoQWFFNjlQS3VaRmQ4dDhWZ19hQzhfTlpUNTM3NkxCOFEiLCJlcGsiOnsiY3J2IjoiWDI1NTE5Iiwia3R5IjoiT0tQIiwieCI6IkdmYUhrbHFUdWk3VUhSTXJIYms2Y0llNjFkbDhkd2tqQUZCcTI2aGMzUjgifX0",
                    "recipients": [
                        {
                            "encrypted_key": "QlQcNWkAUKbmOhDagbX5Vo9w1BzowrwfIrDtPxkpFXjtzwyq0tYiGA",
                            "header": {
                                "kid": "did:peer:2.Vz6MkhMTSwCfytUNmrEQ4We5i6B2ywM8hAcHvRDD293QGrqSS.Ez6LSm3LPosAtT2qUtFNU9Y2y9P9wQUfJjGgs98uchVJtZjfS.SeyJpZCI6IiNkaWRjb21tIiwidCI6ImRtIiwicyI6Imh0dHBzOi8vZGV2LW9wZW4tbWVzc2FnZXMtYXBpLWx0aDRnb3dkeXEtZXcuYS5ydW4uYXBwL21lc3NhZ2VzIiwiYSI6WyJkaWRjb21tL3YyIl19#6LSm3LPo"
                            }
                        }
                    ],
                    "tag": "Ba3PnsIur7cgeLxSSH_CNw"
                }
            }
        }
    ]
}

Why

Because its not usable to actually consume the message itself that was sent.

The message below was the original message going through the encrypt message function.
And I want to see that after I unpack with the right secrets.

What am i missing?

{
    "id": "1234567890",
    "typ": "application/didcomm-plain+json",
    "type": "http://example.com/protocols/lets_do_lunch/1.0/proposal",
    "body": {
        "messagespecificattribute": "and its value"
    },
    "from": "did:peer:2.Vz6MkkkhBpeffjdyRCpyv1h17ZH4fJ6amEu2cujuaBcf2bmor.Ez6LScPkBqUGgUnFdxEPviCBAhdeKrhrHidLCyrcjq6SdvSj6.SeyJpZCI6IiNkaWRjb21tIiwidCI6ImRtIiwicyI6Imh0dHBzOi8vZGV2LW9wZW4tbWVzc2FnZXMtYXBpLWx0aDRnb3dkeXEtZXcuYS5ydW4uYXBwL21lc3NhZ2VzIiwiYSI6WyJkaWRjb21tL3YyIl19",
    "to": [
        "did:peer:2.Vz6MkhMTSwCfytUNmrEQ4We5i6B2ywM8hAcHvRDD293QGrqSS.Ez6LSm3LPosAtT2qUtFNU9Y2y9P9wQUfJjGgs98uchVJtZjfS.SeyJpZCI6IiNkaWRjb21tIiwidCI6ImRtIiwicyI6Imh0dHBzOi8vZGV2LW9wZW4tbWVzc2FnZXMtYXBpLWx0aDRnb3dkeXEtZXcuYS5ydW4uYXBwL21lc3NhZ2VzIiwiYSI6WyJkaWRjb21tL3YyIl19"
    ],
    "created_time": 1516269022,
    "expires_time": 1516385931
}

Success Criteria

encrypt and pack a message, and un pack and decrypt the same message with the same library

@brianorwhatever
Copy link

I believe what you are decrypting and seeing here is the "forward" message for the routing protocol. Your encryption is wrapping the message with 2 layers and you are unwrapping 1 layer and expecting to see the inner envelope. The "next" property in the body tells you to now send that message to that did

@vongohren
Copy link
Author

vongohren commented Nov 17, 2022

@brianorwhatever thanks for your suggestion, but im just following the demos.
But based on what you say do you suggest that i send the unpacked message through the unpack again? Ref the attached image. This is SICPAs suggestion of what is common case: https://github.com/sicpa-dlab/didcomm-rust/blob/main/wasm/README.md#1-build-an-encrypted-didcomm-message-for-the-given-recipient. Very similar

image

@vongohren
Copy link
Author

This is the leading code in, meaning its not that much one can screw up
image

@brianorwhatever
Copy link

hmm yeah, and looking closer your did:peer doesn't have routingKeys in it as I had assumed. Not sure what else to suggest.. will need feedback from sicpa folks

@vongohren
Copy link
Author

@brianorwhatever yeah I was hoping I could avoid routing keys, as I dont see the need as of now? I will push hardman on discord and see if there is anything he can push

@vongohren
Copy link
Author

vongohren commented Nov 21, 2022

The problem at hand is that I expect to be able to read decrypted data when I unpack

Adding code so its easier to copy paste. But its really just the example code.

const to = "did:peer:2.Vz6MkhMTSwCfytUNmrEQ4We5i6B2ywM8hAcHvRDD293QGrqSS.Ez6LSm3LPosAtT2qUtFNU9Y2y9P9wQUfJjGgs98uchVJtZjfS.SeyJpZCI6IiNkaWRjb21tIiwidCI6ImRtIiwicyI6Imh0dHBzOi8vZGV2LW9wZW4tbWVzc2FnZXMtYXBpLWx0aDRnb3dkeXEtZXcuYS5ydW4uYXBwL21lc3NhZ2VzIiwiYSI6WyJkaWRjb21tL3YyIl19"
  const from = "did:peer:2.Vz6MkkkhBpeffjdyRCpyv1h17ZH4fJ6amEu2cujuaBcf2bmor.Ez6LScPkBqUGgUnFdxEPviCBAhdeKrhrHidLCyrcjq6SdvSj6.SeyJpZCI6IiNkaWRjb21tIiwidCI6ImRtIiwicyI6Imh0dHBzOi8vZGV2LW9wZW4tbWVzc2FnZXMtYXBpLWx0aDRnb3dkeXEtZXcuYS5ydW4uYXBwL21lc3NhZ2VzIiwiYSI6WyJkaWRjb21tL3YyIl19"


  const msg = new Message({
    id: "1234567890",
    typ: "application/didcomm-plain+json",
    type: "http://example.com/protocols/lets_do_lunch/1.0/proposal",
    from: from,
    to: [to],
    created_time: 1516269022,
    expires_time: 1516385931,
    body: { messagespecificattribute: "and its value" },
  });
  // This resolver just resolves as expected and modifys data objects to fit the code. Lots of transformations
  const resolver = new DiwalaDIDResolver();
  // This resolver just calls the nessecary methods and NRIS reads out the key values nessecary to do this secret action. It succeeds so I think it is not relevant.
  const secrets_resolver = new DiwalaSecretsResolver(nris);

  try {
    const [encryptedMsg, encryptMetadata] = await msg.pack_encrypted(to,from,from,resolver, secrets_resolver, {})
    console.log("Metadata of message", encryptMetadata)
    console.log(`Sending message: ${encryptedMsg}`)
    
    const [unpackedMsg, unpackMetadata] = await Message.unpack(
      encryptedMsg,
      resolver,
      secrets_resolver,
      {}
    );
  
    console.log("Reveived message is\n", JSON.stringify(unpackedMsg.as_value()));
    console.log("Reveived message unpack metadata is\n", unpackMetadata);



  } catch (error) {
    console.log(error)
    throw new Error('faild encryption and decryption')
  }  


@vongohren
Copy link
Author

Ok, so this is awkward. Its very easily solvable with adding an unpack option of

{unwrap_re_wrapping_forward: true}

This is default false according to their inline comments.

Adding this, I got the original plain text message and Im able to continue onwards.

It would be great with some clearer documentation, or tests around this so it is clear to how this works

@brianorwhatever
Copy link

I think I have discovered that this only happens on signed messages. I haven't yet been able to reproduce though as I can't successfully create a signed message. It throws Unsupported signature alg which looks to be coming from here

@vongohren
Copy link
Author

@brianorwhatever i tried not to sign it, leaving the sign option to null. And it did not make a difference.

Your unsupported alg, i traversed as well. I dont remember what the solution was. I jsut know that I generate did:peer methods with the following logic:
image

Meaning im able to use the 2020 keys and sign and create. As you mention in #95.

But when it comes to unsupported alg, I needed to have a key that also had keyAgreement. Both sides of the message senders had to have key agreements og the right keytype.

@vongohren
Copy link
Author

vongohren commented Nov 23, 2022

Just important transformation for secrets to work

  getSecretFormat(type: string) {
    if(type='X25519KeyAgreementKey2020') return 'Multibase'
    if(type='Ed25519VerificationKey2020') return 'Multibase'
    if(type='JsonWebKey2020') return 'JWK'
    throw new Error('Unsupported secret format');
  }

  getSecretValue(secret, format) {
    const valueAttribute = formatMap[format]
    const secretValue = secret[valueAttribute]
    if(secretValue) return secretValue
    throw new Error('Unsupported secret value')
  }

  findAndTransformSecret(secrets:DecryptedKMSObject, id: string, did: string) {
    const thisSecret = secrets.keys.find(k=>k.keyId===`#${id}`)
    const type = thisSecret.decrypted.type
    const format = this.getSecretFormat(type)
    const value = this.getSecretValue(thisSecret.decrypted, format)
    const obj = {
      id: `${did}#${id}`,
      type: type,
      secret_material: {
        format,
        value
      }
    }
    return obj

  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants