forked from zhaozg/lua-openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx509.c
More file actions
1580 lines (1458 loc) · 40 KB
/
x509.c
File metadata and controls
1580 lines (1458 loc) · 40 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
/***
x509 modules to create, parse, process X509 objects, sign CSR.
@module x509
@usage
x509 = require'openssl'.x509
*/
#include "openssl.h"
#include "private.h"
#define CRYPTO_LOCK_REF
#include "sk.h"
#if OPENSSL_VERSION_NUMBER < 0x1010000fL || \
(defined(LIBRESSL_VERSION_NUMBER) && (LIBRESSL_VERSION_NUMBER < 0x20700000L))
#define X509_get0_notBefore X509_get_notBefore
#define X509_get0_notAfter X509_get_notAfter
#define X509_set1_notBefore X509_set_notBefore
#define X509_set1_notAfter X509_set_notAfter
#endif
static int openssl_push_purpose(lua_State*L, CONSTIFY_OPENSSL X509_PURPOSE* purpose)
{
lua_newtable(L);
AUXILIAR_SET(L, -1, "purpose", X509_PURPOSE_get_id(purpose), integer);
#if !defined(LIBRESSL_VERSION_NUMBER) || LIBRESSL_VERSION_NUMBER < 0x30900000L
AUXILIAR_SET(L, -1, "trust", X509_PURPOSE_get_trust(purpose), integer);
AUXILIAR_SET(L, -1, "flags", purpose->flags, integer);
#endif
AUXILIAR_SET(L, -1, "name", X509_PURPOSE_get0_name(purpose), string);
AUXILIAR_SET(L, -1, "sname", X509_PURPOSE_get0_sname(purpose), string);
return 1;
};
/***
get special purpose info as table or return all supported purposes
@function purpose
@tparam[opt] number|string purpose purpose id or short name (optional)
@treturn table purpose info table or table of all purposes if no parameter given
*/
static int openssl_x509_purpose(lua_State*L)
{
int ret = lua_type(L, 1);
luaL_argcheck(L,
ret==LUA_TNONE || ret==LUA_TNUMBER || ret==LUA_TSTRING,
1,
"only accpet NONE, string or number as nid or short name");
ret = 0;
if (lua_isnone(L, 1))
{
int count = X509_PURPOSE_get_count();
int i;
lua_newtable(L);
for (i = 0; i < count; i++)
{
CONSTIFY_OPENSSL X509_PURPOSE* purpose = X509_PURPOSE_get0(i);
openssl_push_purpose(L, purpose);
lua_rawseti(L, -2, i + 1);
}
ret = 1;
}
else if (lua_isnumber(L, 1))
{
int n = lua_tointeger(L, 1);
if (n >= X509_PURPOSE_MIN && n <= X509_PURPOSE_MAX)
{
CONSTIFY_OPENSSL X509_PURPOSE* purpose = X509_PURPOSE_get0(n - X509_PURPOSE_MIN);
openssl_push_purpose(L, purpose);
ret = 1;
}
}
else if (lua_isstring(L, 1))
{
char* name = (char*)lua_tostring(L, 1);
int idx = X509_PURPOSE_get_by_sname(name);
if (idx >= 0)
{
CONSTIFY_OPENSSL X509_PURPOSE* purpose = X509_PURPOSE_get0(idx);
openssl_push_purpose(L, purpose);
ret = 1;
}
}
return ret;
};
static const char* usage_mode[] =
{
"standard",
"netscape",
"extend",
NULL
};
/***
get support certtypes
@function certtypes
@tparam[opt='standard'] string type support 'standard','netscape','extend'
@treturn table if type is 'standard' or 'netscape', contains node with {lname=...,sname=...,bitname=...},
if type is 'extend', contains node with {lname=...,sname=...,nid=...}
*/
static int openssl_x509_certtypes(lua_State*L)
{
int mode = luaL_checkoption(L, 1, "standard", usage_mode);
int i, ret=0;
const BIT_STRING_BITNAME* bitname;
switch (mode)
{
case 0:
{
const static BIT_STRING_BITNAME key_usage_type_table[] =
{
{0, "Digital Signature", "digitalSignature"},
{1, "Non Repudiation", "nonRepudiation"},
{2, "Key Encipherment", "keyEncipherment"},
{3, "Data Encipherment", "dataEncipherment"},
{4, "Key Agreement", "keyAgreement"},
{5, "Certificate Sign", "keyCertSign"},
{6, "CRL Sign", "cRLSign"},
{7, "Encipher Only", "encipherOnly"},
{8, "Decipher Only", "decipherOnly"},
{ -1, NULL, NULL}
};
lua_newtable(L);
for (i = 0, bitname = &key_usage_type_table[i]; bitname->bitnum != -1; i++, bitname = &key_usage_type_table[i])
{
openssl_push_bit_string_bitname(L, bitname);
lua_rawseti(L, -2, i + 1);
}
ret = 1;
}
case 1:
{
const static BIT_STRING_BITNAME ns_cert_type_table[] =
{
{0, "SSL Client", "client"},
{1, "SSL Server", "server"},
{2, "S/MIME", "email"},
{3, "Object Signing", "objsign"},
{4, "Unused", "reserved"},
{5, "SSL CA", "sslCA"},
{6, "S/MIME CA", "emailCA"},
{7, "Object Signing CA", "objCA"},
{ -1, NULL, NULL}
};
lua_newtable(L);
for (i = 0, bitname = &ns_cert_type_table[i]; bitname->bitnum != -1; i++, bitname = &ns_cert_type_table[i])
{
openssl_push_bit_string_bitname(L, bitname);
lua_rawseti(L, -2, i + 1);
}
ret = 1;
}
case 2:
{
static const int ext_nids[] =
{
NID_server_auth,
NID_client_auth,
NID_email_protect,
NID_code_sign,
NID_ms_sgc,
NID_ns_sgc,
NID_OCSP_sign,
NID_time_stamp,
NID_dvcs,
NID_anyExtendedKeyUsage
};
int count = sizeof(ext_nids) / sizeof(int);
int nid;
lua_newtable(L);
for (i = 0; i < count; i++)
{
nid = ext_nids[i];
lua_newtable(L);
lua_pushstring(L, OBJ_nid2ln(nid));
lua_setfield(L, -2, "lname");
lua_pushstring(L, OBJ_nid2sn(nid));
lua_setfield(L, -2, "sname");
lua_pushinteger(L, nid);
lua_setfield(L, -2, "nid");
lua_rawseti(L, -2, i + 1);
};
ret = 1;
}
}
return ret;
}
/***
get certificate verify result string message
@function verify_cert_error_string
@tparam number verify_result
@treturn string result message
*/
static int openssl_verify_cert_error_string(lua_State*L)
{
int v = luaL_checkint(L, 1);
const char*s = X509_verify_cert_error_string(v);
lua_pushstring(L, s);
return 1;
}
/***
read x509 from string or bio input
@function read
@tparam bio|string input input data
@tparam[opt='auto'] string format support 'auto','pem','der'
@treturn openssl.x509 certificate object
*/
static int openssl_x509_read(lua_State *L)
{
X509 *cert = NULL;
BIO *in = load_bio_object(L, 1);
int fmt = luaL_checkoption(L, 2, "auto", format);
int ret = 0;
if (fmt == FORMAT_AUTO)
{
fmt = bio_is_der(in) ? FORMAT_DER : FORMAT_PEM;
}
if (fmt == FORMAT_DER)
{
cert = d2i_X509_bio(in, NULL);
}
else if (fmt == FORMAT_PEM)
{
cert = PEM_read_bio_X509(in, NULL, NULL, NULL);
}
BIO_free(in);
if (cert)
{
PUSH_OBJECT(cert, "openssl.x509");
ret = 1;
}
return ret == 1 ? 1 : openssl_pushresult(L, ret);
}
/***
create or generate a new x509 object.
@function new
@tparam[opt] openssl.bn serial serial number
@tparam[opt] openssl.x509_req csr copy x509_name, pubkey and extension to new object
@tparam[opt] x509_name subject subject name set to x509_req
@tparam[opt] stack_of_x509_extension extensions add to x509
@tparam[opt] stack_of_x509_attribute attributes add to x509
@treturn openssl.x509 certificate object
*/
static int openssl_x509_new(lua_State* L)
{
int i = 1;
int ret = 1;
int n = lua_gettop(L);
X509 *x = X509_new();
ret = X509_set_version(x, 2);
if (ret == 1 && ( auxiliar_getclassudata(L, "openssl.bn", i) ||
lua_isstring(L, i) || lua_isnumber(L, i) ))
{
BIGNUM *bn = BN_get(L, i);
ASN1_INTEGER* ai = BN_to_ASN1_INTEGER(bn, NULL);
BN_free(bn);
ret = X509_set_serialNumber(x, ai);
ASN1_INTEGER_free(ai);
i++;
}
for (; i <= n && ret==1; i++)
{
if (ret == 1 && auxiliar_getclassudata(L, "openssl.x509_req", i))
{
X509_REQ* csr = CHECK_OBJECT(i, X509_REQ, "openssl.x509_req");
X509_NAME* xn = X509_REQ_get_subject_name(csr);
ret = X509_set_subject_name(x, xn);
if (ret == 1)
{
STACK_OF(X509_EXTENSION) *exts = X509_REQ_get_extensions(csr);
int j, n1;
n1 = sk_X509_EXTENSION_num(exts);
for (j = 0; ret == 1 && j < n1; j++)
{
ret = X509_add_ext(x, sk_X509_EXTENSION_value(exts, j), j);
}
sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
}
if (ret == 1)
{
EVP_PKEY* pkey = X509_REQ_get_pubkey(csr);
ret = X509_set_pubkey(x, pkey);
EVP_PKEY_free(pkey);
}
i++;
};
if (ret == 1 && auxiliar_getclassudata(L, "openssl.x509_name", i))
{
X509_NAME *xn = CHECK_OBJECT(i, X509_NAME, "openssl.x509_name");
ret = X509_set_subject_name(x, xn);
i++;
}
}
if (ret == 1)
PUSH_OBJECT(x, "openssl.x509");
else
{
X509_free(x);
ret = openssl_pushresult(L, ret);
}
return ret;
};
static luaL_Reg R[] =
{
{"new", openssl_x509_new },
{"read", openssl_x509_read },
{"purpose", openssl_x509_purpose },
{"certtypes", openssl_x509_certtypes },
{"verify_cert_error_string", openssl_verify_cert_error_string },
{NULL, NULL}
};
int openssl_push_general_name(lua_State*L, const GENERAL_NAME* general_name)
{
int type = 0;
void *val;
if (general_name == NULL)
{
lua_pushnil(L);
return 1;
}
lua_newtable(L);
val = GENERAL_NAME_get0_value((GENERAL_NAME*)general_name, &type);
switch (type)
{
case GEN_OTHERNAME:
{
OTHERNAME *otherName = val;
lua_newtable(L);
openssl_push_asn1object(L, otherName->type_id);
PUSH_ASN1_STRING(L, otherName->value->value.asn1_string);
lua_settable(L, -3);
lua_setfield(L, -2, "otherName");
lua_pushstring(L, "otherName");
lua_setfield(L, -2, "type");
break;
}
case GEN_EMAIL:
{
ASN1_IA5STRING *s = val;
PUSH_ASN1_STRING(L, s);
lua_setfield(L, -2, "rfc822Name");
lua_pushstring(L, "rfc822Name");
lua_setfield(L, -2, "type");
break;
}
case GEN_DNS:
{
ASN1_IA5STRING *s = val;
PUSH_ASN1_STRING(L, s);
lua_setfield(L, -2, "dNSName");
lua_pushstring(L, "dNSName");
lua_setfield(L, -2, "type");
break;
}
case GEN_X400:
{
#if OPENSSL_VERSION_NUMBER >= 0x1010100fL && !defined(LIBRESSL_VERSION_NUMBER)
ASN1_STRING *s = val;
PUSH_ASN1_STRING(L, s);
#else
ASN1_TYPE *type = val;
openssl_push_asn1type(L, type);
#endif
lua_setfield(L, -2, "x400Address");
lua_pushstring(L, "x400Address");
lua_setfield(L, -2, "type");
break;
}
case GEN_DIRNAME:
{
X509_NAME* xn = val;
openssl_push_xname_asobject(L, xn);
lua_setfield(L, -2, "directoryName");
lua_pushstring(L, "directoryName");
lua_setfield(L, -2, "type");
}
break;
case GEN_URI:
{
ASN1_IA5STRING *s = val;
PUSH_ASN1_STRING(L, s);
lua_setfield(L, -2, "uniformResourceIdentifier");
lua_pushstring(L, "uniformResourceIdentifier");
lua_setfield(L, -2, "type");
break;
}
case GEN_IPADD:
{
PUSH_ASN1_OCTET_STRING(L, general_name->d.iPAddress);
lua_setfield(L, -2, "iPAddress");
lua_pushstring(L, "iPAddress");
lua_setfield(L, -2, "type");
break;
}
case GEN_EDIPARTY:
{
EDIPARTYNAME *name = val;
lua_newtable(L);
PUSH_ASN1_STRING(L, name->nameAssigner);
lua_setfield(L, -2, "nameAssigner");
PUSH_ASN1_STRING(L, name->partyName);
lua_setfield(L, -2, "partyName");
lua_setfield(L, -2, "ediPartyName");
lua_pushstring(L, "ediPartyName");
lua_setfield(L, -2, "type");
break;
}
case GEN_RID:
{
ASN1_OBJECT *o = val;
openssl_push_asn1object(L, o);
lua_setfield(L, -2, "registeredID");
lua_pushstring(L, "registeredID");
lua_setfield(L, -2, "type");
break;
}
default:
lua_pushstring(L, "unsupport");
lua_setfield(L, -2, "type");
}
return 1;
};
int openssl_push_x509_signature(lua_State *L, const X509_ALGOR *alg, const ASN1_STRING *sig, int i)
{
if (i==0) lua_newtable(L); else i = lua_absindex(L, i);
if (alg != NULL)
{
alg = X509_ALGOR_dup((X509_ALGOR*)alg);
lua_pushliteral(L, "sig_alg");
PUSH_OBJECT(alg, "openssl.x509_algor");
lua_rawset(L, i==0 ? -3 : i);
}
if (sig != NULL)
{
lua_pushliteral(L, "sig");
lua_pushlstring(L, (const char *)sig->data, sig->length);
lua_rawset(L, i==0 ? -3 : i);
}
return i==0 ? 1 : 0;
}
static int check_cert(X509_STORE *ca, X509 *x, STACK_OF(X509) *untrustedchain, int purpose)
{
int ret = 0;
X509_STORE_CTX *csc = X509_STORE_CTX_new();
if (csc)
{
X509_STORE_set_flags(ca, X509_V_FLAG_CHECK_SS_SIGNATURE);
if (X509_STORE_CTX_init(csc, ca, x, untrustedchain) == 1)
{
if (purpose > 0)
{
X509_STORE_CTX_set_purpose(csc, purpose);
}
ret = X509_verify_cert(csc);
if (ret == 1)
ret = X509_V_OK;
else
ret = X509_STORE_CTX_get_error(csc);
}
X509_STORE_CTX_cleanup(csc);
X509_STORE_CTX_free(csc);
return ret;
}
return X509_V_ERR_OUT_OF_MEM;
}
/***
openssl.x509 object
@type x509
*/
/***
export x509_req to string
@function export
@tparam[opt='pem'] string format 'der' or 'pem' default
@treturn string
*/
static int openssl_x509_export(lua_State *L)
{
X509 *cert = CHECK_OBJECT(1, X509, "openssl.x509");
int fmt = luaL_checkoption(L, 2, "pem", format);
BIO* out = NULL;
int ret = 0;
out = BIO_new(BIO_s_mem());
ret = fmt == FORMAT_PEM ? PEM_write_bio_X509(out, cert)
: i2d_X509_bio(out, cert);
if (ret)
{
BUF_MEM *bio_buf;
BIO_get_mem_ptr(out, &bio_buf);
lua_pushlstring(L, bio_buf->data, bio_buf->length);
ret = 1;
}
BIO_free(out);
return ret;
};
/***
parse x509 object as table
@function parse
@tparam[opt=true] shortname default will use short object name
@treturn table result which all x509 information
*/
static int openssl_x509_parse(lua_State *L)
{
int i;
X509 * cert = CHECK_OBJECT(1, X509, "openssl.x509");
int ca = lua_isnone(L, 2) ? X509_check_ca(cert) : lua_toboolean(L, 2);
lua_newtable(L);
#if OPENSSL_VERSION_NUMBER < 0x10100000L
if (cert->name)
{
AUXILIAR_SET(L, -1, "name", cert->name, string);
}
AUXILIAR_SET(L, -1, "valid", cert->valid, boolean);
#endif
AUXILIAR_SET(L, -1, "version", X509_get_version(cert), integer);
openssl_push_xname_asobject(L, X509_get_subject_name(cert));
lua_setfield(L, -2, "subject");
openssl_push_xname_asobject(L, X509_get_issuer_name(cert));
lua_setfield(L, -2, "issuer");
{
char buf[32];
snprintf(buf, sizeof(buf), "%08lx", X509_subject_name_hash(cert));
AUXILIAR_SET(L, -1, "hash", buf, string);
}
PUSH_ASN1_INTEGER(L, X509_get0_serialNumber(cert));
lua_setfield(L, -2, "serialNumber");
PUSH_ASN1_TIME(L, X509_get0_notBefore(cert));
lua_setfield(L, -2, "notBefore");
PUSH_ASN1_TIME(L, X509_get0_notAfter(cert));
lua_setfield(L, -2, "notAfter");
{
CONSTIFY_X509_get0 X509_ALGOR *palg = NULL;
CONSTIFY_X509_get0 ASN1_BIT_STRING *psig = NULL;
X509_get0_signature(&psig, &palg, cert);
openssl_push_x509_signature(L, palg, psig, -1);
}
{
int l = 0;
char* tmpstr = (char *)X509_alias_get0(cert, &l);
if (tmpstr)
{
AUXILIAR_SETLSTR(L, -1, "alias", tmpstr, l);
}
}
AUXILIAR_SET(L, -1, "ca", X509_check_ca(cert), boolean);
lua_newtable(L);
for (i = 0; i < X509_PURPOSE_get_count(); i++)
{
CONSTIFY_OPENSSL X509_PURPOSE *purp = X509_PURPOSE_get0(i);
int id = X509_PURPOSE_get_id(purp);
const char * pname = X509_PURPOSE_get0_sname(purp);
int set = X509_check_purpose(cert, id, ca);
if (set)
{
AUXILIAR_SET(L, -1, pname, 1, boolean);
}
}
lua_setfield(L, -2, "purposes");
{
int n = X509_get_ext_count(cert);
if (n > 0)
{
lua_pushstring(L, "extensions");
lua_newtable(L);
for (i = 0; i < n; i++)
{
X509_EXTENSION *ext = X509_get_ext(cert, i);
ext = X509_EXTENSION_dup(ext);
lua_pushinteger(L, i + 1);
PUSH_OBJECT(ext, "openssl.x509_extension");
lua_rawset(L, -3);
}
lua_rawset(L, -3);
}
}
return 1;
}
static int openssl_x509_free(lua_State *L)
{
X509 *cert = CHECK_OBJECT(1, X509, "openssl.x509");
X509_free(cert);
return 0;
}
/***
get public key of x509
@function pubkey
@treturn openssl.evp_pkey public key
*/
/***
set public key of x509
@function pubkey
@tparam openssl.evp_pkey pubkey public key set to x509
@treturn boolean result, true for success
*/
static int openssl_x509_public_key(lua_State *L)
{
X509 *cert = CHECK_OBJECT(1, X509, "openssl.x509");
if (lua_isnone(L, 2))
{
EVP_PKEY *pkey = X509_get_pubkey(cert);
PUSH_OBJECT(pkey, "openssl.evp_pkey");
return 1;
}
else
{
EVP_PKEY* pkey = CHECK_OBJECT(2, EVP_PKEY, "openssl.evp_pkey");
int ret = X509_set_pubkey(cert, pkey);
return openssl_pushresult(L, ret);
}
}
#if 0
static int verify_cb(int ok, X509_STORE_CTX *ctx)
{
int err;
X509 *err_cert;
/*
* it is ok to use a self signed certificate This case will catch both
* the initial ok == 0 and the final ok == 1 calls to this function
*/
err = X509_STORE_CTX_get_error(ctx);
if (err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
return 1;
/*
* BAD we should have gotten an error. Normally if everything worked
* X509_STORE_CTX_get_error(ctx) will still be set to
* DEPTH_ZERO_SELF_....
*/
if (ok)
{
//BIO_printf(bio_err, "error with certificate to be certified - should be self signed\n");
return 0;
}
else
{
err_cert = X509_STORE_CTX_get_current_cert(ctx);
//print_name(bio_err, NULL, X509_get_subject_name(err_cert), 0);
//BIO_printf(bio_err, "error with certificate - error %d at depth %d\n%s\n", err, X509_STORE_CTX_get_error_depth(ctx), X509_verify_cert_error_string(err));
return 1;
}
}
#endif
/***
check x509 with ca certchian and option purpose
purpose can be one of: ssl_client, ssl_server, ns_ssl_server, smime_sign, smime_encrypt, crl_sign, any, ocsp_helper, timestamp_sign
@function check
@tparam x509_store cacerts
@tparam x509_store untrusted certs containing a bunch of certs that are not trusted but may be useful in validating the certificate.
@tparam[opt] string purpose to check supported
@treturn boolean result true for check pass
@treturn integer verify result
-- @see OpenSSL function: X509_verify_cert_error_string
*/
/***
check x509 with evp_pkey
@function check
@tparam openssl.evp_pkey pkey private key witch match with x509 pubkey
@treturn boolean result true for check pass
*/
static int openssl_x509_check(lua_State *L)
{
X509 * cert = CHECK_OBJECT(1, X509, "openssl.x509");
if (auxiliar_getclassudata(L, "openssl.evp_pkey", 2))
{
EVP_PKEY * key = CHECK_OBJECT(2, EVP_PKEY, "openssl.evp_pkey");
lua_pushboolean(L, X509_check_private_key(cert, key));
return 1;
}
else
{
X509_STORE* store = CHECK_OBJECT(2, X509_STORE, "openssl.x509_store");
STACK_OF(X509)* untrustedchain = lua_isnoneornil(L, 3) ? NULL : openssl_sk_x509_fromtable(L, 3);
int purpose = 0;
int ret = 0;
if (!lua_isnone(L, 4))
{
int purpose_id = X509_PURPOSE_get_by_sname((char*)luaL_optstring(L, 4, "any"));
if (purpose_id >= 0)
{
CONSTIFY_OPENSSL X509_PURPOSE* ppurpose = X509_PURPOSE_get0(purpose_id);
if (ppurpose)
purpose = X509_PURPOSE_get_id(ppurpose);
}
}
#if 0
X509_STORE_set_verify_cb_func(store, verify_cb);
#endif
ret = check_cert(store, cert, untrustedchain, purpose);
if (untrustedchain!=NULL) sk_X509_pop_free(untrustedchain, X509_free);
lua_pushboolean(L, ret == X509_V_OK);
lua_pushinteger(L, ret);
return 2;
}
}
/***
The functions return 1 for a successful match, 0 for a failed match and -1 for
an internal error: typically a memory allocation failure or an ASN.1 decoding
error.
All functions can also return -2 if the input is malformed. For example,
X509_check_host() returns -2 if the provided name contains embedded NULs.
*/
static int openssl_push_check_result(lua_State *L, int ret, const char* name)
{
switch (ret)
{
case 1:
lua_pushboolean(L, 1);
if (name)
{
lua_pushstring(L, name);
ret = 2;
}
break;
case 0:
lua_pushboolean(L, 0);
ret = 1;
break;
case -1:
lua_pushnil(L);
lua_pushliteral(L, "internal");
ret = 2;
case -2:
lua_pushnil(L);
lua_pushliteral(L, "malformed");
ret = 2;
default:
lua_pushnil(L);
lua_pushinteger(L, ret);
ret = 2;
}
return ret;
}
#if OPENSSL_VERSION_NUMBER > 0x10002000L
/***
check x509 for host (only for openssl 1.0.2 or greater)
@function check_host
@tparam string host hostname to check for match match with x509 subject
@treturn boolean result true if host is present and matches the certificate
*/
static int openssl_x509_check_host(lua_State *L)
{
X509 * cert = CHECK_OBJECT(1, X509, "openssl.x509");
size_t sz;
const char* hostname = luaL_checklstring(L, 2, &sz);
int flags = luaL_optint(L, 3, 0);
char *peer = NULL;
int ret = X509_check_host(cert, hostname, sz, flags, &peer);
ret = openssl_push_check_result(L, ret, peer);
OPENSSL_free(peer);
return ret;
}
/***
check x509 for email address (only for openssl 1.0.2 or greater)
@tparam string email to check for match match with x509 subject
@treturn boolean result true if host is present and matches the certificate
@function check_email
*/
static int openssl_x509_check_email(lua_State *L)
{
X509 * cert = CHECK_OBJECT(1, X509, "openssl.x509");
size_t sz;
const char *email = luaL_checklstring(L, 2, &sz);
int flags = luaL_optint(L, 3, 0);
int ret = X509_check_email(cert, email, sz, flags);
return openssl_push_check_result(L, ret, NULL);
}
/***
check x509 for ip address (ipv4 or ipv6, only for openssl 1.0.2 or greater)
@function check_ip_asc
@tparam string ip to check for match match with x509 subject
@treturn boolean result true if host is present and matches the certificate
*/
static int openssl_x509_check_ip(lua_State *L)
{
X509 * cert = CHECK_OBJECT(1, X509, "openssl.x509");
const char *ip = luaL_checkstring(L, 2);
int flags = luaL_optint(L, 3, 0);
int ret = X509_check_ip_asc(cert, ip, flags);
return openssl_push_check_result(L, ret, NULL);
}
#endif
IMP_LUA_SK(X509, x509)
#if 0
static STACK_OF(X509) * load_all_certs_from_file(BIO *in)
{
STACK_OF(X509) *stack = sk_X509_new_null();
if (stack)
{
STACK_OF(X509_INFO) *sk = PEM_X509_INFO_read_bio(in, NULL, NULL, NULL);
/* scan over it and pull out the certs */
while (sk_X509_INFO_num(sk))
{
X509_INFO *xi = sk_X509_INFO_shift(sk);
if (xi->x509 != NULL)
{
sk_X509_push(stack, xi->x509);
xi->x509 = NULL;
}
X509_INFO_free(xi);
}
sk_X509_INFO_free(sk);
};
if (sk_X509_num(stack) == 0)
{
sk_X509_free(stack);
stack = NULL;
}
return stack;
};
#endif
/***
get subject name of x509
@function subject
@treturn openssl.x509_name subject name
*/
/***
set subject name of x509
@function subject
@tparam openssl.x509_name subject
@treturn boolean result true for success
*/
static int openssl_x509_subject(lua_State* L)
{
X509* cert = CHECK_OBJECT(1, X509, "openssl.x509");
if (lua_isnone(L, 2))
{
X509_NAME* xn = X509_get_subject_name(cert);
return openssl_push_xname_asobject(L, xn);
}
else
{
X509_NAME *xn = CHECK_OBJECT(2, X509_NAME, "openssl.x509_name");
int ret = X509_set_subject_name(cert, xn);
return openssl_pushresult(L, ret);
}
}
/***
get issuer name of x509
@function issuer
@tparam[opt=false] boolean asobject true for return as x509_name object, or as table
@treturn[1] x509_name issuer
@treturn[1] table issuer name as table
*/
/***
set issuer name of x509
@function issuer
@tparam openssl.x509_name name
@treturn boolean result true for success
*/
static int openssl_x509_issuer(lua_State* L)
{
X509* cert = CHECK_OBJECT(1, X509, "openssl.x509");
if (lua_isnone(L, 2))
{
X509_NAME* xn = X509_get_issuer_name(cert);
return openssl_push_xname_asobject(L, xn);
}
else
{
X509_NAME* xn = CHECK_OBJECT(2, X509_NAME, "openssl.x509_name");
int ret = X509_set_issuer_name(cert, xn);
return openssl_pushresult(L, ret);
}
}
/***
get digest of x509 object
@function digest
@tparam[opt='sha1'] evp_digest|string md_alg default use 'sha1'
@treturn string digest result
*/
static int openssl_x509_digest(lua_State* L)
{
unsigned int bytes;
unsigned char buffer[EVP_MAX_MD_SIZE];
X509 *cert = CHECK_OBJECT(1, X509, "openssl.x509");
const EVP_MD *digest = get_digest(L, 2, "sha256");
int ret = X509_digest(cert, digest, buffer, &bytes);
if (ret == 1)
{
lua_pushlstring(L, (const char*)buffer, bytes);
}
return ret == 1 ? ret : openssl_pushresult(L, ret);
};
/***
get or set notbefore valid time of x509
@function notbefore
@tparam[opt] string|number notbefore time to set (optional)
@treturn[1] string notbefore time string when getting
@treturn[2] boolean true when setting successfully
@treturn[3] nil when error occurs
@treturn[3] string error message when error occurs
*/
static int openssl_x509_notbefore(lua_State *L)
{
X509* cert = CHECK_OBJECT(1, X509, "openssl.x509");
if (lua_isnone(L, 2))
{
return PUSH_ASN1_TIME(L, X509_get0_notBefore(cert));
}
else
{
ASN1_TIME* at = NULL;
int ret = 1;
if (lua_isnumber(L, 2))
{
time_t time = lua_tointeger(L, 2);
at = ASN1_TIME_new();
ASN1_TIME_set(at, time);
}
else if (lua_isstring(L, 2))
{
const char* time = lua_tostring(L, 2);
at = ASN1_TIME_new();
if (ASN1_TIME_set_string(at, time) != 1)
{
ASN1_TIME_free(at);
at = NULL;
}
}
if (at)
{
ret = X509_set1_notBefore(cert, at);
ASN1_TIME_free(at);
}
else
ret = 0;
return openssl_pushresult(L, ret);
};