~ubuntu-branches/ubuntu/saucy/python-keyring/saucy

« back to all changes in this revision

Viewing changes to keyring/backends/win32_crypto.py

  • Committer: Package Import Robot
  • Author(s): Dmitry Shachnev, Jakub Wilk, Dmitry Shachnev
  • Date: 2013-06-22 11:03:11 UTC
  • mfrom: (1.1.6) (4.1.8 sid)
  • Revision ID: package-import@ubuntu.com-20130622110311-hnoz93tbfi0wwtf6
Tags: 1.4-1
* Team upload.

[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Dmitry Shachnev ]
* New upstream release (closes: #697215).
* Added convert-crypto-keyring script for conversion of pre-0.9 Crypto
  keyrings to the new format.
* Recommend python[3]-secretstorage, as the default backend now uses it.
* Suggest python[3]-gi and gir1.2-gnomekeyring-1.0, as the GNOME Keyring
  backend now uses it.
* Suggest python-gdata, python-keyczar and python[3]-[py]kde4, as other
  backends use them (closes: #618760).
* Drop all previous patches, applied upstream.
* debian/patches/no-pytest-runner.patch: do not require pytest-runner,
  it is not in Debian.
* debian/patches/fix-importkiller.patch: fix ImportKiller not working
  with Python 3.x (fixes the testsuite).
* debian/patches/catch-dbus-errors.patch: catch D-Bus errors when checking
  if SecretService is supported (can be dropped when the next SecretStorage
  version is released, where this is fixed).
* Bump Standards-Version to 3.9.4 and debhelper compat level to 9.
* Disable HTTP traffic in debian/rules.
* Run upstream testsuite during build; simplify other debian/rules parts.
* Bump python-defaults build-dependency to ensure that python2.7 is
  available, as we use it directly in tests.
* Build-depend on python[3]-mock and python[3]-crypto, for the testsuite.
* Remove build directory, egg-info directory and .pyc files in clean
  target.
* Update some file names to match upstream renamings.

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