Ticket #59011: patch-dmg2img-partof-openssl111-fix-evp-part.diff

File patch-dmg2img-partof-openssl111-fix-evp-part.diff, 4.0 KB (added by kencu (Ken), 5 years ago)

WIP

  • vfdecrypt.c

    old new  
    212212/* DES3-EDE unwrap operation loosely based on to RFC 2630, section 12.6
    213213 *    wrapped_key has to be 40 bytes in length.  */
    214214int apple_des3_ede_unwrap_key(uint8_t *wrapped_key, int wrapped_key_len, uint8_t *decryptKey, uint8_t *unwrapped_key) {
    215   EVP_CIPHER_CTX ctx;
     215  EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    216216  uint8_t *TEMP1, *TEMP2, *CEKICV;
    217217  uint8_t IV[8] = { 0x4a, 0xdd, 0xa2, 0x2c, 0x79, 0xe8, 0x21, 0x05 };
    218218  int outlen, tmplen, i;
    219219
    220   EVP_CIPHER_CTX_init(&ctx);
     220  EVP_CIPHER_CTX_init(ctx);
    221221  /* result of the decryption operation shouldn't be bigger than ciphertext */
    222222  TEMP1 = malloc(wrapped_key_len);
    223223  TEMP2 = malloc(wrapped_key_len);
    224224  CEKICV = malloc(wrapped_key_len);
    225225  /* uses PKCS#7 padding for symmetric key operations by default */
    226   EVP_DecryptInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, decryptKey, IV);
     226  EVP_DecryptInit_ex(ctx, EVP_des_ede3_cbc(), NULL, decryptKey, IV);
    227227
    228   if(!EVP_DecryptUpdate(&ctx, TEMP1, &outlen, wrapped_key, wrapped_key_len)) {
     228  if(!EVP_DecryptUpdate(ctx, TEMP1, &outlen, wrapped_key, wrapped_key_len)) {
    229229    fprintf(stderr, "internal error (1) during key unwrap operation!\n");
    230230    return(-1);
    231231  }
    232   if(!EVP_DecryptFinal_ex(&ctx, TEMP1 + outlen, &tmplen)) {
     232  if(!EVP_DecryptFinal_ex(ctx, TEMP1 + outlen, &tmplen)) {
    233233    fprintf(stderr, "internal error (2) during key unwrap operation!\n");
    234234    return(-1);
    235235  }
    236236  outlen += tmplen;
    237   EVP_CIPHER_CTX_cleanup(&ctx);
     237  EVP_CIPHER_CTX_cleanup(ctx);
    238238
    239239  /* reverse order of TEMP3 */
    240240  for(i = 0; i < outlen; i++) TEMP2[i] = TEMP1[outlen - i - 1];
    241241
    242   EVP_CIPHER_CTX_init(&ctx);
     242  EVP_CIPHER_CTX_init(ctx);
    243243  /* uses PKCS#7 padding for symmetric key operations by default */
    244   EVP_DecryptInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, decryptKey, TEMP2);
    245   if(!EVP_DecryptUpdate(&ctx, CEKICV, &outlen, TEMP2+8, outlen-8)) {
     244  EVP_DecryptInit_ex(ctx, EVP_des_ede3_cbc(), NULL, decryptKey, TEMP2);
     245  if(!EVP_DecryptUpdate(ctx, CEKICV, &outlen, TEMP2+8, outlen-8)) {
    246246    fprintf(stderr, "internal error (3) during key unwrap operation!\n");
    247247    return(-1);
    248248  }
    249   if(!EVP_DecryptFinal_ex(&ctx, CEKICV + outlen, &tmplen)) {
     249  if(!EVP_DecryptFinal_ex(ctx, CEKICV + outlen, &tmplen)) {
    250250    fprintf(stderr, "internal error (4) during key unwrap operation!\n");
    251251    return(-1);
    252252  }
    253253
    254254  outlen += tmplen;
    255   EVP_CIPHER_CTX_cleanup(&ctx);
     255  EVP_CIPHER_CTX_cleanup(ctx);
    256256
    257257  memcpy(unwrapped_key, CEKICV+4, outlen-4);
    258258  free(TEMP1);
     
    279279int unwrap_v2_header(char *passphrase, cencrypted_v2_pwheader *header, uint8_t *aes_key, uint8_t *hmacsha1_key) {
    280280  /* derived key is a 3DES-EDE key */
    281281  uint8_t derived_key[192/8];
    282   EVP_CIPHER_CTX ctx;
     282  EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();;
    283283  uint8_t *TEMP1;
    284284  int outlen, tmplen;
    285285
     
    288288
    289289  print_hex(derived_key, 192/8);
    290290
    291   EVP_CIPHER_CTX_init(&ctx);
     291  EVP_CIPHER_CTX_init(ctx);
    292292  /* result of the decryption operation shouldn't be bigger than ciphertext */
    293293  TEMP1 = malloc(header->encrypted_keyblob_size);
    294294  /* uses PKCS#7 padding for symmetric key operations by default */
    295   EVP_DecryptInit_ex(&ctx, EVP_des_ede3_cbc(), NULL, derived_key, header->blob_enc_iv);
     295  EVP_DecryptInit_ex(ctx, EVP_des_ede3_cbc(), NULL, derived_key, header->blob_enc_iv);
    296296
    297   if(!EVP_DecryptUpdate(&ctx, TEMP1, &outlen, header->encrypted_keyblob, header->encrypted_keyblob_size)) {
     297  if(!EVP_DecryptUpdate(ctx, TEMP1, &outlen, header->encrypted_keyblob, header->encrypted_keyblob_size)) {
    298298    fprintf(stderr, "internal error (1) during key unwrap operation!\n");
    299299    return(-1);
    300300  }
    301   if(!EVP_DecryptFinal_ex(&ctx, TEMP1 + outlen, &tmplen)) {
     301  if(!EVP_DecryptFinal_ex(ctx, TEMP1 + outlen, &tmplen)) {
    302302    fprintf(stderr, "internal error (2) during key unwrap operation!\n");
    303303    return(-1);
    304304  }
    305305  outlen += tmplen;
    306   EVP_CIPHER_CTX_cleanup(&ctx);
     306  EVP_CIPHER_CTX_cleanup(ctx);
    307307  memcpy(aes_key, TEMP1, 16);
    308308  memcpy(hmacsha1_key, TEMP1, 20);
    309309