HEX
Server: Apache
System: Windows NT MAGNETO-ARM 10.0 build 22000 (Windows 10) AMD64
User: Michel (0)
PHP: 7.4.7
Disabled: NONE
Upload Files
File: C:/Ruby27-x64/share/doc/ruby/html/Gem/Security.html
<!DOCTYPE html>

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

<title>module Gem::Security - 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-Gem::Security-label-Signing+gems">Signing gems</a>
    <li><a href="#module-Gem::Security-label-Walkthrough">Walkthrough</a>
    <li><a href="#module-Gem::Security-label-Building+your+certificate">Building your certificate</a>
    <li><a href="#module-Gem::Security-label-Signing+Gems">Signing Gems</a>
    <li><a href="#module-Gem::Security-label-Manually+signing+gems">Manually signing gems</a>
    <li><a href="#module-Gem::Security-label-Signed+gems+and+security+policies">Signed gems and security policies</a>
    <li><a href="#module-Gem::Security-label-Certificate+chains">Certificate chains</a>
    <li><a href="#module-Gem::Security-label-Signing+certificates">Signing certificates</a>
    <li><a href="#module-Gem::Security-label-Manually+verifying+signatures">Manually verifying signatures</a>
    <li><a href="#module-Gem::Security-label-OpenSSL+Reference">OpenSSL Reference</a>
    <li><a href="#module-Gem::Security-label-Bugs-2FTODO">Bugs/TODO</a>
    <li><a href="#module-Gem::Security-label-Original+author">Original author</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-alt_name_or_x509_entry">::alt_name_or_x509_entry</a>
    
    <li ><a href="#method-c-create_cert">::create_cert</a>
    
    <li ><a href="#method-c-create_cert_email">::create_cert_email</a>
    
    <li ><a href="#method-c-create_cert_self_signed">::create_cert_self_signed</a>
    
    <li ><a href="#method-c-create_key">::create_key</a>
    
    <li ><a href="#method-c-email_to_name">::email_to_name</a>
    
    <li ><a href="#method-c-re_sign">::re_sign</a>
    
    <li ><a href="#method-c-reset">::reset</a>
    
    <li ><a href="#method-c-sign">::sign</a>
    
    <li ><a href="#method-c-trust_dir">::trust_dir</a>
    
    <li ><a href="#method-c-trusted_certificates">::trusted_certificates</a>
    
    <li ><a href="#method-c-write">::write</a>
    
  </ul>
</div>

  </div>
</nav>

<main role="main" aria-labelledby="module-Gem::Security">
  <h1 id="module-Gem::Security" class="module">
    module Gem::Security
  </h1>

  <section class="description">
    
<h1 id="module-Gem::Security-label-Signing+gems">Signing gems<span><a href="#module-Gem::Security-label-Signing+gems">&para;</a> <a href="#top">&uarr;</a></span></h1>

<p>The <a href="Security.html"><code>Gem::Security</code></a> implements cryptographic signatures for gems.  The section below is a step-by-step guide to using signed gems and generating your own.</p>

<h2 id="module-Gem::Security-label-Walkthrough">Walkthrough<span><a href="#module-Gem::Security-label-Walkthrough">&para;</a> <a href="#top">&uarr;</a></span></h2>

<h3 id="module-Gem::Security-label-Building+your+certificate">Building your certificate<span><a href="#module-Gem::Security-label-Building+your+certificate">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>In order to start signing your gems, you&#39;ll need to build a private key and a self-signed certificate.  Here&#39;s how:</p>

<pre># build a private key and certificate for yourself:
$ gem cert --build you@example.com</pre>

