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

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

<title>module OpenSSL - 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="module">
<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="#module-OpenSSL-label-Examples">Examples</a>
    <li><a href="#module-OpenSSL-label-Keys">Keys</a>
    <li><a href="#module-OpenSSL-label-Creating+a+Key">Creating a Key</a>
    <li><a href="#module-OpenSSL-label-Exporting+a+Key">Exporting a Key</a>
    <li><a href="#module-OpenSSL-label-Loading+a+Key">Loading a Key</a>
    <li><a href="#module-OpenSSL-label-Loading+an+Encrypted+Key">Loading an Encrypted Key</a>
    <li><a href="#module-OpenSSL-label-RSA+Encryption">RSA Encryption</a>
    <li><a href="#module-OpenSSL-label-Encryption+-26+Decryption">Encryption &amp; Decryption</a>
    <li><a href="#module-OpenSSL-label-Signatures">Signatures</a>
    <li><a href="#module-OpenSSL-label-PBKDF2+Password-based+Encryption">PBKDF2 Password-based Encryption</a>
    <li><a href="#module-OpenSSL-label-Encryption">Encryption</a>
    <li><a href="#module-OpenSSL-label-Decryption">Decryption</a>
    <li><a href="#module-OpenSSL-label-PKCS+-235+Password-based+Encryption">PKCS #5 Password-based Encryption</a>
    <li><a href="#module-OpenSSL-label-Encryption">Encryption</a>
    <li><a href="#module-OpenSSL-label-Decryption">Decryption</a>
    <li><a href="#module-OpenSSL-label-X509+Certificates">X509 Certificates</a>
    <li><a href="#module-OpenSSL-label-Creating+a+Certificate">Creating a Certificate</a>
    <li><a href="#module-OpenSSL-label-Certificate+Extensions">Certificate Extensions</a>
    <li><a href="#module-OpenSSL-label-Signing+a+Certificate">Signing a Certificate</a>
    <li><a href="#module-OpenSSL-label-Loading+a+Certificate">Loading a Certificate</a>
    <li><a href="#module-OpenSSL-label-Verifying+a+Certificate">Verifying a Certificate</a>
    <li><a href="#module-OpenSSL-label-Certificate+Authority">Certificate Authority</a>
    <li><a href="#module-OpenSSL-label-CA+Key">CA Key</a>
    <li><a href="#module-OpenSSL-label-CA+Certificate">CA Certificate</a>
    <li><a href="#module-OpenSSL-label-Certificate+Signing+Request">Certificate Signing Request</a>
    <li><a href="#module-OpenSSL-label-Creating+a+Certificate+from+a+CSR">Creating a Certificate from a CSR</a>
    <li><a href="#module-OpenSSL-label-SSL+and+TLS+Connections">SSL and TLS Connections</a>
    <li><a href="#module-OpenSSL-label-SSL+Server">SSL Server</a>
    <li><a href="#module-OpenSSL-label-SSL+client">SSL client</a>
    <li><a href="#module-OpenSSL-label-Peer+Verification">Peer Verification</a>
  </ul>
</div>


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

  <ul class="link-list" role="directory">
    <li ><a href="#method-c-Digest">::Digest</a>
    <li ><a href="#method-c-debug">::debug</a>
    <li ><a href="#method-c-debug-3D">::debug=</a>
    <li ><a href="#method-c-errors">::errors</a>
    <li ><a href="#method-c-fips_mode">::fips_mode</a>
    <li ><a href="#method-c-fips_mode-3D">::fips_mode=</a>
    <li ><a href="#method-c-fixed_length_secure_compare">::fixed_length_secure_compare</a>
    <li ><a href="#method-c-mem_check_start">::mem_check_start</a>
    <li ><a href="#method-c-print_mem_leaks">::print_mem_leaks</a>
    <li ><a href="#method-c-secure_compare">::secure_compare</a>
  </ul>
</div>

  </div>
</nav>

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

  <section class="description">
    
<p><a href="OpenSSL.html"><code>OpenSSL</code></a> provides <a href="OpenSSL/SSL.html"><code>SSL</code></a>, TLS and general purpose cryptography.  It wraps the <a href="https://www.openssl.org/">OpenSSL</a> library.</p>

<h1 id="module-OpenSSL-label-Examples">Examples<span><a href="#module-OpenSSL-label-Examples">&para;</a> <a href="#top">&uarr;</a></span></h1>

<p>All examples assume you have loaded <a href="OpenSSL.html"><code>OpenSSL</code></a> with:</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;openssl&#39;</span>
</pre>

<p>These examples build atop each other.  For example the key created in the next is used in throughout these examples.</p>

<h2 id="module-OpenSSL-label-Keys">Keys<span><a href="#module-OpenSSL-label-Keys">&para;</a> <a href="#top">&uarr;</a></span></h2>

<h3 id="module-OpenSSL-label-Creating+a+Key">Creating a Key<span><a href="#module-OpenSSL-label-Creating+a+Key">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>This example creates a 2048 bit RSA keypair and writes it to the current directory.</p>

<pre class="ruby"><span class="ruby-identifier">key</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">open</span> <span class="ruby-string">&#39;private_key.pem&#39;</span>, <span class="ruby-string">&#39;w&#39;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">io</span><span class="ruby-operator">|</span> <span class="ruby-identifier">io</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">to_pem</span> <span class="ruby-keyword">end</span>
<span class="ruby-identifier">open</span> <span class="ruby-string">&#39;public_key.pem&#39;</span>, <span class="ruby-string">&#39;w&#39;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">io</span><span class="ruby-operator">|</span> <span class="ruby-identifier">io</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">public_key</span>.<span class="ruby-identifier">to_pem</span> <span class="ruby-keyword">end</span>
</pre>

<h3 id="module-OpenSSL-label-Exporting+a+Key">Exporting a Key<span><a href="#module-OpenSSL-label-Exporting+a+Key">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Keys saved to disk without encryption are not secure as anyone who gets ahold of the key may use it unless it is encrypted.  In order to securely export a key you may export it with a pass phrase.</p>

<pre class="ruby"><span class="ruby-identifier">cipher</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Cipher</span>.<span class="ruby-identifier">new</span> <span class="ruby-string">&#39;AES-256-CBC&#39;</span>
<span class="ruby-identifier">pass_phrase</span> = <span class="ruby-string">&#39;my secure pass phrase goes here&#39;</span>

<span class="ruby-identifier">key_secure</span> = <span class="ruby-identifier">key</span>.<span class="ruby-identifier">export</span> <span class="ruby-identifier">cipher</span>, <span class="ruby-identifier">pass_phrase</span>

