Skip to content

parsing of several keys at a time in Kotlin + expect potentially encrypted keys #1053

@tomholub

Description

@tomholub

as a part of #1051 but only after #1052

In our Pgp.kt implementation https://github.com/FlowCrypt/flowcrypt-email-key-manager/blob/2f62df069eee4e6b229d5faef21944696d332068/shared-lib/src/main/kotlin/com/flowcrypt/shared/crypto/Pgp.kt#L187 we assume that we are parsing a single key each time, and the key - if it's a private key - must be decrypted.

However here on client side we may encounter many keys in a single armored block, or many keys in a single byte array stream (unarmored) so we need a way to parse and return an array.

The current implementation is at https://github.com/FlowCrypt/flowcrypt-mobile-core/blob/f80d5491451b61dcf8e539932b693fb28e84217f/source/mobile-interface/endpoints.ts#L229 :

  public parseKeys = async (_uncheckedReq: any, data: Buffers) => {
    const keyDetails: KeyDetails[] = [];
    const allData = Buf.concat(data);
    const pgpType = await PgpMsg.type({ data: allData });
    if (!pgpType) {
      return fmtRes({ format: 'unknown', keyDetails });
    }
    if (pgpType.armored) {
      // armored
      const { blocks } = MsgBlockParser.detectBlocks(allData.toString());
      for (const block of blocks) {
        const { keys } = await PgpKey.parse(block.content.toString());
        keyDetails.push(...keys);
      }
      return fmtRes({ format: 'armored', keyDetails });
    }
    // binary
    const { keys: openPgpKeys } = await openpgp.key.read(allData);
    for (const openPgpKey of openPgpKeys) {
      keyDetails.push(await PgpKey.details(openPgpKey))
    }
    return fmtRes({ format: 'binary', keyDetails: keyDetails });
  }

It tries to infer the type of the message key it starts parsing, and then it parses either armored keys or binary keys flexibly, and returns a list of parsed keys.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions