summaryrefslogtreecommitdiffstats
path: root/OpenSSL/Timestamp/Factory.html
blob: ea33823fd89440f0460f24a6d3fbc8903b28b22a (plain)
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
<!DOCTYPE html>

<html>
<head>
<meta charset="UTF-8">

<title>class OpenSSL::Timestamp::Factory - RDoc Documentation</title>

<script type="text/javascript">
  var rdoc_rel_prefix = "../../";
  var index_rel_prefix = "../../";
</script>

<script src="../../js/navigation.js" defer></script>
<script src="../../js/search.js" defer></script>
<script src="../../js/search_index.js" defer></script>
<script src="../../js/searcher.js" defer></script>
<script src="../../js/darkfish.js" defer></script>

<link href="../../css/fonts.css" rel="stylesheet">
<link href="../../css/rdoc.css" rel="stylesheet">


<body id="top" role="document" class="class">
<nav role="navigation">
  <div id="project-navigation">
    <div id="home-section" role="region" title="Quick navigation" class="nav-section">
  <h2>
    <a href="../../index.html" rel="home">Home</a>
  </h2>

  <div id="table-of-contents-navigation">
    <a href="../../table_of_contents.html#pages">Pages</a>
    <a href="../../table_of_contents.html#classes">Classes</a>
    <a href="../../table_of_contents.html#methods">Methods</a>
  </div>
</div>

    <div id="search-section" role="search" class="project-section initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <div id="search-field-wrapper">
      <input id="search-field" role="combobox" aria-label="Search"
             aria-autocomplete="list" aria-controls="search-results"
             type="text" name="search" placeholder="Search" spellcheck="false"
             title="Type to search, Up and Down to navigate, Enter to load">
    </div>

    <ul id="search-results" aria-label="Search Results"
        aria-busy="false" aria-expanded="false"
        aria-atomic="false" class="initially-hidden"></ul>
  </form>
</div>

  </div>

  
<div class="nav-section">
  <h3>Table of Contents</h3>

  <ul class="link-list" role="directory">
    <li><a href="#class-OpenSSL::Timestamp::Factory-label-Example-3A+Inclusion+of+-28untrusted-29+intermediate+certificates">Example: Inclusion of (untrusted) intermediate certificates</a>
    <li><a href="#class-OpenSSL::Timestamp::Factory-label-Attributes">Attributes</a>
    <li><a href="#class-OpenSSL::Timestamp::Factory-label-default_policy_id">default_policy_id</a>
    <li><a href="#class-OpenSSL::Timestamp::Factory-label-serial_number">serial_number</a>
    <li><a href="#class-OpenSSL::Timestamp::Factory-label-gen_time">gen_time</a>
    <li><a href="#class-OpenSSL::Timestamp::Factory-label-additional_certs">additional_certs</a>
    <li><a href="#class-OpenSSL::Timestamp::Factory-label-allowed_digests">allowed_digests</a>
  </ul>
</div>


  <div id="class-metadata">
    
    
<div id="parent-class-section" class="nav-section">
  <h3>Parent</h3>

  <p class="link">Object
</div>

    
    
    
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
  <h3>Methods</h3>

  <ul class="link-list" role="directory">
    <li ><a href="#method-i-create_timestamp">#create_timestamp</a>
  </ul>
</div>

  </div>
</nav>

<main role="main" aria-labelledby="class-OpenSSL::Timestamp::Factory">
  <h1 id="class-OpenSSL::Timestamp::Factory" class="class">
    class OpenSSL::Timestamp::Factory
  </h1>

  <section class="description">
    
<p>Used to generate a <a href="Response.html"><code>Response</code></a> from scratch.</p>

<p>Please bear in mind that the implementation will always apply and prefer the policy object identifier given in the request over the default policy id specified in the <a href="Factory.html"><code>Factory</code></a>. As a consequence, <code>default_policy_id</code> will only be applied if no <a href="Request.html#method-i-policy_id"><code>Request#policy_id</code></a> was given. But this also means that one needs to check the policy identifier in the request manually before creating the <a href="Response.html"><code>Response</code></a>, e.g. to check whether it complies to a specific set of acceptable policies.</p>

<p>There exists also the possibility to add certificates (instances of <a href="../X509/Certificate.html"><code>OpenSSL::X509::Certificate</code></a>) besides the timestamping certificate that will be included in the resulting timestamp token if <a href="Request.html#method-i-cert_requested-3F"><code>Request#cert_requested?</code></a> is <code>true</code>. Ideally, one would also include any intermediate certificates (the root certificate can be left out - in order to trust it any verifying party will have to be in its possession anyway). This simplifies validation of the timestamp since these intermediate certificates are “already there” and need not be passed as external parameters to <a href="Response.html#method-i-verify"><code>Response#verify</code></a> anymore, thus minimizing external resources needed for verification.</p>

