2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-02-24 13:29:41 -08:00
|
|
|
|
|
|
|
#ifdef XP_WIN
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "mar_private.h"
|
|
|
|
#include "mar.h"
|
|
|
|
#include "cryptox.h"
|
|
|
|
|
2014-10-22 18:00:15 -07:00
|
|
|
int
|
|
|
|
mar_read_entire_file(const char * filePath, uint32_t maxSize,
|
|
|
|
/*out*/ const uint8_t * *data,
|
|
|
|
/*out*/ uint32_t *size)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
FILE * f;
|
|
|
|
|
|
|
|
if (!filePath || !data || !size) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
f = fopen(filePath, "rb");
|
|
|
|
if (!f) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = -1;
|
|
|
|
if (!fseeko(f, 0, SEEK_END)) {
|
|
|
|
int64_t fileSize = ftello(f);
|
|
|
|
if (fileSize > 0 && fileSize <= maxSize && !fseeko(f, 0, SEEK_SET)) {
|
|
|
|
unsigned char * fileData;
|
|
|
|
|
|
|
|
*size = (unsigned int) fileSize;
|
|
|
|
fileData = malloc(*size);
|
|
|
|
if (fileData) {
|
|
|
|
if (fread(fileData, *size, 1, f) == 1) {
|
|
|
|
*data = fileData;
|
|
|
|
result = 0;
|
|
|
|
} else {
|
|
|
|
free(fileData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-07 10:34:38 -07:00
|
|
|
fclose(f);
|
|
|
|
|
2014-10-22 18:00:15 -07:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
int mar_extract_and_verify_signatures_fp(FILE *fp,
|
|
|
|
CryptoX_ProviderHandle provider,
|
|
|
|
CryptoX_PublicKey *keys,
|
|
|
|
uint32_t keyCount);
|
|
|
|
int mar_verify_signatures_for_fp(FILE *fp,
|
|
|
|
CryptoX_ProviderHandle provider,
|
|
|
|
CryptoX_PublicKey *keys,
|
|
|
|
const uint8_t * const *extractedSignatures,
|
|
|
|
uint32_t keyCount,
|
|
|
|
uint32_t *numVerified);
|
2012-02-24 13:29:41 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads the specified number of bytes from the file pointer and
|
|
|
|
* stores them in the passed buffer.
|
|
|
|
*
|
|
|
|
* @param fp The file pointer to read from.
|
|
|
|
* @param buffer The buffer to store the read results.
|
|
|
|
* @param size The number of bytes to read, buffer must be
|
|
|
|
* at least of this size.
|
2012-10-17 06:39:44 -07:00
|
|
|
* @param ctxs Pointer to the first element in an array of verify context.
|
|
|
|
* @param count The number of elements in ctxs
|
2012-02-24 13:29:41 -08:00
|
|
|
* @param err The name of what is being written to in case of error.
|
|
|
|
* @return 0 on success
|
|
|
|
* -1 on read error
|
|
|
|
* -2 on verify update error
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ReadAndUpdateVerifyContext(FILE *fp,
|
|
|
|
void *buffer,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t size,
|
2012-10-17 06:39:44 -07:00
|
|
|
CryptoX_SignatureHandle *ctxs,
|
|
|
|
uint32_t count,
|
2012-02-24 13:29:41 -08:00
|
|
|
const char *err)
|
|
|
|
{
|
2012-10-17 06:39:44 -07:00
|
|
|
uint32_t k;
|
|
|
|
if (!fp || !buffer || !ctxs || count == 0 || !err) {
|
2012-02-24 13:29:41 -08:00
|
|
|
fprintf(stderr, "ERROR: Invalid parameter specified.\n");
|
|
|
|
return CryptoX_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!size) {
|
|
|
|
return CryptoX_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fread(buffer, size, 1, fp) != 1) {
|
|
|
|
fprintf(stderr, "ERROR: Could not read %s\n", err);
|
|
|
|
return CryptoX_Error;
|
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
for (k = 0; k < count; k++) {
|
|
|
|
if (CryptoX_Failed(CryptoX_VerifyUpdate(&ctxs[k], buffer, size))) {
|
|
|
|
fprintf(stderr, "ERROR: Could not update verify context for %s\n", err);
|
|
|
|
return -2;
|
|
|
|
}
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
return CryptoX_Success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-10-17 06:39:44 -07:00
|
|
|
* Verifies a MAR file by verifying each signature with the corresponding
|
|
|
|
* certificate. That is, the first signature will be verified using the first
|
|
|
|
* certificate given, the second signature will be verified using the second
|
|
|
|
* certificate given, etc. The signature count must exactly match the number of
|
|
|
|
* certificates given, and all signature verifications must succeed.
|
2012-02-24 13:29:41 -08:00
|
|
|
*
|
2014-10-22 18:00:15 -07:00
|
|
|
* @param mar The file who's signature should be calculated
|
2012-10-17 06:39:44 -07:00
|
|
|
* @param certData Pointer to the first element in an array of
|
|
|
|
* certificate data
|
|
|
|
* @param certDataSizes Pointer to the first element in an array for size of
|
|
|
|
* the data stored
|
|
|
|
* @param certCount The number of elements in certData and certDataSizes
|
2012-02-24 13:29:41 -08:00
|
|
|
* @return 0 on success
|
|
|
|
*/
|
|
|
|
int
|
2014-10-22 18:00:15 -07:00
|
|
|
mar_verify_signatures(MarFile *mar,
|
|
|
|
const uint8_t * const *certData,
|
|
|
|
const uint32_t *certDataSizes,
|
|
|
|
uint32_t certCount) {
|
2012-10-17 06:39:44 -07:00
|
|
|
int rv = -1;
|
2012-02-24 13:29:41 -08:00
|
|
|
CryptoX_ProviderHandle provider = CryptoX_InvalidHandleValue;
|
2012-10-17 06:39:44 -07:00
|
|
|
CryptoX_PublicKey keys[MAX_SIGNATURES];
|
|
|
|
uint32_t k;
|
2012-02-24 13:29:41 -08:00
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
memset(keys, 0, sizeof(keys));
|
|
|
|
|
|
|
|
if (!mar || !certData || !certDataSizes || certCount == 0) {
|
2012-02-24 13:29:41 -08:00
|
|
|
fprintf(stderr, "ERROR: Invalid parameter specified.\n");
|
2012-10-17 06:39:44 -07:00
|
|
|
goto failure;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mar->fp) {
|
|
|
|
fprintf(stderr, "ERROR: MAR file is not open.\n");
|
2012-10-17 06:39:44 -07:00
|
|
|
goto failure;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (CryptoX_Failed(CryptoX_InitCryptoProvider(&provider))) {
|
|
|
|
fprintf(stderr, "ERROR: Could not init crytpo library.\n");
|
2012-10-17 06:39:44 -07:00
|
|
|
goto failure;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
for (k = 0; k < certCount; ++k) {
|
|
|
|
if (CryptoX_Failed(CryptoX_LoadPublicKey(provider, certData[k], certDataSizes[k],
|
2014-10-22 18:00:15 -07:00
|
|
|
&keys[k]))) {
|
2012-10-17 06:39:44 -07:00
|
|
|
fprintf(stderr, "ERROR: Could not load public key.\n");
|
|
|
|
goto failure;
|
|
|
|
}
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
rv = mar_extract_and_verify_signatures_fp(mar->fp, provider, keys, certCount);
|
|
|
|
|
|
|
|
failure:
|
2012-02-24 13:29:41 -08:00
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
for (k = 0; k < certCount; ++k) {
|
|
|
|
if (keys[k]) {
|
|
|
|
CryptoX_FreePublicKey(&keys[k]);
|
|
|
|
}
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
2012-10-17 06:39:44 -07:00
|
|
|
|
2012-02-24 13:29:41 -08:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-10-17 06:39:44 -07:00
|
|
|
* Extracts each signature from the specified MAR file,
|
|
|
|
* then calls mar_verify_signatures_for_fp to verify each signature.
|
|
|
|
*
|
2012-02-24 13:29:41 -08:00
|
|
|
* @param fp An opened MAR file handle
|
|
|
|
* @param provider A library provider
|
2012-10-17 06:39:44 -07:00
|
|
|
* @param keys The public keys to use to verify the MAR
|
|
|
|
* @param keyCount The number of keys pointed to by keys
|
2012-02-24 13:29:41 -08:00
|
|
|
* @return 0 on success
|
|
|
|
*/
|
|
|
|
int
|
2012-10-17 06:39:44 -07:00
|
|
|
mar_extract_and_verify_signatures_fp(FILE *fp,
|
|
|
|
CryptoX_ProviderHandle provider,
|
|
|
|
CryptoX_PublicKey *keys,
|
|
|
|
uint32_t keyCount) {
|
2012-02-24 13:29:41 -08:00
|
|
|
char buf[5] = {0};
|
2012-10-17 06:39:44 -07:00
|
|
|
uint32_t signatureCount, signatureLen, numVerified = 0;
|
|
|
|
uint32_t signatureAlgorithmIDs[MAX_SIGNATURES];
|
2012-02-24 13:29:41 -08:00
|
|
|
int rv = -1;
|
2012-08-22 08:56:38 -07:00
|
|
|
int64_t curPos;
|
2012-10-17 06:39:44 -07:00
|
|
|
uint8_t *extractedSignatures[MAX_SIGNATURES];
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2012-02-24 13:29:41 -08:00
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
memset(signatureAlgorithmIDs, 0, sizeof(signatureAlgorithmIDs));
|
|
|
|
memset(extractedSignatures, 0, sizeof(extractedSignatures));
|
|
|
|
|
2012-02-24 13:29:41 -08:00
|
|
|
if (!fp) {
|
|
|
|
fprintf(stderr, "ERROR: Invalid file pointer passed.\n");
|
|
|
|
return CryptoX_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* To protect against invalid MAR files, we assumes that the MAR file
|
|
|
|
size is less than or equal to MAX_SIZE_OF_MAR_FILE. */
|
|
|
|
if (fseeko(fp, 0, SEEK_END)) {
|
|
|
|
fprintf(stderr, "ERROR: Could not seek to the end of the MAR file.\n");
|
|
|
|
return CryptoX_Error;
|
|
|
|
}
|
|
|
|
if (ftello(fp) > MAX_SIZE_OF_MAR_FILE) {
|
|
|
|
fprintf(stderr, "ERROR: MAR file is too large to be verified.\n");
|
|
|
|
return CryptoX_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip to the start of the signature block */
|
|
|
|
if (fseeko(fp, SIGNATURE_BLOCK_OFFSET, SEEK_SET)) {
|
|
|
|
fprintf(stderr, "ERROR: Could not seek to the signature block.\n");
|
|
|
|
return CryptoX_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the number of signatures */
|
|
|
|
if (fread(&signatureCount, sizeof(signatureCount), 1, fp) != 1) {
|
|
|
|
fprintf(stderr, "ERROR: Could not read number of signatures.\n");
|
|
|
|
return CryptoX_Error;
|
|
|
|
}
|
|
|
|
signatureCount = ntohl(signatureCount);
|
|
|
|
|
|
|
|
/* Check that we have less than the max amount of signatures so we don't
|
|
|
|
waste too much of either updater's or signmar's time. */
|
|
|
|
if (signatureCount > MAX_SIGNATURES) {
|
|
|
|
fprintf(stderr, "ERROR: At most %d signatures can be specified.\n",
|
|
|
|
MAX_SIGNATURES);
|
|
|
|
return CryptoX_Error;
|
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
for (i = 0; i < signatureCount; i++) {
|
2012-02-24 13:29:41 -08:00
|
|
|
/* Get the signature algorithm ID */
|
2012-10-17 06:39:44 -07:00
|
|
|
if (fread(&signatureAlgorithmIDs[i], sizeof(uint32_t), 1, fp) != 1) {
|
2012-02-24 13:29:41 -08:00
|
|
|
fprintf(stderr, "ERROR: Could not read signatures algorithm ID.\n");
|
|
|
|
return CryptoX_Error;
|
|
|
|
}
|
2012-10-17 06:39:44 -07:00
|
|
|
signatureAlgorithmIDs[i] = ntohl(signatureAlgorithmIDs[i]);
|
2012-02-24 13:29:41 -08:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if (fread(&signatureLen, sizeof(uint32_t), 1, fp) != 1) {
|
2012-02-24 13:29:41 -08:00
|
|
|
fprintf(stderr, "ERROR: Could not read signatures length.\n");
|
|
|
|
return CryptoX_Error;
|
|
|
|
}
|
|
|
|
signatureLen = ntohl(signatureLen);
|
|
|
|
|
|
|
|
/* To protected against invalid input make sure the signature length
|
|
|
|
isn't too big. */
|
|
|
|
if (signatureLen > MAX_SIGNATURE_LENGTH) {
|
|
|
|
fprintf(stderr, "ERROR: Signature length is too large to verify.\n");
|
|
|
|
return CryptoX_Error;
|
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
extractedSignatures[i] = malloc(signatureLen);
|
|
|
|
if (!extractedSignatures[i]) {
|
2012-02-24 13:29:41 -08:00
|
|
|
fprintf(stderr, "ERROR: Could allocate buffer for signature.\n");
|
|
|
|
return CryptoX_Error;
|
|
|
|
}
|
2012-10-17 06:39:44 -07:00
|
|
|
if (fread(extractedSignatures[i], signatureLen, 1, fp) != 1) {
|
2012-02-24 13:29:41 -08:00
|
|
|
fprintf(stderr, "ERROR: Could not read extracted signature.\n");
|
2012-10-17 06:39:44 -07:00
|
|
|
for (i = 0; i < signatureCount; ++i) {
|
|
|
|
free(extractedSignatures[i]);
|
|
|
|
}
|
2012-02-24 13:29:41 -08:00
|
|
|
return CryptoX_Error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't try to verify signatures we don't know about */
|
2012-10-17 06:39:44 -07:00
|
|
|
if (signatureAlgorithmIDs[i] != 1) {
|
|
|
|
fprintf(stderr, "ERROR: Unknown signature algorithm ID.\n");
|
|
|
|
for (i = 0; i < signatureCount; ++i) {
|
|
|
|
free(extractedSignatures[i]);
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
2012-10-17 06:39:44 -07:00
|
|
|
return CryptoX_Error;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
curPos = ftello(fp);
|
|
|
|
rv = mar_verify_signatures_for_fp(fp,
|
|
|
|
provider,
|
|
|
|
keys,
|
|
|
|
(const uint8_t * const *)extractedSignatures,
|
|
|
|
signatureCount,
|
|
|
|
&numVerified);
|
|
|
|
for (i = 0; i < signatureCount; ++i) {
|
|
|
|
free(extractedSignatures[i]);
|
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:42 -07:00
|
|
|
/* If we reached here and we verified every
|
2012-02-24 13:29:41 -08:00
|
|
|
signature, return success. */
|
2012-10-17 06:39:44 -07:00
|
|
|
if (numVerified == signatureCount && keyCount == numVerified) {
|
2012-02-24 13:29:41 -08:00
|
|
|
return CryptoX_Success;
|
2012-10-17 06:39:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (numVerified == 0) {
|
2012-10-17 06:39:42 -07:00
|
|
|
fprintf(stderr, "ERROR: Not all signatures were verified.\n");
|
2012-10-17 06:39:44 -07:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "ERROR: Only %d of %d signatures were verified.\n",
|
|
|
|
numVerified, signatureCount);
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
2012-10-17 06:39:44 -07:00
|
|
|
return CryptoX_Error;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-10-17 06:39:44 -07:00
|
|
|
* Verifies a MAR file by verifying each signature with the corresponding
|
|
|
|
* certificate. That is, the first signature will be verified using the first
|
|
|
|
* certificate given, the second signature will be verified using the second
|
|
|
|
* certificate given, etc. The signature count must exactly match the number of
|
|
|
|
* certificates given, and all signature verifications must succeed.
|
2012-02-24 13:29:41 -08:00
|
|
|
*
|
|
|
|
* @param fp An opened MAR file handle
|
|
|
|
* @param provider A library provider
|
2012-10-17 06:39:44 -07:00
|
|
|
* @param keys A pointer to the first element in an
|
|
|
|
* array of keys.
|
|
|
|
* @param extractedSignatures Pointer to the first element in an array
|
|
|
|
* of extracted signatures.
|
|
|
|
* @param signatureCount The number of signatures in the MAR file
|
|
|
|
* @param numVerified Out parameter which will be filled with
|
|
|
|
* the number of verified signatures.
|
|
|
|
* This information can be useful for printing
|
|
|
|
* error messages.
|
|
|
|
* @return 0 on success, *numVerified == signatureCount.
|
2012-02-24 13:29:41 -08:00
|
|
|
*/
|
|
|
|
int
|
2012-10-17 06:39:44 -07:00
|
|
|
mar_verify_signatures_for_fp(FILE *fp,
|
|
|
|
CryptoX_ProviderHandle provider,
|
|
|
|
CryptoX_PublicKey *keys,
|
|
|
|
const uint8_t * const *extractedSignatures,
|
|
|
|
uint32_t signatureCount,
|
|
|
|
uint32_t *numVerified)
|
|
|
|
{
|
|
|
|
CryptoX_SignatureHandle signatureHandles[MAX_SIGNATURES];
|
2012-02-24 13:29:41 -08:00
|
|
|
char buf[BLOCKSIZE];
|
2012-10-17 06:39:44 -07:00
|
|
|
uint32_t signatureLengths[MAX_SIGNATURES];
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t i;
|
2012-10-17 06:39:44 -07:00
|
|
|
int rv = CryptoX_Error;
|
2012-02-24 13:29:41 -08:00
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
memset(signatureHandles, 0, sizeof(signatureHandles));
|
|
|
|
memset(signatureLengths, 0, sizeof(signatureLengths));
|
|
|
|
|
|
|
|
if (!extractedSignatures || !numVerified) {
|
2012-02-24 13:29:41 -08:00
|
|
|
fprintf(stderr, "ERROR: Invalid parameter specified.\n");
|
2012-10-17 06:39:44 -07:00
|
|
|
goto failure;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
*numVerified = 0;
|
|
|
|
|
2012-02-24 13:29:41 -08:00
|
|
|
/* This function is only called when we have at least one signature,
|
|
|
|
but to protected against future people who call this function we
|
|
|
|
make sure a non zero value is passed in.
|
|
|
|
*/
|
|
|
|
if (!signatureCount) {
|
|
|
|
fprintf(stderr, "ERROR: There must be at least one signature.\n");
|
2012-10-17 06:39:44 -07:00
|
|
|
goto failure;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
for (i = 0; i < signatureCount; i++) {
|
|
|
|
if (CryptoX_Failed(CryptoX_VerifyBegin(provider,
|
|
|
|
&signatureHandles[i], &keys[i]))) {
|
|
|
|
fprintf(stderr, "ERROR: Could not initialize signature handle.\n");
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
}
|
2012-02-24 13:29:41 -08:00
|
|
|
|
|
|
|
/* Skip to the start of the file */
|
|
|
|
if (fseeko(fp, 0, SEEK_SET)) {
|
|
|
|
fprintf(stderr, "ERROR: Could not seek to start of the file\n");
|
2012-10-17 06:39:44 -07:00
|
|
|
goto failure;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Bytes 0-3: MAR1
|
|
|
|
Bytes 4-7: index offset
|
|
|
|
Bytes 8-15: size of entire MAR
|
|
|
|
*/
|
|
|
|
if (CryptoX_Failed(ReadAndUpdateVerifyContext(fp, buf,
|
|
|
|
SIGNATURE_BLOCK_OFFSET +
|
2012-08-22 08:56:38 -07:00
|
|
|
sizeof(uint32_t),
|
2012-10-17 06:39:44 -07:00
|
|
|
signatureHandles,
|
|
|
|
signatureCount,
|
2012-02-24 13:29:41 -08:00
|
|
|
"signature block"))) {
|
2012-10-17 06:39:44 -07:00
|
|
|
goto failure;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
/* Read the signature block */
|
2012-02-24 13:29:41 -08:00
|
|
|
for (i = 0; i < signatureCount; i++) {
|
|
|
|
/* Get the signature algorithm ID */
|
|
|
|
if (CryptoX_Failed(ReadAndUpdateVerifyContext(fp,
|
|
|
|
&buf,
|
2012-08-22 08:56:38 -07:00
|
|
|
sizeof(uint32_t),
|
2012-10-17 06:39:44 -07:00
|
|
|
signatureHandles,
|
|
|
|
signatureCount,
|
2012-02-24 13:29:41 -08:00
|
|
|
"signature algorithm ID"))) {
|
2012-10-17 06:39:44 -07:00
|
|
|
goto failure;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (CryptoX_Failed(ReadAndUpdateVerifyContext(fp,
|
2012-10-17 06:39:44 -07:00
|
|
|
&signatureLengths[i],
|
2012-08-22 08:56:38 -07:00
|
|
|
sizeof(uint32_t),
|
2012-10-17 06:39:44 -07:00
|
|
|
signatureHandles,
|
|
|
|
signatureCount,
|
2012-02-24 13:29:41 -08:00
|
|
|
"signature length"))) {
|
2012-10-17 06:39:44 -07:00
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
signatureLengths[i] = ntohl(signatureLengths[i]);
|
|
|
|
if (signatureLengths[i] > MAX_SIGNATURE_LENGTH) {
|
|
|
|
fprintf(stderr, "ERROR: Embedded signature length is too large.\n");
|
|
|
|
goto failure;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip past the signature itself as those are not included */
|
2012-10-17 06:39:44 -07:00
|
|
|
if (fseeko(fp, signatureLengths[i], SEEK_CUR)) {
|
2012-02-24 13:29:41 -08:00
|
|
|
fprintf(stderr, "ERROR: Could not seek past signature.\n");
|
2012-10-17 06:39:44 -07:00
|
|
|
goto failure;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
/* Read the rest of the file after the signature block */
|
2012-02-24 13:29:41 -08:00
|
|
|
while (!feof(fp)) {
|
|
|
|
int numRead = fread(buf, 1, BLOCKSIZE , fp);
|
|
|
|
if (ferror(fp)) {
|
|
|
|
fprintf(stderr, "ERROR: Error reading data block.\n");
|
2012-10-17 06:39:44 -07:00
|
|
|
goto failure;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
for (i = 0; i < signatureCount; i++) {
|
|
|
|
if (CryptoX_Failed(CryptoX_VerifyUpdate(&signatureHandles[i],
|
|
|
|
buf, numRead))) {
|
|
|
|
fprintf(stderr, "ERROR: Error updating verify context with"
|
|
|
|
" data block.\n");
|
|
|
|
goto failure;
|
|
|
|
}
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
/* Verify the signatures */
|
|
|
|
for (i = 0; i < signatureCount; i++) {
|
|
|
|
if (CryptoX_Failed(CryptoX_VerifySignature(&signatureHandles[i],
|
|
|
|
&keys[i],
|
|
|
|
extractedSignatures[i],
|
|
|
|
signatureLengths[i]))) {
|
|
|
|
fprintf(stderr, "ERROR: Error verifying signature.\n");
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
++*numVerified;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|
|
|
|
|
2012-10-17 06:39:44 -07:00
|
|
|
rv = CryptoX_Success;
|
|
|
|
failure:
|
|
|
|
for (i = 0; i < signatureCount; i++) {
|
2014-04-22 17:45:59 -07:00
|
|
|
CryptoX_FreeSignatureHandle(&signatureHandles[i]);
|
2012-10-17 06:39:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2012-02-24 13:29:41 -08:00
|
|
|
}
|