/** * Gets the DER encoded public key */ LONGBOW_TEST_CASE(openssl_commandline, parcPkcs12KeyStore_GetEncodedPublicKey) { PARCPkcs12KeyStore *publicKeyStore = parcPkcs12KeyStore_Open("test_rsa.p12", "blueberry", PARC_HASH_SHA256); PARCKeyStore *keyStore = parcKeyStore_Create(publicKeyStore, PARCPkcs12KeyStoreAsKeyStore); parcPkcs12KeyStore_Release(&publicKeyStore); PARCPublicKeySigner *publicKeySigner = parcPublicKeySigner_Create(keyStore, PARCSigningAlgorithm_RSA, PARC_HASH_SHA256); parcKeyStore_Release(&keyStore); PARCSigner *signer = parcSigner_Create(publicKeySigner, PARCPublicKeySignerAsSigner); parcPublicKeySigner_Release(&publicKeySigner); assertNotNull(signer, "Got null result from opening openssl pkcs12 file"); PARCBuffer *pubkey_der = parcKeyStore_GetDEREncodedPublicKey(parcSigner_GetKeyStore(signer)); assertNotNull(pubkey_der, "got null public key der for external pkcs12"); // read in the "truth" from the command line utilities int fd = open("test_rsa_pub.der", O_RDONLY); uint8_t true_der[1024]; ssize_t read_bytes = read(fd, true_der, 1024); close(fd); assertTrue(read_bytes == 162, "could not read %d byte digest from test_rsa_pub_sha256.bin", 162); const uint8_t *bb_buffer = parcByteArray_Array(parcBuffer_Array(pubkey_der)); size_t bb_length = parcBuffer_Remaining(pubkey_der); assertTrue(bb_length == read_bytes, "Incorrect digest length returned from GetCertificateDigest: %zu", bb_length); assertTrue(memcmp(bb_buffer, true_der, read_bytes) == 0, "digests did not match"); parcSigner_Release(&signer); parcBuffer_Release(&pubkey_der); }
PARCSigner * component_Codec_GetSigner(RtaConnection *conn) { PARCSigner *signer = NULL; SignerType signertype = signer_GetImplementationType(rtaConnection_GetParameters(conn)); switch (signertype) { case SignerType_SymmetricKeySigner: { struct symmetrickeysigner_params params; bool success = symmetricKeySigner_GetConnectionParams(rtaConnection_GetParameters(conn), ¶ms); assertTrue(success, "Could not retrieve symmetricKeySigner_GetConnectionParams"); PARCSymmetricKeyStore *symmetricKeyStore = parcSymmetricKeyStore_OpenFile(params.filename, params.password, PARCCryptoHashType_SHA256); PARCSymmetricKeySigner *symmetricKeySigner = parcSymmetricKeySigner_Create(symmetricKeyStore, PARCCryptoHashType_SHA256); parcSymmetricKeyStore_Release(&symmetricKeyStore); signer = parcSigner_Create(symmetricKeySigner, PARCSymmetricKeySignerAsSigner); parcSymmetricKeySigner_Release(&symmetricKeySigner); assertNotNull(signer, "got null opening FileKeystore '%s'\n", params.filename); break; } case SignerType_PublicKeySigner: { struct publickeysigner_params params; bool success = publicKeySigner_GetConnectionParams(rtaConnection_GetParameters(conn), ¶ms); assertTrue(success, "Could not retrieve publicKeySigner_GetConnectionParams"); PARCPkcs12KeyStore *pkcs12KeyStore = parcPkcs12KeyStore_Open(params.filename, params.password, PARCCryptoHashType_SHA256); PARCKeyStore *keyStore = parcKeyStore_Create(pkcs12KeyStore, PARCPkcs12KeyStoreAsKeyStore); parcPkcs12KeyStore_Release(&pkcs12KeyStore); PARCPublicKeySigner *publicKeySigner = parcPublicKeySigner_Create(keyStore, PARCSigningAlgorithm_RSA, PARCCryptoHashType_SHA256); parcKeyStore_Release(&keyStore); signer = parcSigner_Create(publicKeySigner, PARCPublicKeySignerAsSigner); parcPublicKeySigner_Release(&publicKeySigner); assertNotNull(signer, "got null opening FileKeystore '%s'\n", params.filename); break; } default: assertTrue(0, "Unsupported signer type %d", signertype); } assertNotNull(signer, "Did not match a known signer"); return signer; }
/** * read in the openssl command-line generated pkcs12 file */ LONGBOW_TEST_CASE(openssl_commandline, parcPkcs12KeyStore_GetPublicKeyDigest) { PARCPkcs12KeyStore *publicKeyStore = parcPkcs12KeyStore_Open("test_rsa.p12", "blueberry", PARC_HASH_SHA256); PARCKeyStore *keyStore = parcKeyStore_Create(publicKeyStore, PARCPkcs12KeyStoreAsKeyStore); parcPkcs12KeyStore_Release(&publicKeyStore); PARCPublicKeySigner *publicKeySigner = parcPublicKeySigner_Create(keyStore, PARCSigningAlgorithm_RSA, PARC_HASH_SHA256); parcKeyStore_Release(&keyStore); PARCSigner *signer = parcSigner_Create(publicKeySigner, PARCPublicKeySignerAsSigner); parcPublicKeySigner_Release(&publicKeySigner); assertNotNull(signer, "parcPkcs12KeyStore_Open(\"test_rsa.p12\", \"blueberry\", PARC_HASH_SHA256) returned NULL"); PARCCryptoHash *pkd = parcKeyStore_GetVerifierKeyDigest(parcSigner_GetKeyStore(signer)); assertNotNull(pkd, "got null public key digest for external pkcs12"); // read in the "truth" from the command line utilities int fd = open("test_rsa_pub_sha256.bin", O_RDONLY); uint8_t true_digest[SHA256_DIGEST_LENGTH]; ssize_t read_bytes = read(fd, true_digest, SHA256_DIGEST_LENGTH); close(fd); assertTrue(read_bytes == SHA256_DIGEST_LENGTH, "could not read %d byte digest from test_rsa_pub_sha256.bin", SHA256_DIGEST_LENGTH); PARCBuffer *digest = parcCryptoHash_GetDigest(pkd); const uint8_t *bb_buffer = parcByteArray_Array(parcBuffer_Array(digest)); size_t bb_length = parcBuffer_Remaining(digest); assertTrue(bb_length == SHA256_DIGEST_LENGTH, "Incorrect digest length returned from GetPublicKeyDigest: %zu", bb_length); assertTrue(memcmp(bb_buffer, true_digest, SHA256_DIGEST_LENGTH) == 0, "digests did not match"); parcSigner_Release(&signer); parcCryptoHash_Release(&pkd); }