/usr/share/doc/python-pyasn1-doc/html/example-use-case.html is in python-pyasn1-doc 0.4.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Example use case — ASN.1 types and codecs 0.4 documentation</title>
<link rel="stylesheet" href="_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.4',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="shortcut icon" href="_static/favicon.ico"/>
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Download & Install" href="download.html" />
<link rel="prev" title="Native Python types" href="pyasn1/codec/native/contents.html" />
<link rel="stylesheet" href="_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body>
<div class="document">
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<p class="logo">
<a href="contents.html">
<img class="logo" src="_static/logo.svg" alt="Logo"/>
</a>
</p>
<p class="blurb"><p align=left><i><b>Brewing free software for the greater good</i></b></p></p>
<h3>Navigation</h3>
<ul>
<li class="toctree-l1"><a class="reference internal" href="pyasn1/contents.html">Library documentation</a></li>
</ul>
<ul class="current">
<li class="toctree-l1 current"><a class="current reference internal" href="#">Example use case</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#grab-asn-1-schema-for-ssh-keys">Grab ASN.1 schema for SSH keys</a></li>
<li class="toctree-l2"><a class="reference internal" href="#compile-asn-1-schema-into-python">Compile ASN.1 schema into Python</a></li>
<li class="toctree-l2"><a class="reference internal" href="#read-your-ssh-id-rsa">Read your ~/.ssh/id_rsa</a></li>
<li class="toctree-l2"><a class="reference internal" href="#play-with-the-keys">Play with the keys</a></li>
<li class="toctree-l2"><a class="reference internal" href="#transform-to-built-ins">Transform to built-ins</a></li>
<li class="toctree-l2"><a class="reference internal" href="#write-it-back">Write it back</a></li>
</ul>
</li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="download.html">Download & Install</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="changelog.html">Changelog</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="license.html">License</a></li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="contents.html">Documentation overview</a><ul>
<li>Previous: <a href="pyasn1/codec/native/contents.html" title="previous chapter">Native Python types</a></li>
<li>Next: <a href="download.html" title="next chapter">Download & Install</a></li>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="example-use-case">
<h1>Example use case<a class="headerlink" href="#example-use-case" title="Permalink to this headline">¶</a></h1>
<div class="toctree-wrapper compound">
</div>
<p>To briefly explain how to approach pyasn1, consider a quick workflow example.</p>
<div class="section" id="grab-asn-1-schema-for-ssh-keys">
<h2>Grab ASN.1 schema for SSH keys<a class="headerlink" href="#grab-asn-1-schema-for-ssh-keys" title="Permalink to this headline">¶</a></h2>
<p>ASN.1 is widely used in many Internet protocols. Frequently, whenever ASN.1 is employed,
data structures are described in ASN.1 schema language right in the RFC.
Take <a class="reference external" href="https://www.ietf.org/rfc/rfc2437.txt">RFC2437</a> for example – we can look into
it and weed out data structures specification into a local file:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># pkcs-1.asn</span>
<span class="n">PKCS</span><span class="o">-</span><span class="mi">1</span> <span class="p">{</span><span class="n">iso</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="n">member</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="n">us</span><span class="p">(</span><span class="mi">840</span><span class="p">)</span> <span class="n">rsadsi</span><span class="p">(</span><span class="mi">113549</span><span class="p">)</span> <span class="n">pkcs</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="n">pkcs</span><span class="o">-</span><span class="mi">1</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="n">modules</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="n">pkcs</span><span class="o">-</span><span class="mi">1</span><span class="p">(</span><span class="mi">1</span><span class="p">)}</span>
<span class="n">DEFINITIONS</span> <span class="n">EXPLICIT</span> <span class="n">TAGS</span> <span class="p">::</span><span class="o">=</span> <span class="n">BEGIN</span>
<span class="n">RSAPrivateKey</span> <span class="p">::</span><span class="o">=</span> <span class="n">SEQUENCE</span> <span class="p">{</span>
<span class="n">version</span> <span class="n">Version</span><span class="p">,</span>
<span class="n">modulus</span> <span class="n">INTEGER</span><span class="p">,</span>
<span class="n">publicExponent</span> <span class="n">INTEGER</span><span class="p">,</span>
<span class="n">privateExponent</span> <span class="n">INTEGER</span><span class="p">,</span>
<span class="n">prime1</span> <span class="n">INTEGER</span><span class="p">,</span>
<span class="n">prime2</span> <span class="n">INTEGER</span><span class="p">,</span>
<span class="n">exponent1</span> <span class="n">INTEGER</span><span class="p">,</span>
<span class="n">exponent2</span> <span class="n">INTEGER</span><span class="p">,</span>
<span class="n">coefficient</span> <span class="n">INTEGER</span>
<span class="p">}</span>
<span class="n">Version</span> <span class="p">::</span><span class="o">=</span> <span class="n">INTEGER</span>
<span class="n">END</span>
</pre></div>
</div>
</div>
<div class="section" id="compile-asn-1-schema-into-python">
<h2>Compile ASN.1 schema into Python<a class="headerlink" href="#compile-asn-1-schema-into-python" title="Permalink to this headline">¶</a></h2>
<p>In the best case, you should be able to automatically compile ASN.1 spec into
Python classes. For that purpose we have the <a class="reference external" href="https://github.com/kimgr/asn1ate">asn1ate</a>
tool:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ pyasn1gen.py pkcs-1.asn > rsakey.py
</pre></div>
</div>
<p>Though it may not work out as, as it stands now, asn1ate does not support
all ASN.1 language constructs.</p>
<p>Alternatively, you could check out the <a class="reference external" href="https://github.com/etingof/pyasn1-modules">pyasn1-modules</a>
package to see if it already has the ASN.1 spec you are looking for compiled and shipped
there. Then just install the package, import the data structure you need and use it:</p>
<div class="highlight-bash"><div class="highlight"><pre><span></span>$ pip install pyasn1-modules
</pre></div>
</div>
<p>As a last resort, you could express ASN.1 in Python by hand. The end result
should be a declarative Python code resembling original ASN.1 syntax like
this:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># rsakey.py</span>
<span class="k">class</span> <span class="nc">Version</span><span class="p">(</span><span class="n">Integer</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">class</span> <span class="nc">RSAPrivateKey</span><span class="p">(</span><span class="n">Sequence</span><span class="p">):</span>
<span class="n">componentType</span> <span class="o">=</span> <span class="n">NamedTypes</span><span class="p">(</span>
<span class="n">NamedType</span><span class="p">(</span><span class="s1">'version'</span><span class="p">,</span> <span class="n">Version</span><span class="p">()),</span>
<span class="n">NamedType</span><span class="p">(</span><span class="s1">'modulus'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">()),</span>
<span class="n">NamedType</span><span class="p">(</span><span class="s1">'publicExponent'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">()),</span>
<span class="n">NamedType</span><span class="p">(</span><span class="s1">'privateExponent'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">()),</span>
<span class="n">NamedType</span><span class="p">(</span><span class="s1">'prime1'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">()),</span>
<span class="n">NamedType</span><span class="p">(</span><span class="s1">'prime2'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">()),</span>
<span class="n">NamedType</span><span class="p">(</span><span class="s1">'exponent1'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">()),</span>
<span class="n">NamedType</span><span class="p">(</span><span class="s1">'exponent2'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">()),</span>
<span class="n">NamedType</span><span class="p">(</span><span class="s1">'coefficient'</span><span class="p">,</span> <span class="n">Integer</span><span class="p">())</span>
<span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="read-your-ssh-id-rsa">
<h2>Read your ~/.ssh/id_rsa<a class="headerlink" href="#read-your-ssh-id-rsa" title="Permalink to this headline">¶</a></h2>
<p>Given we’ve put our Python classes into the <cite>rsakey.py</cite> module, we could import
the top-level object for SSH keys container and initialize it from our
<cite>~/.ssh/id_rsa</cite> file (for sake of simplicity here we assume no passphrase is
set on the key file):</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">base64</span> <span class="kn">import</span> <span class="n">b64decode</span>
<span class="kn">from</span> <span class="nn">pyasn1.codec.der.decoder</span> <span class="kn">import</span> <span class="n">decode</span> <span class="k">as</span> <span class="n">der_decoder</span>
<span class="kn">from</span> <span class="nn">rsakey</span> <span class="kn">import</span> <span class="n">RSAPrivateKey</span>
<span class="c1"># Read SSH key from file (assuming no passphrase)</span>
<span class="k">with</span> <span class="nb">open</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'.ssh/id_rsa'</span><span class="p">)</span> <span class="k">as</span> <span class="n">key_file</span><span class="p">:</span>
<span class="n">b64_serialisation</span> <span class="o">=</span> <span class="s1">''</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">key_file</span><span class="o">.</span><span class="n">readlines</span><span class="p">()[</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
<span class="c1"># Undo BASE64 serialisation</span>
<span class="n">der_serialisation</span> <span class="o">=</span> <span class="n">b64decode</span><span class="p">(</span><span class="n">b64_serialisation</span><span class="p">)</span>
<span class="c1"># Undo DER serialisation, reconstruct SSH key structure</span>
<span class="n">private_key</span><span class="p">,</span> <span class="n">rest_of_input</span> <span class="o">=</span> <span class="n">der_decoder</span><span class="p">(</span><span class="n">der_serialisation</span><span class="p">,</span> <span class="n">asn1Spec</span><span class="o">=</span><span class="n">RSAPrivateKey</span><span class="p">())</span>
</pre></div>
</div>
<p>Once we have Python ASN.1 structures initialized, we could inspect them:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="s1">'</span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span> <span class="n">private_key</span><span class="p">)</span>
<span class="go">RSAPrivateKey:</span>
<span class="go"> version=0</span>
<span class="go"> modulus=280789907761334970323210643584308373...</span>
<span class="go"> publicExponent=65537</span>
<span class="go"> privateExponent=1704567874679144879123080924...</span>
<span class="go"> prime1=1780178536719561265324798296279384073...</span>
<span class="go"> prime2=1577313184995269616049017780493740138...</span>
<span class="go"> exponent1=1193974819720845247396384239609024...</span>
<span class="go"> exponent2=9240965721817961178848297404494811...</span>
<span class="go"> coefficient=10207364473358910343346707141115...</span>
</pre></div>
</div>
</div>
<div class="section" id="play-with-the-keys">
<h2>Play with the keys<a class="headerlink" href="#play-with-the-keys" title="Permalink to this headline">¶</a></h2>
<p>As well as use them nearly as we do with native Python types:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">pk</span> <span class="o">=</span> <span class="n">private_key</span>
<span class="go">>>></span>
<span class="gp">>>> </span><span class="n">pk</span><span class="p">[</span><span class="s1">'prime1'</span><span class="p">]</span> <span class="o">*</span> <span class="n">pk</span><span class="p">[</span><span class="s1">'prime2'</span><span class="p">]</span> <span class="o">==</span> <span class="n">pk</span><span class="p">[</span><span class="s1">'modulus'</span><span class="p">]</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">pk</span><span class="p">[</span><span class="s1">'prime1'</span><span class="p">]</span> <span class="o">==</span> <span class="n">pk</span><span class="p">[</span><span class="s1">'modulus'</span><span class="p">]</span> <span class="o">//</span> <span class="n">pk</span><span class="p">[</span><span class="s1">'prime2'</span><span class="p">]</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">pk</span><span class="p">[</span><span class="s1">'exponent1'</span><span class="p">]</span> <span class="o">==</span> <span class="n">pk</span><span class="p">[</span><span class="s1">'privateExponent'</span><span class="p">]</span> <span class="o">%</span> <span class="p">(</span><span class="n">pk</span><span class="p">[</span><span class="s1">'prime1'</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="n">pk</span><span class="p">[</span><span class="s1">'exponent2'</span><span class="p">]</span> <span class="o">==</span> <span class="n">pk</span><span class="p">[</span><span class="s1">'privateExponent'</span><span class="p">]</span> <span class="o">%</span> <span class="p">(</span><span class="n">pk</span><span class="p">[</span><span class="s1">'prime2'</span><span class="p">]</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Technically, pyasn1 classes <a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#emulating-container-types">emulate</a>
Python built-in types.</p>
</div>
<div class="section" id="transform-to-built-ins">
<h2>Transform to built-ins<a class="headerlink" href="#transform-to-built-ins" title="Permalink to this headline">¶</a></h2>
<p>ASN.1 data structures exhibit a way more complicated behaviour compared to
Python types. You may wish to simplify things by turning the whole tree of
pyasn1 objects into an analogous tree made of base Python types:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pyasn1.codec.native.encoder</span> <span class="kn">import</span> <span class="n">encode</span>
<span class="gp">>>> </span><span class="o">...</span>
<span class="gp">>>> </span><span class="n">py_private_key</span> <span class="o">=</span> <span class="n">encode</span><span class="p">(</span><span class="n">private_key</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">py_private_key</span>
<span class="go">{'version': 0, 'modulus': 280789907761334970323210643584308373, 'publicExponent': 65537,</span>
<span class="go"> 'privateExponent': 1704567874679144879123080924, 'prime1': 1780178536719561265324798296279384073,</span>
<span class="go"> 'prime2': 1577313184995269616049017780493740138, 'exponent1': 1193974819720845247396384239609024,</span>
<span class="go"> 'exponent2': 9240965721817961178848297404494811, 'coefficient': 10207364473358910343346707141115}</span>
</pre></div>
</div>
<p>You can do vice-versa: initialize ASN.1 structure from a dict:</p>
<div class="highlight-pycon"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pyasn1.codec.native.decoder</span> <span class="kn">import</span> <span class="n">decode</span>
<span class="gp">>>> </span><span class="n">py_private_key</span> <span class="o">=</span> <span class="p">{</span><span class="s1">'modulus'</span><span class="p">:</span> <span class="mi">280789907761334970323210643584308373</span><span class="p">}</span>
<span class="gp">>>> </span><span class="n">private_key</span> <span class="o">=</span> <span class="n">decode</span><span class="p">(</span><span class="n">py_private_key</span><span class="p">,</span> <span class="n">asn1Spec</span><span class="o">=</span><span class="n">RSAPrivateKey</span><span class="p">())</span>
</pre></div>
</div>
</div>
<div class="section" id="write-it-back">
<h2>Write it back<a class="headerlink" href="#write-it-back" title="Permalink to this headline">¶</a></h2>
<p>Possibly not that applicable to the SSH key example, but you can of course modify
any part of the ASN.1 data structure and serialise it back into the same or other
wire representation:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">pyasn1.codec.der.encoder</span> <span class="kn">import</span> <span class="n">encode</span> <span class="k">as</span> <span class="n">der_encoder</span>
<span class="c1"># Serialise SSH key data structure into DER stream</span>
<span class="n">der_serialisation</span> <span class="o">=</span> <span class="n">der_encoder</span><span class="p">(</span><span class="n">private_key</span><span class="p">)</span>
<span class="c1"># Serialise DER stream into BASE64 stream</span>
<span class="n">b64_serialisation</span> <span class="o">=</span> <span class="s1">'-----BEGIN RSA PRIVATE KEY-----</span><span class="se">\n</span><span class="s1">'</span>
<span class="n">b64_serialisation</span> <span class="o">+=</span> <span class="n">b64encode</span><span class="p">(</span><span class="n">der_serialisation</span><span class="p">)</span>
<span class="n">b64_serialisation</span> <span class="o">+=</span> <span class="s1">'-----END RSA PRIVATE KEY-----</span><span class="se">\n</span><span class="s1">'</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'.ssh/id_rsa.new'</span><span class="p">,</span> <span class="s1">'w'</span><span class="p">)</span> <span class="k">as</span> <span class="n">key_file</span><span class="p">:</span>
<span class="n">key_file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">b64_serialisation</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2005-2018, Ilya Etingof <etingof@gmail.com>.
</div>
</body>
</html>
|