<span class="ruby-identifier">open</span> <span class="ruby-string">&#39;private.secure.pem&#39;</span>, <span class="ruby-string">&#39;w&#39;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">io</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">io</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">key_secure</span>
<span class="ruby-keyword">end</span>
</pre>

<p><a href="OpenSSL/Cipher.html#method-c-ciphers"><code>OpenSSL::Cipher.ciphers</code></a> returns a list of available ciphers.</p>

<h3 id="module-OpenSSL-label-Loading+a+Key">Loading a Key<span><a href="#module-OpenSSL-label-Loading+a+Key">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>A key can also be loaded from a file.</p>

<pre class="ruby"><span class="ruby-identifier">key2</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-constant">File</span>.<span class="ruby-identifier">read</span> <span class="ruby-string">&#39;private_key.pem&#39;</span>
<span class="ruby-identifier">key2</span>.<span class="ruby-identifier">public?</span> <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-identifier">key2</span>.<span class="ruby-identifier">private?</span> <span class="ruby-comment"># =&gt; true</span>
</pre>

<p>or</p>

<pre class="ruby"><span class="ruby-identifier">key3</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-constant">File</span>.<span class="ruby-identifier">read</span> <span class="ruby-string">&#39;public_key.pem&#39;</span>
<span class="ruby-identifier">key3</span>.<span class="ruby-identifier">public?</span> <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-identifier">key3</span>.<span class="ruby-identifier">private?</span> <span class="ruby-comment"># =&gt; false</span>
</pre>

<h3 id="module-OpenSSL-label-Loading+an+Encrypted+Key">Loading an Encrypted Key<span><a href="#module-OpenSSL-label-Loading+an+Encrypted+Key">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p><a href="OpenSSL.html"><code>OpenSSL</code></a> will prompt you for your pass phrase when loading an encrypted key. If you will not be able to type in the pass phrase you may provide it when loading the key:</p>

<pre class="ruby"><span class="ruby-identifier">key4_pem</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span> <span class="ruby-string">&#39;private.secure.pem&#39;</span>
<span class="ruby-identifier">pass_phrase</span> = <span class="ruby-string">&#39;my secure pass phrase goes here&#39;</span>
<span class="ruby-identifier">key4</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-identifier">key4_pem</span>, <span class="ruby-identifier">pass_phrase</span>
</pre>

<h2 id="module-OpenSSL-label-RSA+Encryption">RSA Encryption<span><a href="#module-OpenSSL-label-RSA+Encryption">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>RSA provides encryption and decryption using the public and private keys. You can use a variety of padding methods depending upon the intended use of encrypted data.</p>

<h3 id="module-OpenSSL-label-Encryption+-26+Decryption">Encryption &amp; Decryption<span><a href="#module-OpenSSL-label-Encryption+-26+Decryption">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Asymmetric public/private key encryption is slow and victim to attack in cases where it is used without padding or directly to encrypt larger chunks of data. Typical use cases for RSA encryption involve “wrapping” a symmetric key with the public key of the recipient who would “unwrap” that symmetric key again using their private key. The following illustrates a simplified example of such a key transport scheme. It shouldn’t be used in practice, though, standardized protocols should always be preferred.</p>

<pre class="ruby"><span class="ruby-identifier">wrapped_key</span> = <span class="ruby-identifier">key</span>.<span class="ruby-identifier">public_encrypt</span> <span class="ruby-identifier">key</span>
</pre>

<p>A symmetric key encrypted with the public key can only be decrypted with the corresponding private key of the recipient.</p>

<pre class="ruby"><span class="ruby-identifier">original_key</span> = <span class="ruby-identifier">key</span>.<span class="ruby-identifier">private_decrypt</span> <span class="ruby-identifier">wrapped_key</span>
</pre>

<p>By default PKCS#1 padding will be used, but it is also possible to use other forms of padding, see <a href="OpenSSL/PKey/RSA.html"><code>PKey::RSA</code></a> for further details.</p>

<h3 id="module-OpenSSL-label-Signatures">Signatures<span><a href="#module-OpenSSL-label-Signatures">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Using “private_encrypt” to encrypt some data with the private key is equivalent to applying a digital signature to the data. A verifying party may validate the signature by comparing the result of decrypting the signature with “public_decrypt” to the original data. However, <a href="OpenSSL/PKey.html"><code>OpenSSL::PKey</code></a> already has methods “sign” and “verify” that handle digital signatures in a standardized way - “private_encrypt” and “public_decrypt” shouldn’t be used in practice.</p>

<p>To sign a document, a cryptographically secure hash of the document is computed first, which is then signed using the private key.</p>

<pre class="ruby"><span class="ruby-identifier">signature</span> = <span class="ruby-identifier">key</span>.<span class="ruby-identifier">sign</span> <span class="ruby-string">&#39;SHA256&#39;</span>, <span class="ruby-identifier">document</span>
</pre>

<p>To validate the signature, again a hash of the document is computed and the signature is decrypted using the public key. The result is then compared to the hash just computed, if they are equal the signature was valid.</p>

<pre class="ruby"><span class="ruby-keyword">if</span> <span class="ruby-identifier">key</span>.<span class="ruby-identifier">verify</span> <span class="ruby-string">&#39;SHA256&#39;</span>, <span class="ruby-identifier">signature</span>, <span class="ruby-identifier">document</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&#39;Valid&#39;</span>
<span class="ruby-keyword">else</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-string">&#39;Invalid&#39;</span>
<span class="ruby-keyword">end</span>
</pre>

<h2 id="module-OpenSSL-label-PBKDF2+Password-based+Encryption">PBKDF2 Password-based Encryption<span><a href="#module-OpenSSL-label-PBKDF2+Password-based+Encryption">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>If supported by the underlying <a href="OpenSSL.html"><code>OpenSSL</code></a> version used, Password-based Encryption should use the features of <a href="OpenSSL/PKCS5.html"><code>PKCS5</code></a>. If not supported or if required by legacy applications, the older, less secure methods specified in RFC 2898 are also supported (see below).</p>

<p><a href="OpenSSL/PKCS5.html"><code>PKCS5</code></a> supports PBKDF2 as it was specified in PKCS#5 <a href="http://www.rsa.com/rsalabs/node.asp?id=2127">v2.0</a>. It still uses a password, a salt, and additionally a number of iterations that will slow the key derivation process down. The slower this is, the more work it requires being able to brute-force the resulting key.</p>

<h3 id="module-OpenSSL-label-Encryption">Encryption<span><a href="#module-OpenSSL-label-Encryption">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>The strategy is to first instantiate a <a href="OpenSSL/Cipher.html"><code>Cipher</code></a> for encryption, and then to generate a random IV plus a key derived from the password using PBKDF2. PKCS #5 v2.0 recommends at least 8 bytes for the salt, the number of iterations largely depends on the hardware being used.</p>

