libidav/crypto.c

Sun, 17 Sep 2017 17:56:02 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 17 Sep 2017 17:56:02 +0200
changeset 309
8a474d2ad209
parent 267
171498cb2137
child 349
0b4ecadaf3f9
permissions
-rw-r--r--

adds option to dav get for downloading only the directory structure

this is an undocumented feature and may be replaced in the future

40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
185
cd42cccee550 updated copyright notice
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 183
diff changeset
4 * Copyright 2016 Olaf Wintermann. All rights reserved.
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <string.h>
267
171498cb2137 some mingw fixes
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 261
diff changeset
32 #include <libidav/utils.h>
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <openssl/rand.h>
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "crypto.h"
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
261
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
37 #if OPENSSL_VERSION_NUMBER < 10000000
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
38
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
39 static EVP_CIPHER_CTX* create_evp_cipher_ctx() {
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
40 EVP_CIPHER_CTX *ctx = malloc(sizeof(EVP_CIPHER_CTX));
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
41 EVP_CIPHER_CTX_init(ctx);
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
42 return ctx;
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
43 }
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
44
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
45 static void free_evp_cipher_ctx(EVP_CIPHER_CTX *ctx) {
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
46 EVP_CIPHER_CTX_cleanup(ctx);
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
47 free(ctx);
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
48 }
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
49
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
50 #define EVP_CIPHER_CTX_new() create_evp_cipher_ctx()
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
51 #define EVP_CIPHER_CTX_free(ctx) free_evp_cipher_ctx(ctx)
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
52
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
53 #endif
f60d742a62a0 fixes build with ancient openssl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 260
diff changeset
54
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 AESDecrypter* aes_decrypter_new(DavKey *key, void *stream, dav_write_func write_func) {
207
de23f8881e9f fixed hash verification and head requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
56 AESDecrypter *dec = calloc(1, sizeof(AESDecrypter));
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
57 SHA256_Init(&dec->sha256);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 dec->stream = stream;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 dec->write = write_func;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 dec->key = key;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 dec->init = 0;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 dec->ivpos = 0;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 return dec;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 void aes_decrypter_init(AESDecrypter *dec) {
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
68 //EVP_CIPHER_CTX_init(&dec->ctx);
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
69 dec->ctx = EVP_CIPHER_CTX_new();
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 dec->init = 1;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 if(dec->key->type == DAV_KEY_AES128) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 EVP_DecryptInit_ex(
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
73 dec->ctx,
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 EVP_aes_128_cbc(),
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 NULL,
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 dec->key->data,
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 dec->ivtmp);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 } else if(dec->key->type == DAV_KEY_AES256) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 EVP_DecryptInit_ex(
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
80 dec->ctx,
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 EVP_aes_256_cbc(),
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 NULL,
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 dec->key->data,
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 dec->ivtmp);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 } else {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 fprintf(stderr, "unknown key type\n");
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 exit(-1);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 size_t aes_write(const void *buf, size_t s, size_t n, AESDecrypter *dec) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 int len = s*n;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 if(!dec->init) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 size_t n = 16 - dec->ivpos;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 size_t cp = n > len ? len : n;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 memcpy(dec->ivtmp + dec->ivpos, buf, cp);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 dec->ivpos += cp;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 if(dec->ivpos >= 16) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 aes_decrypter_init(dec);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 if(len == cp) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 return len;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 } else {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 buf = (char*)buf + cp;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 len -= cp;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 int outlen = len + 16;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 unsigned char *out = malloc(outlen);
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
111 EVP_DecryptUpdate(dec->ctx, out, &len, buf, len);
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
112 ssize_t wlen = dec->write(out, 1, len, dec->stream);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
113 SHA256_Update(&dec->sha256, out, wlen);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 free(out);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 return (s*n) / s;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117
207
de23f8881e9f fixed hash verification and head requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
118 void aes_decrypter_shutdown(AESDecrypter *dec) {
237
fd9135bc7580 fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
119 if(dec->init) {
fd9135bc7580 fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
120 void *out = malloc(128);
fd9135bc7580 fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
121 int len = 0;
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
122 EVP_DecryptFinal_ex(dec->ctx, out, &len);
237
fd9135bc7580 fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
123 dec->write(out, 1, len, dec->stream);
fd9135bc7580 fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
124 SHA256_Update(&dec->sha256, out, len);
fd9135bc7580 fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
125 free(out);
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
126 //EVP_CIPHER_CTX_cleanup(&dec->ctx);
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
127 EVP_CIPHER_CTX_free(dec->ctx);
237
fd9135bc7580 fixed crash when encrypted streams are empty
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 207
diff changeset
128 }
207
de23f8881e9f fixed hash verification and head requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
129 }
de23f8881e9f fixed hash verification and head requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
130
de23f8881e9f fixed hash verification and head requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
131 void aes_decrypter_close(AESDecrypter *dec) {
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 free(dec);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 AESEncrypter* aes_encrypter_new(DavKey *key, void *stream, dav_read_func read_func) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 unsigned char *iv = malloc(16);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 if(!RAND_bytes(iv, 16)) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 free(iv);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 return NULL;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 AESEncrypter *enc = malloc(sizeof(AESEncrypter));
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
144 SHA256_Init(&enc->sha256);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 enc->stream = stream;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 enc->read = read_func;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 enc->tmp = NULL;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 enc->tmplen = 0;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 enc->tmpoff = 0;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 enc->end = 0;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 enc->iv = iv;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 enc->ivlen = 16;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
154 //EVP_CIPHER_CTX_init(&enc->ctx);
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
155 enc->ctx = EVP_CIPHER_CTX_new();
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 if(key->type == DAV_KEY_AES128) {
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
157 EVP_EncryptInit_ex(enc->ctx, EVP_aes_128_cbc(), NULL, key->data, enc->iv);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 } else if(key->type == DAV_KEY_AES256) {
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
159 EVP_EncryptInit_ex(enc->ctx, EVP_aes_256_cbc(), NULL, key->data, enc->iv);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 } else {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 fprintf(stderr, "unknown key type\n");
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 exit(-1);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 return enc;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 size_t aes_read(void *buf, size_t s, size_t n, AESEncrypter *enc) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 size_t len = s*n;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 if(enc->tmp) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 size_t tmp_diff = enc->tmplen - enc->tmpoff;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 size_t cp_len = tmp_diff > len ? len : tmp_diff;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 memcpy(buf, enc->tmp + enc->tmpoff, cp_len);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 enc->tmpoff += cp_len;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 if(enc->tmpoff >= enc->tmplen) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 free(enc->tmp);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 enc->tmp = NULL;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 enc->tmplen = 0;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 enc->tmpoff = 0;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 return cp_len / s;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 if(enc->end) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 return 0;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 void *in = malloc(len);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 size_t in_len = enc->read(in, 1, len, enc->stream);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
190 SHA256_Update(&enc->sha256, in, in_len);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
191
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 unsigned char *out = NULL;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 int outlen = 0;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 size_t ivl = enc->ivlen;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 if(in_len != 0) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 outlen = len + 16;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 out = malloc(outlen + ivl);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 if(enc->iv) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 memcpy(out, enc->iv, ivl);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 }
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
201 EVP_EncryptUpdate(enc->ctx, out + ivl, &outlen, in, in_len);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 free(in);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 } else {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 out = malloc(16);
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
205 EVP_EncryptFinal_ex(enc->ctx, out, &outlen);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 enc->end = 1;
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
207 free(in);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 enc->tmp = (char*)out;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 enc->tmplen = outlen + ivl;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 enc->tmpoff = 0;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 if(enc->iv) {
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
214 free(enc->iv);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 enc->iv = NULL;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 enc->ivlen = 0;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 return aes_read(buf, s, n, enc);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 void aes_encrypter_close(AESEncrypter *enc) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 if(enc->tmp) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 free(enc->tmp);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 }
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
226 if(enc->iv) {
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
227 free(enc->iv);
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
228 }
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
229 //EVP_CIPHER_CTX_cleanup(&enc->ctx);
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
230 EVP_CIPHER_CTX_free(enc->ctx);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 free(enc);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
235 char* aes_encrypt(char *in, size_t len, DavKey *key) {
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
236 unsigned char iv[16];
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 if(!RAND_bytes(iv, 16)) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 return NULL;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
241 //EVP_CIPHER_CTX ctx;
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
242 //EVP_CIPHER_CTX_init(&ctx);
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
243 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 if(key->type == DAV_KEY_AES128) {
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
245 EVP_EncryptInit_ex(
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
246 ctx,
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
247 EVP_aes_128_cbc(),
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
248 NULL,
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
249 (unsigned char*)key->data,
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
250 iv);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 } else if(key->type == DAV_KEY_AES256) {
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
252 EVP_EncryptInit_ex(
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
253 ctx,
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
254 EVP_aes_256_cbc(),
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
255 NULL,
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
256 (unsigned char*)key->data,
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
257 iv);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 } else {
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
259 //EVP_CIPHER_CTX_cleanup(&ctx);
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
260 EVP_CIPHER_CTX_free(ctx);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 return NULL;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
264 //int len = strlen(in);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 int buflen = len + 64;
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
266 unsigned char *buf = calloc(1, buflen);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267 memcpy(buf, iv, 16);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 int l = buflen - 16;
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
270 EVP_EncryptUpdate(ctx, buf + 16, &l, (unsigned char*)in, len);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 int f = 0;
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
273 EVP_EncryptFinal_ex(ctx, buf + 16 + l, &f);
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
274 char *out = util_base64encode((char*)buf, 16 + l + f);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 free(buf);
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
276 EVP_CIPHER_CTX_free(ctx);
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
277 //EVP_CIPHER_CTX_cleanup(&ctx);
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
278
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 return out;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
282 char* aes_decrypt(char *in, size_t *length, DavKey *key) {
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 int len;
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
284 unsigned char *buf = (unsigned char*)util_base64decode_len(in, &len);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
286 //EVP_CIPHER_CTX ctx;
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
287 //EVP_CIPHER_CTX_init(&ctx);
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
288 EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 if(key->type == DAV_KEY_AES128) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290 EVP_DecryptInit_ex(
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
291 ctx,
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 EVP_aes_128_cbc(),
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 NULL,
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 key->data,
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 buf);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 } else if(key->type == DAV_KEY_AES256) {
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 EVP_DecryptInit_ex(
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
298 ctx,
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 EVP_aes_256_cbc(),
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 NULL,
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 key->data,
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 buf);
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 } else {
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
304 //EVP_CIPHER_CTX_cleanup(&ctx);
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
305 EVP_CIPHER_CTX_free(ctx);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 return NULL;
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 }
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
309 unsigned char *out = malloc(len + 1);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 int outlen = len;
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
311 unsigned char *in_buf = buf + 16;
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 int inlen = len - 16;
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
313 int f = 0;
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
315 EVP_DecryptUpdate(ctx, out, &outlen, in_buf, inlen);
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
316 EVP_DecryptFinal_ex(ctx, out + outlen, &f);
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 out[outlen + f] = '\0';
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318 free(buf);
260
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
319 //EVP_CIPHER_CTX_cleanup(&ctx);
26f5f817429e fixes build with openssl 1.1
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 237
diff changeset
320 EVP_CIPHER_CTX_free(ctx);
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
321
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
322 *length = outlen + f;
43
03076907b58a added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 40
diff changeset
323 return (char*)out;
40
a95ee94b9204 supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 }
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
325
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
326
207
de23f8881e9f fixed hash verification and head requests
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 185
diff changeset
327 void dav_get_hash(SHA256_CTX *sha256, unsigned char *buf) {
155
b85248a8aee2 fixed some cases of incompatible types in crypto functions
Mike Becker <universe@uap-core.de>
parents: 150
diff changeset
328 SHA256_Final((unsigned char*)buf, sha256);
150
37fb12574acd added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 75
diff changeset
329 }

mercurial