-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathopenssl_symmetric_provider.c
271 lines (244 loc) · 7.54 KB
/
openssl_symmetric_provider.c
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
* Copyright 2018 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* This is an example of using crypto API of libcouchbase. The implementation should not be considered as production
* ready, because it uses hardcoded keys, insecure memory allocation, copying and comparison. Consult documentation of
* your crypto library on how to properly work with keys and buffers.
*/
#include <stdlib.h>
#include <string.h>
#include "openssl_symmetric_provider.h"
#include <openssl/ssl.h>
#include <openssl/conf.h>
#include <openssl/evp.h>
#include <openssl/err.h>
static void osp_free(lcbcrypto_PROVIDER *provider)
{
free(provider);
}
static void osp_release_bytes(lcbcrypto_PROVIDER *provider, void *bytes)
{
free(bytes);
(void)provider;
}
static const char *osp_get_key_id(lcbcrypto_PROVIDER *provider)
{
return common_aes256_key_id;
}
static lcb_error_t osp_generate_iv(struct lcbcrypto_PROVIDER *provider, uint8_t **iv, size_t *iv_len)
{
*iv_len = AES256_IV_SIZE;
*iv = malloc(*iv_len);
memcpy(*iv, common_aes256_iv, *iv_len);
(void)provider;
return LCB_SUCCESS;
}
static lcb_error_t osp_sign(struct lcbcrypto_PROVIDER *provider, const lcbcrypto_SIGV *inputs, size_t inputs_num,
uint8_t **sig, size_t *sig_len)
{
const EVP_MD *md;
uint8_t out[EVP_MAX_MD_SIZE];
size_t out_len = EVP_MAX_MD_SIZE;
int rc, key_len = strlen((const char *)common_hmac_sha256_key);
EVP_MD_CTX *ctx = NULL;
EVP_PKEY *key = NULL;
size_t ii;
md = EVP_get_digestbyname("SHA256");
if (md == NULL) {
return LCB_EINVAL;
}
key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, common_hmac_sha256_key, key_len);
if (key == NULL) {
return LCB_EINVAL;
}
ctx = EVP_MD_CTX_new();
if (ctx == NULL) {
EVP_PKEY_free(key);
return LCB_EINVAL;
}
rc = EVP_DigestSignInit(ctx, NULL, md, NULL, key);
if (rc != 1) {
EVP_PKEY_free(key);
return LCB_EINVAL;
}
for (ii = 0; ii < inputs_num; ii++) {
rc = EVP_DigestSignUpdate(ctx, inputs[ii].data, inputs[ii].len);
if (rc != 1) {
EVP_PKEY_free(key);
EVP_MD_CTX_destroy(ctx);
return LCB_EINVAL;
}
}
rc = EVP_DigestSignFinal(ctx, out, &out_len);
if (rc != 1 || out_len == 0) {
EVP_PKEY_free(key);
EVP_MD_CTX_destroy(ctx);
return LCB_EINVAL;
}
*sig = malloc(out_len);
memcpy(*sig, out, out_len);
*sig_len = out_len;
return LCB_SUCCESS;
}
static lcb_error_t osp_verify_signature(struct lcbcrypto_PROVIDER *provider, const lcbcrypto_SIGV *inputs,
size_t inputs_num, uint8_t *sig, size_t sig_len)
{
const EVP_MD *md;
uint8_t actual[EVP_MAX_MD_SIZE];
size_t actual_len = EVP_MAX_MD_SIZE;
int rc, key_len = strlen((const char *)common_hmac_sha256_key);
EVP_MD_CTX *ctx = NULL;
EVP_PKEY *key = NULL;
size_t ii;
md = EVP_get_digestbyname("SHA256");
if (md == NULL) {
return LCB_EINVAL;
}
key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, common_hmac_sha256_key, key_len);
if (key == NULL) {
return LCB_EINVAL;
}
ctx = EVP_MD_CTX_new();
if (ctx == NULL) {
EVP_PKEY_free(key);
return LCB_EINVAL;
}
rc = EVP_DigestSignInit(ctx, NULL, md, NULL, key);
if (rc != 1) {
EVP_PKEY_free(key);
return LCB_EINVAL;
}
for (ii = 0; ii < inputs_num; ii++) {
rc = EVP_DigestSignUpdate(ctx, inputs[ii].data, inputs[ii].len);
if (rc != 1) {
EVP_PKEY_free(key);
EVP_MD_CTX_destroy(ctx);
return LCB_EINVAL;
}
}
rc = EVP_DigestSignFinal(ctx, actual, &actual_len);
if (rc != 1 || actual_len == 0) {
EVP_PKEY_free(key);
EVP_MD_CTX_destroy(ctx);
return LCB_EINVAL;
}
if (memcmp(actual, sig, sig_len < actual_len ? sig_len : actual_len) == 0) {
return LCB_SUCCESS;
}
return LCB_EINVAL;
}
static lcb_error_t osp_encrypt(struct lcbcrypto_PROVIDER *provider, const uint8_t *input, size_t input_len,
const uint8_t *iv, size_t iv_len, uint8_t **output, size_t *output_len)
{
EVP_CIPHER_CTX *ctx;
const EVP_CIPHER *cipher;
int rc, len, block_len, out_len;
uint8_t *out;
if (iv_len != 16) {
return LCB_EINVAL;
}
ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
return LCB_EINVAL;
}
cipher = EVP_aes_256_cbc();
rc = EVP_EncryptInit_ex(ctx, cipher, NULL, common_aes256_key, iv);
if (rc != 1) {
EVP_CIPHER_CTX_free(ctx);
return LCB_EINVAL;
}
block_len = EVP_CIPHER_block_size(cipher);
out = calloc(input_len + block_len - 1, sizeof(uint8_t));
rc = EVP_EncryptUpdate(ctx, out, &len, input, input_len);
if (rc != 1) {
free(out);
EVP_CIPHER_CTX_free(ctx);
return LCB_EINVAL;
}
out_len = len;
rc = EVP_EncryptFinal_ex(ctx, out + len, &len);
if (rc != 1) {
free(out);
EVP_CIPHER_CTX_free(ctx);
return LCB_EINVAL;
}
out_len += len;
EVP_CIPHER_CTX_free(ctx);
*output = out;
*output_len = out_len;
return LCB_SUCCESS;
}
static lcb_error_t osp_decrypt(struct lcbcrypto_PROVIDER *provider, const uint8_t *input, size_t input_len,
const uint8_t *iv, size_t iv_len, uint8_t **output, size_t *output_len)
{
EVP_CIPHER_CTX *ctx;
const EVP_CIPHER *cipher;
int rc, len, out_len;
uint8_t *out;
if (iv_len != 16) {
return LCB_EINVAL;
}
ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
return LCB_EINVAL;
}
cipher = EVP_aes_256_cbc();
rc = EVP_DecryptInit_ex(ctx, cipher, NULL, common_aes256_key, iv);
if (rc != 1) {
EVP_CIPHER_CTX_free(ctx);
return LCB_EINVAL;
}
out = calloc(input_len, sizeof(uint8_t));
rc = EVP_DecryptUpdate(ctx, out, &len, input, input_len);
if (rc != 1) {
free(out);
EVP_CIPHER_CTX_free(ctx);
return LCB_EINVAL;
}
out_len = len;
rc = EVP_DecryptFinal_ex(ctx, out + len, &len);
if (rc != 1) {
free(out);
EVP_CIPHER_CTX_free(ctx);
return LCB_EINVAL;
}
out_len += len;
EVP_CIPHER_CTX_free(ctx);
*output = out;
*output_len = out_len;
return LCB_SUCCESS;
}
lcbcrypto_PROVIDER *osp_create()
{
lcbcrypto_PROVIDER *provider = calloc(1, sizeof(lcbcrypto_PROVIDER));
provider->version = 1;
provider->destructor = osp_free;
provider->v.v1.release_bytes = osp_release_bytes;
provider->v.v1.generate_iv = osp_generate_iv;
provider->v.v1.sign = osp_sign;
provider->v.v1.verify_signature = osp_verify_signature;
provider->v.v1.encrypt = osp_encrypt;
provider->v.v1.decrypt = osp_decrypt;
provider->v.v1.get_key_id = osp_get_key_id;
return provider;
}
void osp_initialize()
{
SSL_library_init();
SSL_load_error_strings();
EVP_add_cipher(EVP_aes_256_cbc());
}