~ubuntu-branches/ubuntu/precise/python-keyring/precise-updates

« back to all changes in this revision

Viewing changes to keyring/backends/win32_crypto.py

  • Committer: Package Import Robot
  • Author(s): Carl Chenet
  • Date: 2012-02-14 12:07:30 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20120214120730-ekuysup9a2s5yl9d
Tags: 0.7.1-1
* New upstream version (Closes: #656680, #624690)
* debian/control
  - Add X-Python-Version for Python 3.2
  - Add B-D for Python 3.2
  - Add unzip for B-D to repack upstream sources
  - Add python3-keyring description
  - Recommends python3-crypto for Python3 binary package
* Add python-keyring.install and python3-keyring.install files
* debian/rules
  - Execute unit tests if available
  - repack upstream sources
* debian/watch
  - New URL to catch upstream sources

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
 
 
3
import sys
 
4
if sys.platform != 'win32':
 
5
    raise ImportError('Windows-only module')
 
6
 
 
7
 
 
8
from ctypes import Structure, wintypes, POINTER, windll, \
 
9
     WinDLL, c_void_p, WINFUNCTYPE, cast, create_string_buffer, \
 
10
     c_char_p, byref, memmove
 
11
 
 
12
 
 
13
# Crypto API ctypes bindings
 
14
 
 
15
class DATA_BLOB(Structure):
 
16
    _fields_ = [('cbData', wintypes.DWORD),
 
17
                ('pbData', POINTER(wintypes.BYTE))]
 
18
 
 
19
class CRYPTPROTECT_PROMPTSTRUCT(Structure):
 
20
    _fields_ = [('cbSize', wintypes.DWORD),
 
21
                ('dwPromptFlags', wintypes.DWORD),
 
22
                ('hwndApp', wintypes.HWND),
 
23
                ('szPrompt', POINTER(wintypes.WCHAR))]
 
24
 
 
25
# Flags for CRYPTPROTECT_PROMPTSTRUCT
 
26
 
 
27
CRYPTPROTECT_PROMPT_ON_UNPROTECT = 1
 
28
CRYPTPROTECT_PROMPT_ON_PROTECT   = 2
 
29
 
 
30
# Flags for CryptProtectData/CryptUnprotectData
 
31
 
 
32
CRYPTPROTECT_UI_FORBIDDEN      = 0x01
 
33
CRYPTPROTECT_LOCAL_MACHINE     = 0x04
 
34
CRYPTPROTECT_CRED_SYNC         = 0x08
 
35
CRYPTPROTECT_AUDIT             = 0x10
 
36
CRYPTPROTECT_NO_RECOVERY       = 0x20
 
37
CRYPTPROTECT_VERIFY_PROTECTION = 0x40
 
38
CRYPTPROTECT_CRED_REGENERATE   = 0x80
 
39
 
 
40
# Crypto API Functions
 
41
 
 
42
_dll = WinDLL('CRYPT32.DLL')
 
43
 
 
44
CryptProtectData = WINFUNCTYPE(wintypes.BOOL,
 
45
                               POINTER(DATA_BLOB),
 
46
                               POINTER(wintypes.WCHAR),
 
47
                               POINTER(DATA_BLOB),
 
48
                               c_void_p,
 
49
                               POINTER(CRYPTPROTECT_PROMPTSTRUCT),
 
50
                               wintypes.DWORD,
 
51
                               POINTER(DATA_BLOB))(('CryptProtectData', _dll))
 
52
 
 
53
CryptUnprotectData = WINFUNCTYPE(wintypes.BOOL,
 
54
                                 POINTER(DATA_BLOB),
 
55
                                 POINTER(wintypes.WCHAR),
 
56
                                 POINTER(DATA_BLOB),
 
57
                                 c_void_p,
 
58
                                 POINTER(CRYPTPROTECT_PROMPTSTRUCT),
 
59
                                 wintypes.DWORD,
 
60
                                 POINTER(DATA_BLOB))(('CryptUnprotectData', _dll))
 
61
 
 
62
# Functions
 
63
 
 
64
def encrypt(data, non_interactive=0):
 
65
    blobin = DATA_BLOB(cbData=len(data),
 
66
                       pbData=cast(c_char_p(data),
 
67
                                   POINTER(wintypes.BYTE)))
 
68
    blobout = DATA_BLOB()
 
69
 
 
70
    if not CryptProtectData(byref(blobin),
 
71
                            u'python-keyring-lib.win32crypto',
 
72
                            None, None, None,
 
73
                            CRYPTPROTECT_UI_FORBIDDEN,
 
74
                            byref(blobout)):
 
75
        raise OSError("Can't encrypt")
 
76
 
 
77
    encrypted = create_string_buffer(blobout.cbData)
 
78
    memmove(encrypted, blobout.pbData, blobout.cbData)
 
79
    windll.kernel32.LocalFree(blobout.pbData)
 
80
    return encrypted.raw
 
81
 
 
82
def decrypt(encrypted, non_interactive=0):
 
83
    blobin = DATA_BLOB(cbData=len(encrypted),
 
84
                       pbData=cast(c_char_p(encrypted),
 
85
                                   POINTER(wintypes.BYTE)))
 
86
    blobout = DATA_BLOB()
 
87
 
 
88
    if not CryptUnprotectData(byref(blobin),
 
89
                              u'python-keyring-lib.win32crypto',
 
90
                              None, None, None,
 
91
                              CRYPTPROTECT_UI_FORBIDDEN,
 
92
                              byref(blobout)):
 
93
        raise OSError("Can't decrypt")
 
94
 
 
95
    data = create_string_buffer(blobout.cbData)
 
96
    memmove(data, blobout.pbData, blobout.cbData)
 
97
    windll.kernel32.LocalFree(blobout.pbData)
 
98
    return data.raw