c - Check if the file is correct using sha1withRsa via openssl functions -


hi, have file_data(xml format) , file_signature(asn1 der), , have certificate(x509 asn1 der). want check if file_data correct, have problems. i'm doing:

main idea: company creates file_data, using sha1 gets hash of file_data, , encrypts hash using rsa private key , gets file_signature. company sends me file_data , file_signature , certificate. public key certificate file_signature , decrypt file_signature using public key , hash_1. file_data , use sha1 hash_2. if hash_1 , hash_2 equal, can trust content of file_data, right?

implementation:

  1. load certificate: d2i_x509_fp() function. have certificate.
  2. get public key of certificate: x509_extract_key, have public key.
  3. now want load file_signature decrypt using public key, file_signature has asn1 der format how can load it, function in openssl should use?
  4. suppose read file_signature, must decrypt using public key, there api purpose?
  5. suppose decrypt file_signature , hash_1.
  6. now must load file_data , hash of using sha1 function hash_2, function must use? sha1(), or sha1_init, sha1_update, sha1_finish?
  7. suppose hash_1 , hash_2, how must compare them, using memcmp?
  8. suppose compare them, if equal, can use file_data.

another question file_signature 128 byte len , when decrypt 128 byte hash_1(am rigth) , when hash of file_data hash_2 it's length 20 bytes, how can compare them, or misunderstand something?

thanks help! p.s. sorry english;).

if file_signature of 128 bytes, not asn.1 encoded. 128 bits key length of 1024 bit key (on low side nowadays, check keylength.com). hashes not directly encrypted if rsa used: first wrapped within asn.1 structure, , padded, according pkcs#1 v1.5 (google it).

normally don't perform hashing separately rsa encrypt. libraries openssl contain functions perform verification hash automatically calculated (no doubt openssl_verify()). functions compare you.

note need establish trust public key, otherwise attacker generate random key pair , send different public key attackers signed data. public keys trusted using direct communication beforehand, or using pki infrastructure (certificate chains).


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -