-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevp_openssl11-patch.lua
404 lines (345 loc) · 11.5 KB
/
evp_openssl11-patch.lua
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
-- Copyright (C) by Daniel Hiltgen ([email protected])
local ffi = require "ffi"
local _C = ffi.C
local _M = { _VERSION = "0.0.2" }
local CONST = {
SHA256_DIGEST = "SHA256",
SHA512_DIGEST = "SHA512",
}
_M.CONST = CONST
-- Reference: https://wiki.openssl.org/index.php/EVP_Signing_and_Verifying
ffi.cdef[[
// Error handling
unsigned long ERR_get_error(void);
const char * ERR_reason_error_string(unsigned long e);
// Basic IO
typedef struct bio_st BIO;
typedef struct bio_method_st BIO_METHOD;
BIO_METHOD *BIO_s_mem(void);
BIO * BIO_new(BIO_METHOD *type);
int BIO_puts(BIO *bp,const char *buf);
void BIO_vfree(BIO *a);
int BIO_write(BIO *b, const void *buf, int len);
// RSA
typedef struct rsa_st RSA;
int RSA_size(const RSA *rsa);
void RSA_free(RSA *rsa);
typedef int pem_password_cb(char *buf, int size, int rwflag, void *userdata);
RSA * PEM_read_bio_RSAPrivateKey(BIO *bp, RSA **rsa, pem_password_cb *cb,
void *u);
RSA * PEM_read_bio_RSAPublicKey(BIO *bp, RSA **rsa, pem_password_cb *cb,
void *u);
// EVP PKEY
typedef struct evp_pkey_st EVP_PKEY;
typedef struct engine_st ENGINE;
EVP_PKEY *EVP_PKEY_new(void);
int EVP_PKEY_set1_RSA(EVP_PKEY *pkey,RSA *key);
EVP_PKEY *EVP_PKEY_new_mac_key(int type, ENGINE *e,
const unsigned char *key, int keylen);
void EVP_PKEY_free(EVP_PKEY *key);
int i2d_RSA(RSA *a, unsigned char **out);
// PUBKEY
EVP_PKEY *PEM_read_bio_PUBKEY(BIO *bp, EVP_PKEY **x,
pem_password_cb *cb, void *u);
// X509
typedef struct x509_st X509;
X509 *PEM_read_bio_X509(BIO *bp, X509 **x, pem_password_cb *cb, void *u);
EVP_PKEY * X509_get_pubkey(X509 *x);
void X509_free(X509 *a);
void EVP_PKEY_free(EVP_PKEY *key);
int i2d_X509(X509 *a, unsigned char **out);
X509 *d2i_X509_bio(BIO *bp, X509 **x);
// X509 store
typedef struct x509_store_st X509_STORE;
typedef struct X509_crl_st X509_CRL;
X509_STORE *X509_STORE_new(void );
int X509_STORE_add_cert(X509_STORE *ctx, X509 *x);
// Use this if we want to load the certs directly from a variables
int X509_STORE_add_crl(X509_STORE *ctx, X509_CRL *x);
int X509_STORE_load_locations (X509_STORE *ctx,
const char *file, const char *dir);
void X509_STORE_free(X509_STORE *v);
// X509 store context
typedef struct x509_store_ctx_st X509_STORE_CTX;
X509_STORE_CTX *X509_STORE_CTX_new(void);
int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store,
X509 *x509, void *chain);
int X509_verify_cert(X509_STORE_CTX *ctx);
void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx);
int X509_STORE_CTX_get_error(X509_STORE_CTX *ctx);
const char *X509_verify_cert_error_string(long n);
void X509_STORE_CTX_free(X509_STORE_CTX *ctx);
// EVP Sign/Verify
typedef struct env_md_ctx_st EVP_MD_CTX;
typedef struct env_md_st EVP_MD;
typedef struct evp_pkey_ctx_st EVP_PKEY_CTX;
const EVP_MD *EVP_get_digestbyname(const char *name);
// Begin Patch: openssl 1.1 函数更名 EVP_MD_CTX_create -> EVP_MD_CTX_new EVP_MD_CTX_destroy -> EVP_MD_CTX_free
//EVP_MD_CTX *EVP_MD_CTX_create(void);
EVP_MD_CTX *EVP_MD_CTX_new(void);
//void EVP_MD_CTX_destroy(EVP_MD_CTX *ctx);
void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
// End Patch
int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type, ENGINE *impl);
int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey);
int EVP_DigestUpdate(EVP_MD_CTX *ctx,const void *d,
size_t cnt);
int EVP_DigestSignFinal(EVP_MD_CTX *ctx,
unsigned char *sigret, size_t *siglen);
int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx,
const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey);
int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx,
unsigned char *sig, size_t siglen);
// Fingerprints
int X509_digest(const X509 *data,const EVP_MD *type,
unsigned char *md, unsigned int *len);
]]
local function _err(ret)
local code = _C.ERR_get_error()
if code == 0 then
return ret, "Zero error code (null arguments?)"
end
return ret, ffi.string(_C.ERR_reason_error_string(code))
end
local RSASigner = {}
_M.RSASigner = RSASigner
--- Create a new RSASigner
-- @param pem_private_key A private key string in PEM format
-- @returns RSASigner, err_string
function RSASigner.new(self, pem_private_key)
local bio = _C.BIO_new(_C.BIO_s_mem())
ffi.gc(bio, _C.BIO_vfree)
if _C.BIO_puts(bio, pem_private_key) < 0 then
return _err()
end
-- TODO might want to support password protected private keys...
local rsa = _C.PEM_read_bio_RSAPrivateKey(bio, nil, nil, nil)
ffi.gc(rsa, _C.RSA_free)
local evp_pkey = _C.EVP_PKEY_new()
if not evp_pkey then
return _err()
end
ffi.gc(evp_pkey, _C.EVP_PKEY_free)
if _C.EVP_PKEY_set1_RSA(evp_pkey, rsa) ~= 1 then
return _err()
end
self.evp_pkey = evp_pkey
return self, nil
end
--- Sign a message
-- @param message The message to sign
-- @param digest_name The digest format to use (e.g., "SHA256")
-- @returns signature, error_string
function RSASigner.sign(self, message, digest_name)
local buf = ffi.new("unsigned char[?]", 1024)
local len = ffi.new("size_t[1]", 1024)
local ctx = _C.EVP_MD_CTX_new()
if not ctx then
return _err()
end
ffi.gc(ctx, _C.EVP_MD_CTX_free)
local md = _C.EVP_get_digestbyname(digest_name)
if not md then
return _err()
end
if _C.EVP_DigestInit_ex(ctx, md, nil) ~= 1 then
return _err()
end
local ret = _C.EVP_DigestSignInit(ctx, nil, md, nil, self.evp_pkey)
if ret ~= 1 then
return _err()
end
if _C.EVP_DigestUpdate(ctx, message, #message) ~= 1 then
return _err()
end
if _C.EVP_DigestSignFinal(ctx, buf, len) ~= 1 then
return _err()
end
return ffi.string(buf, len[0]), nil
end
local RSAVerifier = {}
_M.RSAVerifier = RSAVerifier
--- Create a new RSAVerifier
-- @param key_source An instance of Cert or PublicKey used for verification
-- @returns RSAVerifier, error_string
function RSAVerifier.new(self, key_source)
if not key_source then
return nil, "You must pass in an key_source for a public key"
end
local evp_public_key = key_source.public_key
self.evp_pkey = evp_public_key
return self, nil
end
--- Verify a message is properly signed
-- @param message The original message
-- @param the signature to verify
-- @param digest_name The digest type that was used to sign
-- @returns bool, error_string
function RSAVerifier.verify(self, message, sig, digest_name)
local md = _C.EVP_get_digestbyname(digest_name)
if not md then
return _err(false)
end
local ctx = _C.EVP_MD_CTX_new()
if not ctx then
return _err(false)
end
ffi.gc(ctx, _C.EVP_MD_CTX_free)
if _C.EVP_DigestInit_ex(ctx, md, nil) ~= 1 then
return _err(false)
end
local ret = _C.EVP_DigestVerifyInit(ctx, nil, md, nil, self.evp_pkey)
if ret ~= 1 then
return _err(false)
end
if _C.EVP_DigestUpdate(ctx, message, #message) ~= 1 then
return _err(false)
end
local sig_bin = ffi.new("unsigned char[?]", #sig)
ffi.copy(sig_bin, sig, #sig)
if _C.EVP_DigestVerifyFinal(ctx, sig_bin, #sig) == 1 then
return true, nil
else
return false, "Verification failed"
end
end
local Cert = {}
_M.Cert = Cert
--- Create a new Certificate object
-- @param payload A PEM or DER format X509 certificate
-- @returns Cert, error_string
function Cert.new(self, payload)
if not payload then
return nil, "Must pass a PEM or binary DER cert"
end
local bio = _C.BIO_new(_C.BIO_s_mem())
ffi.gc(bio, _C.BIO_vfree)
local x509
if payload:find('-----BEGIN') then
if _C.BIO_puts(bio, payload) < 0 then
return _err()
end
x509 = _C.PEM_read_bio_X509(bio, nil, nil, nil)
else
if _C.BIO_write(bio, payload, #payload) < 0 then
return _err()
end
x509 = _C.d2i_X509_bio(bio, nil)
end
if not x509 then
return _err()
end
ffi.gc(x509, _C.X509_free)
self.x509 = x509
local public_key, err = self:get_public_key()
if not public_key then
return nil, err
end
ffi.gc(public_key, _C.EVP_PKEY_free)
self.public_key = public_key
return self, nil
end
--- Retrieve the DER format of the certificate
-- @returns Binary DER format
function Cert.get_der(self)
local bufp = ffi.new("unsigned char *[1]")
local len = _C.i2d_X509(self.x509, bufp)
if len < 0 then
return _err()
end
local der = ffi.string(bufp[0], len)
return der, nil
end
--- Retrieve the cert fingerprint
-- @param digest_name the Type of digest to use (e.g., "SHA256")
-- @returns fingerprint_string
function Cert.get_fingerprint(self, digest_name)
local md = _C.EVP_get_digestbyname(digest_name)
if not md then
return _err()
end
local buf = ffi.new("unsigned char[?]", 32)
local len = ffi.new("unsigned int[1]", 32)
if _C.X509_digest(self.x509, md, buf, len) ~= 1 then
return _err()
end
local raw = ffi.string(buf, len[0])
local t = {}
raw:gsub('.', function (c) table.insert(t, string.format('%02X', string.byte(c))) end)
return table.concat(t, ":"), nil
end
--- Retrieve the public key from the CERT
-- @returns An OpenSSL EVP PKEY object representing the public key
function Cert.get_public_key(self)
local evp_pkey = _C.X509_get_pubkey(self.x509)
if not evp_pkey then
return _err()
end
return evp_pkey, nil
end
--- Verify the Certificate is trusted
-- @param trusted_cert_file File path to a list of PEM encoded trusted certificates
-- @return bool, error_string
function Cert.verify_trust(self, trusted_cert_file)
local store = _C.X509_STORE_new()
if not store then
return _err(false)
end
ffi.gc(store, _C.X509_STORE_free)
if _C.X509_STORE_load_locations(store, trusted_cert_file, nil) ~=1 then
return _err(false)
end
local ctx = _C.X509_STORE_CTX_new()
if not store then
return _err(false)
end
ffi.gc(ctx, _C.X509_STORE_CTX_free)
if _C.X509_STORE_CTX_init(ctx, store, self.x509, nil) ~= 1 then
return _err(false)
end
if _C.X509_verify_cert(ctx) ~= 1 then
local code = _C.X509_STORE_CTX_get_error(ctx)
local msg = ffi.string(_C.X509_verify_cert_error_string(code))
_C.X509_STORE_CTX_cleanup(ctx)
return false, msg
end
_C.X509_STORE_CTX_cleanup(ctx)
return true, nil
end
local PublicKey = {}
_M.PublicKey = PublicKey
--- Create a new PublicKey object
--
-- If a PEM fornatted key is provided, the key must start with
--
-- ----- BEGIN PUBLIC KEY -----
--
-- @param payload A PEM or DER format public key file
-- @return PublicKey, error_string
function PublicKey.new(self, payload)
if not payload then
return nil, "Must pass a PEM or binary DER public key"
end
local bio = _C.BIO_new(_C.BIO_s_mem())
ffi.gc(bio, _C.BIO_vfree)
local pkey
if payload:find('-----BEGIN') then
if _C.BIO_puts(bio, payload) < 0 then
return _err()
end
pkey = _C.PEM_read_bio_PUBKEY(bio, nil, nil, nil)
else
if _C.BIO_write(bio, payload, #payload) < 0 then
return _err()
end
pkey = _C.d2i_PUBKEY_bio(bio, nil)
end
if not pkey then
return _err()
end
ffi.gc(pkey, _C.EVP_PKEY_free)
self.public_key = pkey
return self, nil
end
return _M