summaryrefslogtreecommitdiff
path: root/src/secret.c
blob: 7222ddd06dd2e1284998ad0b29516b6d0823d7d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "secret.h"

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/rand.h>

#include "util.h"

#define SECRET_PREFIX "enc:v1:"
#define SECRET_KEY_LEN 32
#define SECRET_NONCE_LEN 12
#define SECRET_TAG_LEN 16

static int hex_val(int c)
{
    if (c >= '0' && c <= '9')
        return c - '0';
    if (c >= 'a' && c <= 'f')
        return c - 'a' + 10;
    if (c >= 'A' && c <= 'F')
        return c - 'A' + 10;
    return -1;
}

static int secret_key(unsigned char out[SECRET_KEY_LEN])
{
    const char *s = getenv("BOKFD_SECRET_KEY");
    if (!s || !*s)
        return -1;
    size_t n = strlen(s);
    if (n == 2 * SECRET_KEY_LEN) {
        int hex = 1;
        for (size_t i = 0; i < n && hex; i++)
            if (hex_val((unsigned char)s[i]) < 0)
                hex = 0;
        if (hex) {
            for (size_t i = 0; i < SECRET_KEY_LEN; i++)
                out[i] = (unsigned char)((hex_val(s[2 * i]) << 4) |
                                         hex_val(s[2 * i + 1]));
            return 0;
        }
    }
    if (n % 4 == 1 || n > 172)
        return -1;
    char padded[176];
    memcpy(padded, s, n);
    while (n % 4)
        padded[n++] = '=';
    padded[n] = '\0';
    unsigned char *raw = NULL;
    size_t raw_n = 0;
    if (util_b64_decode(padded, n, &raw, &raw_n) != 0)
        return -1;
    int ok = raw_n == SECRET_KEY_LEN;
    if (ok)
        memcpy(out, raw, SECRET_KEY_LEN);
    free(raw);
    return ok ? 0 : -1;
}

int secret_available(void)
{
    unsigned char key[SECRET_KEY_LEN] = { 0 };
    int ok = secret_key(key) == 0;
    OPENSSL_cleanse(key, sizeof key);
    return ok;
}

int secret_encrypt(const char *plain, char **out)
{
    if (out)
        *out = NULL;
    if (!plain || !out)
        return -1;
    size_t plen = strlen(plain);
    if (plen > (size_t)INT_MAX)
        return -1;
    unsigned char key[SECRET_KEY_LEN], nonce[SECRET_NONCE_LEN];
    if (secret_key(key) != 0)
        return -1;
    if (RAND_bytes(nonce, SECRET_NONCE_LEN) != 1) {
        OPENSSL_cleanse(key, sizeof key);
        return -1;
    }
    size_t clen = plen + SECRET_TAG_LEN;
    unsigned char *ct = xmalloc(clen);
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    int len = 0, total = 0, ok = 0;
    if (ctx &&
        EVP_EncryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL) == 1 &&
        EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, SECRET_NONCE_LEN,
                            NULL) == 1 &&
        EVP_EncryptInit_ex(ctx, NULL, NULL, key, nonce) == 1 &&
        EVP_EncryptUpdate(ctx, ct, &len, (const unsigned char *)plain,
                          (int)plen) == 1) {
        total = len;
        ok = EVP_EncryptFinal_ex(ctx, ct + total, &len) == 1;
        total += len;
    }
    if (ok)
        ok = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, SECRET_TAG_LEN,
                                 ct + total) == 1;
    EVP_CIPHER_CTX_free(ctx);
    OPENSSL_cleanse(key, sizeof key);
    if (!ok || total != (int)plen) {
        free(ct);
        return -1;
    }
    total += SECRET_TAG_LEN;
    char *nonce_b64 = util_b64(nonce, sizeof nonce);
    char *ct_b64 = util_b64(ct, (size_t)total);
    free(ct);
    size_t olen =
        strlen(SECRET_PREFIX) + strlen(nonce_b64) + 1 + strlen(ct_b64) + 1;
    char *s = xmalloc(olen);
    snprintf(s, olen, "%s%s:%s", SECRET_PREFIX, nonce_b64, ct_b64);
    free(nonce_b64);
    free(ct_b64);
    *out = s;
    return 0;
}

int secret_decrypt(const char *stored, char **out)
{
    if (out)
        *out = NULL;
    if (!stored || !out)
        return -1;
    if (strncmp(stored, SECRET_PREFIX, strlen(SECRET_PREFIX)) != 0)
        return -1;
    const char *p = stored + strlen(SECRET_PREFIX);
    const char *sep = strchr(p, ':');
    if (!sep || sep == p || !sep[1])
        return -1;
    unsigned char key[SECRET_KEY_LEN];
    if (secret_key(key) != 0)
        return -1;
    unsigned char *nonce = NULL, *ct = NULL;
    size_t nonce_n = 0, ct_n = 0;
    if (util_b64_decode(p, (size_t)(sep - p), &nonce, &nonce_n) != 0) {
        OPENSSL_cleanse(key, sizeof key);
        return -1;
    }
    if (util_b64_decode(sep + 1, strlen(sep + 1), &ct, &ct_n) != 0) {
        free(nonce);
        OPENSSL_cleanse(key, sizeof key);
        return -1;
    }
    if (nonce_n != SECRET_NONCE_LEN || ct_n < SECRET_TAG_LEN ||
        ct_n - SECRET_TAG_LEN > (size_t)INT_MAX) {
        free(nonce);
        free(ct);
        OPENSSL_cleanse(key, sizeof key);
        return -1;
    }
    size_t plen = ct_n - SECRET_TAG_LEN;
    unsigned char *pt = xmalloc(plen + 1);
    EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
    int len = 0, total = 0, ok = 0;
    if (ctx &&
        EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL) == 1 &&
        EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, SECRET_NONCE_LEN,
                            NULL) == 1 &&
        EVP_DecryptInit_ex(ctx, NULL, NULL, key, nonce) == 1 &&
        EVP_DecryptUpdate(ctx, pt, &len, ct, (int)plen) == 1) {
        total = len;
        if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, SECRET_TAG_LEN,
                                ct + plen) == 1)
            ok = EVP_DecryptFinal_ex(ctx, pt + total, &len) == 1;
        total += len;
    }
    EVP_CIPHER_CTX_free(ctx);
    OPENSSL_cleanse(key, sizeof key);
    free(nonce);
    free(ct);
    if (!ok || total != (int)plen) {
        OPENSSL_cleanse(pt, plen + 1);
        free(pt);
        return -1;
    }
    pt[total] = '\0';
    *out = (char *)pt;
    return 0;
}

int secret_key_is_secret(const char *key)
{
    return key && strcmp(key, "smtp_password") == 0;
}