<p>This could take anywhere from a few seconds to a minute or two, depending on the speed of your computer (public key algorithms aren&#39;t exactly the speediest crypto algorithms in the world).  When it&#39;s finished, you&#39;ll see the files “gem-private_key.pem” and “gem-public_cert.pem” in the current directory.</p>

<p>First things first: Move both files to ~/.gem if you don&#39;t already have a key and certificate in that directory.  Ensure the file permissions make the key unreadable by others (by default the file is saved securely).</p>

<p>Keep your private key hidden; if it&#39;s compromised, someone can sign packages as you (note: PKI has ways of mitigating the risk of stolen keys; more on that later).</p>

<h3 id="module-Gem::Security-label-Signing+Gems">Signing Gems<span><a href="#module-Gem::Security-label-Signing+Gems">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>In RubyGems 2 and newer there is no extra work to sign a gem.  RubyGems will automatically find your key and certificate in your home directory and use them to sign newly packaged gems.</p>

<p>If your certificate is not self-signed (signed by a third party) RubyGems will attempt to load the certificate chain from the trusted certificates. Use <code>gem cert --add signing_cert.pem</code> to add your signers as trusted certificates.  See below for further information on certificate chains.</p>

<p>If you build your gem it will automatically be signed.  If you peek inside your gem file, you&#39;ll see a couple of new files have been added:</p>

<pre>$ tar tf your-gem-1.0.gem
metadata.gz
metadata.gz.sum
metadata.gz.sig # metadata signature
data.tar.gz
data.tar.gz.sum
data.tar.gz.sig # data signature</pre>

<h3 id="module-Gem::Security-label-Manually+signing+gems">Manually signing gems<span><a href="#module-Gem::Security-label-Manually+signing+gems">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>If you wish to store your key in a separate secure location you&#39;ll need to set your gems up for signing by hand.  To do this, set the <code>signing_key</code> and <code>cert_chain</code> in the gemspec before packaging your gem:</p>

<pre class="ruby"><span class="ruby-identifier">s</span>.<span class="ruby-identifier">signing_key</span> = <span class="ruby-string">&#39;/secure/path/to/gem-private_key.pem&#39;</span>
<span class="ruby-identifier">s</span>.<span class="ruby-identifier">cert_chain</span> = <span class="ruby-node">%w[/secure/path/to/gem-public_cert.pem]</span>
</pre>

<p>When you package your gem with these options set RubyGems will automatically load your key and certificate from the secure paths.</p>

<h3 id="module-Gem::Security-label-Signed+gems+and+security+policies">Signed gems and security policies<span><a href="#module-Gem::Security-label-Signed+gems+and+security+policies">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>Now let&#39;s verify the signature.  Go ahead and install the gem, but add the following options: <code>-P HighSecurity</code>, like this:</p>

<pre># install the gem with using the security policy &quot;HighSecurity&quot;
$ sudo gem install your.gem -P HighSecurity</pre>

<p>The <code>-P</code> option sets your security policy – we&#39;ll talk about that in just a minute.  Eh, what&#39;s this?</p>

<pre>$ gem install -P HighSecurity your-gem-1.0.gem
ERROR:  While executing gem ... (Gem::Security::Exception)
    root cert /CN=you/DC=example is not trusted</pre>

<p>The culprit here is the security policy.  RubyGems has several different security policies.  Let&#39;s take a short break and go over the security policies.  Here&#39;s a list of the available security policies, and a brief description of each one:</p>
<ul><li>
<p><a href="Security.html#NoSecurity"><code>NoSecurity</code></a> - Well, no security at all.  Signed packages are treated like unsigned packages.</p>
</li><li>
<p><a href="Security.html#LowSecurity"><code>LowSecurity</code></a> - Pretty much no security.  If a package is signed then RubyGems will make sure the signature matches the signing certificate, and that the signing certificate hasn&#39;t expired, but that&#39;s it.  A malicious user could easily circumvent this kind of security.</p>
</li><li>
<p><a href="Security.html#MediumSecurity"><code>MediumSecurity</code></a> - Better than <a href="Security.html#LowSecurity"><code>LowSecurity</code></a> and <a href="Security.html#NoSecurity"><code>NoSecurity</code></a>, but still fallible.  Package contents are verified against the signing certificate, and the signing certificate is checked for validity, and checked against the rest of the certificate chain (if you don&#39;t know what a certificate chain is, stay tuned, we&#39;ll get to that). The biggest improvement over <a href="Security.html#LowSecurity"><code>LowSecurity</code></a> is that <a href="Security.html#MediumSecurity"><code>MediumSecurity</code></a> won&#39;t install packages that are signed by untrusted sources. Unfortunately, <a href="Security.html#MediumSecurity"><code>MediumSecurity</code></a> still isn&#39;t totally secure – a malicious user can still unpack the gem, strip the signatures, and distribute the gem unsigned.</p>
</li><li>
<p><a href="Security.html#HighSecurity"><code>HighSecurity</code></a> - Here&#39;s the bugger that got us into this mess. The <a href="Security.html#HighSecurity"><code>HighSecurity</code></a> policy is identical to the <a href="Security.html#MediumSecurity"><code>MediumSecurity</code></a> policy, except that it does not allow unsigned gems.  A malicious user doesn&#39;t have a whole lot of options here; they can&#39;t modify the package contents without invalidating the signature, and they can&#39;t modify or remove signature or the signing certificate chain, or RubyGems will simply refuse to install the package.  Oh well, maybe they&#39;ll have better luck causing problems for CPAN users instead :).</p>
</li></ul>

<p>The reason RubyGems refused to install your shiny new signed gem was because it was from an untrusted source.  Well, your code is infallible (naturally), so you need to add yourself as a trusted source:</p>

<pre># add trusted certificate
gem cert --add ~/.gem/gem-public_cert.pem</pre>

<p>You&#39;ve now added your public certificate as a trusted source.  Now you can install packages signed by your private key without any hassle.  Let&#39;s try the install command above again:</p>

<pre># install the gem with using the HighSecurity policy (and this time
# without any shenanigans)
$ gem install -P HighSecurity your-gem-1.0.gem
Successfully installed your-gem-1.0
1 gem installed</pre>

<p>This time RubyGems will accept your signed package and begin installing.</p>

<p>While you&#39;re waiting for RubyGems to work it&#39;s magic, have a look at some of the other security commands by running <code>gem help cert</code>:</p>

<pre>Options:
  -a, --add CERT                   Add a trusted certificate.
  -l, --list [FILTER]              List trusted certificates where the
                                   subject contains FILTER
  -r, --remove FILTER              Remove trusted certificates where the
                                   subject contains FILTER
  -b, --build EMAIL_ADDR           Build private key and self-signed
                                   certificate for EMAIL_ADDR
  -C, --certificate CERT           Signing certificate for --sign
  -K, --private-key KEY            Key for --sign or --build
  -s, --sign CERT                  Signs CERT with the key from -K
                                   and the certificate from -C</pre>

<p>We&#39;ve already covered the <code>--build</code> option, and the <code>--add</code>, <code>--list</code>, and <code>--remove</code> commands seem fairly straightforward; they allow you to add, list, and remove the certificates in your trusted certificate list.  But what&#39;s with this <code>--sign</code> option?</p>

<h3 id="module-Gem::Security-label-Certificate+chains">Certificate chains<span><a href="#module-Gem::Security-label-Certificate+chains">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>To answer that question, let&#39;s take a look at “certificate chains”, a concept I mentioned earlier.  There are a couple of problems with self-signed certificates: first of all, self-signed certificates don&#39;t offer a whole lot of security.  Sure, the certificate says Yukihiro Matsumoto, but how do I know it was actually generated and signed by matz himself unless he gave me the certificate in person?</p>

<p>The second problem is scalability.  Sure, if there are 50 gem authors, then I have 50 trusted certificates, no problem.  What if there are 500 gem authors?  1000?  Having to constantly add new trusted certificates is a pain, and it actually makes the trust system less secure by encouraging RubyGems users to blindly trust new certificates.</p>

<p>Here&#39;s where certificate chains come in.  A certificate chain establishes an arbitrarily long chain of trust between an issuing certificate and a child certificate.  So instead of trusting certificates on a per-developer basis, we use the PKI concept of certificate chains to build a logical hierarchy of trust.  Here&#39;s a hypothetical example of a trust hierarchy based (roughly) on geography:</p>

<pre>                    --------------------------
                    | rubygems@rubygems.org |
                    --------------------------
                                |
              -----------------------------------
              |                                 |
  ----------------------------    -----------------------------
  |  seattlerb@seattlerb.org |    | dcrubyists@richkilmer.com |
  ----------------------------    -----------------------------
       |                |                 |             |
---------------   ----------------   -----------   --------------
|   drbrain   |   |   zenspider  |   | pabs@dc |   | tomcope@dc |
---------------   ----------------   -----------   --------------</pre>

<p>Now, rather than having 4 trusted certificates (one for drbrain, zenspider, pabs@dc, and tomecope@dc), a user could actually get by with one certificate, the “rubygems@rubygems.org” certificate.</p>

<p>Here&#39;s how it works:</p>

<p>I install “rdoc-3.12.gem”, a package signed by “drbrain”.  I&#39;ve never heard of “drbrain”, but his certificate has a valid signature from the “seattle.rb@seattlerb.org” certificate, which in turn has a valid signature from the “rubygems@rubygems.org” certificate.  Voila!  At this point, it&#39;s much more reasonable for me to trust a package signed by “drbrain”, because I can establish a chain to “rubygems@rubygems.org”, which I do trust.</p>

<h3 id="module-Gem::Security-label-Signing+certificates">Signing certificates<span><a href="#module-Gem::Security-label-Signing+certificates">&para;</a> <a href="#top">&uarr;</a></span></h3>

<p>The <code>--sign</code> option allows all this to happen.  A developer creates their build certificate with the <code>--build</code> option, then has their certificate signed by taking it with them to their next regional Ruby meetup (in our hypothetical example), and it&#39;s signed there by the person holding the regional RubyGems signing certificate, which is signed at the next RubyConf by the holder of the top-level RubyGems certificate.  At each point the issuer runs the same command:</p>

<pre># sign a certificate with the specified key and certificate
# (note that this modifies client_cert.pem!)
$ gem cert -K /mnt/floppy/issuer-priv_key.pem -C issuer-pub_cert.pem
   --sign client_cert.pem</pre>

<p>Then the holder of issued certificate (in this case, your buddy “drbrain”), can start using this signed certificate to sign RubyGems.  By the way, in order to let everyone else know about his new fancy signed certificate, “drbrain” would save his newly signed certificate as <code>~/.gem/gem-public_cert.pem</code></p>

<p>Obviously this RubyGems trust infrastructure doesn&#39;t exist yet.  Also, in the “real world”, issuers actually generate the child certificate from a certificate request, rather than sign an existing certificate.  And our hypothetical infrastructure is missing a certificate revocation system. These are that can be fixed in the future…</p>

<p>At this point you should know how to do all of these new and interesting things:</p>
<ul><li>
<p>build a gem signing key and certificate</p>
</li><li>
<p>adjust your security policy</p>
</li><li>
<p>modify your trusted certificate list</p>
</li><li>
<p>sign a certificate</p>
</li></ul>

<h2 id="module-Gem::Security-label-Manually+verifying+signatures">Manually verifying signatures<span><a href="#module-Gem::Security-label-Manually+verifying+signatures">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>In case you don&#39;t trust RubyGems you can verify gem signatures manually:</p>
<ol><li>
<p>Fetch and unpack the gem</p>

<pre>gem fetch some_signed_gem
tar -xf some_signed_gem-1.0.gem</pre>
</li><li>
<p>Grab the public key from the gemspec</p>

<pre>gem spec some_signed_gem-1.0.gem cert_chain | \
  ruby -ryaml -e &#39;puts YAML.load_documents($stdin)&#39; &gt; public_key.crt</pre>
</li><li>
<p>Generate a SHA1 hash of the data.tar.gz</p>

<pre class="ruby"><span class="ruby-identifier">openssl</span> <span class="ruby-identifier">dgst</span> <span class="ruby-operator">-</span><span class="ruby-identifier">sha1</span> <span class="ruby-operator">&lt;</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">tar</span>.<span class="ruby-identifier">gz</span> <span class="ruby-operator">&gt;</span> <span class="ruby-identifier">my</span>.<span class="ruby-identifier">hash</span>
</pre>
</li><li>
<p>Verify the signature</p>

<pre>openssl rsautl -verify -inkey public_key.crt -certin \
  -in data.tar.gz.sig &gt; verified.hash</pre>
</li><li>
<p>Compare your hash to the verified hash</p>

<pre>diff -s verified.hash my.hash</pre>
</li><li>
<p>Repeat 5 and 6 with metadata.gz</p>
</li></ol>

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

<p>The .pem files generated by –build and –sign are PEM files.  Here&#39;s a couple of useful <a href="../OpenSSL.html"><code>OpenSSL</code></a> commands for manipulating them:</p>

<pre># convert a PEM format X509 certificate into DER format:
# (note: Windows .cer files are X509 certificates in DER format)
$ openssl x509 -in input.pem -outform der -out output.der

# print out the certificate in a human-readable format:
$ openssl x509 -in input.pem -noout -text</pre>

<p>And you can do the same thing with the private key file as well:</p>

<pre># convert a PEM format RSA key into DER format:
$ openssl rsa -in input_key.pem -outform der -out output_key.der

# print out the key in a human readable format:
$ openssl rsa -in input_key.pem -noout -text</pre>

<h2 id="module-Gem::Security-label-Bugs-2FTODO">Bugs/TODO<span><a href="#module-Gem::Security-label-Bugs-2FTODO">&para;</a> <a href="#top">&uarr;</a></span></h2>
<ul><li>
<p>There&#39;s no way to define a system-wide trust list.</p>
</li><li>
<p>custom security policies (from a YAML file, etc)</p>
</li><li>
<p>Simple method to generate a signed certificate request</p>
</li><li>
<p>Support for OCSP, SCVP, CRLs, or some other form of cert status check (list is in order of preference)</p>
</li><li>
<p>Support for encrypted private keys</p>
</li><li>
<p>Some sort of semi-formal trust hierarchy (see long-winded explanation above)</p>
</li><li>
<p>Path discovery (for gem certificate chains that don&#39;t have a self-signed root) – by the way, since we don&#39;t have this, THE ROOT OF THE CERTIFICATE CHAIN MUST BE SELF SIGNED if <a href="Security/Policy.html#attribute-i-verify_root"><code>Policy#verify_root</code></a> is true (and it is for the <a href="Security.html#MediumSecurity"><code>MediumSecurity</code></a> and <a href="Security.html#HighSecurity"><code>HighSecurity</code></a> policies)</p>
</li><li>
<p>Better explanation of X509 naming (ie, we don&#39;t have to use email addresses)</p>
</li><li>
<p>Honor AIA field (see note about OCSP above)</p>
</li><li>
<p>Honor extension restrictions</p>
</li><li>
<p>Might be better to store the certificate chain as a PKCS#7 or PKCS#12 file, instead of an array embedded in the metadata.</p>
</li><li>
<p>Flexible signature and key algorithms, not hard-coded to RSA and SHA1.</p>
</li></ul>

<h2 id="module-Gem::Security-label-Original+author">Original author<span><a href="#module-Gem::Security-label-Original+author">&para;</a> <a href="#top">&uarr;</a></span></h2>

<p>Paul Duncan &lt;pabs@pablotron.org&gt; <a href="http://pablotron.org">pablotron.org</a>/</p>

  </section>

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

    

    
    <section class="constants-list">
      <header>
        <h3>Constants</h3>
      </header>
      <dl>
      
        <dt id="AlmostNoSecurity">AlmostNoSecurity
        
        <dd><p>AlmostNo security policy: only verify that the signing certificate is the one that actually signed the data.  Make no attempt to verify the signing certificate chain.</p>

<p>This policy is basically useless. better than nothing, but can still be easily spoofed, and is not recommended.</p>
        
      
        <dt id="DIGEST_ALGORITHM">DIGEST_ALGORITHM
        
        <dd><p><a href="../Digest.html"><code>Digest</code></a> algorithm used to sign gems</p>
        
      
        <dt id="EXTENSIONS">EXTENSIONS
        
        <dd><p>The default set of extensions are:</p>
<ul><li>
<p>The certificate is not a certificate authority</p>
</li><li>
<p>The key for the certificate may be used for key and data encipherment and digital signatures</p>
</li><li>
<p>The certificate contains a subject key identifier</p>
</li></ul>
        
      
        <dt id="HighSecurity">HighSecurity
        
        <dd><p>High security policy: only allow signed gems to be installed, verify the signing certificate, verify the signing certificate chain all the way to the root certificate, and only trust root certificates that we have explicitly allowed trust for.</p>

<p>This security policy is significantly more difficult to bypass, and offers a reasonable guarantee that the contents of the gem have not been altered.</p>
        
      
        <dt id="KEY_ALGORITHM">KEY_ALGORITHM
        
        <dd><p>Algorithm for creating the key pair used to sign gems</p>
        
      
        <dt id="KEY_CIPHER">KEY_CIPHER
        
        <dd><p>Cipher used to encrypt the key pair used to sign gems. Must be in the list returned by <a href="../OpenSSL/Cipher.html#method-c-ciphers"><code>OpenSSL::Cipher.ciphers</code></a></p>
        
      
        <dt id="KEY_LENGTH">KEY_LENGTH
        
        <dd><p>Length of keys created by <a href="Security.html#KEY_ALGORITHM"><code>KEY_ALGORITHM</code></a></p>
        
      
        <dt id="LowSecurity">LowSecurity
        
        <dd><p>Low security policy: only verify that the signing certificate is actually the gem signer, and that the signing certificate is valid.</p>

<p>This policy is better than nothing, but can still be easily spoofed, and is not recommended.</p>
        
      
        <dt id="MediumSecurity">MediumSecurity
        
        <dd><p>Medium security policy: verify the signing certificate, verify the signing certificate chain all the way to the root certificate, and only trust root certificates that we have explicitly allowed trust for.</p>

<p>This security policy is reasonable, but it allows unsigned packages, so a malicious person could simply delete the package signature and pass the gem off as unsigned.</p>
        
      
        <dt id="NoSecurity">NoSecurity
        
        <dd><p>No security policy: all package signature checks are disabled.</p>
        
      
        <dt id="ONE_DAY">ONE_DAY
        
        <dd><p>One day in seconds</p>
        
      
        <dt id="ONE_YEAR">ONE_YEAR
        
        <dd><p>One year in seconds</p>
        
      
        <dt id="Policies">Policies
        
        <dd><p><a href="../Hash.html"><code>Hash</code></a> of configured security policies</p>
        
      
        <dt id="SigningPolicy">SigningPolicy
        
        <dd><p><a href="Security/Policy.html"><code>Policy</code></a> used to verify a certificate and key when signing a gem</p>
        
      
      </dl>
    </section>
    

    

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

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

        <div class="method-description">
          
          
          
          

          
          <div class="method-source-code" id="alt_name_or_x509_entry-source">
            <pre><span class="ruby-comment"># File lib/rubygems/security.rb, line 406</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">alt_name_or_x509_entry</span>(<span class="ruby-identifier">certificate</span>, <span class="ruby-identifier">x509_entry</span>)
  <span class="ruby-identifier">alt_name</span> = <span class="ruby-identifier">certificate</span>.<span class="ruby-identifier">extensions</span>.<span class="ruby-identifier">find</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">extension</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">extension</span>.<span class="ruby-identifier">oid</span> <span class="ruby-operator">==</span> <span class="ruby-node">&quot;#{x509_entry}AltName&quot;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">return</span> <span class="ruby-identifier">alt_name</span>.<span class="ruby-identifier">value</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">alt_name</span>

  <span class="ruby-identifier">certificate</span>.<span class="ruby-identifier">send</span> <span class="ruby-identifier">x509_entry</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-c-create_cert" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">create_cert</span><span
            class="method-args">(subject, key, age = ONE_YEAR, extensions = EXTENSIONS, serial = 1)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Creates an unsigned certificate for <code>subject</code> and <code>key</code>.  The lifetime of the key is from the current time to <code>age</code> which defaults to one year.</p>

<p>The <code>extensions</code> restrict the key to the indicated uses.</p>
          
          

          
          <div class="method-source-code" id="create_cert-source">
            <pre><span class="ruby-comment"># File lib/rubygems/security.rb, line 422</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">create_cert</span>(<span class="ruby-identifier">subject</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">age</span> = <span class="ruby-constant">ONE_YEAR</span>, <span class="ruby-identifier">extensions</span> = <span class="ruby-constant">EXTENSIONS</span>,
                     <span class="ruby-identifier">serial</span> = <span class="ruby-value">1</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">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">version</span>    = <span class="ruby-value">2</span>
  <span class="ruby-identifier">cert</span>.<span class="ruby-identifier">serial</span>     = <span class="ruby-identifier">serial</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-identifier">age</span>

  <span class="ruby-identifier">cert</span>.<span class="ruby-identifier">subject</span>    = <span class="ruby-identifier">subject</span>

  <span class="ruby-identifier">ef</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">extensions</span> = <span class="ruby-identifier">extensions</span>.<span class="ruby-identifier">map</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">ext_name</span>, <span class="ruby-identifier">value</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">ef</span>.<span class="ruby-identifier">create_extension</span> <span class="ruby-identifier">ext_name</span>, <span class="ruby-identifier">value</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">cert</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-c-create_cert_email" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">create_cert_email</span><span
            class="method-args">(email, key, age = ONE_YEAR, extensions = EXTENSIONS)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Creates a self-signed certificate with an issuer and subject from <code>email</code>, a subject alternative name of <code>email</code> and the given <code>extensions</code> for the <code>key</code>.</p>
          
          

          
          <div class="method-source-code" id="create_cert_email-source">
            <pre><span class="ruby-comment"># File lib/rubygems/security.rb, line 449</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">create_cert_email</span>(<span class="ruby-identifier">email</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">age</span> = <span class="ruby-constant">ONE_YEAR</span>, <span class="ruby-identifier">extensions</span> = <span class="ruby-constant">EXTENSIONS</span>)
  <span class="ruby-identifier">subject</span> = <span class="ruby-identifier">email_to_name</span> <span class="ruby-identifier">email</span>

  <span class="ruby-identifier">extensions</span> = <span class="ruby-identifier">extensions</span>.<span class="ruby-identifier">merge</span> <span class="ruby-string">&quot;subjectAltName&quot;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-node">&quot;email:#{email}&quot;</span>

  <span class="ruby-identifier">create_cert_self_signed</span> <span class="ruby-identifier">subject</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">age</span>, <span class="ruby-identifier">extensions</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-c-create_cert_self_signed" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">create_cert_self_signed</span><span
            class="method-args">(subject, key, age = ONE_YEAR, extensions = EXTENSIONS, serial = 1)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Creates a self-signed certificate with an issuer and subject of <code>subject</code> and the given <code>extensions</code> for the <code>key</code>.</p>
          
          

          
          <div class="method-source-code" id="create_cert_self_signed-source">
            <pre><span class="ruby-comment"># File lib/rubygems/security.rb, line 461</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">create_cert_self_signed</span>(<span class="ruby-identifier">subject</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">age</span> = <span class="ruby-constant">ONE_YEAR</span>,
                                 <span class="ruby-identifier">extensions</span> = <span class="ruby-constant">EXTENSIONS</span>, <span class="ruby-identifier">serial</span> = <span class="ruby-value">1</span>)
  <span class="ruby-identifier">certificate</span> = <span class="ruby-identifier">create_cert</span> <span class="ruby-identifier">subject</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">age</span>, <span class="ruby-identifier">extensions</span>

  <span class="ruby-identifier">sign</span> <span class="ruby-identifier">certificate</span>, <span class="ruby-identifier">key</span>, <span class="ruby-identifier">certificate</span>, <span class="ruby-identifier">age</span>, <span class="ruby-identifier">extensions</span>, <span class="ruby-identifier">serial</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-c-create_key" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">create_key</span><span
            class="method-args">(length = KEY_LENGTH, algorithm = KEY_ALGORITHM)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Creates a new key pair of the specified <code>length</code> and <code>algorithm</code>.  The default is a 3072 bit RSA key.</p>
          
          

          
          <div class="method-source-code" id="create_key-source">
            <pre><span class="ruby-comment"># File lib/rubygems/security.rb, line 472</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">create_key</span>(<span class="ruby-identifier">length</span> = <span class="ruby-constant">KEY_LENGTH</span>, <span class="ruby-identifier">algorithm</span> = <span class="ruby-constant">KEY_ALGORITHM</span>)
  <span class="ruby-identifier">algorithm</span>.<span class="ruby-identifier">new</span> <span class="ruby-identifier">length</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

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

        <div class="method-description">
          
          <p>Turns <code>email_address</code> into an <a href="../OpenSSL/X509/Name.html"><code>OpenSSL::X509::Name</code></a></p>
          
          

          
          <div class="method-source-code" id="email_to_name-source">
            <pre><span class="ruby-comment"># File lib/rubygems/security.rb, line 479</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">email_to_name</span>(<span class="ruby-identifier">email_address</span>)
  <span class="ruby-identifier">email_address</span> = <span class="ruby-identifier">email_address</span>.<span class="ruby-identifier">gsub</span>(<span class="ruby-regexp">/[^\w@.-]+/i</span>, <span class="ruby-string">&#39;_&#39;</span>)

  <span class="ruby-identifier">cn</span>, <span class="ruby-identifier">dcs</span> = <span class="ruby-identifier">email_address</span>.<span class="ruby-identifier">split</span> <span class="ruby-string">&#39;@&#39;</span>

  <span class="ruby-identifier">dcs</span> = <span class="ruby-identifier">dcs</span>.<span class="ruby-identifier">split</span> <span class="ruby-string">&#39;.&#39;</span>

  <span class="ruby-identifier">name</span> = <span class="ruby-node">&quot;CN=#{cn}/#{dcs.map { |dc| &quot;DC=#{dc}&quot; }.join &#39;/&#39;}&quot;</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-identifier">name</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-c-re_sign" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">re_sign</span><span
            class="method-args">(expired_certificate, private_key, age = ONE_YEAR, extensions = EXTENSIONS)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Signs <code>expired_certificate</code> with <code>private_key</code> if the keys match and the expired certificate was self-signed.</p>
          
          

          
          <div class="method-source-code" id="re_sign-source">
            <pre><span class="ruby-comment"># File lib/rubygems/security.rb, line 497</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">re_sign</span>(<span class="ruby-identifier">expired_certificate</span>, <span class="ruby-identifier">private_key</span>, <span class="ruby-identifier">age</span> = <span class="ruby-constant">ONE_YEAR</span>,
                 <span class="ruby-identifier">extensions</span> = <span class="ruby-constant">EXTENSIONS</span>)
  <span class="ruby-identifier">raise</span> <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">Security</span><span class="ruby-operator">::</span><span class="ruby-constant">Exception</span>,
        <span class="ruby-string">&quot;incorrect signing key for re-signing &quot;</span> <span class="ruby-operator">+</span>
        <span class="ruby-node">&quot;#{expired_certificate.subject}&quot;</span> <span class="ruby-keyword">unless</span>
    <span class="ruby-identifier">expired_certificate</span>.<span class="ruby-identifier">public_key</span>.<span class="ruby-identifier">to_pem</span> <span class="ruby-operator">==</span> <span class="ruby-identifier">private_key</span>.<span class="ruby-identifier">public_key</span>.<span class="ruby-identifier">to_pem</span>

  <span class="ruby-keyword">unless</span> <span class="ruby-identifier">expired_certificate</span>.<span class="ruby-identifier">subject</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">==</span>
         <span class="ruby-identifier">expired_certificate</span>.<span class="ruby-identifier">issuer</span>.<span class="ruby-identifier">to_s</span>
    <span class="ruby-identifier">subject</span> = <span class="ruby-identifier">alt_name_or_x509_entry</span> <span class="ruby-identifier">expired_certificate</span>, <span class="ruby-value">:subject</span>
    <span class="ruby-identifier">issuer</span>  = <span class="ruby-identifier">alt_name_or_x509_entry</span> <span class="ruby-identifier">expired_certificate</span>, <span class="ruby-value">:issuer</span>

    <span class="ruby-identifier">raise</span> <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">Security</span><span class="ruby-operator">::</span><span class="ruby-constant">Exception</span>,
          <span class="ruby-node">&quot;#{subject} is not self-signed, contact #{issuer} &quot;</span> <span class="ruby-operator">+</span>
          <span class="ruby-string">&quot;to obtain a valid certificate&quot;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">serial</span> = <span class="ruby-identifier">expired_certificate</span>.<span class="ruby-identifier">serial</span> <span class="ruby-operator">+</span> <span class="ruby-value">1</span>

  <span class="ruby-identifier">create_cert_self_signed</span>(<span class="ruby-identifier">expired_certificate</span>.<span class="ruby-identifier">subject</span>, <span class="ruby-identifier">private_key</span>, <span class="ruby-identifier">age</span>,
                          <span class="ruby-identifier">extensions</span>, <span class="ruby-identifier">serial</span>)
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

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

        <div class="method-description">
          
          <p>Resets the trust directory for verifying gems.</p>
          
          

          
          <div class="method-source-code" id="reset-source">
            <pre><span class="ruby-comment"># File lib/rubygems/security.rb, line 523</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">reset</span>
  <span class="ruby-ivar">@trust_dir</span> = <span class="ruby-keyword">nil</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-c-sign" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">sign</span><span
            class="method-args">(certificate, signing_key, signing_cert, age = ONE_YEAR, extensions = EXTENSIONS, serial = 1)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Sign the public key from <code>certificate</code> with the <code>signing_key</code> and <code>signing_cert</code>, using the <a href="Security.html#DIGEST_ALGORITHM"><code>Gem::Security::DIGEST_ALGORITHM</code></a>.  Uses the default certificate validity range and extensions.</p>

<p>Returns the newly signed certificate.</p>
          
          

          
          <div class="method-source-code" id="sign-source">
            <pre><span class="ruby-comment"># File lib/rubygems/security.rb, line 534</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">sign</span>(<span class="ruby-identifier">certificate</span>, <span class="ruby-identifier">signing_key</span>, <span class="ruby-identifier">signing_cert</span>,
              <span class="ruby-identifier">age</span> = <span class="ruby-constant">ONE_YEAR</span>, <span class="ruby-identifier">extensions</span> = <span class="ruby-constant">EXTENSIONS</span>, <span class="ruby-identifier">serial</span> = <span class="ruby-value">1</span>)
  <span class="ruby-identifier">signee_subject</span> = <span class="ruby-identifier">certificate</span>.<span class="ruby-identifier">subject</span>
  <span class="ruby-identifier">signee_key</span>     = <span class="ruby-identifier">certificate</span>.<span class="ruby-identifier">public_key</span>

  <span class="ruby-identifier">alt_name</span> = <span class="ruby-identifier">certificate</span>.<span class="ruby-identifier">extensions</span>.<span class="ruby-identifier">find</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">extension</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">extension</span>.<span class="ruby-identifier">oid</span> <span class="ruby-operator">==</span> <span class="ruby-string">&#39;subjectAltName&#39;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">extensions</span> = <span class="ruby-identifier">extensions</span>.<span class="ruby-identifier">merge</span> <span class="ruby-string">&#39;subjectAltName&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-identifier">alt_name</span>.<span class="ruby-identifier">value</span> <span class="ruby-keyword">if</span>
    <span class="ruby-identifier">alt_name</span>

  <span class="ruby-identifier">issuer_alt_name</span> = <span class="ruby-identifier">signing_cert</span>.<span class="ruby-identifier">extensions</span>.<span class="ruby-identifier">find</span> <span class="ruby-keyword">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">extension</span><span class="ruby-operator">|</span>
    <span class="ruby-identifier">extension</span>.<span class="ruby-identifier">oid</span> <span class="ruby-operator">==</span> <span class="ruby-string">&#39;subjectAltName&#39;</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">extensions</span> = <span class="ruby-identifier">extensions</span>.<span class="ruby-identifier">merge</span> <span class="ruby-string">&#39;issuerAltName&#39;</span> <span class="ruby-operator">=&gt;</span> <span class="ruby-identifier">issuer_alt_name</span>.<span class="ruby-identifier">value</span> <span class="ruby-keyword">if</span>
    <span class="ruby-identifier">issuer_alt_name</span>

  <span class="ruby-identifier">signed</span> = <span class="ruby-identifier">create_cert</span> <span class="ruby-identifier">signee_subject</span>, <span class="ruby-identifier">signee_key</span>, <span class="ruby-identifier">age</span>, <span class="ruby-identifier">extensions</span>, <span class="ruby-identifier">serial</span>
  <span class="ruby-identifier">signed</span>.<span class="ruby-identifier">issuer</span> = <span class="ruby-identifier">signing_cert</span>.<span class="ruby-identifier">subject</span>

  <span class="ruby-identifier">signed</span>.<span class="ruby-identifier">sign</span> <span class="ruby-identifier">signing_key</span>, <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">Security</span><span class="ruby-operator">::</span><span class="ruby-constant">DIGEST_ALGORITHM</span>.<span class="ruby-identifier">new</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

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

        <div class="method-description">
          
          <p>Returns a <a href="Security/TrustDir.html"><code>Gem::Security::TrustDir</code></a> which wraps the directory where trusted certificates live.</p>
          
          

          
          <div class="method-source-code" id="trust_dir-source">
            <pre><span class="ruby-comment"># File lib/rubygems/security.rb, line 563</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">trust_dir</span>
  <span class="ruby-keyword">return</span> <span class="ruby-ivar">@trust_dir</span> <span class="ruby-keyword">if</span> <span class="ruby-ivar">@trust_dir</span>

  <span class="ruby-identifier">dir</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">join</span> <span class="ruby-constant">Gem</span>.<span class="ruby-identifier">user_home</span>, <span class="ruby-string">&#39;.gem&#39;</span>, <span class="ruby-string">&#39;trust&#39;</span>

  <span class="ruby-ivar">@trust_dir</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">Gem</span><span class="ruby-operator">::</span><span class="ruby-constant">Security</span><span class="ruby-operator">::</span><span class="ruby-constant">TrustDir</span>.<span class="ruby-identifier">new</span> <span class="ruby-identifier">dir</span>
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-c-trusted_certificates" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">trusted_certificates</span><span
            class="method-args">(&amp;block)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Enumerates the trusted certificates via <a href="Security/TrustDir.html"><code>Gem::Security::TrustDir</code></a>.</p>
          
          

          
          <div class="method-source-code" id="trusted_certificates-source">
            <pre><span class="ruby-comment"># File lib/rubygems/security.rb, line 574</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">trusted_certificates</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
  <span class="ruby-identifier">trust_dir</span>.<span class="ruby-identifier">each_certificate</span>(<span class="ruby-operator">&amp;</span><span class="ruby-identifier">block</span>)
<span class="ruby-keyword">end</span></pre>
          </div>
          
        </div>

        

        
      </div>

    
      <div id="method-c-write" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">write</span><span
            class="method-args">(pemmable, path, permissions = 0600, passphrase = nil, cipher = KEY_CIPHER)</span>
          
          <span class="method-click-advice">click to toggle source</span>
          
        </div>
        

        <div class="method-description">
          
          <p>Writes <code>pemmable</code>, which must respond to <code>to_pem</code> to <code>path</code> with the given <code>permissions</code>. If passed <code>cipher</code> and <code>passphrase</code> those arguments will be passed to <code>to_pem</code>.</p>
          
          

          
          <div class="method-source-code" id="write-source">
            <pre><span class="ruby-comment"># File lib/rubygems/security.rb, line 583</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword">self</span>.<span class="ruby-identifier ruby-title">write</span>(<span class="ruby-identifier">pemmable</span>, <span class="ruby-identifier">path</span>, <span class="ruby-identifier">permissions</span> = <span class="ruby-value">0600</span>, <span class="ruby-identifier">passphrase</span> = <span class="ruby-keyword">nil</span>, <span class="ruby-identifier">cipher</span> = <span class="ruby-constant">KEY_CIPHER</span>)
  <span class="ruby-identifier">path</span> = <span class="ruby-constant">File</span>.<span class="ruby-identifier">expand_path</span> <span class="ruby-identifier">path</span>

  <span class="ruby-constant">File</span>.<span class="ruby-identifier">open</span> <span class="ruby-identifier">path</span>, <span class="ruby-string">&#39;wb&#39;</span>, <span class="ruby-identifier">permissions</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-keyword">if</span> <span class="ruby-identifier">passphrase</span> <span class="ruby-keyword">and</span> <span class="ruby-identifier">cipher</span>
      <span class="ruby-identifier">io</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">pemmable</span>.<span class="ruby-identifier">to_pem</span> <span class="ruby-identifier">cipher</span>, <span class="ruby-identifier">passphrase</span>
    <span class="ruby-keyword">else</span>
      <span class="ruby-identifier">io</span>.<span class="ruby-identifier">write</span> <span class="ruby-identifier">pemmable</span>.<span class="ruby-identifier">to_pem</span>
    <span class="ruby-keyword">end</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-identifier">path</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.2.1.1.
  <p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>