Skip to content

Commit 71cd1e3

Browse files
committed
Add to_text for PKCS7 and Timestamp::Response
1 parent 97305cf commit 71cd1e3

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

ext/openssl/ossl_pkcs7.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -847,6 +847,25 @@ ossl_pkcs7_to_der(VALUE self)
847847
return str;
848848
}
849849

850+
static VALUE
851+
ossl_pkcs7_to_text(VALUE self)
852+
{
853+
PKCS7 *pkcs7;
854+
BIO *out;
855+
VALUE str;
856+
857+
GetPKCS7(self, pkcs7);
858+
if(!(out = BIO_new(BIO_s_mem())))
859+
ossl_raise(ePKCS7Error, NULL);
860+
if(!PKCS7_print_ctx(out, pkcs7, 0, NULL)) {
861+
BIO_free(out);
862+
ossl_raise(ePKCS7Error, NULL);
863+
}
864+
str = ossl_membio2str(out);
865+
866+
return str;
867+
}
868+
850869
static VALUE
851870
ossl_pkcs7_to_pem(VALUE self)
852871
{
@@ -1056,6 +1075,7 @@ Init_ossl_pkcs7(void)
10561075
rb_define_method(cPKCS7, "to_pem", ossl_pkcs7_to_pem, 0);
10571076
rb_define_alias(cPKCS7, "to_s", "to_pem");
10581077
rb_define_method(cPKCS7, "to_der", ossl_pkcs7_to_der, 0);
1078+
rb_define_method(cPKCS7, "to_text", ossl_pkcs7_to_text, 0);
10591079

10601080
cPKCS7Signer = rb_define_class_under(cPKCS7, "SignerInfo", rb_cObject);
10611081
rb_define_const(cPKCS7, "Signer", cPKCS7Signer);

ext/openssl/ossl_ts.c

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,25 @@ ossl_ts_req_to_der(VALUE self)
503503
return asn1_to_der((void *)req, (int (*)(void *, unsigned char **))i2d_TS_REQ);
504504
}
505505

506+
static VALUE
507+
ossl_ts_req_to_text(VALUE self)
508+
{
509+
TS_REQ *req;
510+
BIO *out;
511+
512+
GetTSRequest(self, req);
513+
514+
out = BIO_new(BIO_s_mem());
515+
if (!out) ossl_raise(eTimestampError, NULL);
516+
517+
if (!TS_REQ_print_bio(out, req)) {
518+
BIO_free(out);
519+
ossl_raise(eTimestampError, NULL);
520+
}
521+
522+
return ossl_membio2str(out);
523+
}
524+
506525
static VALUE
507526
ossl_ts_resp_alloc(VALUE klass)
508527
{
@@ -757,6 +776,25 @@ ossl_ts_resp_to_der(VALUE self)
757776
return asn1_to_der((void *)resp, (int (*)(void *, unsigned char **))i2d_TS_RESP);
758777
}
759778

