OpenSSL pit Record

TOC

OpenSSL API has a built-in types and many ways to achieve the same function there is a lot of API, API usage must understand these differences and still have time, because the specific use different API or to themselves to try it

To summarize the pit:

  • openssl two kinds of RSA public key formats
    require different API to read
    when using the command line generated by the need to add different options
  • About MD5 generation
    seems there are two, there is a more complicated:
    there are init (), update (), there is an end of the function, forgot the name

I use a simple implementation, is a MD5 (), look at the Internet, this simple function calls the underlying complexity of the above function is implemented

And during the time MD5 generated by sizeof strlen and the results are likely to be different

  • RSA encryption and decryption on the problem
    because the RSA encryption 16 bytes, 128 bytes is obtained ciphertext, so at the time of transmission and reception needs to be noted that the size of the received, encrypted transmission block, and the size of the array reception problems
  • In the RSA encryption and decryption functions may return -1
  1. Size of the array does not
  2. Encryption and decryption key mismatch
  • AES encryption is not used directly when a random 16-byte array
    OpenSSl provides structure 16 according to the AES array of bytes AES encryption and decryption
    performing AES encryption and decryption when this structure is to use
  • Stability OpenSSL personal feeling is not particularly good
    and sometimes there will be some problems

About RSA PEM file format

PEM private key file format

-----BEGIN RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----

PEM public key file format

-----BEGIN PUBLIC KEY-----
-----END PUBLIC KEY-----

PEM format to extract the public key:

openssl rsa -in key.pem -RSAPublicKey_out -out pubkey.pem
    -in 指定输入的密钥文件
    -out 指定提取生成公钥的文件(PEM RSAPublicKey格式)

API:

//从BIO重加载PublicKey格式公钥证书
RSA *PEM_read_RSA_PUBKEY(FILE *fp, RSA **x, pem_password_cb *cb, void *u);

PEM RSAPublicKey public key file format

-----BEGIN RSA PUBLIC KEY-----
-----END RSA PUBLIC KEY-----

extract:

openssl rsa -in key.pem -RSAPublicKey_out -out pubkey.pem
    -in 指定输入的密钥文件
    -out 指定提取生成公钥的文件(PEM RSAPublicKey格式)

API:

//从BIO重加载RSAPublicKey格式公钥证书
RSA *PEM_read_bio_RSAPublicKey(BIO *bp, RSA **x, pem_password_cb *cb, void *u);

About 0xC0000005: Access Violation Occurs When reading position xxx

They are generally pointer problems

  • Application memory is not released
  • Infinite loop causes a memory leak
  • All third-party libraries related to a memory leak BUG

Common overflow are:

  • Memory allocation was not successful, but used it
  • Although the memory allocation successful, but it has not been initialized to reference it.
  • Memory allocation was successful and has been initialized, but the operation crossed the border memory.
  • After using free or delete release the memory, not the pointer is set to NULL. Resulting in "wild pointer."

区别: RSA_private_decrypt and RSA_private_encrypt

  • No padding when these two functions are the same
  • After specifying the padding, RSA_private_encrypt padding will be added before the modular exponentiation; RSA_private_decrypt padding will be cut after the modular exponentiation

Guess you like

Origin www.cnblogs.com/volva/p/11815202.html