<h3 id="class-OpenSSL::Timestamp::Factory-label-Example-3A+Inclusion+of+-28untrusted-29+intermediate+certificates">Example: Inclusion of (untrusted) intermediate certificates<span><a href="#class-OpenSSL::Timestamp::Factory-label-Example-3A+Inclusion+of+-28untrusted-29+intermediate+certificates">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Assume we received a timestamp request that has set <a href="Request.html#method-i-policy_id"><code>Request#policy_id</code></a> to <code>nil</code> and <a href="Request.html#method-i-cert_requested-3F"><code>Request#cert_requested?</code></a> to true. The raw request bytes are stored in a variable called <code>req_raw</code>. We’d still like to integrate the necessary intermediate certificates (in <code>inter1.cer</code> and <code>inter2.cer</code>) to simplify validation of the resulting <a href="Response.html"><code>Response</code></a>. <code>ts.p12</code> is a PKCS#12-compatible file including the private key and the timestamping certificate.</p>

<pre>req = OpenSSL::Timestamp::Request.new(raw_bytes)
p12 = OpenSSL::PKCS12.new(File.open(&#39;ts.p12&#39;, &#39;rb&#39;), &#39;pwd&#39;)
inter1 = OpenSSL::X509::Certificate.new(File.open(&#39;inter1.cer&#39;, &#39;rb&#39;)
inter2 = OpenSSL::X509::Certificate.new(File.open(&#39;inter2.cer&#39;, &#39;rb&#39;)
fac = OpenSSL::Timestamp::Factory.new
fac.gen_time = Time.now
fac.serial_number = 1
fac.allowed_digests = [&quot;sha256&quot;, &quot;sha384&quot;, &quot;sha512&quot;]
#needed because the Request contained no policy identifier
fac.default_policy_id = &#39;1.2.3.4.5&#39;
fac.additional_certificates = [ inter1, inter2 ]
timestamp = fac.create_timestamp(p12.key, p12.certificate, req)</pre>

<h2 id="class-OpenSSL::Timestamp::Factory-label-Attributes">Attributes<span><a href="#class-OpenSSL::Timestamp::Factory-label-Attributes">&para;</a> <a href="#top">&uarr;</a></span></h2>

<h3 id="class-OpenSSL::Timestamp::Factory-label-default_policy_id"><a href="Factory.html#attribute-i-default_policy_id"><code>default_policy_id</code></a><span><a href="#class-OpenSSL::Timestamp::Factory-label-default_policy_id">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p><a href="Request.html#method-i-policy_id"><code>Request#policy_id</code></a> will always be preferred over this if present in the <a href="Request.html"><code>Request</code></a>, only if <a href="Request.html#method-i-policy_id"><code>Request#policy_id</code></a> is nil default_policy will be used. If none of both is present, a <a href="TimestampError.html"><code>TimestampError</code></a> will be raised when trying to create a <a href="Response.html"><code>Response</code></a>.</p>

<p>call-seq:</p>

<pre>factory.default_policy_id = &quot;string&quot; -&gt; string
factory.default_policy_id            -&gt; string or nil</pre>

<h3 id="class-OpenSSL::Timestamp::Factory-label-serial_number"><a href="Factory.html#attribute-i-serial_number"><code>serial_number</code></a><span><a href="#class-OpenSSL::Timestamp::Factory-label-serial_number">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Sets or retrieves the serial number to be used for timestamp creation. Must be present for timestamp creation.</p>

<p>call-seq:</p>

<pre>factory.serial_number = number -&gt; number
factory.serial_number          -&gt; number or nil</pre>

<h3 id="class-OpenSSL::Timestamp::Factory-label-gen_time"><a href="Factory.html#attribute-i-gen_time"><code>gen_time</code></a><span><a href="#class-OpenSSL::Timestamp::Factory-label-gen_time">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Sets or retrieves the Time value to be used in the <a href="Response.html"><code>Response</code></a>. Must be present for timestamp creation.</p>

<p>call-seq:</p>

<pre>factory.gen_time = Time -&gt; Time
factory.gen_time        -&gt; Time or nil</pre>

<h3 id="class-OpenSSL::Timestamp::Factory-label-additional_certs"><a href="Factory.html#attribute-i-additional_certs"><code>additional_certs</code></a><span><a href="#class-OpenSSL::Timestamp::Factory-label-additional_certs">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Sets or retrieves additional certificates apart from the timestamp certificate (e.g. intermediate certificates) to be added to the <a href="Response.html"><code>Response</code></a>. Must be an Array of <a href="../X509/Certificate.html"><code>OpenSSL::X509::Certificate</code></a>.</p>

<p>call-seq:</p>

<pre>factory.additional_certs = [cert1, cert2] -&gt; [ cert1, cert2 ]
factory.additional_certs                  -&gt; array or nil</pre>

<h3 id="class-OpenSSL::Timestamp::Factory-label-allowed_digests"><a href="Factory.html#attribute-i-allowed_digests"><code>allowed_digests</code></a><span><a href="#class-OpenSSL::Timestamp::Factory-label-allowed_digests">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Sets or retrieves the digest algorithms that the factory is allowed create timestamps for. Known vulnerable or weak algorithms should not be allowed where possible. Must be an Array of String or <a href="../Digest.html"><code>OpenSSL::Digest</code></a> subclass instances.</p>

<p>call-seq:</p>

<pre>factory.allowed_digests = [&quot;sha1&quot;, OpenSSL::Digest.new(&#39;SHA256&#39;).new] -&gt; [ &quot;sha1&quot;, OpenSSL::Digest) ]
factory.allowed_digests                                               -&gt; array or nil</pre>

  </section>

  <section id="5Buntitled-5D" class="documentation-section">



    <section class="attribute-method-details" class="method-section">
      <header>
        <h3>Attributes</h3>
      </header>

      <div id="attribute-i-additional_certs" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">additional_certs</span><span
            class="attribute-access-type">[RW]</span>
        </div>

        <div class="method-description">
        
        </div>
      </div>
      <div id="attribute-i-allowed_digests" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">allowed_digests</span><span
            class="attribute-access-type">[RW]</span>
        </div>

        <div class="method-description">
        
        </div>
      </div>
      <div id="attribute-i-default_policy_id" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">default_policy_id</span><span
            class="attribute-access-type">[RW]</span>
        </div>

        <div class="method-description">
        
        </div>
      </div>
      <div id="attribute-i-gen_time" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">gen_time</span><span
            class="attribute-access-type">[RW]</span>
        </div>

        <div class="method-description">
        
        </div>
      </div>
      <div id="attribute-i-serial_number" class="method-detail">
        <div class="method-heading attribute-method-heading">
          <span class="method-name">serial_number</span><span
            class="attribute-access-type">[RW]</span>
        </div>

        <div class="method-description">
        
        </div>
      </div>
    </section>


     <section id="public-instance-5Buntitled-5D-method-details" class="method-section">
       <header>
         <h3>Public Instance Methods</h3>
       </header>

      <div id="method-i-create_timestamp" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            create_timestamp(key, certificate, request) &rarr; Response
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>Creates a <a href="Response.html"><code>Response</code></a> with the help of an <a href="../PKey.html"><code>OpenSSL::PKey</code></a>, an <a href="../X509/Certificate.html"><code>OpenSSL::X509::Certificate</code></a> and a <a href="Request.html"><code>Request</code></a>.</p>

<p>Mandatory parameters for timestamp creation that need to be set in the Request:</p>
<ul><li>
<p><a href="Request.html#method-i-algorithm"><code>Request#algorithm</code></a></p>
</li><li>
<p><a href="Request.html#method-i-message_imprint"><code>Request#message_imprint</code></a></p>
</li></ul>

<p>Mandatory parameters that need to be set in the Factory:</p>
<ul><li>
<p><a href="Factory.html#attribute-i-serial_number"><code>Factory#serial_number</code></a></p>
</li><li>
<p><a href="Factory.html#attribute-i-gen_time"><code>Factory#gen_time</code></a></p>
</li><li>
<p><a href="Factory.html#attribute-i-allowed_digests"><code>Factory#allowed_digests</code></a></p>
</li></ul>

<p>In addition one of either <a href="Request.html#method-i-policy_id"><code>Request#policy_id</code></a> or <a href="Factory.html#attribute-i-default_policy_id"><code>Factory#default_policy_id</code></a> must be set.</p>

<p>Raises a <a href="TimestampError.html"><code>TimestampError</code></a> if creation fails, though successfully created error responses may be returned.</p>

          <div class="method-source-code" id="create_timestamp-source">
            <pre>static VALUE
ossl_tsfac_create_ts(VALUE self, VALUE key, VALUE certificate, VALUE request)
{
    VALUE serial_number, def_policy_id, gen_time, additional_certs, allowed_digests;
    VALUE str;
    STACK_OF(X509) *inter_certs;
    VALUE tsresp, ret = Qnil;
    EVP_PKEY *sign_key;
    X509 *tsa_cert;
    TS_REQ *req;
    TS_RESP *response = NULL;
    TS_RESP_CTX *ctx = NULL;
    BIO *req_bio;
    ASN1_INTEGER *asn1_serial = NULL;
    ASN1_OBJECT *def_policy_id_obj = NULL;
    long lgen_time;
    const char * err_msg = NULL;
    int status = 0;

    tsresp = NewTSResponse(cTimestampResponse);
    tsa_cert = GetX509CertPtr(certificate);
    sign_key = GetPrivPKeyPtr(key);
    GetTSRequest(request, req);

    gen_time = ossl_tsfac_get_gen_time(self);
    if (!rb_obj_is_instance_of(gen_time, rb_cTime)) {
        err_msg = &quot;@gen_time must be a Time.&quot;;
        goto end;
    }
    lgen_time = NUM2LONG(rb_funcall(gen_time, rb_intern(&quot;to_i&quot;), 0));

    serial_number = ossl_tsfac_get_serial_number(self);
    if (NIL_P(serial_number)) {
        err_msg = &quot;@serial_number must be set.&quot;;
        goto end;
    }
    asn1_serial = num_to_asn1integer(serial_number, NULL);

    def_policy_id = ossl_tsfac_get_default_policy_id(self);
    if (NIL_P(def_policy_id) &amp;&amp; !TS_REQ_get_policy_id(req)) {
        err_msg = &quot;No policy id in the request and no default policy set&quot;;
        goto end;
    }
    if (!NIL_P(def_policy_id) &amp;&amp; !TS_REQ_get_policy_id(req)) {
        def_policy_id_obj = (ASN1_OBJECT*)rb_protect((VALUE (*)(VALUE))obj_to_asn1obj, (VALUE)def_policy_id, &amp;status);
        if (status)
            goto end;
    }

    if (!(ctx = TS_RESP_CTX_new())) {
        err_msg = &quot;Memory allocation failed.&quot;;
        goto end;
    }

    TS_RESP_CTX_set_serial_cb(ctx, ossl_tsfac_serial_cb, &amp;asn1_serial);
    if (!TS_RESP_CTX_set_signer_cert(ctx, tsa_cert)) {
        err_msg = &quot;Certificate does not contain the timestamping extension&quot;;
        goto end;
    }

    additional_certs = ossl_tsfac_get_additional_certs(self);
    if (rb_obj_is_kind_of(additional_certs, rb_cArray)) {
        inter_certs = ossl_protect_x509_ary2sk(additional_certs, &amp;status);
        if (status)
                goto end;

        /* this dups the sk_X509 and ups each cert&#39;s ref count */
        TS_RESP_CTX_set_certs(ctx, inter_certs);
        sk_X509_pop_free(inter_certs, X509_free);
    }

    TS_RESP_CTX_set_signer_key(ctx, sign_key);
    if (!NIL_P(def_policy_id) &amp;&amp; !TS_REQ_get_policy_id(req))
        TS_RESP_CTX_set_def_policy(ctx, def_policy_id_obj);
    if (TS_REQ_get_policy_id(req))
        TS_RESP_CTX_set_def_policy(ctx, TS_REQ_get_policy_id(req));
    TS_RESP_CTX_set_time_cb(ctx, ossl_tsfac_time_cb, &amp;lgen_time);

    allowed_digests = ossl_tsfac_get_allowed_digests(self);
    if (rb_obj_is_kind_of(allowed_digests, rb_cArray)) {
        int i;
        VALUE rbmd;
        const EVP_MD *md;

        for (i = 0; i &lt; RARRAY_LEN(allowed_digests); i++) {
            rbmd = rb_ary_entry(allowed_digests, i);
            md = (const EVP_MD *)rb_protect((VALUE (*)(VALUE))ossl_evp_get_digestbyname, rbmd, &amp;status);
            if (status)
                goto end;
            TS_RESP_CTX_add_md(ctx, md);
        }
    }

    str = rb_protect(ossl_to_der, request, &amp;status);
    if (status)
        goto end;

    req_bio = (BIO*)rb_protect((VALUE (*)(VALUE))ossl_obj2bio, (VALUE)&amp;str, &amp;status);
    if (status)
        goto end;

    response = TS_RESP_create_response(ctx, req_bio);
    BIO_free(req_bio);

    if (!response) {
        err_msg = &quot;Error during response generation&quot;;
        goto end;
    }

    /* bad responses aren&#39;t exceptional, but openssl still sets error
     * information. */
    ossl_clear_error();

    SetTSResponse(tsresp, response);
    ret = tsresp;

end:
    ASN1_INTEGER_free(asn1_serial);
    ASN1_OBJECT_free(def_policy_id_obj);
    TS_RESP_CTX_free(ctx);
    if (err_msg)
        ossl_raise(eTimestampError, err_msg);
    if (status)
        rb_jump_tag(status);
    return ret;
}</pre>
          </div>
        </div>


      </div>

    </section>

  </section>
</main>


<footer id="validator-badges" role="contentinfo">
  <p><a href="https://validator.w3.org/check/referer">Validate</a>
  <p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.3.2.
  <p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>