forked from zhaozg/lua-openssl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathots.c
More file actions
1847 lines (1674 loc) · 49.8 KB
/
ots.c
File metadata and controls
1847 lines (1674 loc) · 49.8 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
/***
timestamp module provide basic Time Stamping Authority (TSA) client and server operations as
specified in RFC 3161 (Time-Stamp Protocol, TSP). A TSA can be part of a PKI deployment and its role
is to provide long term proof of the existence of a certain datum before a particular time.
@module ts
@usage
ts = require'openssl'.ts
*/
#include "openssl.h"
#include "private.h"
#include <stdint.h>
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
#include <openssl/asn1.h>
#include <openssl/ts.h>
/***
create a new ts_msg_imprint object.
@function ts_msg_imprint_new
@tparam string data
@tparam string|integer|asn1_object alg alg name, nid or object identity
@treturn ts_msg_imprint
*/
static int
openssl_ts_msg_imprint_new(lua_State *L)
{
size_t sz = 0;
const char *data = luaL_checklstring(L, 1, &sz);
const EVP_MD *md = get_digest(L, 2, NULL);
TS_MSG_IMPRINT *msg = TS_MSG_IMPRINT_new();
int ret = TS_MSG_IMPRINT_set_msg(msg, (unsigned char *)data, sz);
if (sz != EVP_MD_size(md)) luaL_error(L, "data size not match with digest size");
if (ret == 1) {
X509_ALGOR *alg = X509_ALGOR_new();
#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER >= 0x30900000L
X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_type(md)), V_ASN1_NULL, NULL);
#else
X509_ALGOR_set_md(alg, md);
#endif
if (ret == 1) ret = TS_MSG_IMPRINT_set_algo(msg, alg);
X509_ALGOR_free(alg);
PUSH_OBJECT(msg, "openssl.ts_msg_imprint");
} else
ret = openssl_pushresult(L, ret);
return ret;
}
/***
read and parse ts_msg_imprint from DER encoded data
@function ts_msg_imprint_read
@tparam string data DER encoded ts_msg_imprint data
@treturn ts_msg_imprint|nil parsed object or nil on error
*/
static int
openssl_ts_msg_imprint_read(lua_State *L)
{
size_t sz = 0;
const unsigned char *data = (const unsigned char *)luaL_checklstring(L, 1, &sz);
TS_MSG_IMPRINT *msg = d2i_TS_MSG_IMPRINT(NULL, &data, sz);
if (msg)
PUSH_OBJECT(msg, "openssl.ts_msg_imprint");
else
lua_pushnil(L);
return 1;
}
/***
export a ts_msg_imprint object as DER encoded data
@function export
@treturn string
*/
static int
openssl_ts_msg_imprint_export(lua_State *L)
{
TS_MSG_IMPRINT *msg = CHECK_OBJECT(1, TS_MSG_IMPRINT, "openssl.ts_msg_imprint");
unsigned char *buf = NULL;
int len = i2d_TS_MSG_IMPRINT(msg, &buf);
if (len >= 0) {
lua_pushlstring(L, (const char *)buf, len);
OPENSSL_free(buf);
len = 1;
} else
len = openssl_pushresult(L, len);
return len;
}
/***
get a ts_msg_imprint msg
@function msg
@treturn asn1_string
*/
static int
openssl_ts_msg_imprint_msg(lua_State *L)
{
TS_MSG_IMPRINT *msg = CHECK_OBJECT(1, TS_MSG_IMPRINT, "openssl.ts_msg_imprint");
ASN1_OCTET_STRING *s = TS_MSG_IMPRINT_get_msg(msg);
PUSH_ASN1_OCTET_STRING(L, s);
return 1;
}
/***
get a ts_msg_imprint algo
@function algo
@treturn openssl.x509_algor
*/
static int
openssl_ts_msg_imprint_algo(lua_State *L)
{
TS_MSG_IMPRINT *msg = CHECK_OBJECT(1, TS_MSG_IMPRINT, "openssl.ts_msg_imprint");
X509_ALGOR *a = TS_MSG_IMPRINT_get_algo(msg);
a = X509_ALGOR_dup(a);
PUSH_OBJECT(a, "openssl.x509_algor");
return 1;
}
/***
get a ts_msg_imprint table contains msg and algo fields
@function totable
@treturn table
*/
static int
openssl_ts_msg_imprint_totable(lua_State *L)
{
TS_MSG_IMPRINT *msg = CHECK_OBJECT(1, TS_MSG_IMPRINT, "openssl.ts_msg_imprint");
ASN1_OCTET_STRING *s = TS_MSG_IMPRINT_get_msg(msg);
X509_ALGOR *a = TS_MSG_IMPRINT_get_algo(msg);
lua_newtable(L);
PUSH_ASN1_OCTET_STRING(L, s);
lua_setfield(L, -2, "msg");
a = X509_ALGOR_dup(a);
PUSH_OBJECT(a, "openssl.x509_algor");
lua_setfield(L, -2, "algo");
return 1;
}
/***
duplicate ts_msg_imprint object
@function dup
@treturn ts_msg_imprint
*/
static int
openssl_ts_msg_imprint_dup(lua_State *L)
{
TS_MSG_IMPRINT *msg = CHECK_OBJECT(1, TS_MSG_IMPRINT, "openssl.ts_msg_imprint");
msg = TS_MSG_IMPRINT_dup(msg);
PUSH_OBJECT(msg, "openssl.ts_msg_imprint");
return 1;
}
static int
openssl_ts_msg_imprint_gc(lua_State *L)
{
TS_MSG_IMPRINT *msg = CHECK_OBJECT(1, TS_MSG_IMPRINT, "openssl.ts_msg_imprint");
TS_MSG_IMPRINT_free(msg);
return 0;
}
static luaL_Reg ts_msg_imprint_funcs[] = {
{ "dup", openssl_ts_msg_imprint_dup },
{ "msg", openssl_ts_msg_imprint_msg },
{ "algo", openssl_ts_msg_imprint_algo },
{ "export", openssl_ts_msg_imprint_export },
{ "totable", openssl_ts_msg_imprint_totable },
{ "__gc", openssl_ts_msg_imprint_gc },
{ "__tostring", auxiliar_tostring },
{ NULL, NULL }
};
/***
create new timestamp accuracy object
@function ts_accuracy_new
@tparam[opt] number seconds accuracy in seconds
@tparam[opt] number millis accuracy in milliseconds
@tparam[opt] number micros accuracy in microseconds
@treturn ts_accuracy new timestamp accuracy object or nil on failure
*/
static int
openssl_ts_accuracy_new(lua_State *L)
{
int ret;
time_t seconds = 0;
int millis = 0, micros = 0;
TS_ACCURACY *accuracy = NULL;
ASN1_INTEGER *sec = NULL;
ASN1_INTEGER *mil = NULL;
ASN1_INTEGER *mic = NULL;
seconds = luaL_checkinteger(L, 1);
millis = luaL_optinteger(L, 2, millis);
micros = luaL_optinteger(L, 3, micros);
accuracy = TS_ACCURACY_new();
sec = ASN1_INTEGER_new();
mil = ASN1_INTEGER_new();
mic = ASN1_INTEGER_new();
ret = ASN1_INTEGER_set(sec, (long)seconds);
if (ret == 1) ret = TS_ACCURACY_set_seconds(accuracy, sec);
if (ret == 1) {
ret = ASN1_INTEGER_set(mil, millis);
if (ret == 1) ret = TS_ACCURACY_set_millis(accuracy, mil);
}
if (ret == 1) {
ret = ASN1_INTEGER_set(mic, micros);
if (ret == 1) ret = TS_ACCURACY_set_micros(accuracy, mic);
}
if (ret == 1) {
PUSH_OBJECT(accuracy, "openssl.ts_accuracy");
} else {
TS_ACCURACY_free(accuracy);
ret = 0;
}
ASN1_INTEGER_free(sec);
ASN1_INTEGER_free(mil);
ASN1_INTEGER_free(mic);
return ret;
}
/***
get or set accuracy in seconds
@function seconds
@tparam[opt] number seconds optional seconds value to set
@treturn number current seconds value when called without parameters
@treturn boolean true when setting value successfully
*/
static int
openssl_ts_accuracy_seconds(lua_State *L)
{
int ret = 0;
TS_ACCURACY *accuracy = CHECK_OBJECT(1, TS_ACCURACY, "openssl.ts_accuracy");
if (lua_isnone(L, 2)) {
const ASN1_INTEGER *ai = TS_ACCURACY_get_seconds(accuracy);
lua_pushinteger(L, ASN1_INTEGER_get(ai));
ret = 1;
} else {
time_t seconds = luaL_checkinteger(L, 2);
ASN1_INTEGER *ai = ASN1_INTEGER_new();
ret = ASN1_INTEGER_set(ai, (long)seconds);
if (ret == 1) ret = TS_ACCURACY_set_seconds(accuracy, ai);
ret = openssl_pushresult(L, ret);
ASN1_INTEGER_free(ai);
}
return ret;
}
/***
get or set accuracy in milliseconds
@function millis
@tparam[opt] number millis optional milliseconds value to set
@treturn number current milliseconds value when called without parameters
@treturn boolean true when setting value successfully
*/
static int
openssl_ts_accuracy_millis(lua_State *L)
{
int ret = 0;
TS_ACCURACY *accuracy = CHECK_OBJECT(1, TS_ACCURACY, "openssl.ts_accuracy");
if (lua_isnone(L, 2)) {
const ASN1_INTEGER *ai = TS_ACCURACY_get_millis(accuracy);
lua_pushinteger(L, ASN1_INTEGER_get(ai));
ret = 1;
} else {
int millies = luaL_checkinteger(L, 2);
ASN1_INTEGER *ai = ASN1_INTEGER_new();
ret = ASN1_INTEGER_set(ai, (long)millies);
if (ret == 1) ret = TS_ACCURACY_set_millis(accuracy, ai);
ret = openssl_pushresult(L, ret);
ASN1_INTEGER_free(ai);
}
return ret;
}
/***
get or set timestamp accuracy microseconds
@function micros
@tparam[opt] number micros microseconds value to set (if provided)
@treturn number current microseconds value (if getting)
@treturn boolean success status (if setting)
*/
static int
openssl_ts_accuracy_micros(lua_State *L)
{
int ret = 0;
TS_ACCURACY *accuracy = CHECK_OBJECT(1, TS_ACCURACY, "openssl.ts_accuracy");
if (lua_isnone(L, 2)) {
const ASN1_INTEGER *ai = TS_ACCURACY_get_micros(accuracy);
lua_pushinteger(L, ASN1_INTEGER_get(ai));
ret = 1;
} else {
int micros = luaL_checkinteger(L, 2);
ASN1_INTEGER *ai = ASN1_INTEGER_new();
ret = ASN1_INTEGER_set(ai, (long)micros);
if (ret == 1) ret = TS_ACCURACY_set_micros(accuracy, ai);
ret = openssl_pushresult(L, ret);
ASN1_INTEGER_free(ai);
}
return ret;
}
/***
duplicate timestamp accuracy object
@function dup
@treturn ts_accuracy new duplicated ts_accuracy object
*/
static int
openssl_ts_accuracy_dup(lua_State *L)
{
TS_ACCURACY *accuracy = CHECK_OBJECT(1, TS_ACCURACY, "openssl.ts_accuracy");
accuracy = TS_ACCURACY_dup(accuracy);
PUSH_OBJECT(accuracy, "openssl.ts_accuracy");
return 1;
}
static int
openssl_ts_accuracy_gc(lua_State *L)
{
TS_ACCURACY *accuracy = CHECK_OBJECT(1, TS_ACCURACY, "openssl.ts_accuracy");
TS_ACCURACY_free(accuracy);
return 0;
}
/***
export timestamp accuracy to DER encoded string
@function export
@treturn string DER encoded representation of ts_accuracy object
*/
static int
openssl_ts_accuracy_export(lua_State *L)
{
TS_ACCURACY *accuracy = CHECK_OBJECT(1, TS_ACCURACY, "openssl.ts_accuracy");
unsigned char *buf = NULL;
int len = i2d_TS_ACCURACY(accuracy, &buf);
if (len >= 0) {
lua_pushlstring(L, (const char *)buf, len);
OPENSSL_free(buf);
len = 1;
} else
len = openssl_pushresult(L, len);
return len;
}
/***
read timestamp accuracy from DER data
@function ts_accuracy_read
@tparam string data DER encoded timestamp accuracy data
@treturn ts_accuracy timestamp accuracy object or nil on failure
*/
static int
openssl_ts_accuracy_read(lua_State *L)
{
size_t sz = 0;
const unsigned char *data = (const unsigned char *)luaL_checklstring(L, 1, &sz);
TS_ACCURACY *accuracy = d2i_TS_ACCURACY(NULL, &data, sz);
if (accuracy)
PUSH_OBJECT(accuracy, "openssl.ts_accuracy");
else
lua_pushnil(L);
return 1;
}
/***
convert timestamp accuracy to table representation
@function totable
@treturn table accuracy information with micros, millis, and seconds fields
*/
static int
openssl_ts_accuracy_totable(lua_State *L)
{
TS_ACCURACY *accuracy = CHECK_OBJECT(1, TS_ACCURACY, "openssl.ts_accuracy");
lua_newtable(L);
lua_pushinteger(L, ASN1_INTEGER_get(TS_ACCURACY_get_micros(accuracy)));
lua_setfield(L, -2, "micros");
lua_pushinteger(L, ASN1_INTEGER_get(TS_ACCURACY_get_millis(accuracy)));
lua_setfield(L, -2, "millis");
lua_pushinteger(L, ASN1_INTEGER_get(TS_ACCURACY_get_seconds(accuracy)));
lua_setfield(L, -2, "seconds");
return 1;
}
static luaL_Reg ts_accuracy_funcs[] = {
{ "dup", openssl_ts_accuracy_dup },
{ "micros", openssl_ts_accuracy_micros },
{ "millis", openssl_ts_accuracy_millis },
{ "seconds", openssl_ts_accuracy_seconds },
{ "export", openssl_ts_accuracy_export },
{ "totable", openssl_ts_accuracy_totable },
{ "__gc", openssl_ts_accuracy_gc },
{ "__tostring", auxiliar_tostring },
{ NULL, NULL }
};
/***
get version of ts_tst_info object object
@function version
@treturn integer
*/
static int
openssl_ts_info_version(lua_State *L)
{
TS_TST_INFO *info = CHECK_OBJECT(1, TS_TST_INFO, "openssl.ts_tst_info");
lua_pushinteger(L, TS_TST_INFO_get_version(info));
return 1;
}
/***
get policy_id of ts_tst_info object object
@function policy_id
@treturn asn1_object
*/
static int
openssl_ts_info_policy_id(lua_State *L)
{
TS_TST_INFO *info = CHECK_OBJECT(1, TS_TST_INFO, "openssl.ts_tst_info");
openssl_push_asn1object(L, TS_TST_INFO_get_policy_id(info));
return 1;
}
/***
get msg_imprint of ts_tst_info object object
@function msg_imprint
@treturn ts_msg_imprint
*/
static int
openssl_ts_info_msg_imprint(lua_State *L)
{
TS_TST_INFO *info = CHECK_OBJECT(1, TS_TST_INFO, "openssl.ts_tst_info");
TS_MSG_IMPRINT *msg = TS_TST_INFO_get_msg_imprint(info);
if (msg) {
msg = TS_MSG_IMPRINT_dup(msg);
PUSH_OBJECT(msg, "openssl.ts_msg_imprint");
} else
lua_pushnil(L);
return 1;
}
/***
get serialNumber of ts_tst_info object object
@function serial
@treturn openssl.bn
*/
static int
openssl_ts_info_serial(lua_State *L)
{
TS_TST_INFO *info = CHECK_OBJECT(1, TS_TST_INFO, "openssl.ts_tst_info");
openssl_push_asn1integer_as_bn(L, TS_TST_INFO_get_serial(info));
return 1;
}
/***
get time of ts_tst_info object object
@function time
@treturn asn1_time
*/
static int
openssl_ts_info_time(lua_State *L)
{
TS_TST_INFO *info = CHECK_OBJECT(1, TS_TST_INFO, "openssl.ts_tst_info");
openssl_push_asn1(L, TS_TST_INFO_get_time(info), V_ASN1_GENERALIZEDTIME);
return 1;
}
/***
get accuracy of ts_tst_info object object
@function accuracy
@treturn table
*/
static int
openssl_ts_info_accuracy(lua_State *L)
{
int ret = 0;
TS_TST_INFO *info = CHECK_OBJECT(1, TS_TST_INFO, "openssl.ts_tst_info");
if (lua_isnone(L, 2)) {
TS_ACCURACY *accuracy = TS_TST_INFO_get_accuracy(info);
accuracy = TS_ACCURACY_dup(accuracy);
PUSH_OBJECT(accuracy, "openssl.ts_accuracy");
ret = 1;
} else {
TS_ACCURACY *accuracy = CHECK_OBJECT(2, TS_ACCURACY, "openssl.ts_accuracy");
int ret = TS_TST_INFO_set_accuracy(info, accuracy);
ret = openssl_pushresult(L, ret);
}
return ret;
}
/***
get ordering of ts_tst_info object object
@function ording
@treturn table
*/
/***
get ordering flag from timestamp info
@function ordering
@treturn boolean true if ordering is required
*/
static int
openssl_ts_info_ordering(lua_State *L)
{
TS_TST_INFO *info = CHECK_OBJECT(1, TS_TST_INFO, "openssl.ts_tst_info");
lua_pushboolean(L, TS_TST_INFO_get_ordering(info));
return 1;
}
/***
get nonce of ts_tst_info object object
@function nonce
@treturn openssl.bn
*/
static int
openssl_ts_info_nonce(lua_State *L)
{
TS_TST_INFO *info = CHECK_OBJECT(1, TS_TST_INFO, "openssl.ts_tst_info");
openssl_push_asn1integer_as_bn(L, TS_TST_INFO_get_nonce(info));
return 1;
}
/***
get tsa nonce of ts_tst_info object object
@function tsa
@treturn x509.name
*/
static int
openssl_ts_info_tsa(lua_State *L)
{
TS_TST_INFO *info = CHECK_OBJECT(1, TS_TST_INFO, "openssl.ts_tst_info");
openssl_push_general_name(L, TS_TST_INFO_get_tsa(info));
return 1;
}
/***
get extensions nonce of ts_tst_info object object
@function extensions
@treturn table
*/
static int
openssl_ts_info_extensions(lua_State *L)
{
TS_TST_INFO *info = CHECK_OBJECT(1, TS_TST_INFO, "openssl.ts_tst_info");
STACK_OF(X509_EXTENSION) *exts = TS_TST_INFO_get_exts(info);
if (exts) {
openssl_sk_x509_extension_totable(L, exts);
} else
lua_pushnil(L);
return 1;
}
static int
openssl_ts_info_gc(lua_State *L)
{
TS_TST_INFO *info = CHECK_OBJECT(1, TS_TST_INFO, "openssl.ts_tst_info");
TS_TST_INFO_free(info);
return 0;
}
static luaL_Reg ts_tst_info_funcs[] = {
{ "version", openssl_ts_info_version },
{ "policy_id", openssl_ts_info_policy_id },
{ "msg_imprint", openssl_ts_info_msg_imprint },
{ "serial", openssl_ts_info_serial },
{ "time", openssl_ts_info_time },
{ "accuracy", openssl_ts_info_accuracy },
{ "ordering", openssl_ts_info_ordering },
{ "nonce", openssl_ts_info_nonce },
{ "tsa", openssl_ts_info_tsa },
{ "extensions", openssl_ts_info_extensions },
{ "__gc", openssl_ts_info_gc },
{ "__tostring", auxiliar_tostring },
{ NULL, NULL }
};
/***
create a new ts_req object.
@function req_new
@tparam[opt=1] integer version
@treturn openssl.ts_req timestamp sign request object
-- @see openssl/ts.h:TS_REQ_
*/
static int openssl_ts_req_new(lua_State *L)
{
TS_REQ *ts_req = TS_REQ_new();
long version = luaL_optinteger(L, 1, 1);
int ret = TS_REQ_set_version(ts_req, version);
if (ret == 1) {
PUSH_OBJECT(ts_req, "openssl.ts_req");
return 1;
}
TS_REQ_free(ts_req);
return 0;
}
/***
read ts_req object from string or bio data
@function req_read
@tparam string|bio input
@treturn openssl.ts_req timestamp sign request object
-- @see openssl/ts.h:TS_REQ_
*/
static int openssl_ts_req_read(lua_State *L)
{
BIO *in = load_bio_object(L, 1);
TS_REQ *ts_req = d2i_TS_REQ_bio(in, NULL);
BIO_free(in);
if (ts_req) {
PUSH_OBJECT(ts_req, "openssl.ts_req");
return 1;
}
return 0;
}
/***
read ts_resp object from string or bio input
@function resp_read
@tparam string|bio input
@treturn openssl.ts_resp object
*/
static int openssl_ts_resp_read(lua_State *L)
{
BIO *in = load_bio_object(L, 1);
TS_RESP *res = d2i_TS_RESP_bio(in, NULL);
BIO_free(in);
if (res) {
PUSH_OBJECT(res, "openssl.ts_resp");
} else
lua_pushnil(L);
return 1;
}
/***
create ts_resp_ctx object
@function resp_ctx_new
@tparam openssl.x509 signer timestamp certificate
@tparam openssl.evp_pkey pkey private key to sign ts_req
@tparam asn1_object|string|nid identity for default policy object
@treturn openssl.ts_resp_ctx object
*/
static int openssl_ts_resp_ctx_new(lua_State *L)
{
TS_RESP_CTX *ctx = NULL;
X509 *signer = NULL;
EVP_PKEY *pkey = NULL;
ASN1_OBJECT *obj = NULL;
int ret = 1;
luaL_argcheck(L, auxiliar_getclassudata(L, "openssl.x509", 1), 1, "need openssl.x509 object");
luaL_argcheck(L, auxiliar_getclassudata(L, "openssl.evp_pkey", 2), 2, "need openssl.pkey object");
luaL_argcheck(L,
auxiliar_getclassudata(L, "openssl.asn1_object", 3) || lua_isnumber(L, 3)
|| lua_isstring(L, 3),
3,
"need openssl.asn1_object, nid string or number");
signer = CHECK_OBJECT(1, X509, "openssl.x509");
pkey = CHECK_OBJECT(2, EVP_PKEY, "openssl.evp_pkey");
obj = openssl_get_asn1object(L, 3, 0);
if (signer && pkey) {
ret = X509_check_private_key(signer, pkey);
if (ret != 1) luaL_error(L, "signer cert and private key not match");
}
ctx = TS_RESP_CTX_new();
if (ret == 1 && obj) ret = TS_RESP_CTX_set_def_policy(ctx, obj);
if (ret == 1 && signer) ret = TS_RESP_CTX_set_signer_cert(ctx, signer);
if (ret == 1 && pkey) ret = TS_RESP_CTX_set_signer_key(ctx, pkey);
if (ret == 1) {
PUSH_OBJECT(ctx, "openssl.ts_resp_ctx");
openssl_newvalue(L, ctx);
} else {
TS_RESP_CTX_free(ctx);
ctx = NULL;
lua_pushnil(L);
}
if (obj) ASN1_OBJECT_free(obj);
return 1;
}
/***
create ts_verify_ctx object
@function verify_ctx_new
@tparam[opt=nil] string|ts_req reqdata
@treturn ts_verify_ctx object
*/
static int openssl_ts_verify_ctx_new(lua_State *L)
{
TS_VERIFY_CTX *ctx = NULL;
if (lua_isnone(L, 1)) {
ctx = TS_VERIFY_CTX_new();
} else if (lua_isstring(L, 1)) {
BIO *bio = load_bio_object(L, 1);
TS_REQ *req = d2i_TS_REQ_bio(bio, NULL);
BIO_free(bio);
if (req) {
ctx = TS_REQ_to_TS_VERIFY_CTX(req, NULL);
TS_REQ_free(req);
} else {
luaL_argerror(L, 1, "must be ts_req data or object or nil");
}
} else {
TS_REQ *req = CHECK_OBJECT(1, TS_REQ, "openssl.ts_req");
ctx = TS_REQ_to_TS_VERIFY_CTX(req, NULL);
}
if (ctx) {
PUSH_OBJECT(ctx, "openssl.ts_verify_ctx");
} else
lua_pushnil(L);
return 1;
}
static luaL_Reg R[] = {
{ "req_new", openssl_ts_req_new },
{ "req_read", openssl_ts_req_read },
{ "resp_read", openssl_ts_resp_read },
{ "ts_accuracy_read", openssl_ts_accuracy_read },
{ "ts_msg_imprint_read", openssl_ts_msg_imprint_read },
{ "resp_ctx_new", openssl_ts_resp_ctx_new },
{ "verify_ctx_new", openssl_ts_verify_ctx_new },
{ "ts_accuracy_new", openssl_ts_accuracy_new },
{ "ts_msg_imprint_new", openssl_ts_msg_imprint_new },
{ NULL, NULL }
};
/***
openssl.ts_req object
@type ts_req
*/
/***
make a clone of ts_req object
@function dup
@treturn openssl.ts_req
*/
static int
openssl_ts_req_dup(lua_State *L)
{
TS_REQ *req = CHECK_OBJECT(1, TS_REQ, "openssl.ts_req");
req = TS_REQ_dup(req);
PUSH_OBJECT(req, "openssl.ts_req");
return 1;
}
/***
get cert_req
@function cert_req
@treturn boolean true for set or not
*/
/***
set cert_req
@function cert_req
@tparam boolean cert_req
@treturn boolean result
*/
static int
openssl_ts_req_cert_req(lua_State *L)
{
TS_REQ *req = CHECK_OBJECT(1, TS_REQ, "openssl.ts_req");
if (lua_isnone(L, 2)) {
lua_pushboolean(L, TS_REQ_get_cert_req(req));
return 1;
} else {
int cert_req = auxiliar_checkboolean(L, 2);
int ret = TS_REQ_set_cert_req(req, cert_req);
return openssl_pushresult(L, ret);
}
}
/***
get nonce
@function nonce
@treturn openssl.bn openssl.bn object
*/
/***
set nonce
@tparam string|bn nonce
@treturn boolean result
@function nonce
*/
static int
openssl_ts_req_nonce(lua_State *L)
{
TS_REQ *req = CHECK_OBJECT(1, TS_REQ, "openssl.ts_req");
if (lua_isnone(L, 2)) {
return openssl_push_asn1integer_as_bn(L, TS_REQ_get_nonce(req));
} else {
BIGNUM *bn = BN_get(L, 2);
if (bn) {
ASN1_INTEGER *ai = BN_to_ASN1_INTEGER(bn, NULL);
int ret = TS_REQ_set_nonce(req, ai);
ASN1_INTEGER_free(ai);
BN_free(bn);
return openssl_pushresult(L, ret);
}
return luaL_argerror(L, 2, "invalid openssl.bn or can't convert to openssl.bn");
}
}
/***
get policy_id
@function policy_id
@treturn asn1_object
*/
/***
set policy_id
@function policy_id
@tparam asn1_object|number id identity for asn1_object
@treturn boolean result
*/
static int
openssl_ts_req_policy_id(lua_State *L)
{
TS_REQ *req = CHECK_OBJECT(1, TS_REQ, "openssl.ts_req");
if (lua_isnone(L, 2)) {
ASN1_OBJECT *obj = TS_REQ_get_policy_id(req);
openssl_push_asn1object(L, obj);
return 1;
} else {
ASN1_OBJECT *obj = openssl_get_asn1object(L, 2, 0);
int ret = TS_REQ_set_policy_id(req, obj);
ASN1_OBJECT_free(obj);
return openssl_pushresult(L, ret);
}
}
/***
get version
@treturn integer
@function version
*/
/***
set version
@tparam integer version
@treturn boolean result
@function version
*/
static int
openssl_ts_req_version(lua_State *L)
{
TS_REQ *req = CHECK_OBJECT(1, TS_REQ, "openssl.ts_req");
if (lua_isnone(L, 2)) {
lua_pushinteger(L, TS_REQ_get_version(req));
return 1;
} else {
long v = luaL_checkinteger(L, 2);
int ret = TS_REQ_set_version(req, v);
return openssl_pushresult(L, ret);
}
}
/***
get msg_imprint
@function msg_imprint
@treturn string octet octet string
@treturn table with algorithm and paramater
*/
/***
set msg_imprint
@function msg_imprint
@tparam string data digest value of message
@tparam[opt='sha'] string|evp_md md_alg
@treturn boolean result
*/
static int
openssl_ts_req_msg_imprint(lua_State *L)
{
TS_REQ *req = CHECK_OBJECT(1, TS_REQ, "openssl.ts_req");
if (lua_isnone(L, 2)) {
TS_MSG_IMPRINT *msg = TS_REQ_get_msg_imprint(req);
if (msg) {
msg = TS_MSG_IMPRINT_dup(msg);
PUSH_OBJECT(msg, "openssl.ts_msg_imprint");
} else
lua_pushnil(L);
return 1;
} else {
TS_MSG_IMPRINT *msg = CHECK_OBJECT(2, TS_MSG_IMPRINT, "openssl.ts_msg_imprint");
int ret = TS_REQ_set_msg_imprint(req, msg);
return openssl_pushresult(L, ret);
}
};
/***
create ts_verify_ctx from ts_req object
@function to_verify_ctx
@treturn ts_verify_ctx object
*/
static int openssl_ts_req_to_verify_ctx(lua_State *L)
{
TS_REQ *req = CHECK_OBJECT(1, TS_REQ, "openssl.ts_req");
TS_VERIFY_CTX *ctx = TS_REQ_to_TS_VERIFY_CTX(req, NULL);
PUSH_OBJECT(ctx, "openssl.ts_verify_ctx");
return 1;
}
/***
export ts_req to string
@function export
@treturn string
*/
static int openssl_ts_req_export(lua_State *L)
{
TS_REQ *ts_req = CHECK_OBJECT(1, TS_REQ, "openssl.ts_req");
unsigned char *data = NULL;
int len = i2d_TS_REQ(ts_req, &data);
if (len > 0) {
lua_pushlstring(L, (const char *)data, (size_t)len);
OPENSSL_free(data);
return 1;
}
return 0;
}
/***
add X509 extension to timestamp request
@function add_ext
@tparam x509_extension extension X509 extension to add
@tparam[opt] number location position to insert extension
@treturn boolean true on success, false on failure
*/
static int openssl_ts_req_add_ext(lua_State *L)
{
TS_REQ *ts_req = CHECK_OBJECT(1, TS_REQ, "openssl.ts_req");
X509_EXTENSION *x = CHECK_OBJECT(2, X509_EXTENSION, "openssl.x509_extension");
int loc = luaL_optint(L, 3, TS_REQ_get_ext_count(ts_req));
int ret;
ret = TS_REQ_add_ext(ts_req, x, loc);
return openssl_pushresult(L, ret);
}
/***
get info as table
@function info
@treturn table
*/
static int openssl_ts_req_info(lua_State *L)
{
TS_REQ *req = CHECK_OBJECT(1, TS_REQ, "openssl.ts_req");
lua_newtable(L);
lua_pushinteger(L, TS_REQ_get_version(req));
lua_setfield(L, -2, "version");
AUXILIAR_SET(L, -1, "cert_req", TS_REQ_get_cert_req(req), boolean);
if (TS_REQ_get_policy_id(req)) {
openssl_push_asn1object(L, TS_REQ_get_policy_id(req));
lua_setfield(L, -2, "policy_id");
}
openssl_push_asn1integer_as_bn(L, TS_REQ_get_nonce(req));
lua_setfield(L, -2, "nonce");
lua_newtable(L);
{
TS_MSG_IMPRINT *msg_inprint = TS_REQ_get_msg_imprint(req);
ASN1_OCTET_STRING *os = TS_MSG_IMPRINT_get_msg(msg_inprint);
X509_ALGOR *alg = TS_MSG_IMPRINT_get_algo(msg_inprint);
AUXILIAR_SETLSTR(L, -1, "hashed_msg", (const char *)ASN1_STRING_get0_data(os), ASN1_STRING_length(os));
alg = X509_ALGOR_dup(alg);
PUSH_OBJECT(alg, "openssl.x509_algor");
lua_setfield(L, -2, "hash_algo");
}
lua_setfield(L, -2, "msg_imprint");
if (TS_REQ_get_exts(req)) {
lua_pushstring(L, "extensions");
openssl_sk_x509_extension_totable(L, TS_REQ_get_exts(req));
lua_rawset(L, -3);
}