Bump version to 1.0.4
[mod_auth_gssapi.git] / src / crypto.c
1 /* Copyright (C) 2014 mod_auth_gssapi authors - See COPYING for (C) terms */
2
3 #include <openssl/evp.h>
4 #include <openssl/hmac.h>
5 #include <openssl/rand.h>
6 #include <stdbool.h>
7 #include "crypto.h"
8
9 struct seal_key {
10     const EVP_CIPHER *cipher;
11     const EVP_MD *md;
12     unsigned char *ekey;
13     unsigned char *hkey;
14 };
15
16 apr_status_t SEAL_KEY_CREATE(apr_pool_t *p, struct seal_key **skey,
17                              struct databuf *keys)
18 {
19     struct seal_key *n;
20     int keylen;
21     int ret;
22
23     n = apr_pcalloc(p, sizeof(*n));
24     if (!n) return ENOMEM;
25
26     n->cipher = EVP_aes_128_cbc();
27     if (!n->cipher) {
28         ret = EFAULT;
29         goto done;
30     }
31
32     keylen = n->cipher->key_len;
33
34     n->md = EVP_sha256();
35     if (!n->md) {
36         ret = EFAULT;
37         goto done;
38     }
39
40     n->ekey = apr_palloc(p, keylen);
41     if (!n->ekey) {
42         ret = ENOMEM;
43         goto done;
44     }
45
46     n->hkey = apr_palloc(p, keylen);
47     if (!n->hkey) {
48         ret = ENOMEM;
49         goto done;
50     }
51
52     if (keys) {
53         if (keys->length != (keylen * 2)) {
54             ret = EINVAL;
55             goto done;
56         }
57         memcpy(n->ekey, keys->value, keylen);
58         memcpy(n->hkey, keys->value + keylen, keylen);
59     } else {
60         ret = RAND_bytes(n->ekey, keylen);
61         if (ret == 0) {
62             ret = EFAULT;
63             goto done;
64         }
65
66         ret = RAND_bytes(n->hkey, keylen);
67         if (ret == 0) {
68             ret = EFAULT;
69             goto done;
70         }
71     }
72
73     ret = 0;
74 done:
75     if (ret) {
76         free(n->ekey);
77         free(n->hkey);
78         free(n);
79     } else {
80         *skey = n;
81     }
82     return ret;
83 }
84
85 apr_status_t SEAL_BUFFER(apr_pool_t *p, struct seal_key *skey,
86                          struct databuf *plain, struct databuf *cipher)
87 {
88     apr_status_t err = EFAULT;
89     EVP_CIPHER_CTX ctx = { 0 };
90     HMAC_CTX hmac_ctx = { 0 };
91     uint8_t rbuf[16];
92     unsigned int len;
93     int outlen, totlen;
94     int ret;
95
96     EVP_CIPHER_CTX_init(&ctx);
97
98     /* confounder to avoid exposing random numbers directly to clients
99      * as IVs */
100     ret = RAND_bytes(rbuf, 16);
101     if (ret == 0) goto done;
102
103     if (cipher->length == 0) {
104         /* add space for confounder and padding and MAC */
105         cipher->length = (plain->length / 16 + 2) * 16;
106         cipher->value = apr_palloc(p, cipher->length + skey->md->md_size);
107         if (!cipher->value) {
108             err = ENOMEM;
109             goto done;
110         }
111     }
112
113     ret = EVP_EncryptInit_ex(&ctx, skey->cipher, NULL, skey->ekey, NULL);
114     if (ret == 0) goto done;
115     totlen = 0;
116
117     outlen = cipher->length;
118     ret = EVP_EncryptUpdate(&ctx, cipher->value, &outlen, rbuf, 16);
119     if (ret == 0) goto done;
120     totlen += outlen;
121
122     outlen = cipher->length - totlen;
123     ret = EVP_EncryptUpdate(&ctx, &cipher->value[totlen], &outlen,
124                             plain->value, plain->length);
125     if (ret == 0) goto done;
126     totlen += outlen;
127
128     outlen = cipher->length - totlen;
129     ret = EVP_EncryptFinal_ex(&ctx, &cipher->value[totlen], &outlen);
130     if (ret == 0) goto done;
131     totlen += outlen;
132
133     /* now MAC the buffer */
134     HMAC_CTX_init(&hmac_ctx);
135
136     ret = HMAC_Init_ex(&hmac_ctx, skey->hkey,
137                        skey->cipher->key_len, skey->md, NULL);
138     if (ret == 0) goto done;
139
140     ret = HMAC_Update(&hmac_ctx, cipher->value, totlen);
141     if (ret == 0) goto done;
142
143     ret = HMAC_Final(&hmac_ctx, &cipher->value[totlen], &len);
144     if (ret == 0) goto done;
145
146     cipher->length = totlen + len;
147     err = 0;
148
149 done:
150     EVP_CIPHER_CTX_cleanup(&ctx);
151     HMAC_CTX_cleanup(&hmac_ctx);
152     return err;
153 }
154
155 apr_status_t UNSEAL_BUFFER(apr_pool_t *p, struct seal_key *skey,
156                            struct databuf *cipher, struct databuf *plain)
157 {
158     apr_status_t err = EFAULT;
159     EVP_CIPHER_CTX ctx = { 0 };
160     HMAC_CTX hmac_ctx = { 0 };
161     unsigned char mac[skey->md->md_size];
162     unsigned int len;
163     int outlen, totlen;
164     volatile bool equal = true;
165     int ret, i;
166
167     /* check MAC first */
168     HMAC_CTX_init(&hmac_ctx);
169
170     ret = HMAC_Init_ex(&hmac_ctx, skey->hkey,
171                        skey->cipher->key_len, skey->md, NULL);
172     if (ret == 0) goto done;
173
174     cipher->length -= skey->md->md_size;
175
176     ret = HMAC_Update(&hmac_ctx, cipher->value, cipher->length);
177     if (ret == 0) goto done;
178
179     ret = HMAC_Final(&hmac_ctx, mac, &len);
180     if (ret == 0) goto done;
181
182     if (len != skey->md->md_size) goto done;
183     for (i = 0; i < skey->md->md_size; i++) {
184         if (cipher->value[cipher->length + i] != mac[i]) equal = false;
185         /* not breaking intentionally,
186          * or we would allow an oracle attack */
187     }
188     if (!equal) goto done;
189
190     EVP_CIPHER_CTX_init(&ctx);
191
192     if (plain->length == 0) {
193         plain->length = cipher->length;
194         plain->value = apr_palloc(p, plain->length);
195         if (!plain->value) {
196             err = ENOMEM;
197             goto done;
198         }
199     }
200
201     ret = EVP_DecryptInit_ex(&ctx, skey->cipher, NULL, skey->ekey, NULL);
202     if (ret == 0) goto done;
203
204     totlen = 0;
205     outlen = plain->length;
206     ret = EVP_DecryptUpdate(&ctx, plain->value, &outlen,
207                             cipher->value, cipher->length);
208     if (ret == 0) goto done;
209
210     totlen += outlen;
211     outlen = plain->length - totlen;
212     ret = EVP_DecryptFinal_ex(&ctx, plain->value, &outlen);
213     if (ret == 0) goto done;
214
215     totlen += outlen;
216     /* now remove the confounder */
217     totlen -= 16;
218     memmove(plain->value, plain->value + 16, totlen);
219
220     plain->length = totlen;
221     err = 0;
222
223 done:
224     EVP_CIPHER_CTX_cleanup(&ctx);
225     HMAC_CTX_cleanup(&hmac_ctx);
226     return err;
227 }