Пример #1
0
LONGBOW_TEST_CASE(Global, parcCryptoHash_GetDigest)
{
    int fd_buffer = open("test_digest_bytes_128.bin", O_RDONLY);
    int fd_truth = open("test_digest_bytes_128.sha256", O_RDONLY);
    assertFalse(fd_buffer < 0, "Could not open %s: %s", "test_digest_bytes_128.bin", strerror(errno));
    assertFalse(fd_truth < 0, "Could not open %s: %s", "test_digest_bytes_128.sha256", strerror(errno));

    uint8_t scratch[bufferLength];
    ssize_t read_length = read(fd_truth, scratch, bufferLength);

    PARCCryptoHash *hashTruth = parcCryptoHash_CreateFromArray(PARC_HASH_SHA256, scratch, read_length);

    read_length = read(fd_buffer, scratch, bufferLength);

    PARCCryptoHasher *hasher = parcCryptoHasher_Create(PARC_HASH_SHA256);
    parcCryptoHasher_Init(hasher);
    parcCryptoHasher_UpdateBytes(hasher, scratch, read_length);

    PARCCryptoHash *hashTest = parcCryptoHasher_Finalize(hasher);

    assertTrue(parcBuffer_Equals(parcCryptoHash_GetDigest(hashTruth), parcCryptoHash_GetDigest(hashTest)), "Expected to be true");

    parcCryptoHasher_Release(&hasher);
    parcCryptoHash_Release(&hashTruth);
    parcCryptoHash_Release(&hashTest);

    close(fd_buffer);
    close(fd_truth);
}
Пример #2
0
LONGBOW_TEST_CASE(Global, parcCryptoHash_Equals)
{
    int fd1 = open("test_digest_bytes_128.sha256", O_RDONLY);
    assertFalse(fd1 < 0, "Could not open %s: %s", "test_digest_bytes_128.sha256", strerror(errno));

    uint8_t scratch1[bufferLength];
    ssize_t read_length = read(fd1, scratch1, bufferLength);

    PARCCryptoHash *hash1 = parcCryptoHash_CreateFromArray(PARC_HASH_SHA256, scratch1, read_length);
    PARCCryptoHash *hash2 = parcCryptoHash_CreateFromArray(PARC_HASH_SHA256, scratch1, read_length);
    PARCCryptoHash *hash3 = parcCryptoHash_CreateFromArray(PARC_HASH_SHA256, scratch1, read_length);

    int fd2 = open("test_digest_bytes_128.sha512", O_RDONLY);
    assertFalse(fd2 < 0, "Could not open %s: %s", "test_digest_bytes_128.sha512", strerror(errno));

    uint8_t scratch2[bufferLength];
    read_length = read(fd2, scratch2, bufferLength);

    PARCCryptoHash *unequalhash = parcCryptoHash_CreateFromArray(PARC_HASH_SHA256, scratch2, read_length);

    parcObjectTesting_AssertEqualsFunction(parcCryptoHash_Equals, hash1, hash2, hash3, unequalhash);

    parcCryptoHash_Release(&hash1);
    parcCryptoHash_Release(&hash2);
    parcCryptoHash_Release(&hash3);
    parcCryptoHash_Release(&unequalhash);

    close(fd1);
    close(fd2);
}
Пример #3
0
/**
 * Get the certificate digest from the openssl command line pkcs12
 */
