#!/usr/bin/python sig=""" -----BEGIN PGP SIGNED MESSAGE----- """ import sys, pure import os # # 2003/8/23 # # Read an OpenSSL PRIVATE KEY file and store the secret key # which must have an emty passphrase in order to extract the # secret decryption value. # ######################################################### def readNbytes(N): global i Length = 0L while N > 0 : Length = Length * 256 + ord(keyfile[i]) i = i + 1 N = N - 1 return Length ######################################################### def readMPI(): global i Length = 0 Length = readNbytes(2) # in bits Bytes = Length / 8 if Length % 8 != 0 : Bytes = Bytes + 1 # read L octets print "Reading " , pure.toString(Length), " bit or ", pure.toString(Bytes), " bytes" X = 1 MPI = 0L while X <= Bytes : MPI = MPI * 256 + ord(keyfile[int(i)]) i = i + 1 X = X + 1 return MPI def HexToLong (Hexadecimal): number = 0L index = 0L for C in Hexadecimal : if ord(C)>=48 and ord(C)<=57 : # decimal digit number = number * 16 + (ord(C)-48) else: if ord(C) == 65 : # A number = number * 16 + 10 if ord(C) == 66 : # B number = number * 16 + 11 if ord(C) == 67 : # C number = number * 16 + 12 if ord(C) == 68 : # D number = number * 16 + 13 if ord(C) == 69 : # E number = number * 16 + 14 if ord(C) == 70 : # F number = number * 16 + 15 return number ######################################################### if len(sys.argv) != 3 : print "usage: read-opensslkey SSLPrivateKeyfile outfile" else: try: PIPE = os.popen("openssl asn1parse -in " + sys.argv[1]) keyfile = PIPE. readlines() PIPE.close() print "Reading " + str(len(keyfile)) + " lines." except: print "Cannot read input file" sys.exit(3) Modulus = Encryption = Decryption = 0L UserID = "OpenSSL RSA PRIVATE KEY" M = E = D = "" KEY = "" if len(keyfile) > 1: M = keyfile[2][48:-1] E = keyfile[3][48:-1] D = keyfile[4][48:-1] H1 = keyfile[5][48:-1] H2 = keyfile[6][48:-1] H3 = keyfile[7][48:-1] H4 = keyfile[8][48:-1] H5 = keyfile[9][48:-1] try : Modulus = HexToLong(M) Encryption = HexToLong(E) Decryption = HexToLong(D) X1 = HexToLong(H1) X2 = HexToLong(H2) X3 = HexToLong(H3) X4 = HexToLong(H4) X5 = HexToLong(H5) except: print "Error: Cannot read key material." sys.exit(4) KEY = KEY + "Modulus = " + pure.toString(Modulus) + "\n" KEY = KEY + "Encryption = " + pure.toString(Encryption) + "\n" KEY = KEY + "Decryption = " + pure.toString(Decryption) + "\n" KEY = KEY + "Hashmodulus = 1" + "\n" KEY = KEY + "Generator = 1" + "\n" KEY = KEY + UserID + "\n" KEY = KEY + "Protection = None" + "\n" KEY = KEY + "Securityhash = None" + "\n" print KEY print "----------------------" print X1, "\n" print X2, "\n" print X3, "\n" print X4, "\n" print X5, "\n" print "----------------------" else: print "Secret key is encrypted.\n" sys.exit(3) #------------------------------------------------------------------# outfile = sys.argv[2] try: FILE = open (outfile, "w") FILE.write(KEY) FILE.close() except: print "Cannot write file to filesystem" sys.exit(3) print "EXIT" sys.exit(0) ################################################################## sig=""" -----BEGIN PGP SIGNATURE----- Version: 2.6.3in Charset: noconv iQEVAwUBP0cx2r6wVDeIE49tAQHR8gf+KZKBCmyt8L4XxLi5u6So4Bqc7AA9ciHV Xof82NF+msefwM04GffwL36SOEVdF+SndZEeKYCa+V29EIcJEIw+yeRdssDgyLYV kO5enMulKu8CxO519MnovfviB4UFPeSyBhiS2z6hVlvxxtY/RdTWeQAJ5lWWeoF8 CzckA5MDBnMsBMBNVGGer6qx6l6ANuz47aBi6KGH22oetMSPOMNT9h/02ThpocP/ 3i6veKg8Ic5QkFB8NqEIo5uV2DvRGFQWXisog+wt7FhuOi3r+oWvkgGxGYDaioux 5570H0wQlDMnqw/tJShQbln4AwHR/QfSHntIeTsjWyEdVNL8oU12AQ== =/9vl -----END PGP SIGNATURE----- """