29 #include <openssl/evp.h> 40 #define GET_16BITS_FROM_ARRAY(X, Y) ((X[Y] << 8) | X[Y + 1]) 90 static void (*VerificationCallback)(void) = NULL;
100 static void HashLoadFailCallback(
void *userData );
101 static void HashLoadedCallback(
void *userData,
S_CONTENT *content );
102 static void FreeDigestList(
HashDigest *listStart);
109 static void SigLoadedCallback(
void *userData,
S_CONTENT *content );
110 static void SigLoadFailCallback(
void *userData );
113 static void CertCallback(
void);
130 VerificationCallback = callback;
144 listEntry = hashfileList;
150 nextEntry = listEntry->
next;
153 if (listEntry->
url != NULL)
160 if (hashData->
data != &noFile && hashData->
data != NULL)
171 listEntry = nextEntry;
192 hashfileUrl = GetHashfileUrl(url);
193 if (hashfileUrl == NULL)
198 hashFile = FindHashInfo(hashfileUrl);
199 if (hashFile != NULL)
201 switch (hashFile->
state)
204 success = ReloadHashfile(hashFile);
211 status = GetFailureStatus(hashFile);
225 status = VerifyUrl(url, content, length, hashFile);
228 status = GetFailureStatus(hashFile);
240 success = LoadHashfile(hashfileUrl);
286 status = GetFailureStatus(hashFile);
290 success = CheckUrlPath(url, hashFile);
298 digest = FindDigestForUrl(url, hashFile);
301 success = CheckHashes(content, length, digest);
323 listEntry = hashfileList;
325 while (listEntry != NULL)
327 differ = strcmp((
char *)url, (
char *)listEntry->
url);
334 listEntry = listEntry->
next;
346 static U8BIT* GetHashfileUrl(
const U8BIT *url)
352 U32BIT hashfileNameLen = strlen((
char *)hashfileName);
354 slash = (
U8BIT *)strrchr((
char *)
url,
'/');
355 assert(slash != NULL);
356 length = slash - url + 1;
358 hashfileUrl =
MHEG5getMem(length + hashfileNameLen + 1);
359 if (hashfileUrl != NULL)
361 memcpy(hashfileUrl, url, length);
362 memcpy(hashfileUrl + length, hashfileName, hashfileNameLen);
363 hashfileUrl[length + hashfileNameLen] =
'\0';
381 hashName.
len = strlen((
char *)hashfile->
url);
385 (
void *)hashfile, HashLoadedCallback, HashLoadFailCallback );
387 success = LoadNextSignatureFile(hashfile);
420 if (listEntry != NULL)
436 listEntry->
next = hashfileList;
438 hashfileList = listEntry;
443 hashName.
len = strlen((
char *)url);
447 (
void *)listEntry, HashLoadedCallback, HashLoadFailCallback);
449 success = LoadNextSignatureFile(listEntry);
460 static void HashLoadedCallback(
void *userData,
S_CONTENT *content )
471 listEntry = hashfileList;
475 while (listEntry != NULL)
477 if (listEntry == cacheHandle)
481 listEntry = listEntry->
next;
484 if (listEntry != NULL)
486 currentFile = &cacheHandle->
hashData;
490 if (fileBuffer.
data != NULL)
493 fileBuffer.
len = content->
size;
495 if (fileBuffer.
len != currentFile->
len)
499 else if (currentFile->
data == NULL || currentFile->
data == &noFile)
505 differ = memcmp(fileBuffer.
data, currentFile->
data, content->
size);
517 if (currentFile->
data != NULL && currentFile->
data != &noFile)
526 currentFile->
data = fileBuffer.
data;
527 currentFile->
len = fileBuffer.
len;
529 success = ParseHashfile(cacheHandle);
539 VerifyHashfile(cacheHandle);
579 if (call && VerificationCallback != NULL)
581 VerificationCallback();
590 static void HashLoadFailCallback(
void *userData )
602 listEntry = hashfileList;
603 while (listEntry != NULL)
605 if (listEntry == cacheHandle)
609 listEntry = listEntry->
next;
612 if (listEntry != NULL)
617 if (hashData->
data != NULL)
619 if (hashData->
data == &noFile)
627 hashData->
data = &noFile;
634 hashData->
data = &noFile;
650 if (call && VerificationCallback != NULL)
652 VerificationCallback();
680 if (hashfile != NULL && hashfile->
hashData.
data != &noFile)
684 dataEnd = fileData + fileSize;
712 if (digestCount == 0)
714 if (fileData + 2 <= dataEnd)
732 previousDigest = NULL;
741 if (fileData + 1 <= dataEnd)
776 if (fileData + 2 <= dataEnd)
785 if (continueParsing && nameCount != 1)
793 if (fileData + 1 <= dataEnd)
795 nameLength = *fileData;
806 if (fileData + nameLength <= dataEnd)
819 if (fileData + digestLength <= dataEnd)
855 static void FreeDigestList(
HashDigest *listStart)
860 while (current != NULL)
862 next = current->
next;
879 hashfileUrl = GetHashfileUrl(url);
880 if (hashfileUrl != NULL)
882 hashfile = FindHashInfo(hashfileUrl);
887 if (hashfile != NULL)
924 name = (
U8BIT *)strrchr((
char *)
url,
'/') + 1;
925 assert(name != NULL);
926 nameLength = strlen((
char *)name);
929 while (digest != NULL)
939 digest = digest->
next;
954 U16BIT urlDirectoryLength;
955 U8BIT *hashDirectory;
956 U16BIT hashDirectoryLength;
970 slash = (
U8BIT *)strrchr((
char *)
url,
'/');
971 urlDirectoryLength = slash -
url;
975 if (hashDirectoryLength == 0 || hashDirectoryLength > urlDirectoryLength)
982 differ =
MHEG5strncmp(hashDirectory, url, hashDirectoryLength );
990 if (hashDirectory[hashDirectoryLength - 1] !=
'/' &&
991 url[hashDirectoryLength] !=
'/')
1012 U8BIT contentDigest[EVP_MAX_MD_SIZE];
1017 switch (digest->
type)
1026 EVP_DigestInit(&ctx, EVP_sha1());
1027 EVP_DigestUpdate(&ctx, content, contentLength);
1028 digestSize = EVP_MD_CTX_size(&ctx);
1029 EVP_DigestFinal(&ctx, contentDigest, NULL);
1032 differ = memcmp(contentDigest, digest->
digest, digestSize);
1053 prefix[4] = (
U8BIT)(contentLength >> 24);
1054 prefix[5] = (
U8BIT)(contentLength >> 16);
1055 prefix[6] = (
U8BIT)(contentLength >> 8);
1056 prefix[7] = (
U8BIT)contentLength;
1059 EVP_DigestInit(&ctx, EVP_sha1());
1060 EVP_DigestUpdate(&ctx, prefix, 8);
1061 EVP_DigestUpdate(&ctx, content, contentLength);
1062 digestSize = EVP_MD_CTX_size(&ctx);
1063 EVP_DigestFinal(&ctx, contentDigest, NULL);
1066 differ = memcmp(contentDigest, digest->
digest, digestSize);
1087 static void SigLoadFailCallback(
void *userData )
1094 listEntry = hashfileList;
1097 while (listEntry != NULL)
1099 if (listEntry == cacheHandle)
1103 listEntry = listEntry->
next;
1105 if (listEntry != NULL)
1120 if (call && VerificationCallback)
1122 VerificationCallback();
1130 static void SigLoadedCallback(
void *userData,
S_CONTENT *content )
1138 listEntry = hashfileList;
1141 while (listEntry != NULL)
1143 if (listEntry == cacheHandle)
1147 listEntry = listEntry->
next;
1150 if (listEntry != NULL)
1155 if (currentFile->
data)
1158 currentFile->
data = NULL;
1163 if (currentFile->
data != NULL)
1165 currentFile->
len = content->
size;
1166 memcpy(currentFile->
data, content->
data, content->
size);
1170 VerifyHashfile(cacheHandle);
1181 currentFile->
len = 0;
1194 if (call && VerificationCallback)
1196 VerificationCallback();
1217 if (VerificationCallback)
1219 VerificationCallback();
1224 LoadNextSignatureFile(hashfile);
1253 sigUrl = GetNextSigUrl(hashfile);
1256 sigName.
data = sigUrl;
1257 sigName.
len = strlen((
char *)sigUrl);
1259 (
void *)hashfile, SigLoadedCallback, SigLoadFailCallback );
1289 sigName = (
U8BIT *)
"auth.sig.";
1290 sigNameLen = strlen((
char *)sigName);
1292 slash = (
U8BIT *)strrchr((
char *)hashfile->
url,
'/');
1293 assert(slash != NULL);
1294 length = slash - hashfile->
url + 1;
1299 memcpy(sigUrl, hashfile->
url, length);
1300 memcpy(sigUrl + length, sigName, sigNameLen);
1304 sprintf((
char *)(sigUrl + length + sigNameLen),
"%u", (
unsigned int)hashfile->
signatureIndex);
1316 static void CertCallback(
void)
1320 cacheEntry = hashfileList;
1321 while (cacheEntry != NULL)
1325 VerifyHashfile(cacheEntry);
1327 cacheEntry = cacheEntry->
next;
Functions relating to HTTPS Signature Certificates.
struct sHashfileData * next
void MHEG5ResetVerification(void)
Reset the state of the verification subsystem.
void MHEG5SetVerificationCallback(void(*callback)(void))
Register a callback to be fired when the state of the verification file changes.
void MHEG5ResetHashFile(U8BIT *url)
Reset the state and clear cache of the hashfile associated with a content file.
struct sHashDigest * next
MHEG5ContentVerification_t MHEG5IsContentVerified(U8BIT *url, U8BIT *content, U32BIT length)
void * MHEG5FileOrmGet(MHEG5String name, U16BIT priority, void *userData, F_CB_Good cbGood, F_CB_Fail cbFail)
Get a file. The file will be loaded and one of the callback functions called when request is resolved...
#define FRP_CACHE_DEFAULT
struct sHashfileData HashfileData
Functions relating to HTTPS Content Verification.
MHEG5SigVerifyStatus_t MHEG5IsValidSignature(MHEG5String *hash, MHEG5String *signature)
Check whether the signature is valid.
struct sHashDigest HashDigest
struct sHashfileData * previous
#define MHEG5strncmp(a, b, n)
File interface functions to DSMCC component.
#define GET_16BITS_FROM_ARRAY(X, Y)
void MHEG5SetSigVerifyCallback(void(*callback)(void))
Sets the callback to be called when pending requests are resolved.
MHEG5String digestPathname
MHEG5ContentVerification_t
Header file - Function prototypes for operating system.