summaryrefslogtreecommitdiffstats
path: root/OpenSSL/PKey/PKey.html
blob: 5f981ef217d4ed6ec5274682b73dc49b94332a25 (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
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
<!DOCTYPE html>

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

<title>class OpenSSL::PKey::PKey - 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 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-c-new">::new</a>
    <li ><a href="#method-i-inspect">#inspect</a>
    <li ><a href="#method-i-oid">#oid</a>
    <li ><a href="#method-i-private_to_der">#private_to_der</a>
    <li ><a href="#method-i-private_to_pem">#private_to_pem</a>
    <li ><a href="#method-i-public_to_der">#public_to_der</a>
    <li ><a href="#method-i-public_to_pem">#public_to_pem</a>
    <li ><a href="#method-i-sign">#sign</a>
    <li ><a href="#method-i-verify">#verify</a>
  </ul>
</div>

  </div>
</nav>

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

  <section class="description">
    
<p>An abstract class that bundles signature creation (<a href="PKey.html#method-i-sign"><code>PKey#sign</code></a>) and validation (<a href="PKey.html#method-i-verify"><code>PKey#verify</code></a>) that is common to all implementations except <a href="DH.html"><code>OpenSSL::PKey::DH</code></a></p>
<ul><li>
<p><a href="RSA.html"><code>OpenSSL::PKey::RSA</code></a></p>
</li><li>
<p><a href="DSA.html"><code>OpenSSL::PKey::DSA</code></a></p>
</li><li>
<p><a href="EC.html"><code>OpenSSL::PKey::EC</code></a></p>
</li></ul>

  </section>

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





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

      <div id="method-c-new" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            new &rarr; self
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>Because <a href="PKey.html"><code>PKey</code></a> is an abstract class, actually calling this method explicitly will raise a NotImplementedError.</p>

          <div class="method-source-code" id="new-source">
            <pre>static VALUE
ossl_pkey_initialize(VALUE self)
{
    if (rb_obj_is_instance_of(self, cPKey)) {
        ossl_raise(rb_eTypeError, &quot;OpenSSL::PKey::PKey can&#39;t be instantiated directly&quot;);
    }
    return self;
}</pre>
          </div>
        </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-inspect" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            inspect &rarr; string
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>Returns a string describing the <a href="PKey.html"><code>PKey</code></a> object.</p>

          <div class="method-source-code" id="inspect-source">
            <pre>static VALUE
ossl_pkey_inspect(VALUE self)
{
    EVP_PKEY *pkey;
    int nid;

    GetPKey(self, pkey);
    nid = EVP_PKEY_id(pkey);
    return rb_sprintf(&quot;#&lt;%&quot;PRIsVALUE&quot;:%p oid=%s&gt;&quot;,
                      rb_class_name(CLASS_OF(self)), (void *)self,
                      OBJ_nid2sn(nid));
}</pre>
          </div>
        </div>


      </div>

      <div id="method-i-oid" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            oid &rarr; string
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>Returns the short name of the OID associated with <em>pkey</em>.</p>

          <div class="method-source-code" id="oid-source">
            <pre>static VALUE
ossl_pkey_oid(VALUE self)
{
    EVP_PKEY *pkey;
    int nid;

    GetPKey(self, pkey);
    nid = EVP_PKEY_id(pkey);
    return rb_str_new_cstr(OBJ_nid2sn(nid));
}</pre>
          </div>
        </div>


      </div>

      <div id="method-i-private_to_der" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            private_to_der                   &rarr; string
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        <div class="method-heading">
          <span class="method-callseq">
            private_to_der(cipher, password) &rarr; string
          </span>
        </div>

        <div class="method-description">
          <p>Serializes the private key to DER-encoded PKCS #8 format. If called without arguments, unencrypted PKCS #8 PrivateKeyInfo format is used. If called with a cipher name and a password, PKCS #8 EncryptedPrivateKeyInfo format with PBES2 encryption scheme is used.</p>

          <div class="method-source-code" id="private_to_der-source">
            <pre>static VALUE
ossl_pkey_private_to_der(int argc, VALUE *argv, VALUE self)
{
    return do_pkcs8_export(argc, argv, self, 1);
}</pre>
          </div>
        </div>


      </div>

      <div id="method-i-private_to_pem" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            private_to_pem                   &rarr; string
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        <div class="method-heading">
          <span class="method-callseq">
            private_to_pem(cipher, password) &rarr; string
          </span>
        </div>

        <div class="method-description">
          <p>Serializes the private key to PEM-encoded PKCS #8 format. See <a href="PKey.html#method-i-private_to_der"><code>private_to_der</code></a> for more details.</p>

          <div class="method-source-code" id="private_to_pem-source">
            <pre>static VALUE
ossl_pkey_private_to_pem(int argc, VALUE *argv, VALUE self)
{
    return do_pkcs8_export(argc, argv, self, 0);
}</pre>
          </div>
        </div>


      </div>

      <div id="method-i-public_to_der" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            public_to_der &rarr; string
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>Serializes the public key to DER-encoded X.509 SubjectPublicKeyInfo format.</p>

          <div class="method-source-code" id="public_to_der-source">
            <pre>static VALUE
ossl_pkey_public_to_der(VALUE self)
{
    return do_spki_export(self, 1);
}</pre>
          </div>
        </div>


      </div>

      <div id="method-i-public_to_pem" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            public_to_pem &rarr; string
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>Serializes the public key to PEM-encoded X.509 SubjectPublicKeyInfo format.</p>

          <div class="method-source-code" id="public_to_pem-source">
            <pre>static VALUE
ossl_pkey_public_to_pem(VALUE self)
{
    return do_spki_export(self, 0);
}</pre>
          </div>
        </div>


      </div>

      <div id="method-i-sign" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            sign(digest, data) &rarr; String
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>To sign the String <em>data</em>, <em>digest</em>, an instance of <a href="../Digest.html"><code>OpenSSL::Digest</code></a>, must be provided. The return value is again a String containing the signature. A <a href="PKeyError.html"><code>PKeyError</code></a> is raised should errors occur. Any previous state of the <a href="../Digest.html"><code>Digest</code></a> instance is irrelevant to the signature outcome, the digest instance is reset to its initial state during the operation.</p>

<h2 id="method-i-sign-label-Example">Example<span><a href="#method-i-sign-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h2>

<pre class="ruby"><span class="ruby-identifier">data</span> = <span class="ruby-string">&#39;Sign me!&#39;</span>
<span class="ruby-identifier">digest</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Digest</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;SHA256&#39;</span>)
<span class="ruby-identifier">pkey</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span><span class="ruby-operator">::</span><span class="ruby-constant">RSA</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">2048</span>)
<span class="ruby-identifier">signature</span> = <span class="ruby-identifier">pkey</span>.<span class="ruby-identifier">sign</span>(<span class="ruby-identifier">digest</span>, <span class="ruby-identifier">data</span>)
</pre>

          <div class="method-source-code" id="sign-source">
            <pre>static VALUE
ossl_pkey_sign(VALUE self, VALUE digest, VALUE data)
{
    EVP_PKEY *pkey;
    const EVP_MD *md;
    EVP_MD_CTX *ctx;
    unsigned int buf_len;
    VALUE str;
    int result;

    pkey = GetPrivPKeyPtr(self);
    md = ossl_evp_get_digestbyname(digest);
    StringValue(data);
    str = rb_str_new(0, EVP_PKEY_size(pkey));

    ctx = EVP_MD_CTX_new();
    if (!ctx)
        ossl_raise(ePKeyError, &quot;EVP_MD_CTX_new&quot;);
    if (!EVP_SignInit_ex(ctx, md, NULL)) {
        EVP_MD_CTX_free(ctx);
        ossl_raise(ePKeyError, &quot;EVP_SignInit_ex&quot;);
    }
    if (!EVP_SignUpdate(ctx, RSTRING_PTR(data), RSTRING_LEN(data))) {
        EVP_MD_CTX_free(ctx);
        ossl_raise(ePKeyError, &quot;EVP_SignUpdate&quot;);
    }
    result = EVP_SignFinal(ctx, (unsigned char *)RSTRING_PTR(str), &amp;buf_len, pkey);
    EVP_MD_CTX_free(ctx);
    if (!result)
        ossl_raise(ePKeyError, &quot;EVP_SignFinal&quot;);
    rb_str_set_len(str, buf_len);

    return str;
}</pre>
          </div>
        </div>


      </div>

      <div id="method-i-verify" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            verify(digest, signature, data) &rarr; String
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>To verify the String <em>signature</em>, <em>digest</em>, an instance of <a href="../Digest.html"><code>OpenSSL::Digest</code></a>, must be provided to re-compute the message digest of the original <em>data</em>, also a String. The return value is <code>true</code> if the signature is valid, <code>false</code> otherwise. A <a href="PKeyError.html"><code>PKeyError</code></a> is raised should errors occur. Any previous state of the <a href="../Digest.html"><code>Digest</code></a> instance is irrelevant to the validation outcome, the digest instance is reset to its initial state during the operation.</p>

<h2 id="method-i-verify-label-Example">Example<span><a href="#method-i-verify-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h2>

<pre class="ruby"><span class="ruby-identifier">data</span> = <span class="ruby-string">&#39;Sign me!&#39;</span>
<span class="ruby-identifier">digest</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Digest</span>.<span class="ruby-identifier">new</span>(<span class="ruby-string">&#39;SHA256&#39;</span>)
<span class="ruby-identifier">pkey</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">PKey</span><span class="ruby-operator">::</span><span class="ruby-constant">RSA</span>.<span class="ruby-identifier">new</span>(<span class="ruby-value">2048</span>)
<span class="ruby-identifier">signature</span> = <span class="ruby-identifier">pkey</span>.<span class="ruby-identifier">sign</span>(<span class="ruby-identifier">digest</span>, <span class="ruby-identifier">data</span>)
<span class="ruby-identifier">pub_key</span> = <span class="ruby-identifier">pkey</span>.<span class="ruby-identifier">public_key</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">pub_key</span>.<span class="ruby-identifier">verify</span>(<span class="ruby-identifier">digest</span>, <span class="ruby-identifier">signature</span>, <span class="ruby-identifier">data</span>) <span class="ruby-comment"># =&gt; true</span>
</pre>

          <div class="method-source-code" id="verify-source">
            <pre>static VALUE
ossl_pkey_verify(VALUE self, VALUE digest, VALUE sig, VALUE data)
{
    EVP_PKEY *pkey;
    const EVP_MD *md;
    EVP_MD_CTX *ctx;
    int siglen, result;

    GetPKey(self, pkey);
    ossl_pkey_check_public_key(pkey);
    md = ossl_evp_get_digestbyname(digest);
    StringValue(sig);
    siglen = RSTRING_LENINT(sig);
    StringValue(data);

    ctx = EVP_MD_CTX_new();
    if (!ctx)
        ossl_raise(ePKeyError, &quot;EVP_MD_CTX_new&quot;);
    if (!EVP_VerifyInit_ex(ctx, md, NULL)) {
        EVP_MD_CTX_free(ctx);
        ossl_raise(ePKeyError, &quot;EVP_VerifyInit_ex&quot;);
    }
    if (!EVP_VerifyUpdate(ctx, RSTRING_PTR(data), RSTRING_LEN(data))) {
        EVP_MD_CTX_free(ctx);
        ossl_raise(ePKeyError, &quot;EVP_VerifyUpdate&quot;);
    }
    result = EVP_VerifyFinal(ctx, (unsigned char *)RSTRING_PTR(sig), siglen, pkey);
    EVP_MD_CTX_free(ctx);
    switch (result) {
    case 0:
        ossl_clear_error();
        return Qfalse;
    case 1:
        return Qtrue;
    default:
        ossl_raise(ePKeyError, &quot;EVP_VerifyFinal&quot;);
    }
}</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>