<pre>cipher = OpenSSL::Cipher.new &#39;AES-256-CBC&#39;
cipher.encrypt
iv = cipher.random_iv

pwd = &#39;some hopefully not to easily guessable password&#39;
salt = OpenSSL::Random.random_bytes 16
iter = 20000
key_len = cipher.key_len
digest = OpenSSL::Digest.new(&#39;SHA256&#39;)

key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, salt, iter, key_len, digest)
cipher.key = key

Now encrypt the data:

encrypted = cipher.update document
encrypted &lt;&lt; cipher.final</pre>

<h3 id="module-OpenSSL-label-Decryption">Decryption<span><a href="#module-OpenSSL-label-Decryption">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Use the same steps as before to derive the symmetric AES key, this time setting the <a href="OpenSSL/Cipher.html"><code>Cipher</code></a> up for decryption.</p>

<pre>cipher = OpenSSL::Cipher.new &#39;AES-256-CBC&#39;
cipher.decrypt
cipher.iv = iv # the one generated with #random_iv

pwd = &#39;some hopefully not to easily guessable password&#39;
salt = ... # the one generated above
iter = 20000
key_len = cipher.key_len
digest = OpenSSL::Digest.new(&#39;SHA256&#39;)

key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, salt, iter, key_len, digest)
cipher.key = key

Now decrypt the data:

decrypted = cipher.update encrypted
decrypted &lt;&lt; cipher.final</pre>

<h2 id="module-OpenSSL-label-PKCS+-235+Password-based+Encryption">PKCS #5 Password-based Encryption<span><a href="#module-OpenSSL-label-PKCS+-235+Password-based+Encryption">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>PKCS #5 is a password-based encryption standard documented at <a href="http://www.ietf.org/rfc/rfc2898.txt">RFC2898</a>.  It allows a short password or passphrase to be used to create a secure encryption key. If possible, PBKDF2 as described above should be used if the circumstances allow it.</p>

<p>PKCS #5 uses a <a href="OpenSSL/Cipher.html"><code>Cipher</code></a>, a pass phrase and a salt to generate an encryption key.</p>

<pre class="ruby"><span class="ruby-identifier">pass_phrase</span> = <span class="ruby-string">&#39;my secure pass phrase goes here&#39;</span>
<span class="ruby-identifier">salt</span> = <span class="ruby-string">&#39;8 octets&#39;</span>
</pre>

<h3 id="module-OpenSSL-label-Encryption">Encryption<span><a href="#module-OpenSSL-label-Encryption">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>First set up the cipher for encryption</p>

<pre class="ruby"><span class="ruby-identifier">encryptor</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Cipher</span>.<span class="ruby-identifier">new</span> <span class="ruby-string">&#39;AES-256-CBC&#39;</span>
<span class="ruby-identifier">encryptor</span>.<span class="ruby-identifier">encrypt</span>
<span class="ruby-identifier">encryptor</span>.<span class="ruby-identifier">pkcs5_keyivgen</span> <span class="ruby-identifier">pass_phrase</span>, <span class="ruby-identifier">salt</span>
</pre>

<p>Then pass the data you want to encrypt through</p>

<pre class="ruby"><span class="ruby-identifier">encrypted</span> = <span class="ruby-identifier">encryptor</span>.<span class="ruby-identifier">update</span> <span class="ruby-string">&#39;top secret document&#39;</span>
<span class="ruby-identifier">encrypted</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">encryptor</span>.<span class="ruby-identifier">final</span>
</pre>

<h3 id="module-OpenSSL-label-Decryption">Decryption<span><a href="#module-OpenSSL-label-Decryption">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Use a new <a href="OpenSSL/Cipher.html"><code>Cipher</code></a> instance set up for decryption</p>

<pre class="ruby"><span class="ruby-identifier">decryptor</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Cipher</span>.<span class="ruby-identifier">new</span> <span class="ruby-string">&#39;AES-256-CBC&#39;</span>
<span class="ruby-identifier">decryptor</span>.<span class="ruby-identifier">decrypt</span>
<span class="ruby-identifier">decryptor</span>.<span class="ruby-identifier">pkcs5_keyivgen</span> <span class="ruby-identifier">pass_phrase</span>, <span class="ruby-identifier">salt</span>
</pre>

<p>Then pass the data you want to decrypt through</p>

<pre class="ruby"><span class="ruby-identifier">plain</span> = <span class="ruby-identifier">decryptor</span>.<span class="ruby-identifier">update</span> <span class="ruby-identifier">encrypted</span>
<span class="ruby-identifier">plain</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">decryptor</span>.<span class="ruby-identifier">final</span>
</pre>

<h2 id="module-OpenSSL-label-X509+Certificates"><a href="OpenSSL/X509.html"><code>X509</code></a> Certificates<span><a href="#module-OpenSSL-label-X509+Certificates">&para;</a> <a href="#top">&uarr;</a></span></h2>

<h3 id="module-OpenSSL-label-Creating+a+Certificate">Creating a Certificate<span><a href="#module-OpenSSL-label-Creating+a+Certificate">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>This example creates a self-signed certificate using an RSA key and a SHA1 signature.</p>

<pre class="ruby"><span class="ruby-identifier">key</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">name</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">X509</span><span class="ruby-operator">::</span><span class="ruby-constant">Name</span>.<span class="ruby-identifier">parse</span> <span class="ruby-string">&#39;/CN=nobody/DC=example&#39;</span>

<span class="ruby-identifier">cert</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">X509</span><span class="ruby-operator">::</span><span class="ruby-constant">Certificate</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">cert</span>.<span class="ruby-identifier">version</span> = <span class="ruby-value">2</span>
<span class="ruby-identifier">cert</span>.<span class="ruby-identifier">serial</span> = <span class="ruby-value">0</span>
<span class="ruby-identifier">cert</span>.<span class="ruby-identifier">not_before</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>
<span class="ruby-identifier">cert</span>.<span class="ruby-identifier">not_after</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span> <span class="ruby-operator">+</span> <span class="ruby-value">3600</span>

<span class="ruby-identifier">cert</span>.<span class="ruby-identifier">public_key</span> = <span class="ruby-identifier">key</span>.<span class="ruby-identifier">public_key</span>
<span class="ruby-identifier">cert</span>.<span class="ruby-identifier">subject</span> = <span class="ruby-identifier">name</span>
</pre>

