libidav/resource.c

changeset 151
a316613205dc
parent 150
37fb12574acd
child 155
b85248a8aee2
equal deleted inserted replaced
150:37fb12574acd 151:a316613205dc
648 CURL *handle = sn->handle; 648 CURL *handle = sn->handle;
649 util_set_url(res->session, dav_resource_get_href(res)); 649 util_set_url(res->session, dav_resource_get_href(res));
650 650
651 // check encryption 651 // check encryption
652 AESDecrypter *dec = NULL; 652 AESDecrypter *dec = NULL;
653 DavKey *key = NULL;
653 if(DAV_DECRYPT_CONTENT(sn)) { 654 if(DAV_DECRYPT_CONTENT(sn)) {
654 char *keyname = dav_get_property_ns(res, DAV_NS, "crypto-key"); 655 char *keyname = dav_get_property_ns(res, DAV_NS, "crypto-key");
655 if(keyname) { 656 if(keyname) {
656 DavKey *key = dav_context_get_key(sn->context, keyname); 657 key = dav_context_get_key(sn->context, keyname);
657 if(key) { 658 if(key) {
658 dec = aes_decrypter_new(key, stream, write_fnc); 659 dec = aes_decrypter_new(key, stream, write_fnc);
659 stream = dec; 660 stream = dec;
660 write_fnc = (dav_write_func)aes_write; 661 write_fnc = (dav_write_func)aes_write;
661 } 662 }
684 685
685 int status = 0; 686 int status = 0;
686 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status); 687 curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
687 if(ret == CURLE_OK && (status >= 200 && status < 300)) { 688 if(ret == CURLE_OK && (status >= 200 && status < 300)) {
688 int verify_failed = 0; 689 int verify_failed = 0;
689 if(DAV_DECRYPT_CONTENT(sn)) { 690 if(DAV_DECRYPT_CONTENT(sn) && key) {
690 // try to verify the content 691 // try to verify the content
691 char *res_hash = dav_get_property_ns(res, DAV_NS, "crypto-hash"); 692 char *res_hash = dav_get_property_ns(res, DAV_NS, "crypto-hash");
692 693
693 if(res_hash) { 694 if(res_hash) {
694 size_t len = 0; 695 size_t len = 0;
695 char *dec_hash = aes_decrypt(res_hash, &len, sn->key); 696 char *dec_hash = aes_decrypt(res_hash, &len, key);
696 char *hex_hash = util_hexstr(dec_hash, 32); 697 char *hex_hash = util_hexstr(dec_hash, 32);
697 if(strcmp(hash, hex_hash)) { 698 if(strcmp(hash, hex_hash)) {
698 verify_failed = 1; 699 verify_failed = 1;
699 } 700 }
700 free(dec_hash); 701 free(dec_hash);
701 free(hex_hash); 702 free(hex_hash);
702 } 703 }
703 } 704 }
705 if(hash) {
706 free(hash);
707 }
704 708
705 if(verify_failed) { 709 if(verify_failed) {
706 res->session->error = DAV_CONTENT_VERIFICATION_ERROR; 710 res->session->error = DAV_CONTENT_VERIFICATION_ERROR;
707 return 1; 711 return 1;
708 } 712 }
709 713
710 res->session->error = DAV_OK; 714 res->session->error = DAV_OK;
711 return 0; 715 return 0;
712 } else { 716 } else {
717 if(hash) {
718 free(hash);
719 }
713 dav_session_set_error(res->session, ret, status); 720 dav_session_set_error(res->session, ret, status);
714 return 1; 721 return 1;
715 } 722 }
716 } 723 }
717 724

mercurial