1
votes

Hi, i have file_data(xml format) and file_signature(ASN1 DER), and also have certificate(X509 ASN1 DER). I want to check if file_data is correct, but I have some problems. what I'm doing:

Main Idea: Some company A creates file_data, then using SHA1 gets hash of the file_data, and encrypts this hash using RSA private key and gets file_signature. Then company A sends me file_data and file_signature and certificate. I get public key from certificate get file_signature and decrypt file_signature using public key and get hash_1. Then i get file_data and use SHA1 to get hash_2. If hash_1 and hash_2 is equal, i can trust to content of the file_data, am I right?

Implementation:

  1. Load certificate: d2i_X509_fp() function. Now I have certificate.
  2. Get public key of the certificate: X509_extract_key, now i have public key.
  3. Now i want to load file_signature to decrypt it using public key, BUT file_signature has ASN1 DER format how I can load it, what function in OpenSSl should I use?
  4. Suppose I read file_signature, now I must decrypt it using my public key, is there any API for this purpose?
  5. Suppose I decrypt file_signature and get hash_1.
  6. Now I must load file_data and get hash of it using SHA1 function hash_2, what function I must use? SHA1(), or SHA1_Init, SHA1_Update, SHA1_Finish?
  7. Suppose I get hash_1 and hash_2, how i must compare them, using memcmp?
  8. Suppose I compare them, if they are equal, i can use file_data.

another question is that file_signature is 128 byte len and when i decrypt it i get 128 byte hash_1(Am I rigth) , but when i get hash of the file_data hash_2 it's length is only 20 bytes, so how I can compare them, or I misunderstand something?

Thanks for your help! p.s. sorry for my english;).

1
ASN1 DER is not a signature format. ASN.1 is a standard used to describe data structures. DER (Distinguished Encoding Rules) is a specification for ASN.1 structure encoding. Do you any other information on the signature format (raw RSA/PKCS 1, CMS...)?Jcs

1 Answers

0
votes

If you get a file_signature of 128 bytes, then it is probably not ASN.1 encoded. 128 bits is exactly the key length of a 1024 bit key (on the low side nowadays, check keylength.com). Hashes are not directly encrypted if RSA is used: first it is wrapped within an ASN.1 structure, and then it is padded, all according to PKCS#1 v1.5 (Google it).

Normally you don't perform the hashing separately from the RSA encrypt. Libraries like openssl will contain functions to perform verification where the hash is automatically calculated (no doubt this would be openssl_verify()). Those functions will also do the compare for you.

Note that you will need to establish trust for the public key, otherwise an attacker could just generate a random key pair and send you a different public key together with the attackers signed data. Normally public keys are trusted using direct communication beforehand, or using a PKI infrastructure (certificate chains).