<h3 id="module-OpenSSL-label-Certificate+Extensions">Certificate Extensions<span><a href="#module-OpenSSL-label-Certificate+Extensions">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>You can add extensions to the certificate with OpenSSL::SSL::ExtensionFactory to indicate the purpose of the certificate.</p>

<pre class="ruby"><span class="ruby-identifier">extension_factory</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">X509</span><span class="ruby-operator">::</span><span class="ruby-constant">ExtensionFactory</span>.<span class="ruby-identifier">new</span> <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">cert</span>

<span class="ruby-identifier">cert</span>.<span class="ruby-identifier">add_extension</span> \
  <span class="ruby-identifier">extension_factory</span>.<span class="ruby-identifier">create_extension</span>(<span class="ruby-string">&#39;basicConstraints&#39;</span>, <span class="ruby-string">&#39;CA:FALSE&#39;</span>, <span class="ruby-keyword">true</span>)

<span class="ruby-identifier">cert</span>.<span class="ruby-identifier">add_extension</span> \
  <span class="ruby-identifier">extension_factory</span>.<span class="ruby-identifier">create_extension</span>(
    <span class="ruby-string">&#39;keyUsage&#39;</span>, <span class="ruby-string">&#39;keyEncipherment,dataEncipherment,digitalSignature&#39;</span>)

<span class="ruby-identifier">cert</span>.<span class="ruby-identifier">add_extension</span> \
  <span class="ruby-identifier">extension_factory</span>.<span class="ruby-identifier">create_extension</span>(<span class="ruby-string">&#39;subjectKeyIdentifier&#39;</span>, <span class="ruby-string">&#39;hash&#39;</span>)
</pre>

<p>The list of supported extensions (and in some cases their possible values) can be derived from the “objects.h” file in the <a href="OpenSSL.html"><code>OpenSSL</code></a> source code.</p>

<h3 id="module-OpenSSL-label-Signing+a+Certificate">Signing a Certificate<span><a href="#module-OpenSSL-label-Signing+a+Certificate">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>To sign a certificate set the issuer and use <a href="OpenSSL/X509/Certificate.html#method-i-sign"><code>OpenSSL::X509::Certificate#sign</code></a> with a digest algorithm.  This creates a self-signed cert because we’re using the same name and key to sign the certificate as was used to create the certificate.</p>

<pre class="ruby"><span class="ruby-identifier">cert</span>.<span class="ruby-identifier">issuer</span> = <span class="ruby-identifier">name</span>
<span class="ruby-identifier">cert</span>.<span class="ruby-identifier">sign</span> <span class="ruby-identifier">key</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;SHA1&#39;</span>)

<span class="ruby-identifier">open</span> <span class="ruby-string">&#39;certificate.pem&#39;</span>, <span class="ruby-string">&#39;w&#39;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">io</span><span class="ruby-operator">|</span> <span class="ruby-identifier">io</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">cert</span>.<span class="ruby-identifier">to_pem</span> <span class="ruby-keyword">end</span>
</pre>

<h3 id="module-OpenSSL-label-Loading+a+Certificate">Loading a Certificate<span><a href="#module-OpenSSL-label-Loading+a+Certificate">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Like a key, a cert can also be loaded from a file.</p>

<pre class="ruby"><span class="ruby-identifier">cert2</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">X509</span><span class="ruby-operator">::</span><span class="ruby-constant">Certificate</span>.<span class="ruby-identifier">new</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span> <span class="ruby-string">&#39;certificate.pem&#39;</span>
</pre>

<h3 id="module-OpenSSL-label-Verifying+a+Certificate">Verifying a Certificate<span><a href="#module-OpenSSL-label-Verifying+a+Certificate">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Certificate#verify will return true when a certificate was signed with the given public key.</p>

<pre class="ruby"><span class="ruby-identifier">raise</span> <span class="ruby-string">&#39;certificate can not be verified&#39;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">cert2</span>.<span class="ruby-identifier">verify</span> <span class="ruby-identifier">key</span>
</pre>

<h2 id="module-OpenSSL-label-Certificate+Authority">Certificate Authority<span><a href="#module-OpenSSL-label-Certificate+Authority">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>A certificate authority (CA) is a trusted third party that allows you to verify the ownership of unknown certificates.  The CA issues key signatures that indicate it trusts the user of that key.  A user encountering the key can verify the signature by using the CA’s public key.</p>

<h3 id="module-OpenSSL-label-CA+Key">CA Key<span><a href="#module-OpenSSL-label-CA+Key">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>CA keys are valuable, so we encrypt and save it to disk and make sure it is not readable by other users.</p>

<pre class="ruby"><span class="ruby-identifier">ca_key</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">pass_phrase</span> = <span class="ruby-string">&#39;my secure pass phrase goes here&#39;</span>

<span class="ruby-identifier">cipher</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Cipher</span>.<span class="ruby-identifier">new</span> <span class="ruby-string">&#39;AES-256-CBC&#39;</span>

<span class="ruby-identifier">open</span> <span class="ruby-string">&#39;ca_key.pem&#39;</span>, <span class="ruby-string">&#39;w&#39;</span>, <span class="ruby-value">0400</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">io</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">io</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">ca_key</span>.<span class="ruby-identifier">export</span>(<span class="ruby-identifier">cipher</span>, <span class="ruby-identifier">pass_phrase</span>)
<span class="ruby-keyword">end</span>
</pre>

<h3 id="module-OpenSSL-label-CA+Certificate">CA Certificate<span><a href="#module-OpenSSL-label-CA+Certificate">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>A CA certificate is created the same way we created a certificate above, but with different extensions.</p>

<pre class="ruby"><span class="ruby-identifier">ca_name</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">X509</span><span class="ruby-operator">::</span><span class="ruby-constant">Name</span>.<span class="ruby-identifier">parse</span> <span class="ruby-string">&#39;/CN=ca/DC=example&#39;</span>

<span class="ruby-identifier">ca_cert</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">X509</span><span class="ruby-operator">::</span><span class="ruby-constant">Certificate</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">ca_cert</span>.<span class="ruby-identifier">serial</span> = <span class="ruby-value">0</span>
<span class="ruby-identifier">ca_cert</span>.<span class="ruby-identifier">version</span> = <span class="ruby-value">2</span>
<span class="ruby-identifier">ca_cert</span>.<span class="ruby-identifier">not_before</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>
<span class="ruby-identifier">ca_cert</span>.<span class="ruby-identifier">not_after</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span> <span class="ruby-operator">+</span> <span class="ruby-value">86400</span>

<span class="ruby-identifier">ca_cert</span>.<span class="ruby-identifier">public_key</span> = <span class="ruby-identifier">ca_key</span>.<span class="ruby-identifier">public_key</span>
<span class="ruby-identifier">ca_cert</span>.<span class="ruby-identifier">subject</span> = <span class="ruby-identifier">ca_name</span>
<span class="ruby-identifier">ca_cert</span>.<span class="ruby-identifier">issuer</span> = <span class="ruby-identifier">ca_name</span>

<span class="ruby-identifier">extension_factory</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">X509</span><span class="ruby-operator">::</span><span class="ruby-constant">ExtensionFactory</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">extension_factory</span>.<span class="ruby-identifier">subject_certificate</span> = <span class="ruby-identifier">ca_cert</span>
<span class="ruby-identifier">extension_factory</span>.<span class="ruby-identifier">issuer_certificate</span> = <span class="ruby-identifier">ca_cert</span>

<span class="ruby-identifier">ca_cert</span>.<span class="ruby-identifier">add_extension</span> \
  <span class="ruby-identifier">extension_factory</span>.<span class="ruby-identifier">create_extension</span>(<span class="ruby-string">&#39;subjectKeyIdentifier&#39;</span>, <span class="ruby-string">&#39;hash&#39;</span>)
</pre>

<p>This extension indicates the CA’s key may be used as a CA.</p>

<pre class="ruby"><span class="ruby-identifier">ca_cert</span>.<span class="ruby-identifier">add_extension</span> \
  <span class="ruby-identifier">extension_factory</span>.<span class="ruby-identifier">create_extension</span>(<span class="ruby-string">&#39;basicConstraints&#39;</span>, <span class="ruby-string">&#39;CA:TRUE&#39;</span>, <span class="ruby-keyword">true</span>)
</pre>

<p>This extension indicates the CA’s key may be used to verify signatures on both certificates and certificate revocations.</p>

<pre class="ruby"><span class="ruby-identifier">ca_cert</span>.<span class="ruby-identifier">add_extension</span> \
  <span class="ruby-identifier">extension_factory</span>.<span class="ruby-identifier">create_extension</span>(
    <span class="ruby-string">&#39;keyUsage&#39;</span>, <span class="ruby-string">&#39;cRLSign,keyCertSign&#39;</span>, <span class="ruby-keyword">true</span>)
</pre>

<p>Root CA certificates are self-signed.</p>

<pre class="ruby"><span class="ruby-identifier">ca_cert</span>.<span class="ruby-identifier">sign</span> <span class="ruby-identifier">ca_key</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;SHA1&#39;</span>)
</pre>

<p>The CA certificate is saved to disk so it may be distributed to all the users of the keys this CA will sign.</p>

<pre class="ruby"><span class="ruby-identifier">open</span> <span class="ruby-string">&#39;ca_cert.pem&#39;</span>, <span class="ruby-string">&#39;w&#39;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">io</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">io</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">ca_cert</span>.<span class="ruby-identifier">to_pem</span>
<span class="ruby-keyword">end</span>
</pre>

<h3 id="module-OpenSSL-label-Certificate+Signing+Request">Certificate Signing Request<span><a href="#module-OpenSSL-label-Certificate+Signing+Request">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>The CA signs keys through a Certificate Signing Request (CSR).  The CSR contains the information necessary to identify the key.</p>

<pre class="ruby"><span class="ruby-identifier">csr</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">X509</span><span class="ruby-operator">::</span><span class="ruby-constant">Request</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">csr</span>.<span class="ruby-identifier">version</span> = <span class="ruby-value">0</span>
<span class="ruby-identifier">csr</span>.<span class="ruby-identifier">subject</span> = <span class="ruby-identifier">name</span>
<span class="ruby-identifier">csr</span>.<span class="ruby-identifier">public_key</span> = <span class="ruby-identifier">key</span>.<span class="ruby-identifier">public_key</span>
<span class="ruby-identifier">csr</span>.<span class="ruby-identifier">sign</span> <span class="ruby-identifier">key</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;SHA1&#39;</span>)
</pre>

<p>A CSR is saved to disk and sent to the CA for signing.</p>

<pre class="ruby"><span class="ruby-identifier">open</span> <span class="ruby-string">&#39;csr.pem&#39;</span>, <span class="ruby-string">&#39;w&#39;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">io</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">io</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">csr</span>.<span class="ruby-identifier">to_pem</span>
<span class="ruby-keyword">end</span>
</pre>

<h3 id="module-OpenSSL-label-Creating+a+Certificate+from+a+CSR">Creating a Certificate from a CSR<span><a href="#module-OpenSSL-label-Creating+a+Certificate+from+a+CSR">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Upon receiving a CSR the CA will verify it before signing it.  A minimal verification would be to check the CSR’s signature.</p>

<pre class="ruby"><span class="ruby-identifier">csr</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">X509</span><span class="ruby-operator">::</span><span class="ruby-constant">Request</span>.<span class="ruby-identifier">new</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">read</span> <span class="ruby-string">&#39;csr.pem&#39;</span>

<span class="ruby-identifier">raise</span> <span class="ruby-string">&#39;CSR can not be verified&#39;</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">csr</span>.<span class="ruby-identifier">verify</span> <span class="ruby-identifier">csr</span>.<span class="ruby-identifier">public_key</span>
</pre>

<p>After verification a certificate is created, marked for various usages, signed with the CA key and returned to the requester.</p>

<pre class="ruby"><span class="ruby-identifier">csr_cert</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">X509</span><span class="ruby-operator">::</span><span class="ruby-constant">Certificate</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">csr_cert</span>.<span class="ruby-identifier">serial</span> = <span class="ruby-value">0</span>
<span class="ruby-identifier">csr_cert</span>.<span class="ruby-identifier">version</span> = <span class="ruby-value">2</span>
<span class="ruby-identifier">csr_cert</span>.<span class="ruby-identifier">not_before</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>
<span class="ruby-identifier">csr_cert</span>.<span class="ruby-identifier">not_after</span> = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span> <span class="ruby-operator">+</span> <span class="ruby-value">600</span>

<span class="ruby-identifier">csr_cert</span>.<span class="ruby-identifier">subject</span> = <span class="ruby-identifier">csr</span>.<span class="ruby-identifier">subject</span>
<span class="ruby-identifier">csr_cert</span>.<span class="ruby-identifier">public_key</span> = <span class="ruby-identifier">csr</span>.<span class="ruby-identifier">public_key</span>
<span class="ruby-identifier">csr_cert</span>.<span class="ruby-identifier">issuer</span> = <span class="ruby-identifier">ca_cert</span>.<span class="ruby-identifier">subject</span>

<span class="ruby-identifier">extension_factory</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">X509</span><span class="ruby-operator">::</span><span class="ruby-constant">ExtensionFactory</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">extension_factory</span>.<span class="ruby-identifier">subject_certificate</span> = <span class="ruby-identifier">csr_cert</span>
<span class="ruby-identifier">extension_factory</span>.<span class="ruby-identifier">issuer_certificate</span> = <span class="ruby-identifier">ca_cert</span>

<span class="ruby-identifier">csr_cert</span>.<span class="ruby-identifier">add_extension</span> \
  <span class="ruby-identifier">extension_factory</span>.<span class="ruby-identifier">create_extension</span>(<span class="ruby-string">&#39;basicConstraints&#39;</span>, <span class="ruby-string">&#39;CA:FALSE&#39;</span>)

<span class="ruby-identifier">csr_cert</span>.<span class="ruby-identifier">add_extension</span> \
  <span class="ruby-identifier">extension_factory</span>.<span class="ruby-identifier">create_extension</span>(
    <span class="ruby-string">&#39;keyUsage&#39;</span>, <span class="ruby-string">&#39;keyEncipherment,dataEncipherment,digitalSignature&#39;</span>)

<span class="ruby-identifier">csr_cert</span>.<span class="ruby-identifier">add_extension</span> \
  <span class="ruby-identifier">extension_factory</span>.<span class="ruby-identifier">create_extension</span>(<span class="ruby-string">&#39;subjectKeyIdentifier&#39;</span>, <span class="ruby-string">&#39;hash&#39;</span>)

<span class="ruby-identifier">csr_cert</span>.<span class="ruby-identifier">sign</span> <span class="ruby-identifier">ca_key</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;SHA1&#39;</span>)

<span class="ruby-identifier">open</span> <span class="ruby-string">&#39;csr_cert.pem&#39;</span>, <span class="ruby-string">&#39;w&#39;</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">io</span><span class="ruby-operator">|</span>
  <span class="ruby-identifier">io</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">csr_cert</span>.<span class="ruby-identifier">to_pem</span>
<span class="ruby-keyword">end</span>
</pre>

<h2 id="module-OpenSSL-label-SSL+and+TLS+Connections"><a href="OpenSSL/SSL.html"><code>SSL</code></a> and TLS Connections<span><a href="#module-OpenSSL-label-SSL+and+TLS+Connections">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Using our created key and certificate we can create an <a href="OpenSSL/SSL.html"><code>SSL</code></a> or TLS connection. An SSLContext is used to set up an <a href="OpenSSL/SSL.html"><code>SSL</code></a> session.</p>

<pre class="ruby"><span class="ruby-identifier">context</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">SSL</span><span class="ruby-operator">::</span><span class="ruby-constant">SSLContext</span>.<span class="ruby-identifier">new</span>
</pre>

<h3 id="module-OpenSSL-label-SSL+Server"><a href="OpenSSL/SSL.html"><code>SSL</code></a> Server<span><a href="#module-OpenSSL-label-SSL+Server">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>An <a href="OpenSSL/SSL.html"><code>SSL</code></a> server requires the certificate and private key to communicate securely with its clients:</p>

<pre class="ruby"><span class="ruby-identifier">context</span>.<span class="ruby-identifier">cert</span> = <span class="ruby-identifier">cert</span>
<span class="ruby-identifier">context</span>.<span class="ruby-identifier">key</span> = <span class="ruby-identifier">key</span>
</pre>

<p>Then create an SSLServer with a TCP server socket and the context.  Use the SSLServer like an ordinary TCP server.</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>

<span class="ruby-identifier">tcp_server</span> = <span class="ruby-constant">TCPServer</span>.<span class="ruby-identifier">new</span> <span class="ruby-value">5000</span>
<span class="ruby-identifier">ssl_server</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">SSL</span><span class="ruby-operator">::</span><span class="ruby-constant">SSLServer</span>.<span class="ruby-identifier">new</span> <span class="ruby-identifier">tcp_server</span>, <span class="ruby-identifier">context</span>

<span class="ruby-identifier">loop</span> <span class="ruby-keyword">do</span>
  <span class="ruby-identifier">ssl_connection</span> = <span class="ruby-identifier">ssl_server</span>.<span class="ruby-identifier">accept</span>

  <span class="ruby-identifier">data</span> = <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">gets</span>

  <span class="ruby-identifier">response</span> = <span class="ruby-node">&quot;I got #{data.dump}&quot;</span>
  <span class="ruby-identifier">puts</span> <span class="ruby-identifier">response</span>

  <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">puts</span> <span class="ruby-node">&quot;I got #{data.dump}&quot;</span>
  <span class="ruby-identifier">connection</span>.<span class="ruby-identifier">close</span>
<span class="ruby-keyword">end</span>
</pre>

<h3 id="module-OpenSSL-label-SSL+client"><a href="OpenSSL/SSL.html"><code>SSL</code></a> client<span><a href="#module-OpenSSL-label-SSL+client">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>An <a href="OpenSSL/SSL.html"><code>SSL</code></a> client is created with a TCP socket and the context. SSLSocket#connect must be called to initiate the <a href="OpenSSL/SSL.html"><code>SSL</code></a> handshake and start encryption.  A key and certificate are not required for the client socket.</p>

<p>Note that SSLSocket#close doesn’t close the underlying socket by default. Set SSLSocket#sync_close to true if you want.</p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>

<span class="ruby-identifier">tcp_socket</span> = <span class="ruby-constant">TCPSocket</span>.<span class="ruby-identifier">new</span> <span class="ruby-string">&#39;localhost&#39;</span>, <span class="ruby-value">5000</span>
<span class="ruby-identifier">ssl_client</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">SSL</span><span class="ruby-operator">::</span><span class="ruby-constant">SSLSocket</span>.<span class="ruby-identifier">new</span> <span class="ruby-identifier">tcp_socket</span>, <span class="ruby-identifier">context</span>
<span class="ruby-identifier">ssl_client</span>.<span class="ruby-identifier">sync_close</span> = <span class="ruby-keyword">true</span>
<span class="ruby-identifier">ssl_client</span>.<span class="ruby-identifier">connect</span>

<span class="ruby-identifier">ssl_client</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;hello server!&quot;</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">ssl_client</span>.<span class="ruby-identifier">gets</span>

<span class="ruby-identifier">ssl_client</span>.<span class="ruby-identifier">close</span> <span class="ruby-comment"># shutdown the TLS connection and close tcp_socket</span>
</pre>

<h3 id="module-OpenSSL-label-Peer+Verification">Peer Verification<span><a href="#module-OpenSSL-label-Peer+Verification">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>An unverified <a href="OpenSSL/SSL.html"><code>SSL</code></a> connection does not provide much security.  For enhanced security the client or server can verify the certificate of its peer.</p>

<p>The client can be modified to verify the server’s certificate against the certificate authority’s certificate:</p>

<pre class="ruby"><span class="ruby-identifier">context</span>.<span class="ruby-identifier">ca_file</span> = <span class="ruby-string">&#39;ca_cert.pem&#39;</span>
<span class="ruby-identifier">context</span>.<span class="ruby-identifier">verify_mode</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">SSL</span><span class="ruby-operator">::</span><span class="ruby-constant">VERIFY_PEER</span>

<span class="ruby-identifier">require</span> <span class="ruby-string">&#39;socket&#39;</span>

<span class="ruby-identifier">tcp_socket</span> = <span class="ruby-constant">TCPSocket</span>.<span class="ruby-identifier">new</span> <span class="ruby-string">&#39;localhost&#39;</span>, <span class="ruby-value">5000</span>
<span class="ruby-identifier">ssl_client</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">SSL</span><span class="ruby-operator">::</span><span class="ruby-constant">SSLSocket</span>.<span class="ruby-identifier">new</span> <span class="ruby-identifier">tcp_socket</span>, <span class="ruby-identifier">context</span>
<span class="ruby-identifier">ssl_client</span>.<span class="ruby-identifier">connect</span>

<span class="ruby-identifier">ssl_client</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">&quot;hello server!&quot;</span>
<span class="ruby-identifier">puts</span> <span class="ruby-identifier">ssl_client</span>.<span class="ruby-identifier">gets</span>
</pre>

<p>If the server certificate is invalid or <code>context.ca_file</code> is not set when verifying peers an <a href="OpenSSL/SSL/SSLError.html"><code>OpenSSL::SSL::SSLError</code></a> will be raised.</p>

  </section>

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


    <section class="constants-list">
      <header>
        <h3>Constants</h3>
      </header>
      <dl>
        <dt id="OPENSSL_FIPS">OPENSSL_FIPS
        <dd><p>Boolean indicating whether <a href="OpenSSL.html"><code>OpenSSL</code></a> is FIPS-capable or not</p>
        <dt id="OPENSSL_LIBRARY_VERSION">OPENSSL_LIBRARY_VERSION
        <dd>
        <dt id="OPENSSL_VERSION">OPENSSL_VERSION
        <dd><p>Version of <a href="OpenSSL.html"><code>OpenSSL</code></a> the ruby <a href="OpenSSL.html"><code>OpenSSL</code></a> extension was built with</p>
        <dt id="OPENSSL_VERSION_NUMBER">OPENSSL_VERSION_NUMBER
        <dd><p>Version number of <a href="OpenSSL.html"><code>OpenSSL</code></a> the ruby <a href="OpenSSL.html"><code>OpenSSL</code></a> extension was built with (base 16)</p>
        <dt id="VERSION">VERSION
        <dd>
      </dl>
    </section>



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

      <div id="method-c-Digest" class="method-detail ">
        <div class="method-heading">
          <span class="method-name">Digest</span><span
            class="method-args">(name)</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>Returns a <a href="OpenSSL/Digest.html"><code>Digest</code></a> subclass by <em>name</em></p>

<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">&#39;openssl&#39;</span>

<span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Digest</span>(<span class="ruby-string">&quot;MD5&quot;</span>)
<span class="ruby-comment"># =&gt; OpenSSL::Digest::MD5</span>

<span class="ruby-constant">Digest</span>(<span class="ruby-string">&quot;Foo&quot;</span>)
<span class="ruby-comment"># =&gt; NameError: wrong constant name Foo</span>
</pre>

          <div class="method-source-code" id="Digest-source">
            <pre><span class="ruby-comment"># File lib/openssl/digest.rb, line 67</span>
<span class="ruby-keyword">def</span> <span class="ruby-constant">Digest</span>(<span class="ruby-identifier ruby-title">name</span>)
  <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Digest</span>.<span class="ruby-identifier">const_get</span>(<span class="ruby-identifier">name</span>)
<span class="ruby-keyword">end</span></pre>
          </div>
        </div>


      </div>

      <div id="method-c-debug" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            debug &rarr; true | false
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          

          <div class="method-source-code" id="debug-source">
            <pre>static VALUE
ossl_debug_get(VALUE self)
{
    return dOSSL;
}</pre>
          </div>
        </div>


      </div>

      <div id="method-c-debug-3D" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            debug = boolean &rarr; boolean
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>Turns on or off debug mode. With debug mode, all erros added to the <a href="OpenSSL.html"><code>OpenSSL</code></a> error queue will be printed to stderr.</p>

          <div class="method-source-code" id="debug-3D-source">
            <pre>static VALUE
ossl_debug_set(VALUE self, VALUE val)
{
    dOSSL = RTEST(val) ? Qtrue : Qfalse;

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


      </div>

      <div id="method-c-errors" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            errors &rarr; [String...]
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>See any remaining errors held in queue.</p>

<p>Any errors you see here are probably due to a bug in Ruby’s <a href="OpenSSL.html"><code>OpenSSL</code></a> implementation.</p>

          <div class="method-source-code" id="errors-source">
            <pre>VALUE
ossl_get_errors(VALUE _)
{
    VALUE ary;
    long e;

    ary = rb_ary_new();
    while ((e = ERR_get_error()) != 0){
        rb_ary_push(ary, rb_str_new2(ERR_error_string(e, NULL)));
    }

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


      </div>

      <div id="method-c-fips_mode" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            fips_mode &rarr; true | false
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          

          <div class="method-source-code" id="fips_mode-source">
            <pre>static VALUE
ossl_fips_mode_get(VALUE self)
{

#ifdef OPENSSL_FIPS
    VALUE enabled;
    enabled = FIPS_mode() ? Qtrue : Qfalse;
    return enabled;
#else
    return Qfalse;
#endif
}</pre>
          </div>
        </div>


      </div>

      <div id="method-c-fips_mode-3D" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            fips_mode = boolean &rarr; boolean
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>Turns FIPS mode on or off. Turning on FIPS mode will obviously only have an effect for FIPS-capable installations of the <a href="OpenSSL.html"><code>OpenSSL</code></a> library. Trying to do so otherwise will result in an error.</p>

<h3 id="method-c-fips_mode-3D-label-Examples">Examples<span><a href="#method-c-fips_mode-3D-label-Examples">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-constant">OpenSSL</span>.<span class="ruby-identifier">fips_mode</span> = <span class="ruby-keyword">true</span>   <span class="ruby-comment"># turn FIPS mode on</span>
<span class="ruby-constant">OpenSSL</span>.<span class="ruby-identifier">fips_mode</span> = <span class="ruby-keyword">false</span>  <span class="ruby-comment"># and off again</span>
</pre>

          <div class="method-source-code" id="fips_mode-3D-source">
            <pre>static VALUE
ossl_fips_mode_set(VALUE self, VALUE enabled)
{

#ifdef OPENSSL_FIPS
    if (RTEST(enabled)) {
        int mode = FIPS_mode();
        if(!mode &amp;&amp; !FIPS_mode_set(1)) /* turning on twice leads to an error */
            ossl_raise(eOSSLError, &quot;Turning on FIPS mode failed&quot;);
    } else {
        if(!FIPS_mode_set(0)) /* turning off twice is OK */
            ossl_raise(eOSSLError, &quot;Turning off FIPS mode failed&quot;);
    }
    return enabled;
#else
    if (RTEST(enabled))
        ossl_raise(eOSSLError, &quot;This version of OpenSSL does not support FIPS mode&quot;);
    return enabled;
#endif
}</pre>
          </div>
        </div>


      </div>

      <div id="method-c-fixed_length_secure_compare" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            fixed_length_secure_compare(string, string) &rarr; boolean
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>Constant time memory comparison for fixed length strings, such as results of <a href="OpenSSL/HMAC.html"><code>HMAC</code></a> calculations.</p>

<p>Returns <code>true</code> if the strings are identical, <code>false</code> if they are of the same length but not identical. If the length is different, <code>ArgumentError</code> is raised.</p>

          <div class="method-source-code" id="fixed_length_secure_compare-source">
            <pre>static VALUE
ossl_crypto_fixed_length_secure_compare(VALUE dummy, VALUE str1, VALUE str2)
{
    const unsigned char *p1 = (const unsigned char *)StringValuePtr(str1);
    const unsigned char *p2 = (const unsigned char *)StringValuePtr(str2);
    long len1 = RSTRING_LEN(str1);
    long len2 = RSTRING_LEN(str2);

    if (len1 != len2) {
        ossl_raise(rb_eArgError, &quot;inputs must be of equal length&quot;);
    }

    switch (CRYPTO_memcmp(p1, p2, len1)) {
        case 0: return Qtrue;
        default: return Qfalse;
    }
}</pre>
          </div>
        </div>


      </div>

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

        <div class="method-description">
          <p>Calls CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON). Starts tracking memory allocations. See also <a href="OpenSSL.html#method-c-print_mem_leaks"><code>OpenSSL.print_mem_leaks</code></a>.</p>

<p>This is available only when built with a capable <a href="OpenSSL.html"><code>OpenSSL</code></a> and –enable-debug configure option.</p>

          <div class="method-source-code" id="mem_check_start-source">
            <pre>static VALUE
mem_check_start(VALUE self)
{
        CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
        return Qnil;
}</pre>
          </div>
        </div>


      </div>

      <div id="method-c-print_mem_leaks" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            print_mem_leaks &rarr; true | false
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>For debugging the Ruby/OpenSSL library. Calls CRYPTO_mem_leaks_fp(stderr). Prints detected memory leaks to standard error. This cleans the global state up thus you cannot use any methods of the library after calling this.</p>

<p>Returns <code>true</code> if leaks detected, <code>false</code> otherwise.</p>

<p>This is available only when built with a capable <a href="OpenSSL.html"><code>OpenSSL</code></a> and –enable-debug configure option.</p>

<h3 id="method-c-print_mem_leaks-label-Example">Example<span><a href="#method-c-print_mem_leaks-label-Example">&para;</a> <a href="#top">&uarr;</a></span></h3>

<pre class="ruby"><span class="ruby-constant">OpenSSL</span>.<span class="ruby-identifier">mem_check_start</span>
<span class="ruby-constant">NOT_GCED</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">256</span>)

<span class="ruby-keyword">END</span> {
  <span class="ruby-constant">GC</span>.<span class="ruby-identifier">start</span>
  <span class="ruby-constant">OpenSSL</span>.<span class="ruby-identifier">print_mem_leaks</span> <span class="ruby-comment"># will print the leakage</span>
}
</pre>

          <div class="method-source-code" id="print_mem_leaks-source">
            <pre>static VALUE
print_mem_leaks(VALUE self)
{
#if OPENSSL_VERSION_NUMBER &gt;= 0x10100000
    int ret;
#endif

#ifndef HAVE_RB_EXT_RACTOR_SAFE
    // for Ruby 2.x
    void ossl_bn_ctx_free(void); // ossl_bn.c
    ossl_bn_ctx_free();
#endif

#if OPENSSL_VERSION_NUMBER &gt;= 0x10100000
    ret = CRYPTO_mem_leaks_fp(stderr);
    if (ret &lt; 0)
        ossl_raise(eOSSLError, &quot;CRYPTO_mem_leaks_fp&quot;);
    return ret ? Qfalse : Qtrue;
#else
    CRYPTO_mem_leaks_fp(stderr);
    return Qnil;
#endif
}</pre>
          </div>
        </div>


      </div>

      <div id="method-c-secure_compare" class="method-detail ">
        <div class="method-heading">
          <span class="method-callseq">
            secure_compare(string, string) &rarr; boolean
          </span>
          <span class="method-click-advice">click to toggle source</span>
        </div>

        <div class="method-description">
          <p>Constant time memory comparison. Inputs are hashed using SHA-256 to mask the length of the secret. Returns <code>true</code> if the strings are identical, <code>false</code> otherwise.</p>

          <div class="method-source-code" id="secure_compare-source">
            <pre><span class="ruby-comment"># File lib/openssl.rb, line 33</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">secure_compare</span>(<span class="ruby-identifier">a</span>, <span class="ruby-identifier">b</span>)
  <span class="ruby-identifier">hashed_a</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Digest</span>.<span class="ruby-identifier">digest</span>(<span class="ruby-string">&#39;SHA256&#39;</span>, <span class="ruby-identifier">a</span>)
  <span class="ruby-identifier">hashed_b</span> = <span class="ruby-constant">OpenSSL</span><span class="ruby-operator">::</span><span class="ruby-constant">Digest</span>.<span class="ruby-identifier">digest</span>(<span class="ruby-string">&#39;SHA256&#39;</span>, <span class="ruby-identifier">b</span>)
  <span class="ruby-constant">OpenSSL</span>.<span class="ruby-identifier">fixed_length_secure_compare</span>(<span class="ruby-identifier">hashed_a</span>, <span class="ruby-identifier">hashed_b</span>) <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">a</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">b</span>
<span class="ruby-keyword">end</span></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>