-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
Copy pathossl_ts.c
1606 lines (1428 loc) · 48.6 KB
/
ossl_ts.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
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
/*
*
* Copyright (C) 2010 Martin Bosslet <Martin.Bosslet@googlemail.com>
* All rights reserved.
*/
/*
* This program is licenced under the same licence as Ruby.
* (See the file 'COPYING'.)
*/
#include "ossl.h"
#ifndef OPENSSL_NO_TS
#define NewTSRequest(klass) \
TypedData_Wrap_Struct((klass), &ossl_ts_req_type, 0)
#define SetTSRequest(obj, req) do { \
if (!(req)) { \
ossl_raise(rb_eRuntimeError, "TS_REQ wasn't initialized."); \
} \
RTYPEDDATA_DATA(obj) = (req); \
} while (0)
#define GetTSRequest(obj, req) do { \
TypedData_Get_Struct((obj), TS_REQ, &ossl_ts_req_type, (req)); \
if (!(req)) { \
ossl_raise(rb_eRuntimeError, "TS_REQ wasn't initialized."); \
} \
} while (0)
#define NewTSResponse(klass) \
TypedData_Wrap_Struct((klass), &ossl_ts_resp_type, 0)
#define SetTSResponse(obj, resp) do { \
if (!(resp)) { \
ossl_raise(rb_eRuntimeError, "TS_RESP wasn't initialized."); \
} \
RTYPEDDATA_DATA(obj) = (resp); \
} while (0)
#define GetTSResponse(obj, resp) do { \
TypedData_Get_Struct((obj), TS_RESP, &ossl_ts_resp_type, (resp)); \
if (!(resp)) { \
ossl_raise(rb_eRuntimeError, "TS_RESP wasn't initialized."); \
} \
} while (0)
#define NewTSTokenInfo(klass) \
TypedData_Wrap_Struct((klass), &ossl_ts_token_info_type, 0)
#define SetTSTokenInfo(obj, info) do { \
if (!(info)) { \
ossl_raise(rb_eRuntimeError, "TS_TST_INFO wasn't initialized."); \
} \
RTYPEDDATA_DATA(obj) = (info); \
} while (0)
#define GetTSTokenInfo(obj, info) do { \
TypedData_Get_Struct((obj), TS_TST_INFO, &ossl_ts_token_info_type, (info)); \
if (!(info)) { \
ossl_raise(rb_eRuntimeError, "TS_TST_INFO wasn't initialized."); \
} \
} while (0)
#define ossl_tsfac_get_default_policy_id(o) rb_attr_get((o),rb_intern("@default_policy_id"))
#define ossl_tsfac_get_serial_number(o) rb_attr_get((o),rb_intern("@serial_number"))
#define ossl_tsfac_get_gen_time(o) rb_attr_get((o),rb_intern("@gen_time"))
#define ossl_tsfac_get_additional_certs(o) rb_attr_get((o),rb_intern("@additional_certs"))
#define ossl_tsfac_get_allowed_digests(o) rb_attr_get((o),rb_intern("@allowed_digests"))
static VALUE mTimestamp;
static VALUE eTimestampError;
static VALUE cTimestampRequest;
static VALUE cTimestampResponse;
static VALUE cTimestampTokenInfo;
static VALUE cTimestampFactory;
static VALUE sBAD_ALG, sBAD_REQUEST, sBAD_DATA_FORMAT, sTIME_NOT_AVAILABLE;
static VALUE sUNACCEPTED_POLICY, sUNACCEPTED_EXTENSION, sADD_INFO_NOT_AVAILABLE;
static VALUE sSYSTEM_FAILURE;
static void
ossl_ts_req_free(void *ptr)
{
TS_REQ_free(ptr);
}
static const rb_data_type_t ossl_ts_req_type = {
"OpenSSL/Timestamp/Request",
{
0, ossl_ts_req_free,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
};
static void
ossl_ts_resp_free(void *ptr)
{
TS_RESP_free(ptr);
}
static const rb_data_type_t ossl_ts_resp_type = {
"OpenSSL/Timestamp/Response",
{
0, ossl_ts_resp_free,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
};
static void
ossl_ts_token_info_free(void *ptr)
{
TS_TST_INFO_free(ptr);
}
static const rb_data_type_t ossl_ts_token_info_type = {
"OpenSSL/Timestamp/TokenInfo",
{
0, ossl_ts_token_info_free,
},
0, 0, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED,
};
static VALUE
asn1_to_der(void *template, int (*i2d)(void *template, unsigned char **pp))
{
VALUE str;
int len;
unsigned char *p;
if((len = i2d(template, NULL)) <= 0)
ossl_raise(eTimestampError, "Error when encoding to DER");
str = rb_str_new(0, len);
p = (unsigned char *)RSTRING_PTR(str);
if(i2d(template, &p) <= 0)
ossl_raise(eTimestampError, "Error when encoding to DER");
rb_str_set_len(str, p - (unsigned char*)RSTRING_PTR(str));
return str;
}
static ASN1_OBJECT*
obj_to_asn1obj(VALUE obj)
{
ASN1_OBJECT *a1obj;
StringValue(obj);
a1obj = OBJ_txt2obj(RSTRING_PTR(obj), 0);
if(!a1obj) a1obj = OBJ_txt2obj(RSTRING_PTR(obj), 1);
if(!a1obj) ossl_raise(eASN1Error, "invalid OBJECT ID");
return a1obj;
}
static VALUE
obj_to_asn1obj_i(VALUE obj)
{
return (VALUE)obj_to_asn1obj(obj);
}
static VALUE
get_asn1obj(const ASN1_OBJECT *obj)
{
BIO *out;
VALUE ret;
int nid;
if ((nid = OBJ_obj2nid(obj)) != NID_undef)
ret = rb_str_new2(OBJ_nid2sn(nid));
else{
if (!(out = BIO_new(BIO_s_mem())))
ossl_raise(eTimestampError, "BIO_new(BIO_s_mem())");
if (i2a_ASN1_OBJECT(out, obj) <= 0) {
BIO_free(out);
ossl_raise(eTimestampError, "i2a_ASN1_OBJECT");
}
ret = ossl_membio2str(out);
}
return ret;
}
static VALUE
ossl_ts_req_alloc(VALUE klass)
{
TS_REQ *req;
VALUE obj;
obj = NewTSRequest(klass);
if (!(req = TS_REQ_new()))
ossl_raise(eTimestampError, NULL);
SetTSRequest(obj, req);
/* Defaults */
TS_REQ_set_version(req, 1);
TS_REQ_set_cert_req(req, 1);
return obj;
}
/*
* When creating a Request with the +File+ or +string+ parameter, the
* corresponding +File+ or +string+ must be DER-encoded.
*
* call-seq:
* OpenSSL::Timestamp::Request.new(file) -> request
* OpenSSL::Timestamp::Request.new(string) -> request
* OpenSSL::Timestamp::Request.new -> empty request
*/
static VALUE
ossl_ts_req_initialize(int argc, VALUE *argv, VALUE self)
{
TS_REQ *ts_req = DATA_PTR(self);
BIO *in;
VALUE arg;
if(rb_scan_args(argc, argv, "01", &arg) == 0) {
return self;
}
arg = ossl_to_der_if_possible(arg);
in = ossl_obj2bio(&arg);
ts_req = d2i_TS_REQ_bio(in, &ts_req);
BIO_free(in);
if (!ts_req) {
DATA_PTR(self) = NULL;
ossl_raise(eTimestampError, "Error when decoding the timestamp request");
}
DATA_PTR(self) = ts_req;
return self;
}
/*
* Returns the 'short name' of the object identifier that represents the
* algorithm that was used to create the message imprint digest.
*
* call-seq:
* request.algorithm -> string
*/
static VALUE
ossl_ts_req_get_algorithm(VALUE self)
{
TS_REQ *req;
TS_MSG_IMPRINT *mi;
X509_ALGOR *algor;
const ASN1_OBJECT *obj;
GetTSRequest(self, req);
mi = TS_REQ_get_msg_imprint(req);
algor = TS_MSG_IMPRINT_get_algo(mi);
X509_ALGOR_get0(&obj, NULL, NULL, algor);
return get_asn1obj(obj);
}
/*
* Allows to set the object identifier or the 'short name' of the
* algorithm that was used to create the message imprint digest.
*
* ===Example:
* request.algorithm = "SHA1"
*
* call-seq:
* request.algorithm = "string" -> string
*/
static VALUE
ossl_ts_req_set_algorithm(VALUE self, VALUE algo)
{
TS_REQ *req;
TS_MSG_IMPRINT *mi;
ASN1_OBJECT *obj;
X509_ALGOR *algor;
GetTSRequest(self, req);
obj = obj_to_asn1obj(algo);
mi = TS_REQ_get_msg_imprint(req);
algor = TS_MSG_IMPRINT_get_algo(mi);
if (!X509_ALGOR_set0(algor, obj, V_ASN1_NULL, NULL)) {
ASN1_OBJECT_free(obj);
ossl_raise(eTimestampError, "X509_ALGOR_set0");
}
return algo;
}
/*
* Returns the message imprint (digest) of the data to be timestamped.
*
* call-seq:
* request.message_imprint -> string or nil
*/
static VALUE
ossl_ts_req_get_msg_imprint(VALUE self)
{
TS_REQ *req;
TS_MSG_IMPRINT *mi;
ASN1_OCTET_STRING *hashed_msg;
VALUE ret;
GetTSRequest(self, req);
mi = TS_REQ_get_msg_imprint(req);
hashed_msg = TS_MSG_IMPRINT_get_msg(mi);
ret = rb_str_new((const char *)hashed_msg->data, hashed_msg->length);
return ret;
}
/*
* Set the message imprint digest.
*
* call-seq:
* request.message_imprint = "string" -> string
*/
static VALUE
ossl_ts_req_set_msg_imprint(VALUE self, VALUE hash)
{
TS_REQ *req;
TS_MSG_IMPRINT *mi;
StringValue(hash);
GetTSRequest(self, req);
mi = TS_REQ_get_msg_imprint(req);
if (!TS_MSG_IMPRINT_set_msg(mi, (unsigned char *)RSTRING_PTR(hash), RSTRING_LENINT(hash)))
ossl_raise(eTimestampError, "TS_MSG_IMPRINT_set_msg");
return hash;
}
/*
* Returns the version of this request. +1+ is the default value.
*
* call-seq:
* request.version -> Integer
*/
static VALUE
ossl_ts_req_get_version(VALUE self)
{
TS_REQ *req;
GetTSRequest(self, req);
return LONG2NUM(TS_REQ_get_version(req));
}
/*
* Sets the version number for this Request. This should be +1+ for compliant
* servers.
*
* call-seq:
* request.version = number -> Integer
*/
static VALUE
ossl_ts_req_set_version(VALUE self, VALUE version)
{
TS_REQ *req;
long ver;
if ((ver = NUM2LONG(version)) < 0)
ossl_raise(eTimestampError, "version must be >= 0!");
GetTSRequest(self, req);
if (!TS_REQ_set_version(req, ver))
ossl_raise(eTimestampError, "TS_REQ_set_version");
return version;
}
/*
* Returns the 'short name' of the object identifier that represents the
* timestamp policy under which the server shall create the timestamp.
*
* call-seq:
* request.policy_id -> string or nil
*/
static VALUE
ossl_ts_req_get_policy_id(VALUE self)
{
TS_REQ *req;
GetTSRequest(self, req);
if (!TS_REQ_get_policy_id(req))
return Qnil;
return get_asn1obj(TS_REQ_get_policy_id(req));
}
/*
* Allows to set the object identifier that represents the
* timestamp policy under which the server shall create the timestamp. This
* may be left +nil+, implying that the timestamp server will issue the
* timestamp using some default policy.
*
* ===Example:
* request.policy_id = "1.2.3.4.5"
*
* call-seq:
* request.policy_id = "string" -> string
*/
static VALUE
ossl_ts_req_set_policy_id(VALUE self, VALUE oid)
{
TS_REQ *req;
ASN1_OBJECT *obj;
int ok;
GetTSRequest(self, req);
obj = obj_to_asn1obj(oid);
ok = TS_REQ_set_policy_id(req, obj);
ASN1_OBJECT_free(obj);
if (!ok)
ossl_raise(eTimestampError, "TS_REQ_set_policy_id");
return oid;
}
/*
* Returns the nonce (number used once) that the server shall include in its
* response.
*
* call-seq:
* request.nonce -> BN or nil
*/
static VALUE
ossl_ts_req_get_nonce(VALUE self)
{
TS_REQ *req;
const ASN1_INTEGER * nonce;
GetTSRequest(self, req);
if (!(nonce = TS_REQ_get_nonce(req)))
return Qnil;
return asn1integer_to_num(nonce);
}
/*
* Sets the nonce (number used once) that the server shall include in its
* response. If the nonce is set, the server must return the same nonce value in
* a valid Response.
*
* call-seq:
* request.nonce = number -> BN
*/
static VALUE
ossl_ts_req_set_nonce(VALUE self, VALUE num)
{
TS_REQ *req;
ASN1_INTEGER *nonce;
int ok;
GetTSRequest(self, req);
nonce = num_to_asn1integer(num, NULL);
ok = TS_REQ_set_nonce(req, nonce);
ASN1_INTEGER_free(nonce);
if (!ok)
ossl_raise(eTimestampError, NULL);
return num;
}
/*
* Indicates whether the response shall contain the timestamp authority's
* certificate or not.
*
* call-seq:
* request.cert_requested? -> true or false
*/
static VALUE
ossl_ts_req_get_cert_requested(VALUE self)
{
TS_REQ *req;
GetTSRequest(self, req);
return TS_REQ_get_cert_req(req) ? Qtrue: Qfalse;
}
/*
* Specify whether the response shall contain the timestamp authority's
* certificate or not. The default value is +true+.
*
* call-seq:
* request.cert_requested = boolean -> true or false
*/
static VALUE
ossl_ts_req_set_cert_requested(VALUE self, VALUE requested)
{
TS_REQ *req;
GetTSRequest(self, req);
TS_REQ_set_cert_req(req, RTEST(requested));
return requested;
}
/*
* DER-encodes this Request.
*
* call-seq:
* request.to_der -> DER-encoded string
*/
static VALUE
ossl_ts_req_to_der(VALUE self)
{
TS_REQ *req;
TS_MSG_IMPRINT *mi;
X509_ALGOR *algo;
const ASN1_OBJECT *obj;
ASN1_OCTET_STRING *hashed_msg;
GetTSRequest(self, req);
mi = TS_REQ_get_msg_imprint(req);
algo = TS_MSG_IMPRINT_get_algo(mi);
X509_ALGOR_get0(&obj, NULL, NULL, algo);
if (OBJ_obj2nid(obj) == NID_undef)
ossl_raise(eTimestampError, "Message imprint missing algorithm");
hashed_msg = TS_MSG_IMPRINT_get_msg(mi);
if (!hashed_msg->length)
ossl_raise(eTimestampError, "Message imprint missing hashed message");
return asn1_to_der((void *)req, (int (*)(void *, unsigned char **))i2d_TS_REQ);
}
static VALUE
ossl_ts_req_to_text(VALUE self)
{
TS_REQ *req;
BIO *out;
GetTSRequest(self, req);
out = BIO_new(BIO_s_mem());
if (!out) ossl_raise(eTimestampError, NULL);
if (!TS_REQ_print_bio(out, req)) {
BIO_free(out);
ossl_raise(eTimestampError, NULL);
}
return ossl_membio2str(out);
}
static VALUE
ossl_ts_resp_alloc(VALUE klass)
{
TS_RESP *resp;
VALUE obj;
obj = NewTSResponse(klass);
if (!(resp = TS_RESP_new()))
ossl_raise(eTimestampError, NULL);
SetTSResponse(obj, resp);
return obj;
}
/*
* Creates a Response from a +File+ or +string+ parameter, the
* corresponding +File+ or +string+ must be DER-encoded. Please note
* that Response is an immutable read-only class. If you'd like to create
* timestamps please refer to Factory instead.
*
* call-seq:
* OpenSSL::Timestamp::Response.new(file) -> response
* OpenSSL::Timestamp::Response.new(string) -> response
*/
static VALUE
ossl_ts_resp_initialize(VALUE self, VALUE der)
{
TS_RESP *ts_resp = DATA_PTR(self);
BIO *in;
der = ossl_to_der_if_possible(der);
in = ossl_obj2bio(&der);
ts_resp = d2i_TS_RESP_bio(in, &ts_resp);
BIO_free(in);
if (!ts_resp) {
DATA_PTR(self) = NULL;
ossl_raise(eTimestampError, "Error when decoding the timestamp response");
}
DATA_PTR(self) = ts_resp;
return self;
}
/*
* Returns one of GRANTED, GRANTED_WITH_MODS, REJECTION, WAITING,
* REVOCATION_WARNING or REVOCATION_NOTIFICATION. A timestamp token has
* been created only in case +status+ is equal to GRANTED or GRANTED_WITH_MODS.
*
* call-seq:
* response.status -> BN (never nil)
*/
static VALUE
ossl_ts_resp_get_status(VALUE self)
{
TS_RESP *resp;
TS_STATUS_INFO *si;
const ASN1_INTEGER *st;
GetTSResponse(self, resp);
si = TS_RESP_get_status_info(resp);
st = TS_STATUS_INFO_get0_status(si);
return asn1integer_to_num(st);
}
/*
* In cases no timestamp token has been created, this field contains further
* info about the reason why response creation failed. The method returns either
* nil (the request was successful and a timestamp token was created) or one of
* the following:
* * :BAD_ALG - Indicates that the timestamp server rejects the message
* imprint algorithm used in the Request
* * :BAD_REQUEST - Indicates that the timestamp server was not able to process
* the Request properly
* * :BAD_DATA_FORMAT - Indicates that the timestamp server was not able to
* parse certain data in the Request
* * :TIME_NOT_AVAILABLE - Indicates that the server could not access its time
* source
* * :UNACCEPTED_POLICY - Indicates that the requested policy identifier is not
* recognized or supported by the timestamp server
* * :UNACCEPTED_EXTENSIION - Indicates that an extension in the Request is
* not supported by the timestamp server
* * :ADD_INFO_NOT_AVAILABLE -Indicates that additional information requested
* is either not understood or currently not available
* * :SYSTEM_FAILURE - Timestamp creation failed due to an internal error that
* occurred on the timestamp server
*
* call-seq:
* response.failure_info -> nil or symbol
*/
static VALUE
ossl_ts_resp_get_failure_info(VALUE self)
{
TS_RESP *resp;
TS_STATUS_INFO *si;
const ASN1_BIT_STRING *fi;
GetTSResponse(self, resp);
si = TS_RESP_get_status_info(resp);
fi = TS_STATUS_INFO_get0_failure_info(si);
if (!fi)
return Qnil;
if (ASN1_BIT_STRING_get_bit(fi, TS_INFO_BAD_ALG))
return sBAD_ALG;
if (ASN1_BIT_STRING_get_bit(fi, TS_INFO_BAD_REQUEST))
return sBAD_REQUEST;
if (ASN1_BIT_STRING_get_bit(fi, TS_INFO_BAD_DATA_FORMAT))
return sBAD_DATA_FORMAT;
if (ASN1_BIT_STRING_get_bit(fi, TS_INFO_TIME_NOT_AVAILABLE))
return sTIME_NOT_AVAILABLE;
if (ASN1_BIT_STRING_get_bit(fi, TS_INFO_UNACCEPTED_POLICY))
return sUNACCEPTED_POLICY;
if (ASN1_BIT_STRING_get_bit(fi, TS_INFO_UNACCEPTED_EXTENSION))
return sUNACCEPTED_EXTENSION;
if (ASN1_BIT_STRING_get_bit(fi, TS_INFO_ADD_INFO_NOT_AVAILABLE))
return sADD_INFO_NOT_AVAILABLE;
if (ASN1_BIT_STRING_get_bit(fi, TS_INFO_SYSTEM_FAILURE))
return sSYSTEM_FAILURE;
ossl_raise(eTimestampError, "Unrecognized failure info.");
}
/*
* In cases of failure this field may contain an array of strings further
* describing the origin of the failure.
*
* call-seq:
* response.status_text -> Array of strings or nil
*/
static VALUE
ossl_ts_resp_get_status_text(VALUE self)
{
TS_RESP *resp;
TS_STATUS_INFO *si;
const STACK_OF(ASN1_UTF8STRING) *text;
ASN1_UTF8STRING *current;
int i;
VALUE ret = rb_ary_new();
GetTSResponse(self, resp);
si = TS_RESP_get_status_info(resp);
if ((text = TS_STATUS_INFO_get0_text(si))) {
for (i = 0; i < sk_ASN1_UTF8STRING_num(text); i++) {
current = sk_ASN1_UTF8STRING_value(text, i);
rb_ary_push(ret, asn1str_to_str(current));
}
}
return ret;
}
/*
* If a timestamp token is present, this returns it in the form of a
* OpenSSL::PKCS7.
*
* call-seq:
* response.token -> nil or OpenSSL::PKCS7
*/
static VALUE
ossl_ts_resp_get_token(VALUE self)
{
TS_RESP *resp;
PKCS7 *p7;
GetTSResponse(self, resp);
if (!(p7 = TS_RESP_get_token(resp)))
return Qnil;
return ossl_pkcs7_new(p7);
}
/*
* Get the response's token info if present.
*
* call-seq:
* response.token_info -> nil or OpenSSL::Timestamp::TokenInfo
*/
static VALUE
ossl_ts_resp_get_token_info(VALUE self)
{
TS_RESP *resp;
TS_TST_INFO *info, *copy;
VALUE obj;
GetTSResponse(self, resp);
if (!(info = TS_RESP_get_tst_info(resp)))
return Qnil;
obj = NewTSTokenInfo(cTimestampTokenInfo);
if (!(copy = TS_TST_INFO_dup(info)))
ossl_raise(eTimestampError, NULL);
SetTSTokenInfo(obj, copy);
return obj;
}
/*
* If the Request specified to request the TSA certificate
* (Request#cert_requested = true), then this field contains the
* certificate of the timestamp authority.
*
* call-seq:
* response.tsa_certificate -> OpenSSL::X509::Certificate or nil
*/
static VALUE
ossl_ts_resp_get_tsa_certificate(VALUE self)
{
TS_RESP *resp;
PKCS7 *p7;
PKCS7_SIGNER_INFO *ts_info;
X509 *cert;
GetTSResponse(self, resp);
if (!(p7 = TS_RESP_get_token(resp)))
return Qnil;
ts_info = sk_PKCS7_SIGNER_INFO_value(p7->d.sign->signer_info, 0);
cert = PKCS7_cert_from_signer_info(p7, ts_info);
if (!cert)
return Qnil;
return ossl_x509_new(cert);
}
/*
* Returns the Response in DER-encoded form.
*
* call-seq:
* response.to_der -> string
*/
static VALUE
ossl_ts_resp_to_der(VALUE self)
{
TS_RESP *resp;
GetTSResponse(self, resp);
return asn1_to_der((void *)resp, (int (*)(void *, unsigned char **))i2d_TS_RESP);
}
static VALUE
ossl_ts_resp_to_text(VALUE self)
{
TS_RESP *resp;
BIO *out;
GetTSResponse(self, resp);
out = BIO_new(BIO_s_mem());
if (!out) ossl_raise(eTimestampError, NULL);
if (!TS_RESP_print_bio(out, resp)) {
BIO_free(out);
ossl_raise(eTimestampError, NULL);
}
return ossl_membio2str(out);
}
/*
* Verifies a timestamp token by checking the signature, validating the
* certificate chain implied by tsa_certificate and by checking conformance to
* a given Request. Mandatory parameters are the Request associated to this
* Response, and an OpenSSL::X509::Store of trusted roots.
*
* Intermediate certificates can optionally be supplied for creating the
* certificate chain. These intermediate certificates must all be
* instances of OpenSSL::X509::Certificate.
*
* If validation fails, several kinds of exceptions can be raised:
* * TypeError if types don't fit
* * TimestampError if something is wrong with the timestamp token itself, if
* it is not conformant to the Request, or if validation of the timestamp
* certificate chain fails.
*
* call-seq:
* response.verify(Request, root_store) -> Response
* response.verify(Request, root_store, [intermediate_cert]) -> Response
*/
static VALUE
ossl_ts_resp_verify(int argc, VALUE *argv, VALUE self)
{
VALUE ts_req, store, intermediates;
TS_RESP *resp;
TS_REQ *req;
X509_STORE *x509st;
TS_VERIFY_CTX *ctx;
STACK_OF(X509) *x509inter = NULL;
PKCS7* p7;
X509 *cert;
int status, i, ok;
rb_scan_args(argc, argv, "21", &ts_req, &store, &intermediates);
GetTSResponse(self, resp);
GetTSRequest(ts_req, req);
x509st = GetX509StorePtr(store);
if (!(ctx = TS_REQ_to_TS_VERIFY_CTX(req, NULL))) {
ossl_raise(eTimestampError, "Error when creating the verification context.");
}
if (!NIL_P(intermediates)) {
x509inter = ossl_protect_x509_ary2sk(intermediates, &status);
if (status) {
TS_VERIFY_CTX_free(ctx);
rb_jump_tag(status);
}
} else if (!(x509inter = sk_X509_new_null())) {
TS_VERIFY_CTX_free(ctx);
ossl_raise(eTimestampError, "sk_X509_new_null");
}
if (!(p7 = TS_RESP_get_token(resp))) {
TS_VERIFY_CTX_free(ctx);
sk_X509_pop_free(x509inter, X509_free);
ossl_raise(eTimestampError, "TS_RESP_get_token");
}
for (i=0; i < sk_X509_num(p7->d.sign->cert); i++) {
cert = sk_X509_value(p7->d.sign->cert, i);
if (!sk_X509_push(x509inter, cert)) {
sk_X509_pop_free(x509inter, X509_free);
TS_VERIFY_CTX_free(ctx);
ossl_raise(eTimestampError, "sk_X509_push");
}
X509_up_ref(cert);
}
if (!X509_STORE_up_ref(x509st)) {
sk_X509_pop_free(x509inter, X509_free);
TS_VERIFY_CTX_free(ctx);
ossl_raise(eTimestampError, "X509_STORE_up_ref");
}
#ifdef HAVE_TS_VERIFY_CTX_SET0_CERTS
TS_VERIFY_CTX_set0_certs(ctx, x509inter);
TS_VERIFY_CTX_set0_store(ctx, x509st);
#else
# if OSSL_OPENSSL_PREREQ(3, 0, 0) || OSSL_IS_LIBRESSL
TS_VERIFY_CTX_set_certs(ctx, x509inter);
# else
TS_VERIFY_CTS_set_certs(ctx, x509inter);
# endif
TS_VERIFY_CTX_set_store(ctx, x509st);
#endif
TS_VERIFY_CTX_add_flags(ctx, TS_VFY_SIGNATURE);
ok = TS_RESP_verify_response(ctx, resp);
TS_VERIFY_CTX_free(ctx);
if (!ok)
ossl_raise(eTimestampError, "TS_RESP_verify_response");
return self;
}
static VALUE
ossl_ts_token_info_alloc(VALUE klass)
{
TS_TST_INFO *info;
VALUE obj;
obj = NewTSTokenInfo(klass);
if (!(info = TS_TST_INFO_new()))
ossl_raise(eTimestampError, NULL);
SetTSTokenInfo(obj, info);
return obj;
}
/*
* Creates a TokenInfo from a +File+ or +string+ parameter, the
* corresponding +File+ or +string+ must be DER-encoded. Please note
* that TokenInfo is an immutable read-only class. If you'd like to create
* timestamps please refer to Factory instead.
*
* call-seq:
* OpenSSL::Timestamp::TokenInfo.new(file) -> token-info
* OpenSSL::Timestamp::TokenInfo.new(string) -> token-info
*/
static VALUE
ossl_ts_token_info_initialize(VALUE self, VALUE der)
{
TS_TST_INFO *info = DATA_PTR(self);
BIO *in;
der = ossl_to_der_if_possible(der);
in = ossl_obj2bio(&der);
info = d2i_TS_TST_INFO_bio(in, &info);
BIO_free(in);
if (!info) {
DATA_PTR(self) = NULL;
ossl_raise(eTimestampError, "Error when decoding the timestamp token info");
}
DATA_PTR(self) = info;
return self;
}
/*
* Returns the version number of the token info. With compliant servers,
* this value should be +1+ if present. If status is GRANTED or
* GRANTED_WITH_MODS.
*
* call-seq:
* token_info.version -> Integer or nil
*/
static VALUE
ossl_ts_token_info_get_version(VALUE self)
{
TS_TST_INFO *info;
GetTSTokenInfo(self, info);
return LONG2NUM(TS_TST_INFO_get_version(info));
}
/*
* Returns the timestamp policy object identifier of the policy this timestamp
* was created under. If status is GRANTED or GRANTED_WITH_MODS, this is never
* +nil+.
*
* ===Example:
* id = token_info.policy_id
* puts id -> "1.2.3.4.5"
*
* call-seq:
* token_info.policy_id -> string or nil
*/
static VALUE
ossl_ts_token_info_get_policy_id(VALUE self)
{
TS_TST_INFO *info;
GetTSTokenInfo(self, info);
return get_asn1obj(TS_TST_INFO_get_policy_id(info));
}
/*
* Returns the 'short name' of the object identifier representing the algorithm
* that was used to derive the message imprint digest. For valid timestamps,
* this is the same value that was already given in the Request. If status is
* GRANTED or GRANTED_WITH_MODS, this is never +nil+.
*
* ===Example:
* algo = token_info.algorithm
* puts algo -> "SHA1"
*
* call-seq:
* token_info.algorithm -> string or nil
*/
static VALUE
ossl_ts_token_info_get_algorithm(VALUE self)
{
TS_TST_INFO *info;
TS_MSG_IMPRINT *mi;
X509_ALGOR *algo;
const ASN1_OBJECT *obj;
GetTSTokenInfo(self, info);
mi = TS_TST_INFO_get_msg_imprint(info);
algo = TS_MSG_IMPRINT_get_algo(mi);
X509_ALGOR_get0(&obj, NULL, NULL, algo);
return get_asn1obj(obj);
}
/*
* Returns the message imprint digest. For valid timestamps,
* this is the same value that was already given in the Request.
* If status is GRANTED or GRANTED_WITH_MODS, this is never +nil+.
*
* ===Example: