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

How to extract public key from COSE message #116

Open
mhatrepratik11 opened this issue Jun 16, 2024 · 0 comments
Open

How to extract public key from COSE message #116

mhatrepratik11 opened this issue Jun 16, 2024 · 0 comments

Comments

@mhatrepratik11
Copy link

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 !

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

1 participant