6#ifndef WIN32_LEAN_AND_MEAN
7#define WIN32_LEAN_AND_MEAN
23 const uint8_t * *data,
39 if (!fseeko(f, 0, SEEK_END)) {
40 int64_t fileSize = ftello(f);
41 if (fileSize > 0 && fileSize <= maxSize && !fseeko(f, 0, SEEK_SET)) {
42 unsigned char * fileData;
44 *
size = (
unsigned int) fileSize;
45 fileData = malloc(*
size);
47 if (fread(fileData, *
size, 1, f) == 1) {
69 const uint8_t *
const *extractedSignatures,
71 uint32_t *numVerified);
97 if (!fp || !buffer || !ctxs ||
count == 0 || !
err) {
98 fprintf(stderr,
"ERROR: Invalid parameter specified.\n");
106 if (fread(buffer,
size, 1, fp) != 1) {
107 fprintf(stderr,
"ERROR: Could not read %s\n",
err);
111 for (k = 0; k <
count; k++) {
113 fprintf(stderr,
"ERROR: Could not update verify context for %s\n",
err);
137 const uint8_t *
const *certData,
138 const uint32_t *certDataSizes,
139 uint32_t certCount) {
145 memset(keys, 0,
sizeof(keys));
147 if (!mar || !certData || !certDataSizes || certCount == 0) {
148 fprintf(stderr,
"ERROR: Invalid parameter specified.\n");
153 fprintf(stderr,
"ERROR: MAR file is not open.\n");
158 fprintf(stderr,
"ERROR: Could not init crypto library.\n");
162 for (k = 0; k < certCount; ++k) {
165 fprintf(stderr,
"ERROR: Could not load public key.\n");
174 for (k = 0; k < certCount; ++k) {
198 uint32_t signatureCount, signatureLen, numVerified = 0;
204 memset(signatureAlgorithmIDs, 0,
sizeof(signatureAlgorithmIDs));
205 memset(extractedSignatures, 0,
sizeof(extractedSignatures));
208 fprintf(stderr,
"ERROR: Invalid file pointer passed.\n");
214 if (fseeko(fp, 0, SEEK_END)) {
215 fprintf(stderr,
"ERROR: Could not seek to the end of the MAR file.\n");
219 fprintf(stderr,
"ERROR: MAR file is too large to be verified.\n");
225 fprintf(stderr,
"ERROR: Could not seek to the signature block.\n");
230 if (fread(&signatureCount,
sizeof(signatureCount), 1, fp) != 1) {
231 fprintf(stderr,
"ERROR: Could not read number of signatures.\n");
234 signatureCount =
ntohl(signatureCount);
239 fprintf(stderr,
"ERROR: At most %d signatures can be specified.\n",
244 for (
i = 0;
i < signatureCount;
i++) {
246 if (fread(&signatureAlgorithmIDs[
i],
sizeof(uint32_t), 1, fp) != 1) {
247 fprintf(stderr,
"ERROR: Could not read signatures algorithm ID.\n");
250 signatureAlgorithmIDs[
i] =
ntohl(signatureAlgorithmIDs[
i]);
252 if (fread(&signatureLen,
sizeof(uint32_t), 1, fp) != 1) {
253 fprintf(stderr,
"ERROR: Could not read signatures length.\n");
256 signatureLen =
ntohl(signatureLen);
261 fprintf(stderr,
"ERROR: Signature length is too large to verify.\n");
265 extractedSignatures[
i] = malloc(signatureLen);
266 if (!extractedSignatures[
i]) {
267 fprintf(stderr,
"ERROR: Could allocate buffer for signature.\n");
270 if (fread(extractedSignatures[
i], signatureLen, 1, fp) != 1) {
271 fprintf(stderr,
"ERROR: Could not read extracted signature.\n");
272 for (
i = 0;
i < signatureCount; ++
i) {
273 free(extractedSignatures[
i]);
279 if (signatureAlgorithmIDs[
i] != 1) {
280 fprintf(stderr,
"ERROR: Unknown signature algorithm ID.\n");
281 for (
i = 0;
i < signatureCount; ++
i) {
282 free(extractedSignatures[
i]);
291 (
const uint8_t *
const *)extractedSignatures,
294 for (
i = 0;
i < signatureCount; ++
i) {
295 free(extractedSignatures[
i]);
300 if (numVerified == signatureCount && keyCount == numVerified) {
301 assert(rv == 0); (void) rv;
305 if (numVerified == 0) {
306 fprintf(stderr,
"ERROR: Not all signatures were verified.\n");
308 fprintf(stderr,
"ERROR: Only %d of %d signatures were verified.\n",
309 numVerified, signatureCount);
338 const uint8_t *
const *extractedSignatures,
339 uint32_t signatureCount,
340 uint32_t *numVerified)
348 (void) provider; (void) keys;
350 memset(signatureHandles, 0,
sizeof(signatureHandles));
351 memset(signatureLengths, 0,
sizeof(signatureLengths));
353 if (!extractedSignatures || !numVerified) {
354 fprintf(stderr,
"ERROR: Invalid parameter specified.\n");
364 if (!signatureCount) {
365 fprintf(stderr,
"ERROR: There must be at least one signature.\n");
369 for (
i = 0;
i < signatureCount;
i++) {
371 &signatureHandles[
i], &keys[
i]))) {
372 fprintf(stderr,
"ERROR: Could not initialize signature handle.\n");
378 if (fseeko(fp, 0, SEEK_SET)) {
379 fprintf(stderr,
"ERROR: Could not seek to start of the file\n");
392 "signature block"))) {
397 for (
i = 0;
i < signatureCount;
i++) {
404 "signature algorithm ID"))) {
409 &signatureLengths[
i],
413 "signature length"))) {
416 signatureLengths[
i] =
ntohl(signatureLengths[
i]);
418 fprintf(stderr,
"ERROR: Embedded signature length is too large.\n");
423 if (fseeko(fp, signatureLengths[
i], SEEK_CUR)) {
424 fprintf(stderr,
"ERROR: Could not seek past signature.\n");
431 int numRead = fread(buf, 1,
BLOCKSIZE , fp);
433 fprintf(stderr,
"ERROR: Error reading data block.\n");
437 for (
i = 0;
i < signatureCount;
i++) {
440 fprintf(stderr,
"ERROR: Error updating verify context with"
448 for (
i = 0;
i < signatureCount;
i++) {
451 extractedSignatures[
i],
452 signatureLengths[
i]))) {
453 fprintf(stderr,
"ERROR: Error verifying signature.\n");
461 for (
i = 0;
i < signatureCount;
i++) {
#define CryptoX_LoadPublicKey(CryptoHandle, certData, dataSize, publicKey)
#define CryptoX_ProviderHandle
#define CryptoX_InvalidHandleValue
#define CryptoX_InitCryptoProvider(CryptoHandle)
#define CryptoX_Failed(X)
#define CryptoX_VerifyBegin(CryptoHandle, SignatureHandle, PublicKey)
#define CryptoX_FreeSignatureHandle(SignatureHandle)
#define CryptoX_VerifyUpdate(SignatureHandle, buf, len)
#define CryptoX_VerifySignature(hash, publicKey, signedData, len)
#define CryptoX_FreePublicKey(key)
#define CryptoX_PublicKey
#define CryptoX_SignatureHandle
#define MAX_SIZE_OF_MAR_FILE
#define SIGNATURE_BLOCK_OFFSET
#define MAX_SIGNATURE_LENGTH
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)
Verifies a MAR file by verifying each signature with the corresponding certificate.
int ReadAndUpdateVerifyContext(FILE *fp, void *buffer, uint32_t size, CryptoX_SignatureHandle *ctxs, uint32_t count, const char *err)
Reads the specified number of bytes from the file pointer and stores them in the passed buffer.
int mar_extract_and_verify_signatures_fp(FILE *fp, CryptoX_ProviderHandle provider, CryptoX_PublicKey *keys, uint32_t keyCount)
Extracts each signature from the specified MAR file, then calls mar_verify_signatures_for_fp to verif...
int mar_read_entire_file(const char *filePath, uint32_t maxSize, const uint8_t **data, uint32_t *size)
int mar_verify_signatures(MarFile *mar, const uint8_t *const *certData, const uint32_t *certDataSizes, uint32_t certCount)
Verifies a MAR file by verifying each signature with the corresponding certificate.
sal_uInt32 ntohl(sal_uInt32 n)
const wchar_t *typedef int(__stdcall *DllNativeUnregProc)(int