LONGBOW_TEST_CASE(openssl_commandline, parcPkcs12KeyStore_GetCertificateDigest)
{
    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");

    PARCCryptoHash *cert_digest = parcKeyStore_GetCertificateDigest(parcSigner_GetKeyStore(signer));
    assertNotNull(cert_digest, "got null public key digest for external pkcs12");

    // read in the "truth" from the command line utilities

    int fd = open("test_rsa_crt_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);

    const uint8_t *bb_buffer = parcByteArray_Array(parcBuffer_Array(parcCryptoHash_GetDigest(cert_digest)));
    size_t bb_length = parcBuffer_Remaining(parcCryptoHash_GetDigest(cert_digest));
    assertTrue(bb_length == SHA256_DIGEST_LENGTH,
               "Incorrect digest length returned from GetCertificateDigest: %zu", bb_length);

    assertTrue(memcmp(bb_buffer, true_digest, SHA256_DIGEST_LENGTH) == 0, "digests did not match");

    parcSigner_Release(&signer);
    parcCryptoHash_Release(&cert_digest);
}
Пример #4
0
LONGBOW_TEST_CASE(ccnx_internal, parcPkcs12KeyStore_GetCertificateDigest)
{
    // create a file and open it
    const char *password = "******";
    const char *subject = "alice";
    bool result;

    result = parcPkcs12KeyStore_CreateFile(filename, password, subject, 1024, 32);
    assertTrue(result, "got error from parcPkcs12KeyStore_CreatePkcs12File");

    PARCPkcs12KeyStore *publicKeyStore = parcPkcs12KeyStore_Open(filename, password, PARC_HASH_SHA256);
    assertNotNull(publicKeyStore, "Got null result from opening openssl pkcs12 file");
    PARCKeyStore *keyStore = parcKeyStore_Create(publicKeyStore, PARCPkcs12KeyStoreAsKeyStore);
    parcPkcs12KeyStore_Release(&publicKeyStore);

    PARCCryptoHash *cert_digest = parcKeyStore_GetCertificateDigest(keyStore);
    assertNotNull(cert_digest, "got null public key digest for external pkcs12");

    size_t bb_length = parcBuffer_Remaining(parcCryptoHash_GetDigest(cert_digest));
    assertTrue(bb_length == SHA256_DIGEST_LENGTH,
               "Incorrect digest length returned from GetPublicKeyDigest: %zu", bb_length);

    parcKeyStore_Release(&keyStore);
    parcCryptoHash_Release(&cert_digest);
}
Пример #5
0
LONGBOW_TEST_CASE(Global, parcSigner_CreatePublicKey)
{
    _MockSigner *mock = _createSigner();
    PARCSigner *signer = parcSigner_Create(mock, _MockSignerInterface);

    PARCKey *key = parcSigner_CreatePublicKey(signer);

    // Compute the real value
    PARCCryptoHash *hash = parcKeyStore_GetVerifierKeyDigest(mock->keyStore);
    PARCKeyId *keyid = parcKeyId_Create(parcCryptoHash_GetDigest(hash));
    PARCBuffer *derEncodedKey = parcKeyStore_GetDEREncodedPublicKey(mock->keyStore);

    PARCKey *expectedKey = parcKey_CreateFromDerEncodedPublicKey(keyid,
                           parcSigner_GetSigningAlgorithm(signer),
                           derEncodedKey);

    parcBuffer_Release(&derEncodedKey);
    parcKeyId_Release(&keyid);

    parcCryptoHash_Release(&hash);

    assertTrue(parcKey_Equals(key, expectedKey), "Expected public keys to be computed equally.");

    parcKey_Release(&key);
    parcKey_Release(&expectedKey);
    parcSigner_Release(&signer);
    _mockSigner_Release(&mock);
}
LONGBOW_TEST_CASE(Global, ccnxWireFormatMessage_HashProtectedRegion)
{
    //                         >1234<
    const char string[] = "Hello dev null\n";

    PARCBuffer *buffer = parcBuffer_Wrap((void *) string, sizeof(string), 0, sizeof(string));
    size_t start = 5;
    size_t length = 4;

    CCNxWireFormatMessage *message = ccnxWireFormatMessage_FromContentObjectPacketType(CCNxTlvDictionary_SchemaVersion_V1, buffer);

    ccnxWireFormatMessage_SetProtectedRegionStart(message, start);
    ccnxWireFormatMessage_SetProtectedRegionLength(message, length);

    PARCCryptoHasher *hasher = parcCryptoHasher_Create(PARCCryptoHashType_SHA256);
    PARCCryptoHash *hash = ccnxWireFormatMessage_HashProtectedRegion(message, hasher);

    // the correctness of the has is tested in _ccnxWireFormatFacadeV1_ComputeHash
    assertNotNull(hash, "Got null hash from a good packet");

    ccnxWireFormatMessage_Release(&message);
    parcCryptoHash_Release(&hash);
    parcBuffer_Release(&buffer);
    parcCryptoHasher_Release(&hasher);
}
Пример #7
0
CCNxInterestPayloadId *
ccnxInterestPayloadId_CreateAsSHA256Hash(const PARCBuffer *data)
{
    CCNxInterestPayloadId *result = parcObject_CreateInstance(CCNxInterestPayloadId);

    PARCCryptoHasher *hasher = parcCryptoHasher_Create(PARCCryptoHashType_SHA256);
    parcCryptoHasher_Init(hasher);
    parcCryptoHasher_UpdateBuffer(hasher, data);
    PARCCryptoHash *hash = parcCryptoHasher_Finalize(hasher);
    parcCryptoHasher_Release(&hasher);

    PARCBuffer *hashData = parcCryptoHash_GetDigest(hash);
    PARCBuffer *codedHash = parcBuffer_Allocate(parcBuffer_Capacity(hashData) + 1);
    parcBuffer_PutUint8(codedHash, CCNxInterestPayloadId_TypeCode_RFC6920_SHA256);
    parcBuffer_PutBuffer(codedHash, hashData);
    parcBuffer_Flip(codedHash);

    result->nameSegment =
        ccnxNameSegment_CreateTypeValue(CCNxNameLabelType_PAYLOADID, codedHash);

    parcBuffer_Release(&codedHash);
    parcCryptoHash_Release(&hash);

    return result;
}
Пример #8
0
LONGBOW_TEST_CASE(Global, parcCryptoHash_CreateFromArray)
{
    int fd = open("test_digest_bytes_128.sha256", O_RDONLY);
    assertFalse(fd < 0, "Could not open %s: %s", "test_digest_bytes_128.sha256", strerror(errno));

    uint8_t scratch[bufferLength];
    ssize_t read_length = read(fd, scratch, bufferLength);

    PARCCryptoHash *hash = parcCryptoHash_CreateFromArray(PARC_HASH_SHA256, scratch, read_length);
    assertNotNull(hash, "Expected to be non null");

    parcCryptoHash_Release(&hash);
    close(fd);
}
static void
_destroy(struct ccnx_signer_interface **interfaceContextPtr)
{
    assertNotNull(interfaceContextPtr, "Got null double pointer");
    assertNotNull(*interfaceContextPtr, "Got null pointer dereference");

    PARCSigningInterface *interface = (PARCSigningInterface *) *interfaceContextPtr;
    _PARCAesSignerFileStore *keystore = (_PARCAesSignerFileStore *) interface->interfaceContext;

    parcBuffer_Release(&keystore->secretKey);
    parcCryptoHash_Release(&(keystore->secretKeyHash));

    parcCryptoHasher_Release(&(keystore->hasher));

    parcMemory_Deallocate((void **) &keystore);
    parcMemory_Deallocate((void **) &interface);
    *interfaceContextPtr = NULL;
}
Пример #10
0
static bool
_parcSymmetricKeySigner_Finalize(PARCSymmetricKeySigner **instancePtr)
{
    assertNotNull(instancePtr, "Parameter must be a non-null pointer to a PARCSymmetricKeySigner pointer.");
    PARCSymmetricKeySigner *signer = (PARCSymmetricKeySigner *) *instancePtr;
    if (signer->secretKeyHash != NULL) {
        parcCryptoHash_Release(&signer->secretKeyHash);
    }
    if (signer->hasher != NULL) {
        parcCryptoHasher_Release(&signer->hasher);
    }

    if (signer->keyStore != NULL) {
        parcSymmetricKeyStore_Release(&signer->keyStore);
    }
    if (signer->generalKeyStore != NULL) {
        parcKeyStore_Release(&signer->generalKeyStore);
    }

    return true;
}
LONGBOW_TEST_CASE(Global, ccnxWireFormatMessage_CreateContentObjectHash)
{
    //                         >1234<
    const char string[] = "Hello dev null\n";

    PARCBuffer *buffer = parcBuffer_Wrap((void *) string, sizeof(string), 0, sizeof(string));

    CCNxWireFormatMessage *message = ccnxWireFormatMessage_FromContentObjectPacketType(CCNxTlvDictionary_SchemaVersion_V1, buffer);

    PARCCryptoHash *hash = ccnxWireFormatMessage_CreateContentObjectHash(message);
    ccnxWireFormatMessage_Release(&message);
    assertNull(hash, "Expect NULL for hash as it hasn't been encoded yet");

    // We need to create a content object that is hashable
    CCNxName *name = ccnxName_CreateFromCString("lci:/test/content");
    CCNxContentObject *contentObject = ccnxContentObject_CreateWithNameAndPayload(name, buffer);
    ccnxName_Release(&name);

    // This next stuff is to force an encode/decode to setup hash extents
    CCNxCodecNetworkBufferIoVec *iovec = ccnxCodecTlvPacket_DictionaryEncode(contentObject, NULL);
    ccnxContentObject_Release(&contentObject);
    PARCBuffer *encodedMessage = _iovecToParcBuffer(iovec);
    ccnxCodecNetworkBufferIoVec_Release(&iovec);
    // Decode
    message = ccnxWireFormatMessage_Create(encodedMessage);
    CCNxTlvDictionary *dictionary = ccnxWireFormatMessage_GetDictionary(message);
    bool success = ccnxCodecTlvPacket_BufferDecode(encodedMessage, dictionary);
    assertTrue(success, "Failed to decode buffer");
    parcBuffer_Release(&encodedMessage);

    hash = ccnxWireFormatMessage_CreateContentObjectHash(message);

    // the correctness of the hash is tested in _ccnxWireFormatFacadeV1_ComputeHash
    assertNotNull(hash, "Got null hash from a good packet");

    ccnxWireFormatMessage_Release(&message);
    parcCryptoHash_Release(&hash);
    parcBuffer_Release(&buffer);
}
Пример #12
0
LONGBOW_TEST_CASE(Global, parcSigner_SignDigest)
{
    _MockSigner *mock = _createSigner();
    PARCSigner *signer = parcSigner_Create(mock, _MockSignerInterface);
    _mockSigner_Release(&mock);

    PARCBuffer *buffer = parcBuffer_Allocate(10);
    PARCCryptoHash *hash = parcCryptoHash_Create(PARCCryptoHashType_SHA256, buffer);
    PARCSignature *signature = parcSigner_SignDigest(signer, hash);

    assertNotNull(signature, "Expected non-NULL PARCSignature");

    PARCBuffer *bits = parcSignature_GetSignature(signature);
    char *bitstring = parcBuffer_ToString(bits);
    char *expectedString = FAKE_SIGNATURE;
    assertTrue(strcmp(bitstring, expectedString) == 0, "Expected the forced signature as output %s, got %s", expectedString, bitstring);
    parcMemory_Deallocate(&bitstring);

    parcCryptoHash_Release(&hash);
    parcBuffer_Release(&buffer);
    parcSignature_Release(&signature);
    parcSigner_Release(&signer);
}