Skip to content

How to extract public key from COSE message #116

@mhatrepratik11

Description

@mhatrepratik11

I have signed the CBOR message and stored COSE bytes in a file with below code.

private static void encodeMessageToCose(KeyPair keyPairWithKey1, KeyPair keyPairWithKey2) {
		try {
			ObjectMapper objectMapper = new ObjectMapper(new CBORFactory());
			TestVehicleManifestDto testVehicleManifestDto = TestVehicleManifestDto.getArtifactManifestDto();

			// Convert JSON to CBOR
			byte[] cborData = objectMapper.writeValueAsBytes(testVehicleManifestDto);

			OneKey oneKeyWithKey1 = new OneKey(keyPairWithKey1.getPublic(), keyPairWithKey1.getPrivate());
			Signer signerWithKey1 = new Signer(oneKeyWithKey1);
			signerWithKey1.addAttribute(HeaderKeys.Algorithm, AlgorithmID.ECDSA_256.AsCBOR(), COSE.Attribute.PROTECTED);
			signerWithKey1.addAttribute(HeaderKeys.KID, CBORObject.FromObject(KEY_1_ID), COSE.Attribute.PROTECTED);
			
			OneKey oneKeyWithKey2 = new OneKey(keyPairWithKey2.getPublic(), keyPairWithKey2.getPrivate());
			Signer signerWithKey2 = new Signer(oneKeyWithKey2);
			signerWithKey2.addAttribute(HeaderKeys.Algorithm, AlgorithmID.ECDSA_256.AsCBOR(), COSE.Attribute.PROTECTED);
			signerWithKey2.addAttribute(HeaderKeys.KID, CBORObject.FromObject(KEY_2_ID), COSE.Attribute.PROTECTED);

			// Create a Sign1Message
			SignMessage signMessage = new SignMessage();
			signMessage.addAttribute(HeaderKeys.CONTENT_TYPE, CBORObject.FromObject(VEHICLE_MANIFEST_MIME_TYPE_CONSTANT), COSE.Attribute.PROTECTED);
			signMessage.SetContent(cborData);
			signMessage.AddSigner(signerWithKey1);
			signMessage.AddSigner(signerWithKey2);
			signMessage.sign();

			byte[] coseBytes = signMessage.EncodeToBytes();
			System.out.println("COSE Byte data:  = " + Arrays.toString(coseBytes));

			// Write COSE bytes data to manifest file
			writeToManifestFile(coseBytes);

		} catch (CoseException | IOException e) {
			System.out.println("Error Occurred: " + e.getMessage());
		}
	}

Now, I want to get public key from this message. I have seen that signer does not expose any method to access OneKey directly. What is the other way by which I can achieve this?

Thanks in advance !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions