Guest User

P

a guest
Feb 16th, 2010
14,254
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/env python
  2.  
  3. # ineptkeymac.py, version 1
  4.  
  5. # This program runs on Mac OS X, version 10.6.2 and probably several other
  6. # versions. It uses Python 2.6, but it probably also runs on all versions
  7. # 2.x with x >= 5.
  8.  
  9. # This program extracts the private RSA key for your ADE account in a
  10. # standard binary form (DER format) in a file of your choosing. Its purpose
  11. # is to make a backup of that key so that your legally bought ADE encoded
  12. # ebooks can be salvaged in case they would no longer be supported by ADE
  13. # software. No other usages are intended.
  14.  
  15. # It has been tested with the key storage structure of ADE 1.7.1 and 1.7.2
  16. # and Sony Reader Library.
  17.  
  18. # This software does not contain any encryption code. Its only use of
  19. # external encryption software is the use of openssl for the conversion of
  20. # the private key from pem to der format. It doesn't use encryption or
  21. # decryption, however.
  22.  
  23. # You can run this program from the command line (python ineptkeymac.py
  24. # filename), or by doubleclicking when it has been associated with
  25. # Pythonlauncher. When no filename is given it will show a dialog to obtain one.
  26.  
  27. from __future__ import with_statement
  28.  
  29. __license__ = 'GPL v3'
  30.  
  31. import sys
  32. import os
  33. import xml.etree.ElementTree as etree
  34. from contextlib import closing
  35. import Tkinter
  36. import Tkconstants
  37. import tkFileDialog
  38. from tkMessageBox import showerror
  39. from subprocess import Popen, PIPE
  40. import textwrap
  41.  
  42. NS = 'http://ns.adobe.com/adept'
  43. ACTFILE = '~/Library/Application Support/Adobe/Digital Editions/activation.dat'
  44. HEADER = '-----BEGIN PRIVATE KEY-----\n'
  45. FOOTER = '\n-----END PRIVATE KEY-----\n'
  46.  
  47. Gui = False
  48.  
  49. def get_key():
  50.     '''Returns the private key as a binary string (DER format)'''
  51.     try:
  52.         filename = os.path.expanduser(ACTFILE)
  53.         tree = etree.parse(filename)
  54.         xpath = '//{%s}credentials/{%s}privateLicenseKey' % (NS, NS)
  55.         b64key = tree.findtext(xpath)
  56.         pemkey = HEADER + textwrap.fill(b64key, 64) + FOOTER
  57.        
  58.         cmd = ['openssl', 'rsa', '-outform', 'der']
  59.         proc = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
  60.         stdout, stderr = proc.communicate(pemkey)
  61.  
  62.         if proc.returncode != 0:
  63.             error("openssl error: " + stderr)
  64.             return None
  65.         return stdout
  66.            
  67.     except IOError:
  68.         error("Can find keyfile. Maybe you should activate your Adobe ID.")
  69.         sys.exit(1)
  70.        
  71. def store_key(key, keypath):
  72.     '''Store the key in the file given as keypath. If no keypath is given a
  73.    dialog will ask for one.'''
  74.    
  75.     try:
  76.         if keypath is None:
  77.             keypath = get_keypath()
  78.             if not keypath: # Cancelled
  79.                 return
  80.            
  81.         with closing(open(keypath, 'wb')) as outf:
  82.             outf.write(key)
  83.            
  84.     except IOError, e:
  85.         error("Can write keyfile: " + str(e))
  86.  
  87. def get_keypath():
  88.     keypath = tkFileDialog.asksaveasfilename(
  89.         parent = None, title = 'Select file to store ADEPT key',
  90.         initialdir = os.path.expanduser('~/Desktop'),
  91.         initialfile = 'adeptkey.der',
  92.         defaultextension = '.der', filetypes = [('DER-encoded files', '.der'),
  93.                                                 ('All Files', '.*')])
  94.     if keypath:
  95.         keypath = os.path.normpath(keypath)
  96.     return keypath
  97.  
  98. def error(text):
  99.     print text
  100.     if Gui: showerror('Error!', text)
  101.    
  102. def gui_main():
  103.     root = Tkinter.Tk()
  104.     root.iconify()
  105.     global Gui
  106.     Gui = True
  107.     store_key(get_key(), None)
  108.  
  109.     return 0
  110.    
  111. def main(argv=sys.argv):
  112.     progname = os.path.basename(argv[0])
  113.    
  114.     if len(argv) == 1: # assume GUI if no argument given
  115.         return gui_main()
  116.     if len(argv) != 2:
  117.         print "usage: %s KEYFILE" % (progname,)
  118.         return 1
  119.    
  120.     store_key(get_key(), argv[1])
  121.  
  122. if __name__ == '__main__':
  123.     sys.exit(main())
  124.  
RAW Paste Data