Tue, 13 Oct 2015 16:10:58 +0200
don't allow signs for date strings (always expect full years)
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 | * |
75
56962faf2b42
copyright update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
43
diff
changeset
|
4 | * Copyright 2015 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> |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | #include <openssl/rand.h> |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | #include <libidav/utils.h> |
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 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
37 | AESDecrypter* aes_decrypter_new(DavKey *key, void *stream, dav_write_func write_func) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | AESDecrypter *dec = malloc(sizeof(AESDecrypter)); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
39 | SHA256_Init(&dec->sha256); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
40 | dec->stream = stream; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | dec->write = write_func; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | dec->key = key; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
43 | dec->init = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
44 | dec->ivpos = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
45 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
46 | return dec; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
47 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
48 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
49 | void aes_decrypter_init(AESDecrypter *dec) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
50 | EVP_CIPHER_CTX_init(&dec->ctx); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
51 | dec->init = 1; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
52 | if(dec->key->type == DAV_KEY_AES128) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
53 | EVP_DecryptInit_ex( |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
54 | &dec->ctx, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55 | EVP_aes_128_cbc(), |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | NULL, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57 | dec->key->data, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
58 | dec->ivtmp); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
59 | } else if(dec->key->type == DAV_KEY_AES256) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
60 | EVP_DecryptInit_ex( |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
61 | &dec->ctx, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
62 | EVP_aes_256_cbc(), |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
63 | NULL, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
64 | dec->key->data, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
65 | dec->ivtmp); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
66 | } else { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
67 | fprintf(stderr, "unknown key type\n"); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
68 | exit(-1); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
69 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
70 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
71 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
72 | 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
|
73 | int len = s*n; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
74 | if(!dec->init) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
75 | size_t n = 16 - dec->ivpos; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
76 | size_t cp = n > len ? len : n; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
77 | memcpy(dec->ivtmp + dec->ivpos, buf, cp); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
78 | dec->ivpos += cp; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
79 | if(dec->ivpos >= 16) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
80 | aes_decrypter_init(dec); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
81 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
82 | if(len == cp) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
83 | return len; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
84 | } else { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
85 | buf = (char*)buf + cp; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
86 | len -= cp; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
87 | } |
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 | int outlen = len + 16; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | unsigned char *out = malloc(outlen); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92 | 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
|
93 | 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
|
94 | SHA256_Update(&dec->sha256, out, wlen); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
95 | free(out); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
96 | return (s*n) / s; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | void aes_decrypter_close(AESDecrypter *dec) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
100 | void *out = malloc(128); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | int len = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | EVP_DecryptFinal_ex(&dec->ctx, out, &len); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | dec->write(out, 1, len, dec->stream); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | free(out); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | EVP_CIPHER_CTX_cleanup(&dec->ctx); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | free(dec); |
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 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
110 | 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
|
111 | unsigned char *iv = malloc(16); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
112 | if(!RAND_bytes(iv, 16)) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113 | free(iv); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
114 | return NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115 | } |
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 | AESEncrypter *enc = malloc(sizeof(AESEncrypter)); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
118 | SHA256_Init(&enc->sha256); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119 | enc->stream = stream; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120 | enc->read = read_func; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121 | enc->tmp = NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
122 | enc->tmplen = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
123 | enc->tmpoff = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
124 | enc->end = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
125 | enc->iv = iv; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
126 | enc->ivlen = 16; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
127 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
128 | EVP_CIPHER_CTX_init(&enc->ctx); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
129 | if(key->type == DAV_KEY_AES128) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
130 | EVP_EncryptInit_ex(&enc->ctx, EVP_aes_128_cbc(), NULL, key->data, enc->iv); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
131 | } else if(key->type == DAV_KEY_AES256) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
132 | EVP_EncryptInit_ex(&enc->ctx, EVP_aes_256_cbc(), NULL, key->data, enc->iv); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
133 | } else { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
134 | fprintf(stderr, "unknown key type\n"); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
135 | exit(-1); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
136 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
137 | return enc; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
138 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | 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
|
141 | size_t len = s*n; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
142 | if(enc->tmp) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
143 | size_t tmp_diff = enc->tmplen - enc->tmpoff; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
144 | size_t cp_len = tmp_diff > len ? len : tmp_diff; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
145 | memcpy(buf, enc->tmp + enc->tmpoff, cp_len); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
146 | enc->tmpoff += cp_len; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147 | if(enc->tmpoff >= enc->tmplen) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
148 | free(enc->tmp); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
149 | enc->tmp = NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
150 | enc->tmplen = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
151 | enc->tmpoff = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
152 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
153 | return cp_len / s; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
154 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
155 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
156 | if(enc->end) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
157 | return 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
158 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
160 | void *in = malloc(len); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
161 | 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
|
162 | |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
163 | SHA256_Update(&enc->sha256, in, in_len); |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
164 | |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
165 | unsigned char *out = NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
166 | int outlen = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
167 | size_t ivl = enc->ivlen; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
168 | if(in_len != 0) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
169 | outlen = len + 16; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
170 | out = malloc(outlen + ivl); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
171 | if(enc->iv) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
172 | memcpy(out, enc->iv, ivl); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
174 | EVP_EncryptUpdate(&enc->ctx, out + ivl, &outlen, in, in_len); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
175 | free(in); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
176 | } else { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
177 | out = malloc(16); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | EVP_EncryptFinal_ex(&enc->ctx, out, &outlen); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
179 | enc->end = 1; |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
180 | free(in); |
40
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 | enc->tmp = (char*)out; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
183 | enc->tmplen = outlen + ivl; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
184 | enc->tmpoff = 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 | if(enc->iv) { |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
187 | free(enc->iv); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | enc->iv = NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189 | enc->ivlen = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
191 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192 | return aes_read(buf, s, n, enc); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
193 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
194 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
195 | void aes_encrypter_close(AESEncrypter *enc) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
196 | if(enc->tmp) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
197 | free(enc->tmp); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
198 | } |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
199 | if(enc->iv) { |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
200 | free(enc->iv); |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
201 | } |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
202 | EVP_CIPHER_CTX_cleanup(&enc->ctx); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
203 | free(enc); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
204 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
205 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
206 | |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
207 | 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
|
208 | unsigned char iv[16]; |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
209 | if(!RAND_bytes(iv, 16)) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
210 | return NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
211 | } |
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 | EVP_CIPHER_CTX ctx; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
214 | EVP_CIPHER_CTX_init(&ctx); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
215 | if(key->type == DAV_KEY_AES128) { |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
216 | EVP_EncryptInit_ex( |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
217 | &ctx, |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
218 | EVP_aes_128_cbc(), |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
219 | NULL, |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
220 | (unsigned char*)key->data, |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
221 | iv); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
222 | } else if(key->type == DAV_KEY_AES256) { |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
223 | EVP_EncryptInit_ex( |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
224 | &ctx, |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
225 | EVP_aes_256_cbc(), |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
226 | NULL, |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
227 | (unsigned char*)key->data, |
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
228 | iv); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
229 | } else { |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
230 | EVP_CIPHER_CTX_cleanup(&ctx); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
231 | return NULL; |
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 | |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
234 | //int len = strlen(in); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
235 | int buflen = len + 64; |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
236 | unsigned char *buf = calloc(1, buflen); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
237 | memcpy(buf, iv, 16); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
238 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
239 | int l = buflen - 16; |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
240 | 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
|
241 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
242 | int f = 0; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
243 | EVP_EncryptFinal_ex(&ctx, buf + 16 + l, &f); |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
244 | char *out = util_base64encode((char*)buf, 16 + l + f); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
245 | free(buf); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
246 | EVP_CIPHER_CTX_cleanup(&ctx); |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
247 | |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
248 | return out; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
249 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
250 | |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
251 | 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
|
252 | int len; |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
253 | 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
|
254 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
255 | EVP_CIPHER_CTX ctx; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
256 | EVP_CIPHER_CTX_init(&ctx); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
257 | if(key->type == DAV_KEY_AES128) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
258 | EVP_DecryptInit_ex( |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
259 | &ctx, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
260 | EVP_aes_128_cbc(), |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
261 | NULL, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
262 | key->data, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
263 | buf); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
264 | } else if(key->type == DAV_KEY_AES256) { |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
265 | EVP_DecryptInit_ex( |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
266 | &ctx, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
267 | EVP_aes_256_cbc(), |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
268 | NULL, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
269 | key->data, |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
270 | buf); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
271 | } else { |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
272 | EVP_CIPHER_CTX_cleanup(&ctx); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
273 | return NULL; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
274 | } |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
275 | |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
276 | unsigned char *out = malloc(len + 1); |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
277 | int outlen = len; |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
278 | unsigned char *in_buf = buf + 16; |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
279 | int inlen = len - 16; |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
280 | int f = 0; |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
281 | |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
282 | EVP_DecryptUpdate(&ctx, out, &outlen, in_buf, inlen); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
283 | EVP_DecryptFinal_ex(&ctx, out + outlen, &f); |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
284 | out[outlen + f] = '\0'; |
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
285 | free(buf); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
286 | EVP_CIPHER_CTX_cleanup(&ctx); |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
287 | |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
288 | *length = outlen + f; |
43
03076907b58a
added file name encryption
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
40
diff
changeset
|
289 | return (char*)out; |
40
a95ee94b9204
supports whitespace in paths
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
290 | } |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
291 | |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
292 | |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
293 | void dav_get_hash(SHA256_CTX *sha256, char *buf) { |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
294 | #ifdef __sun |
155
b85248a8aee2
fixed some cases of incompatible types in crypto functions
Mike Becker <universe@uap-core.de>
parents:
150
diff
changeset
|
295 | SHA256Final((unsigned char*)buf, sha256); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
296 | #else |
155
b85248a8aee2
fixed some cases of incompatible types in crypto functions
Mike Becker <universe@uap-core.de>
parents:
150
diff
changeset
|
297 | SHA256_Final((unsigned char*)buf, sha256); |
150
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
298 | #endif |
37fb12574acd
added checksums for encrypted resources
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
75
diff
changeset
|
299 | } |