forked from zhaozg/lua-openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpkey.c
More file actions
2549 lines (2287 loc) · 71.5 KB
/
pkey.c
File metadata and controls
2549 lines (2287 loc) · 71.5 KB
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***
pkey module to create and process public or private key, do asymmetric key operations.
@module pkey
@usage
pkey = require'openssl'.pkey
*/
#include <openssl/dh.h>
#include <openssl/dsa.h>
#include <openssl/engine.h>
#include <openssl/rsa.h>
#include "openssl.h"
#include "private.h"
/* Suppress deprecation warnings for low-level key APIs in OpenSSL 3.0+
* This module provides direct Lua bindings to OpenSSL's low-level key APIs.
* These APIs are deprecated in OpenSSL 3.0+ in favor of EVP_PKEY operations,
* but we maintain them for:
* 1. Backward compatibility with existing Lua code
* 2. Complete coverage of OpenSSL key functionality
* 3. Support for legacy key formats (PKCS#1 RSA, etc.)
*
* Migration to pure EVP_PKEY operations would require significant API changes
* and break backward compatibility. The current implementation is safe and
* well-tested across OpenSSL 1.1.x and 3.x versions.
*/
#if defined(__GNUC__) || defined(__clang__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#endif
#if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER)
#include <openssl/core_names.h>
#include <openssl/params.h>
#include <openssl/param_build.h>
#endif
/* LibreSSL does not have OPENSSL_clear_free() */
#include <openssl/crypto.h>
#if !defined(OPENSSL_clear_free)
static inline void OPENSSL_clear_free(void *ptr, size_t len) {
if (ptr) {
OPENSSL_cleanse(ptr, len);
OPENSSL_free(ptr);
}
}
#endif
static int evp_pkey_name2type(const char *name);
static const char *evp_pkey_type2name(int type);
/* Compatibility layer for low-level key access migration to OpenSSL 3.0+ PARAM API */
#if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER)
/* Helper: check if private key exists using PARAM API */
static int
pkey_has_private(EVP_PKEY *pkey)
{
BIGNUM *bn = NULL;
int ret = 0;
size_t priv_len = 0;
int typ = EVP_PKEY_type(EVP_PKEY_id(pkey));
switch (typ) {
#ifndef OPENSSL_NO_RSA
case EVP_PKEY_RSA: {
ret = EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_D, &bn);
break;
}
#endif
#ifndef OPENSSL_NO_DSA
case EVP_PKEY_DSA: {
ret = EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &bn);
if (!ret || bn == NULL) {
/* FIXME: OpenSSL 3.0 DSA priv key param not working? Fallback to legacy way */
DSA* dsa = (DSA*)EVP_PKEY_get0_DSA(pkey);
if (dsa) {
const BIGNUM* priv_key = NULL;
DSA_get0_key(dsa, NULL, &priv_key);
if (priv_key) {
bn = BN_dup(priv_key);
ret = 1;
}
}
}
break;
}
#endif
#ifndef OPENSSL_NO_DH
case EVP_PKEY_DH: {
ret = EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &bn);
break;
}
#endif
#ifndef OPENSSL_NO_EC
case EVP_PKEY_EC:
#ifdef EVP_PKEY_SM2
case EVP_PKEY_SM2:
#endif
{
ret = EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, &bn);
break;
}
#endif
#ifndef OPENSSL_NO_ED25519
case EVP_PKEY_ED25519: {
ret = EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0, &priv_len);
break;
}
#endif
#ifndef OPENSSL_NO_ED448
case EVP_PKEY_ED448: {
ret = EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0, &priv_len);
break;
}
#endif
#ifndef OPENSSL_NO_EX25519
case EVP_PKEY_X25519: {
ret = EVP_PKEY_get_octet_string_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0, &priv_len);
break;
}
#endif
default:
{
EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pkey, NULL);
if (EVP_PKEY_private_check(ctx))
ret = 1;
EVP_PKEY_CTX_free(ctx);
return ret;
}
}
if (ret && bn != NULL) {
ret = !BN_is_zero(bn);
BN_free(bn);
} else if (ret && priv_len > 0) {
ret = 1;
}
return ret;
}
/* Helper: check if key matches expected type */
static int
pkey_is_type(EVP_PKEY *pkey, int expected_type)
{
/* In OpenSSL 3.0+, if EVP_PKEY_id returns the expected type, the key exists */
return pkey != NULL && EVP_PKEY_type(EVP_PKEY_id(pkey)) == expected_type;
}
#endif
int
openssl_pkey_is_private(EVP_PKEY *pkey)
{
#if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER)
return pkey_has_private(pkey);
#else
/* OpenSSL 1.x way */
int ret = 0;
int typ = EVP_PKEY_type(EVP_PKEY_id(pkey));
switch (typ) {
#ifndef OPENSSL_NO_RSA
case EVP_PKEY_RSA: {
RSA *rsa = (RSA *)EVP_PKEY_get0_RSA(pkey);
const BIGNUM *d = NULL;
RSA_get0_key(rsa, NULL, NULL, &d);
ret = d != NULL;
break;
}
#endif
#ifndef OPENSSL_NO_DSA
case EVP_PKEY_DSA: {
DSA *dsa = (DSA *)EVP_PKEY_get0_DSA(pkey);
const BIGNUM *p = NULL;
DSA_get0_key(dsa, NULL, &p);
ret = p != NULL;
break;
}
#endif
#ifndef OPENSSL_NO_DH
case EVP_PKEY_DH: {
DH *dh = (DH *)EVP_PKEY_get0_DH(pkey);
const BIGNUM *p = NULL;
DH_get0_key(dh, NULL, &p);
ret = p != NULL;
break;
}
#endif
#ifndef OPENSSL_NO_EC
case EVP_PKEY_EC:
#ifdef EVP_PKEY_SM2
case EVP_PKEY_SM2:
#endif
{
EC_KEY *ec = (EC_KEY *)EVP_PKEY_get0_EC_KEY(pkey);
const BIGNUM *p = EC_KEY_get0_private_key(ec);
ret = p != NULL;
break;
}
#endif
default:
break;
}
return ret;
#endif
}
#if defined(OPENSSL_SUPPORT_SM2)
static int
openssl_pkey_is_sm2(const EVP_PKEY *pkey)
{
int id;
#if OPENSSL_VERSION_NUMBER > 0x30000000
id = EVP_PKEY_get_id(pkey);
if (id == NID_sm2) return 1;
#else
id = EVP_PKEY_id(pkey);
if (id == EVP_PKEY_SM2) return 1;
#endif
id = EVP_PKEY_base_id(pkey);
if (id == EVP_PKEY_EC) {
#if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER)
/* OpenSSL 3.0+ way: use PARAM API to get curve name */
char curve_name[256];
size_t curve_name_len = sizeof(curve_name);
if (EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
curve_name, sizeof(curve_name), &curve_name_len)) {
/* Check if the group name is SM2 */
if (strcmp(curve_name, "SM2") == 0) {
return 1;
}
/* Convert group name to NID and check */
int curve = OBJ_sn2nid(curve_name);
return curve == NID_sm2;
}
#else
/* OpenSSL 1.x way */
const EC_KEY *ec = EVP_PKEY_get0_EC_KEY((EVP_PKEY *)pkey);
const EC_GROUP *grp = EC_KEY_get0_group(ec);
int curve = EC_GROUP_get_curve_name(grp);
return curve == NID_sm2;
#endif
}
return 0;
}
#endif
/***
read public/private key from data
@function read
@tparam string|openssl.bio input string data or bio object
@tparam[opt=false] boolean priv prikey set true when input is private key
@tparam[opt='auto'] string format format or encoding of input, support 'auto','pem','der'
@tparam[opt] string passhprase when input is private key, or key types 'ec','rsa','dsa','dh'
@treturn openssl.evp_pkey public key
@see evp_pkey
*/
static int
openssl_pkey_read(lua_State *L)
{
EVP_PKEY *key = NULL;
BIO *in = load_bio_object(L, 1);
int priv = lua_isnone(L, 2) ? 0 : auxiliar_checkboolean(L, 2);
int fmt = luaL_checkoption(L, 3, "auto", format);
const char *passphrase = luaL_optstring(L, 4, NULL);
int type = passphrase != NULL ? evp_pkey_name2type(passphrase) : -1;
if (fmt == FORMAT_AUTO) {
fmt = bio_is_der(in) ? FORMAT_DER : FORMAT_PEM;
}
if (!priv) {
if (fmt == FORMAT_PEM) {
switch (type) {
#ifndef OPENSSL_NO_RSA
case EVP_PKEY_RSA: {
RSA *rsa = PEM_read_bio_RSAPublicKey(in, NULL, NULL, NULL);
if (rsa) {
key = EVP_PKEY_new();
EVP_PKEY_assign_RSA(key, rsa);
}
break;
}
#endif
#ifndef OPENSSL_NO_DSA
case EVP_PKEY_DSA: {
DSA *dsa = PEM_read_bio_DSA_PUBKEY(in, NULL, NULL, NULL);
if (dsa) {
key = EVP_PKEY_new();
EVP_PKEY_assign_DSA(key, dsa);
}
break;
}
#endif
#ifndef OPENSSL_NO_EC
case EVP_PKEY_EC: {
EC_KEY *ec = PEM_read_bio_EC_PUBKEY(in, NULL, NULL, NULL);
if (ec) {
key = EVP_PKEY_new();
EVP_PKEY_assign_EC_KEY(key, ec);
}
break;
}
#endif
default: {
key = PEM_read_bio_PUBKEY(in, NULL, NULL, NULL);
break;
}
}
(void)BIO_reset(in);
} else if (fmt == FORMAT_DER) {
switch (type) {
#ifndef OPENSSL_NO_RSA
case EVP_PKEY_RSA: {
RSA *rsa = d2i_RSAPublicKey_bio(in, NULL);
if (rsa) {
key = EVP_PKEY_new();
EVP_PKEY_assign_RSA(key, rsa);
}
break;
}
#endif
#ifndef OPENSSL_NO_DSA
case EVP_PKEY_DSA: {
DSA *dsa = d2i_DSA_PUBKEY_bio(in, NULL);
if (dsa) {
key = EVP_PKEY_new();
EVP_PKEY_assign_DSA(key, dsa);
}
break;
}
#endif
#ifndef OPENSSL_NO_EC
case EVP_PKEY_EC: {
EC_KEY *ec = d2i_EC_PUBKEY_bio(in, NULL);
if (ec) {
key = EVP_PKEY_new();
EVP_PKEY_assign_EC_KEY(key, ec);
}
break;
}
#endif
default:
key = d2i_PUBKEY_bio(in, NULL);
break;
}
(void)BIO_reset(in);
}
} else {
if (fmt == FORMAT_PEM) {
key = PEM_read_bio_PrivateKey(in, NULL, NULL, (void *)passphrase);
(void)BIO_reset(in);
} else if (fmt == FORMAT_DER) {
switch (type) {
#ifndef OPENSSL_NO_RSA
case EVP_PKEY_RSA: {
RSA *rsa = d2i_RSAPrivateKey_bio(in, NULL);
if (rsa) {
key = EVP_PKEY_new();
EVP_PKEY_assign_RSA(key, rsa);
}
break;
}
#endif
#ifndef OPENSSL_NO_DSA
case EVP_PKEY_DSA: {
DSA *dsa = d2i_DSAPrivateKey_bio(in, NULL);
if (dsa) {
key = EVP_PKEY_new();
EVP_PKEY_assign_DSA(key, dsa);
}
break;
}
#endif
#ifndef OPENSSL_NO_EC
case EVP_PKEY_EC: {
EC_KEY *ec = d2i_ECPrivateKey_bio(in, NULL);
if (ec) {
key = EVP_PKEY_new();
EVP_PKEY_assign_EC_KEY(key, ec);
}
break;
}
#endif
default: {
if (passphrase)
key = d2i_PKCS8PrivateKey_bio(in, NULL, NULL, (void *)passphrase);
else
key = d2i_PrivateKey_bio(in, NULL);
break;
}
}
(void)BIO_reset(in);
}
}
BIO_free(in);
if (key) PUSH_OBJECT(key, "openssl.evp_pkey");
return key ? 1 : openssl_pushresult(L, 0);
}
#ifndef OPENSSL_NO_EC
static int
EC_KEY_generate_key_part(EC_KEY *eckey)
{
int ok = 0;
BN_CTX *ctx = NULL;
BIGNUM *priv_key = NULL, *order = NULL;
EC_POINT *pub_key = NULL;
const EC_GROUP *group;
group = EC_KEY_get0_group(eckey);
if ((order = BN_new()) == NULL) goto err;
if ((ctx = BN_CTX_new()) == NULL) goto err;
priv_key = (BIGNUM *)EC_KEY_get0_private_key(eckey);
if (priv_key == NULL) goto err;
if (!EC_GROUP_get_order(group, order, ctx)) goto err;
if (BN_is_zero(priv_key)) goto err;
pub_key = (EC_POINT *)EC_KEY_get0_public_key(eckey);
if (pub_key == NULL) {
pub_key = EC_POINT_new(group);
if (pub_key == NULL) goto err;
EC_KEY_set_public_key(eckey, pub_key);
EC_POINT_free(pub_key);
pub_key = (EC_POINT *)EC_KEY_get0_public_key(eckey);
}
if (!EC_POINT_mul(group, pub_key, priv_key, NULL, NULL, ctx)) goto err;
EC_POINT_make_affine(EC_KEY_get0_group(eckey), pub_key, NULL);
ok = 1;
err:
if (order) BN_free(order);
if (ctx != NULL) BN_CTX_free(ctx);
return (ok);
}
#endif
#define EC_GET_FIELD(_name) \
{ \
lua_getfield(L, -1, #_name); \
if (lua_isstring(L, -1)) { \
size_t l = 0; \
const char *bn = luaL_checklstring(L, -1, &l); \
if (_name == NULL) _name = BN_new(); \
BN_bin2bn((const unsigned char *)bn, l, _name); \
} else if (auxiliar_getclassudata(L, "openssl.bn", -1)) { \
const BIGNUM *bn = CHECK_OBJECT(-1, BIGNUM, "openssl.bn"); \
if (_name == NULL) _name = BN_new(); \
BN_copy(_name, bn); \
} else if (!lua_isnil(L, -1)) \
luaL_error(L, "parameters must have \"%s\" field string or openssl.bn", #_name); \
lua_pop(L, 1); \
}
/***
generate a new ec keypair
@function new
@tparam string alg alg must be 'ec'
@tparam string|number curvename this can be integer as curvename NID
@tparam[opt] integer flags when alg is ec need this.
@treturn openssl.evp_pkey object with mapping to EVP_PKEY in openssl
*/
/***
generate a new keypair
@function new
@tparam[opt='rsa'] string alg accept `rsa`,`dsa`,`dh`
@tparam[opt=2048|512] integer bits `rsa` with 2048, `dh` or `dsa` with 1024
@tparam[opt] integer e when alg is `rsa` give e value default is 0x10001,
when alg is `dh` give generator value default is 2,
when alg is `dsa` give string type seed value default is none.
@tparam[opt] openssl.engine eng
@treturn openssl.evp_pkey object with mapping to EVP_PKEY in openssl
*/
/***
create a new keypair by factors of keypair or get public key only
@function new
@tparam table factors create private/public key, key alg only accept accept 'rsa','dsa','dh','ec'
and must exist</br>
when arg is rsa, table may with key n,e,d,p,q,dmp1,dmq1,iqmp, both are binary string or openssl.bn<br>
when arg is dsa, table may with key p,q,g,priv_key,pub_key, both are binary string or openssl.bn<br>
when arg is dh, table may with key p,g,priv_key,pub_key, both are binary string or openssl.bn<br>
when arg is ec, table may with d,x,y,z,both are binary string or openssl.bn, and with curve_name,
enc_flag, conv_form<br>
@treturn openssl.evp_pkey object with mapping to EVP_PKEY in openssl
@usage
--create rsa public key
pubkey = new({alg='rsa',n=...,e=...}
--create new rsa
rsa = new({alg='rsa',n=...,q=...,e=...,...}
*/
static int openssl_pkey_new(lua_State *L)
{
EVP_PKEY *pkey = NULL;
const char *alg = "rsa";
if (lua_isnoneornil(L, 1) || lua_isstring(L, 1)) {
alg = luaL_optstring(L, 1, alg);
#ifndef OPENSSL_NO_RSA
if (strcasecmp(alg, "rsa") == 0) {
int bits = luaL_optint(L, 2, 2048);
int e = luaL_optint(L, 3, 65537);
ENGINE *eng = lua_isnoneornil(L, 4) ? NULL : CHECK_OBJECT(4, ENGINE, "openssl.engine");
BIGNUM *E = BN_new();
luaL_argcheck(L, e > 0, 3, "e must be positive integer");
BN_set_word(E, e);
RSA *rsa = eng ? RSA_new_method(eng) : RSA_new();
if (RSA_generate_key_ex(rsa, bits, E, NULL)) {
pkey = EVP_PKEY_new();
EVP_PKEY_assign_RSA(pkey, rsa);
} else
RSA_free(rsa);
BN_free(E);
} else
#endif
#ifndef OPENSSL_NO_DSA
if (strcasecmp(alg, "dsa") == 0)
{
int bits = luaL_optint(L, 2, 1024);
size_t seed_len = 0;
const char *seed = luaL_optlstring(L, 3, NULL, &seed_len);
ENGINE *eng = lua_isnoneornil(L, 4) ? NULL : CHECK_OBJECT(4, ENGINE, "openssl.engine");
DSA *dsa = eng ? DSA_new_method(eng) : DSA_new();
if (DSA_generate_parameters_ex(dsa, bits, (byte *)seed, seed_len, NULL, NULL, NULL)
&& DSA_generate_key(dsa))
{
pkey = EVP_PKEY_new();
EVP_PKEY_assign_DSA(pkey, dsa);
} else
DSA_free(dsa);
} else
#endif
#ifndef OPENSSL_NO_DH
if (strcasecmp(alg, "dh") == 0)
{
int bits = luaL_optint(L, 2, 1024);
int generator = luaL_optint(L, 3, 2);
ENGINE *eng = lua_isnoneornil(L, 4) ? NULL : CHECK_OBJECT(4, ENGINE, "openssl.engine");
DH *dh = eng ? DH_new_method(eng) : DH_new();
if (DH_generate_parameters_ex(dh, bits, generator, NULL)) {
if (DH_generate_key(dh)) {
pkey = EVP_PKEY_new();
EVP_PKEY_assign_DH(pkey, dh);
} else
DH_free(dh);
} else
DH_free(dh);
} else
#endif
#ifndef OPENSSL_NO_EC
if (strcasecmp(alg, "ec") == 0)
{
EC_GROUP *group = openssl_get_ec_group(L, 2, 3, 4);
if (!group) luaL_error(L, "failed to get ec_group object");
EC_KEY *ec = NULL;
ec = EC_KEY_new();
if (ec) {
EC_KEY_set_group(ec, group);
EC_GROUP_free(group);
if (EC_KEY_generate_key(ec)) {
#if OPENSSL_VERSION_NUMBER > 0x30000000L
EC_KEY_generate_key_part(ec);
#endif
pkey = EVP_PKEY_new();
EVP_PKEY_assign_EC_KEY(pkey, ec);
} else
EC_KEY_free(ec);
} else
EC_GROUP_free(group);
}
#endif
else
{
luaL_error(L, "not support %s!!!!", alg);
}
} else if (lua_istable(L, 1)) {
/* contruct key from factors in Lua table */
lua_getfield(L, 1, "alg");
alg = luaL_optstring(L, -1, alg);
lua_pop(L, 1);
#ifndef OPENSSL_NO_RSA
if (strcasecmp(alg, "rsa") == 0) {
BIGNUM *n = NULL, *e = NULL, *d = NULL;
BIGNUM *p = NULL, *q = NULL;
BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
lua_getfield(L, 1, "n");
n = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "e");
e = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "d");
d = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "p");
p = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "q");
q = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "dmp1");
dmp1 = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "dmq1");
dmq1 = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "iqmp");
iqmp = BN_get(L, -1);
lua_pop(L, 1);
#if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER)
pkey = openssl_new_pkey_rsa_with(n, e, d,
p, q, dmp1, dmq1, iqmp);
BN_free(n);
BN_free(e);
BN_free(d);
BN_free(p);
BN_free(q);
BN_free(dmp1);
BN_free(dmq1);
BN_free(iqmp);
#else
pkey = EVP_PKEY_new();
if (pkey) {
RSA *rsa = RSA_new();
if (rsa) {
if (RSA_set0_key(rsa, n, e, d) == 1 && (p == NULL || RSA_set0_factors(rsa, p, q) == 1)
&& (dmp1 == NULL || RSA_set0_crt_params(rsa, dmp1, dmq1, iqmp) == 1))
{
if (!EVP_PKEY_assign_RSA(pkey, rsa)) {
RSA_free(rsa);
rsa = NULL;
EVP_PKEY_free(pkey);
pkey = NULL;
}
} else {
RSA_free(rsa);
rsa = NULL;
EVP_PKEY_free(pkey);
pkey = NULL;
}
}
}
#endif
} else
#endif
#ifndef OPENSSL_NO_DSA
if (strcasecmp(alg, "dsa") == 0)
{
BIGNUM *p = NULL, *q = NULL, *g = NULL;
BIGNUM *priv_key = NULL, *pub_key = NULL;
lua_getfield(L, 1, "p");
p = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "q");
q = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "g");
g = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "priv_key");
priv_key = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "pub_key");
pub_key = BN_get(L, -1);
lua_pop(L, 1);
#if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER)
pkey = openssl_new_pkey_dsa_with(p, q, g, pub_key, priv_key);
BN_free(p);
BN_free(q);
BN_free(g);
BN_free(pub_key);
BN_free(priv_key);
#else
pkey = EVP_PKEY_new();
if (pkey) {
DSA *dsa = DSA_new();
if (dsa) {
if (DSA_set0_key(dsa, pub_key, priv_key) == 1 && DSA_set0_pqg(dsa, p, q, g)) {
if (!EVP_PKEY_assign_DSA(pkey, dsa)) {
DSA_free(dsa);
EVP_PKEY_free(pkey);
pkey = NULL;
}
} else {
DSA_free(dsa);
dsa = NULL;
EVP_PKEY_free(pkey);
pkey = NULL;
}
}
}
#endif
} else
#endif
#ifndef OPENSSL_NO_DH
if (strcasecmp(alg, "dh") == 0)
{
BIGNUM *p = NULL, *q = NULL, *g = NULL;
BIGNUM *priv_key = NULL, *pub_key = NULL;
lua_getfield(L, 1, "p");
p = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "q");
q = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "g");
g = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "priv_key");
priv_key = BN_get(L, -1);
lua_pop(L, 1);
lua_getfield(L, 1, "pub_key");
pub_key = BN_get(L, -1);
lua_pop(L, 1);
#if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER)
pkey = openssl_new_pkey_dh_with(p, q, g, pub_key, priv_key);
BN_free(p);
BN_free(q);
BN_free(g);
BN_free(pub_key);
BN_free(priv_key);
#else
pkey = EVP_PKEY_new();
if (pkey) {
DH *dh = DH_new();
if (dh) {
if (DH_set0_key(dh, pub_key, priv_key) == 1 && DH_set0_pqg(dh, p, q, g)) {
if (!EVP_PKEY_assign_DH(pkey, dh)) {
DH_free(dh);
dh = NULL;
EVP_PKEY_free(pkey);
pkey = NULL;
}
} else {
DH_free(dh);
dh = NULL;
EVP_PKEY_free(pkey);
pkey = NULL;
}
}
}
#endif
} else
#endif
#ifndef OPENSSL_NO_EC
if (strcasecmp(alg, "ec") == 0)
{
BIGNUM *d = NULL;
BIGNUM *x = NULL;
BIGNUM *y = NULL;
BIGNUM *z = NULL;
EC_GROUP *group = NULL;
lua_getfield(L, -1, "ec_name");
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_getfield(L, -1, "curve_name");
}
lua_getfield(L, -2, "conv_form");
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_getfield(L, -2, "param_enc");
}
lua_getfield(L, -3, "enc_flag");
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_getfield(L, -3, "asn1_flag");
}
group = openssl_get_ec_group(L, -3, -2, -1);
lua_pop(L, 3);
if (!group) luaL_error(L, "get openssl.ec_group fail");
EC_GET_FIELD(d);
EC_GET_FIELD(x);
EC_GET_FIELD(y);
EC_GET_FIELD(z);
if (z) luaL_error(L, "only accpet affine co-ordinates");
#if OPENSSL_VERSION_NUMBER >= 0x30000000L && !defined(LIBRESSL_VERSION_NUMBER)
pkey = openssl_new_pkey_ec_with(group, x, y, d);
#else
pkey = EVP_PKEY_new();
if (pkey) {
EC_KEY *ec = EC_KEY_new();
if (ec) {
EC_KEY_set_group(ec, group);
if (d) EC_KEY_set_private_key(ec, d);
if (x != NULL && y != NULL) {
EC_POINT *pnt = EC_POINT_new(group);
EC_POINT_set_affine_coordinates(group, pnt, x, y, NULL);
EC_KEY_set_public_key(ec, pnt);
EC_POINT_free(pnt);
} else
EC_KEY_generate_key_part(ec);
if ((d != NULL && EC_KEY_check_key(ec) == 0)
|| EVP_PKEY_assign_EC_KEY(pkey, ec) == 0) {
EC_KEY_free(ec);
EVP_PKEY_free(pkey);
pkey = NULL;
}
}
}
#endif
BN_free(d);
BN_free(x);
BN_free(y);
BN_free(z);
EC_GROUP_free(group);
}
#endif
} else
#ifndef OPENSSL_NO_RSA
if (auxiliar_getclassudata(L, "openssl.rsa", 1))
{
RSA *rsa = CHECK_OBJECT(1, RSA, "openssl.rsa");
pkey = EVP_PKEY_new();
EVP_PKEY_set1_RSA(pkey, rsa);
} else
#endif
#ifndef OPENSSL_NO_EC
if (auxiliar_getclassudata(L, "openssl.ec_key", 1))
{
EC_KEY *ec = CHECK_OBJECT(1, EC_KEY, "openssl.ec_key");
pkey = EVP_PKEY_new();
EVP_PKEY_set1_EC_KEY(pkey, ec);
} else
#endif
#ifndef OPENSSL_NO_DH
if (auxiliar_getclassudata(L, "openssl.dh", 1))
{
DH *dh = CHECK_OBJECT(1, DH, "openssl.dh");
pkey = EVP_PKEY_new();
EVP_PKEY_set1_DH(pkey, dh);
} else
#endif
#ifndef OPENSSL_NO_DSA
if (auxiliar_getclassudata(L, "openssl.dsa", 1))
{
DSA *dsa = CHECK_OBJECT(1, DSA, "openssl.dsa");
pkey = EVP_PKEY_new();
EVP_PKEY_set1_DSA(pkey, dsa);
}
#endif
if (pkey && EVP_PKEY_id(pkey) != NID_undef) {
PUSH_OBJECT(pkey, "openssl.evp_pkey");
return 1;
} else
EVP_PKEY_free(pkey);
return 0;
}
/***
openssl.evp_pkey object
@type evp_pkey
*/
/***
export evp_pkey as pem/der string
@function export
@tparam[opt='pem'] string support export as 'pem' or 'der' format, default is 'pem'
@tparam[opt=false] boolean raw true for export low layer key just rsa,dsa,ec
@tparam[opt] string passphrase if given, export key will encrypt with aes-128-cbc,
only need when export private key
@treturn string
*/
static int openssl_pkey_export(lua_State *L)
{
EVP_PKEY *key;
int ispriv = 0;
int exraw = 0;
int fmt = FORMAT_AUTO;
size_t passphrase_len = 0;
BIO *bio_out = NULL;
int ret = 0;
const EVP_CIPHER *cipher;
const char *passphrase = NULL;
key = CHECK_OBJECT(1, EVP_PKEY, "openssl.evp_pkey");
ispriv = openssl_pkey_is_private(key);
fmt = lua_type(L, 2);
luaL_argcheck(L, fmt == LUA_TSTRING || fmt == LUA_TNONE, 2, "only accept 'pem','der' or none");
fmt = luaL_checkoption(L, 2, "pem", format);
luaL_argcheck(
L, fmt == FORMAT_PEM || fmt == FORMAT_DER, 2, "only accept pem or der, default is pem");
if (!lua_isnone(L, 3)) exraw = lua_toboolean(L, 3);
passphrase = luaL_optlstring(L, 4, NULL, &passphrase_len);
if (passphrase) {
cipher = (EVP_CIPHER *)EVP_aes_128_cbc();
} else {
cipher = NULL;
}
bio_out = BIO_new(BIO_s_mem());
if (fmt == FORMAT_PEM) {
if (exraw == 0) {
ret = ispriv
? PEM_write_bio_PrivateKey(
bio_out, key, cipher, (unsigned char *)passphrase, passphrase_len, NULL, NULL)
: PEM_write_bio_PUBKEY(bio_out, key);
} else {
/* export raw key format */
switch (EVP_PKEY_type(EVP_PKEY_id(key))) {
#ifndef OPENSSL_NO_RSA
case EVP_PKEY_RSA:
ret = ispriv ? PEM_write_bio_RSAPrivateKey(bio_out,
EVP_PKEY_get0_RSA(key),
cipher,
(unsigned char *)passphrase,
passphrase_len,
NULL,
NULL)
: PEM_write_bio_RSAPublicKey(bio_out, EVP_PKEY_get0_RSA(key));
break;
#endif
#ifndef OPENSSL_NO_DSA
case EVP_PKEY_DSA: {
ret = ispriv ? PEM_write_bio_DSAPrivateKey(bio_out,
EVP_PKEY_get0_DSA(key),
cipher,
(unsigned char *)passphrase,
passphrase_len,
NULL,
NULL)
: PEM_write_bio_DSA_PUBKEY(bio_out, EVP_PKEY_get0_DSA(key));
} break;
#endif
#ifndef OPENSSL_NO_EC
case EVP_PKEY_EC:
ret = ispriv ? PEM_write_bio_ECPrivateKey(bio_out,
EVP_PKEY_get0_EC_KEY(key),
cipher,
(unsigned char *)passphrase,
passphrase_len,
NULL,
NULL)
: PEM_write_bio_EC_PUBKEY(bio_out, EVP_PKEY_get0_EC_KEY(key));
break;
#endif
default:
break;
}
}
} else {
/* out put der */
if (exraw == 0) {
ret = ispriv ? (passphrase == NULL
? i2d_PrivateKey_bio(bio_out, key)
: i2d_PKCS8PrivateKey_bio(
bio_out, key, cipher, (char *)passphrase, passphrase_len, NULL, NULL))
: i2d_PUBKEY_bio(bio_out, key);
} else {
/* output raw key, rsa, ec, dh, dsa */
switch (EVP_PKEY_type(EVP_PKEY_id(key))) {
#ifndef OPENSSL_NO_RSA
case EVP_PKEY_RSA:
ret = ispriv ? i2d_RSAPrivateKey_bio(bio_out, EVP_PKEY_get0_RSA(key))