-
Notifications
You must be signed in to change notification settings - Fork 2
/
redicrypt.c
272 lines (214 loc) · 9.18 KB
/
redicrypt.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
272
#include <stdlib.h>
#include <string.h>
#include "dist/redicrypt_go.h"
#include "deps/redismodule.h"
const RedisModuleString* _encrypt(RedisModuleCtx *ctx, char* ptr) {
const char *res = Encrypt(getenv("REDICRYPT_KEY"), ptr);
RedisModuleString *encrypted = RedisModule_CreateString(ctx, res, strlen(res));
return encrypted;
}
const RedisModuleString* _decrypt(RedisModuleCtx *ctx, const char* key, char* ptr) {
const char *res = Decrypt(key, ptr);
RedisModuleString *decrypted = RedisModule_CreateString(ctx, res, strlen(res));
return decrypted;
}
/* Set a redis key to a base64-encoded value */
int SetB64Command (RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 3) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
size_t s;
const char *str = RedisModule_StringPtrLen(argv[2], &s);
const char *res = Base64Encode(str);
RedisModuleString *encoded = RedisModule_CreateString(ctx, res, strlen(res));
RedisModuleCallReply *srep = RedisModule_Call(ctx, "SET", "ss", argv[1], encoded);
if (RedisModule_CallReplyType(srep) == REDISMODULE_REPLY_ERROR) {
return RedisModule_ReplyWithCallReply(ctx, srep);
}
RedisModule_ReplyWithSimpleString(ctx, "OK");
return REDISMODULE_OK;
}
/* Unbase-64encode a redis key value */
int GetB64Command (RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 2) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
RedisModuleCallReply *reply = RedisModule_Call(ctx, "GET", "s", argv[1]);
if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ERROR) {
return RedisModule_ReplyWithCallReply(ctx, reply);
}
size_t len;
char *ptr = RedisModule_CallReplyStringPtr(reply, &len);
const char *res = Base64Decode(ptr);
RedisModuleString *unhashed = RedisModule_CreateString(ctx, res, strlen(res));
RedisModule_ReplyWithString(ctx, unhashed);
return REDISMODULE_OK;
}
/* Set the encrypted value for a key, b64encoding the key */
int BSetEncCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 3) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
size_t s;
const char *str = RedisModule_StringPtrLen(argv[2], &s);
RedisModuleString *ciphered = _encrypt(ctx, str);
const char *res = Base64Encode(argv[1]);
RedisModuleString *key= RedisModule_CreateString(ctx, res, strlen(res));
RedisModuleCallReply *srep = RedisModule_Call(ctx, "SET", "ss", key, ciphered);
if (RedisModule_CallReplyType(srep) == REDISMODULE_REPLY_ERROR) {
return RedisModule_ReplyWithCallReply(ctx, srep);
}
RedisModule_ReplyWithSimpleString(ctx, "OK");
return REDISMODULE_OK;
}
/* Set an encrypted value */
int SetEncCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 3) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
size_t s;
const char *str = RedisModule_StringPtrLen(argv[2], &s);
RedisModuleString *ciphered = _encrypt(ctx, str);
RedisModuleCallReply *srep = RedisModule_Call(ctx, "SET", "ss", argv[1], ciphered);
if (RedisModule_CallReplyType(srep) == REDISMODULE_REPLY_ERROR) {
return RedisModule_ReplyWithCallReply(ctx, srep);
}
RedisModule_ReplyWithSimpleString(ctx, "OK");
return REDISMODULE_OK;
}
/* Generate a new encrytion key, on the back end */
int GenerateKeyCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
RedisModule_AutoMemory(ctx);
size_t s;
const char *bits = RedisModule_StringPtrLen(argv[1], &s);
int ibits = atoi(bits);
if (ibits == 0) {
return REDISMODULE_ERR;
}
GenerateKey(ibits);
RedisModule_ReplyWithSimpleString(ctx, "OK");
return REDISMODULE_OK;
}
/* Give the server a specific encryption key to use */
int SetKeyCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 2) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
size_t s;
const char *key = RedisModule_StringPtrLen(argv[1], &s);
SetKey(key);
RedisModule_ReplyWithSimpleString(ctx, "OK");
return REDISMODULE_OK;
}
/* Set a value, hashed
* [1] = Hash Type
* [2] = Redis key to set
* [3] = Redis value to hash */
int SetHashCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 4) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
size_t s;
const char *hashType = RedisModule_StringPtrLen(argv[1], &s);
const char *str = RedisModule_StringPtrLen(argv[3], &s);
const char *res = Hash(hashType, str);
RedisModuleString *hashed = RedisModule_CreateString(ctx, res, strlen(res));
RedisModuleCallReply *srep = RedisModule_Call(ctx, "SET", "ss", argv[2], hashed);
if (RedisModule_CallReplyType(srep) == REDISMODULE_REPLY_ERROR) {
return RedisModule_ReplyWithCallReply(ctx, srep);
}
RedisModule_ReplyWithSimpleString(ctx, "OK");
return REDISMODULE_OK;
}
/* Decrypt an existing redis key, returning the existing string. */
int GetEncCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 2) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
RedisModuleCallReply *reply = RedisModule_Call(ctx, "GET", "s", argv[1]);
if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ERROR) {
return RedisModule_ReplyWithCallReply(ctx, reply);
}
size_t len;
char *ptr = RedisModule_CallReplyStringPtr(reply,&len);
RedisModuleString *decrypted = _decrypt(ctx, getenv("REDICRYPT_KEY"), ptr);
RedisModule_ReplyWithString(ctx, decrypted);
return REDISMODULE_OK;
}
/* Decrypt an existing redis key, returning the existing string. */
int BGetEncCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc != 2) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
const char *res = Base64Decode(argv[1]);
RedisModuleString *key = RedisModule_CreateString(ctx, res, strlen(res));
RedisModuleCallReply *reply = RedisModule_Call(ctx, "GET", "s", key);
if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ERROR) {
return RedisModule_ReplyWithCallReply(ctx, reply);
}
size_t len;
char *ptr = RedisModule_CallReplyStringPtr(reply,&len);
RedisModuleString *decrypted = _decrypt(ctx, getenv("REDICRYPT_KEY"), ptr);
RedisModule_ReplyWithString(ctx, decrypted);
return REDISMODULE_OK;
}
/* Decrypt existing encrypted key, and recrypt */
int RecryptCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (argc >= 2) return RedisModule_WrongArity(ctx);
RedisModule_AutoMemory(ctx);
RedisModuleCallReply *reply = RedisModule_Call(ctx, "GET", "s", argv[1]);
if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ERROR) {
return RedisModule_ReplyWithCallReply(ctx, reply);
}
size_t len;
char *ptr = RedisModule_CallReplyStringPtr(reply,&len);
// decrypted
RedisModuleString *decrypted = _decrypt(ctx, getenv("OLD_REDICRYPT_KEY"), ptr);
size_t *enc_len;
const char *dechar = RedisModule_StringPtrLen(decrypted, enc_len);
// recrypt
RedisModuleString *ciphered = _encrypt(ctx, dechar);
RedisModuleCallReply *srep = RedisModule_Call(ctx, "SET", "ss", argv[1], ciphered);
if (RedisModule_CallReplyType(srep) == REDISMODULE_REPLY_ERROR) {
return RedisModule_ReplyWithCallReply(ctx, srep);
}
RedisModule_ReplyWithSimpleString(ctx, "OK");
return REDISMODULE_OK;
}
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
if (RedisModule_Init(ctx,"redicrypt",1,REDISMODULE_APIVER_1)
== REDISMODULE_ERR) return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"RC.SETENC",
SetEncCommand, "write",
0, 0, 0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"RC.KEYGEN",
GenerateKeyCommand, "write",
0, 0, 0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"RC.SETKEY",
SetKeyCommand, "write",
0, 0, 0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"RC.BSETENC",
BSetEncCommand, "write",
0, 0, 0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"RC.SETB64",
SetB64Command, "write",
0, 0, 0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"RC.SETHASH",
SetHashCommand, "write",
0, 0, 0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"RC.GETB64",
GetB64Command, "readonly",
0, 0, 0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"RC.BGETENC",
BGetEncCommand, "readonly",
0, 0, 0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"RC.GETENC",
GetEncCommand, "readonly",
0, 0, 0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
if (RedisModule_CreateCommand(ctx,"RC.RECRYPT",
RecryptCommand, "write",
0, 0, 0) == REDISMODULE_ERR)
return REDISMODULE_ERR;
return REDISMODULE_OK;
}