sshkey.py :  » Development » ASN.1-library-for-Python » pyasn1-0.0.11a » examples » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » ASN.1 library for Python 
ASN.1 library for Python » pyasn1 0.0.11a » examples » sshkey.py
# Read unencrypted PKCS#1/PKIX-compliant, PEM&DER encoded private keys on
# stdin, print them pretty and encode back into original wire format.
# Private keys can be generated with "openssl genrsa|gendsa" commands.
import sys, string, base64
from pyasn1.type import univ,namedtype,namedval,constraint
from pyasn1.codec.der import encoder,decoder

class DSAPrivateKey(univ.Sequence):
    """PKIX compliant DSA private key structure"""
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('version', univ.Integer(namedValues=namedval.NamedValues(('v1', 0)))),
        namedtype.NamedType('p', univ.Integer()),
        namedtype.NamedType('q', univ.Integer()),
        namedtype.NamedType('g', univ.Integer()),
        namedtype.NamedType('public', univ.Integer()),
        namedtype.NamedType('private', univ.Integer())
        )

MAX = 16

class OtherPrimeInfo(univ.Sequence):
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('prime', univ.Integer()),
        namedtype.NamedType('exponent', univ.Integer()),
        namedtype.NamedType('coefficient', univ.Integer())
        )
    
class OtherPrimeInfos(univ.SequenceOf):
    componentType = OtherPrimeInfo()
    subtypeSpec = univ.SequenceOf.subtypeSpec + \
                  constraint.ValueSizeConstraint(1, MAX)
    
class RSAPrivateKey(univ.Sequence):
    """PKCS#1 compliant RSA private key structure"""
    componentType = namedtype.NamedTypes(
        namedtype.NamedType('version', univ.Integer(namedValues=namedval.NamedValues(('two-prime', 0), ('multi', 1)))),
        namedtype.NamedType('modulus', univ.Integer()),
        namedtype.NamedType('publicExponent', univ.Integer()),
        namedtype.NamedType('privateExponent', univ.Integer()),
        namedtype.NamedType('prime1', univ.Integer()),
        namedtype.NamedType('prime2', univ.Integer()),
        namedtype.NamedType('exponent1', univ.Integer()),
        namedtype.NamedType('exponent2', univ.Integer()),
        namedtype.NamedType('coefficient', univ.Integer()),
        namedtype.OptionalNamedType('otherPrimeInfos', OtherPrimeInfos())
        )
    
keyMagic = {
    '-----BEGIN DSA PRIVATE KEY-----':
    {'-----END DSA PRIVATE KEY-----': DSAPrivateKey() },
    '-----BEGIN RSA PRIVATE KEY-----':
    {'-----END RSA PRIVATE KEY-----': RSAPrivateKey() }
    }

# Read PEM keys from stdin and print them out in plain text

stSpam, stHam, stDump = 0, 1, 2
state = stSpam
keyCnt = 0

for keyLine in sys.stdin.readlines():
    keyLine = string.strip(keyLine)
    if state == stSpam:
        if state == stSpam:
            if keyMagic.has_key(keyLine):
                keyMagicTail = keyMagic[keyLine]
                keyLines = []
                state = stHam
                continue
    if state == stHam:
        if keyMagicTail.has_key(keyLine):
            asn1Spec = keyMagicTail[keyLine]
            state = stDump
        else:
            keyLines.append(keyLine)
    if state == stDump:
        substrate = ''
        try:
            for keyLine in keyLines:
                substrate = substrate + base64.decodestring(keyLine)
        except TypeError, why:
            print '%s, possibly encrypted key' % (why, )
            state = stSpam
            continue

        key = decoder.decode(substrate, asn1Spec=asn1Spec)[0]
        
        print key.prettyPrint()
        
        if encoder.encode(key) != substrate:
            print 'key re-code yields a diff!'
        
        keyCnt = keyCnt + 1
        state = stSpam

print '*** %s private key(s) re/serialized' % keyCnt
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.