This file has the utility to confirm the information
#import "SignatureVerifier.h"
#embrace
#import
@implementation SignatureVerifier
- (BOOL)verifySignature:(NSString *)jsonString
signature:(NSData *)signatureData
publicKey:(SecKeyRef)publicKey
hashFunction:(NSString *)hashFunction {
// Validate enter parameters
if (!jsonString || !signatureData || !publicKey) {
NSLog(@"Error: Invalid enter parameters");
return NO;
}
// Convert the JSON string to knowledge (normalize if obligatory)
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSLog(@"Json Information utf8: %@", jsonData);
if (!jsonData) {
NSLog(@"Error changing JSON string to knowledge");
return NO;
}
// Hash the JSON knowledge utilizing SHA256
NSData *hashedData = nil;
if ([hashFunction isEqualToString:@"SHA256"]) {
hashedData = [self sha256HashForData:jsonData];
NSLog(@"Hashed Information: %@", hashedData);
} else {
NSLog(@"Unsupported hash perform: %@", hashFunction);
return NO;
}
if (!hashedData) {
NSLog(@"Error: Hashing failed");
return NO;
}
// Confirm the signature
CFErrorRef error = NULL;
BOOL consequence = SecKeyVerifySignature(
publicKey,
kSecKeyAlgorithmRSASignatureMessagePKCS1v15SHA256,
(__bridge CFDataRef) hashedData,
(__bridge CFDataRef) signatureData,
&error
);
if (!consequence || error) {
if (error) {
CFStringRef errorDescription = CFErrorCopyDescription(error);
NSLog(@"Error verifying signature: %@", errorDescription);
CFRelease(errorDescription);
}
return NO;
}
NSLog(@"Signature efficiently verified.");
return YES;
}
- (NSData *)sha256HashForData:(NSData *)knowledge {
if (!knowledge) {
NSLog(@"Error: No knowledge supplied for hashing");
return nil;
}
uint8_t hash[CC_SHA256_DIGEST_LENGTH];
CC_SHA256(knowledge.bytes, (CC_LONG)knowledge.size, hash);
return [NSData dataWithBytes:hash length:CC_SHA256_DIGEST_LENGTH];
}
@finish
That is the principle file utilizing the utility.
Please take into account the opposite utility perform right as they have been checked.
#import
#import "JSONREADER.m"
#import "KeyReader.h"
#import "SignatureVerifier.h"
#import "hex_to_data.h"
int essential(int argc, const char * argv[]) {
@autoreleasepool {
// Create an occasion of KeyReader
KeyReader *keyReader = [[KeyReader alloc] init];
// Studying public key file
NSString *keyFilePath = @"./crl_verify.key";
SecKeyRef publicKey = [keyReader publicKeyFromKeyFile:keyFilePath];
if (publicKey) {
NSLog(@"Public key efficiently created");
// CFRelease(publicKey);
} else {
NSLog(@"Didn't create public key");
}
JSONReader *jsonReader = [[JSONReader alloc] init];
// Studying crl json file
NSString *filePath = @"./crl.json";
NSError *json_read_error = nil;
NSDictionary *jsonContent = nil;
jsonContent = [jsonReader readJSONFromFile:filePath error:&json_read_error];
if (jsonContent) {
NSLog(@"Efficiently learn JSON: %@", jsonContent);
// Instance: Accessing a worth within the JSON dictionary
NSString *signature = jsonContent[@"signature"];
NSLog(@"Worth for 'key': %@", signature);
} else {
NSLog(@"Error: %@", [json_read_error localizedDescription]);
}
// Learn tbs_cert_list
NSError *json_convert_error = nil;
NSString *jsonString = jsonContent[@"tbs_cert_list"];
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData
options:0
error:&json_convert_error];
if (json_convert_error) {
NSLog(@"Error deserializing JSON: %@", [json_convert_error localizedDescription]);
} else {
NSLog(@"tbs_cert_list JSON Dictionary: %@", jsonDict);
}
NSString *signature = jsonContent[@"signature"];
NSLog(@"Hex Signal: %@", signature);
NSData *knowledge = [HexUtils dataFromHexString: signature];
NSLog(@"hex_to_sign_data: %@", knowledge);
// NSData *signatureData = [[NSData alloc] initWithBase64EncodedString:knowledge choices:0];
NSString *signatureData = [HexUtils base64StringFromHexString: signature];
NSLog(@"Base64 Encoded String: %@", signatureData);
// Print parameters
NSLog(@"Verifying signature with the next parameters:");
NSLog(@"JSON String: %@", jsonString);
NSLog(@"Signature Information: %@", signatureData);
NSLog(@"Public Key: %@", publicKey);
NSLog(@"Hash Operate: %@", @"SHA256");
// Instantiate SignatureVerifier``your textual content``
SignatureVerifier *verifier = [[SignatureVerifier alloc] init];
BOOL verified = [verifier verifySignature:jsonString
signature:data
publicKey:publicKey
hashFunction:@"SHA256"];
if (verified) {
NSLog(@"Signature is legitimate!");
} else {
NSLog(@"Signature verification failed.");
}
// Launch public key
CFRelease(publicKey);
}
return 0;
}
So I’ve verified the information utilizing openssl and a python program which is working nice.
However I can’t confirm it on this program. What am I doing incorrect ?
This returns a failure in verification. Nevertheless, it ought to confirm the information.
The codecs and kinds appears nice as per apple doc.