779+
static VALUE
780+
ossl_ts_resp_to_text(VALUE self)
781+
{
782+
TS_RESP *resp;
783+
BIO *out;
784+
785+
GetTSResponse(self, resp);
786+
787+
out = BIO_new(BIO_s_mem());
788+
if (!out) ossl_raise(eTimestampError, NULL);
789+
790+
if (!TS_RESP_print_bio(out, resp)) {
791+
BIO_free(out);
792+
ossl_raise(eTimestampError, NULL);
793+
}
794+
795+
return ossl_membio2str(out);
796+
}
797+
760798
/*
761799
* Verifies a timestamp token by checking the signature, validating the
762800
* certificate chain implied by tsa_certificate and by checking conformance to
@@ -1073,6 +1111,25 @@ ossl_ts_token_info_to_der(VALUE self)
10731111
return asn1_to_der((void *)info, (int (*)(void *, unsigned char **))i2d_TS_TST_INFO);
10741112
}
10751113

1114+
static VALUE
1115+
ossl_ts_token_info_to_text(VALUE self)
1116+
{
1117+
TS_TST_INFO *info;
1118+
BIO *out;
1119+
1120+
GetTSTokenInfo(self, info);
1121+
1122+
out = BIO_new(BIO_s_mem());
1123+
if (!out) ossl_raise(eTimestampError, NULL);
1124+
1125+
if (!TS_TST_INFO_print_bio(out, info)) {
1126+
BIO_free(out);
1127+
ossl_raise(eTimestampError, NULL);
1128+
}
1129+
1130+
return ossl_membio2str(out);
1131+
}
1132+
10761133
static ASN1_INTEGER *
10771134
ossl_tsfac_serial_cb(struct TS_resp_ctx *ctx, void *data)
10781135
{
@@ -1356,6 +1413,7 @@ Init_ossl_ts(void)
13561413
rb_define_method(cTimestampResponse, "token_info", ossl_ts_resp_get_token_info, 0);
13571414
rb_define_method(cTimestampResponse, "tsa_certificate", ossl_ts_resp_get_tsa_certificate, 0);
13581415
rb_define_method(cTimestampResponse, "to_der", ossl_ts_resp_to_der, 0);
1416+
rb_define_method(cTimestampResponse, "to_text", ossl_ts_resp_to_text, 0);
13591417
rb_define_method(cTimestampResponse, "verify", ossl_ts_resp_verify, -1);
13601418

13611419
/* Document-class: OpenSSL::Timestamp::TokenInfo
@@ -1374,6 +1432,7 @@ Init_ossl_ts(void)
13741432
rb_define_method(cTimestampTokenInfo, "ordering", ossl_ts_token_info_get_ordering, 0);
13751433
rb_define_method(cTimestampTokenInfo, "nonce", ossl_ts_token_info_get_nonce, 0);
13761434
rb_define_method(cTimestampTokenInfo, "to_der", ossl_ts_token_info_to_der, 0);
1435+
rb_define_method(cTimestampTokenInfo, "to_text", ossl_ts_token_info_to_text, 0);
13771436

13781437
/* Document-class: OpenSSL::Timestamp::Request
13791438
* Allows to create timestamp requests or parse existing ones. A Request is
@@ -1399,6 +1458,7 @@ Init_ossl_ts(void)
13991458
rb_define_method(cTimestampRequest, "cert_requested=", ossl_ts_req_set_cert_requested, 1);
14001459
rb_define_method(cTimestampRequest, "cert_requested?", ossl_ts_req_get_cert_requested, 0);
14011460
rb_define_method(cTimestampRequest, "to_der", ossl_ts_req_to_der, 0);
1461+
rb_define_method(cTimestampRequest, "to_text", ossl_ts_req_to_text, 0);
14021462

14031463
/*
14041464
* Indicates a successful response. Equal to +0+.

test/openssl/test_pkcs7.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ def test_smime
227227
assert_equal(p7.to_der, OpenSSL::PKCS7.read_smime(smime).to_der)
228228
end
229229

230+
def test_to_text
231+
p7 = OpenSSL::PKCS7.new
232+
p7.type = "signed"
233+
assert_match(/signed/, p7.to_text)
234+
end
235+
230236
def test_degenerate_pkcs7
231237
ca_cert_pem = <<END
232238
-----BEGIN CERTIFICATE-----

test/openssl/test_ts.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,8 @@ def test_response_default_policy
323323
resp = fac.create_timestamp(ee_key, ts_cert_ee, req)
324324
assert_equal(OpenSSL::Timestamp::Response::GRANTED, resp.status)
325325
assert_equal("1.2.3.4.6", resp.token_info.policy_id)
326+
327+
assert_match(/1\.2\.3\.4\.6/, resp.to_text)
326328
end
327329

328330
def test_response_bad_purpose

0 commit comments

Comments
 (0)