Skip to content

Commit

Permalink
Use ctypes on win32
Browse files Browse the repository at this point in the history
  • Loading branch information
fraca7 committed Aug 25, 2011
1 parent b30f7b9 commit ecb3e18
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 115 deletions.
7 changes: 0 additions & 7 deletions extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,4 @@ def get_extensions():
'CoreServices'])
exts.append(osx_keychain_module)

if platform in ['win32'] and sys.getwindowsversion()[-2] == 2:
# windows 2k+
win32_crypto_module = Extension('win32_crypto',
libraries = ['crypt32'],
sources = ['keyring/backends/win32_crypto.c'],)
exts.append(win32_crypto_module)

return exts
11 changes: 6 additions & 5 deletions keyring/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,9 @@ def __init__(self):
super(Win32CryptoKeyring, self).__init__()

try:
import win32_crypto
from backends import win32_crypto
self.crypt_handler = win32_crypto
except ImportError:
except ImportError, e:
self.crypt_handler = None

def filename(self):
Expand Down Expand Up @@ -536,7 +536,7 @@ def __init__(self):
super(Win32CryptoRegistry, self).__init__()

try:
import win32_crypto
from backends import win32_crypto
import _winreg
self.crypt_handler = win32_crypto
except ImportError:
Expand Down Expand Up @@ -599,14 +599,15 @@ def select_windows_backend():
except ImportError:
pass
try:
import win32_crypto, _winreg
from backends import win32_crypto
import _winreg
if (major, minor) >= (5, 0):
# recommend for windows 2k+
return 'reg'
except ImportError:
pass
try:
import win32_crypto
from backends import win32_crypto
return 'file'
except ImportError:
pass
Expand Down
102 changes: 0 additions & 102 deletions keyring/backends/win32_crypto.c

This file was deleted.

98 changes: 98 additions & 0 deletions keyring/backends/win32_crypto.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#!/usr/bin/python

import sys
if sys.platform != 'win32':
raise ImportError('Windows-only module')


from ctypes import Structure, wintypes, POINTER, windll, \
WinDLL, c_void_p, WINFUNCTYPE, cast, create_string_buffer, \
c_char_p, byref, memmove


# Crypto API ctypes bindings

class DATA_BLOB(Structure):
_fields_ = [('cbData', wintypes.DWORD),
('pbData', POINTER(wintypes.BYTE))]

class CRYPTPROTECT_PROMPTSTRUCT(Structure):
_fields_ = [('cbSize', wintypes.DWORD),
('dwPromptFlags', wintypes.DWORD),
('hwndApp', wintypes.HWND),
('szPrompt', POINTER(wintypes.WCHAR))]

# Flags for CRYPTPROTECT_PROMPTSTRUCT

CRYPTPROTECT_PROMPT_ON_UNPROTECT = 1
CRYPTPROTECT_PROMPT_ON_PROTECT = 2

# Flags for CryptProtectData/CryptUnprotectData

CRYPTPROTECT_UI_FORBIDDEN = 0x01
CRYPTPROTECT_LOCAL_MACHINE = 0x04
CRYPTPROTECT_CRED_SYNC = 0x08
CRYPTPROTECT_AUDIT = 0x10
CRYPTPROTECT_NO_RECOVERY = 0x20
CRYPTPROTECT_VERIFY_PROTECTION = 0x40
CRYPTPROTECT_CRED_REGENERATE = 0x80

# Crypto API Functions

_dll = WinDLL('CRYPT32.DLL')

CryptProtectData = WINFUNCTYPE(wintypes.BOOL,
POINTER(DATA_BLOB),
POINTER(wintypes.WCHAR),
POINTER(DATA_BLOB),
c_void_p,
POINTER(CRYPTPROTECT_PROMPTSTRUCT),
wintypes.DWORD,
POINTER(DATA_BLOB))(('CryptProtectData', _dll))

CryptUnprotectData = WINFUNCTYPE(wintypes.BOOL,
POINTER(DATA_BLOB),
POINTER(wintypes.WCHAR),
POINTER(DATA_BLOB),
c_void_p,
POINTER(CRYPTPROTECT_PROMPTSTRUCT),
wintypes.DWORD,
POINTER(DATA_BLOB))(('CryptUnprotectData', _dll))

# Functions

def encrypt(data, non_interactive=0):
blobin = DATA_BLOB(cbData=len(data),
pbData=cast(c_char_p(data),
POINTER(wintypes.BYTE)))
blobout = DATA_BLOB()

if not CryptProtectData(byref(blobin),
u'python-keyring-lib.win32crypto',
None, None, None,
CRYPTPROTECT_UI_FORBIDDEN,
byref(blobout)):
raise OSError("Can't encrypt")

encrypted = create_string_buffer(blobout.cbData)
memmove(encrypted, blobout.pbData, blobout.cbData)
windll.kernel32.LocalFree(blobout.pbData)
return encrypted.raw

def decrypt(encrypted, non_interactive=0):
blobin = DATA_BLOB(cbData=len(encrypted),
pbData=cast(c_char_p(encrypted),
POINTER(wintypes.BYTE)))
blobout = DATA_BLOB()

if not CryptUnprotectData(byref(blobin),
u'python-keyring-lib.win32crypto',
None, None, None,
CRYPTPROTECT_UI_FORBIDDEN,
byref(blobout)):
raise OSError("Can't decrypt")

data = create_string_buffer(blobout.cbData)
memmove(data, blobout.pbData, blobout.cbData)
windll.kernel32.LocalFree(blobout.pbData)
return data.raw
2 changes: 1 addition & 1 deletion keyring/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def init_keyring(self):

def supported(self):
try:
import win32_crypto
from keyring.backends import win32_crypto
if sys.platform in ['win32'] and sys.getwindowsversion()[-2] == 2:
return 1
except ImportError:
Expand Down

0 comments on commit ecb3e18

Please sign in to comment.