def w_parseKeyUsage(space, pCertCtx, flags): with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as size_ptr: if not CertGetEnhancedKeyUsage(pCertCtx, flags, lltype.nullptr(CERT_ENHKEY_USAGE), size_ptr): last_error = rwin32.lastSavedWindowsError() if last_error.winerror == CRYPT_E_NOT_FOUND: return space.w_True raise wrap_windowserror(space, last_error) size = intmask(size_ptr[0]) with lltype.scoped_alloc(rffi.CCHARP.TO, size) as buf: usage = rffi.cast(PCERT_ENHKEY_USAGE, buf) # Now get the actual enhanced usage property if not CertGetEnhancedKeyUsage(pCertCtx, flags, usage, size_ptr): last_error= rwin32.lastSavedWindowsError() if last_error.winerror == CRYPT_E_NOT_FOUND: return space.w_True raise wrap_windowserror(space, last_error) result_w = [None] * usage.c_cUsageIdentifier for i in range(usage.c_cUsageIdentifier): if not usage.c_rgpszUsageIdentifier[i]: continue result_w[i] = space.wrap(rffi.charp2str( usage.c_rgpszUsageIdentifier[i])) return space.newset(result_w)
def enum_crls_w(space, store_name): """enum_crls(store_name) -> [] Retrieve CRLs from Windows' cert store. store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide more cert storages, too. The function returns a list of (bytes, encoding_type) tuples. The encoding_type flag can be interpreted with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING.""" result_w = [] pCrlCtx = lltype.nullptr(CRL_CONTEXT) hStore = CertOpenSystemStore(None, store_name) if not hStore: raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) try: while True: pCrlCtx = CertEnumCRLsInStore(hStore, pCrlCtx) if not pCrlCtx: break w_crl = space.wrapbytes( rffi.charpsize2str(pCrlCtx.c_pbCrlEncoded, intmask(pCrlCtx.c_cbCrlEncoded))) w_enc = w_certEncodingType(space, pCrlCtx.c_dwCertEncodingType) result_w.append(space.newtuple([w_crl, w_enc])) except: raise finally: if pCrlCtx: # loop ended with an error, need to clean up context manually CertFreeCRLContext(pCrlCtx) if not CertCloseStore(hStore, 0): # This error case might shadow another exception. raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) return space.newlist(result_w)
def w_parseKeyUsage(space, pCertCtx, flags): with lltype.scoped_alloc(rwin32.LPDWORD.TO, 1) as size_ptr: if not CertGetEnhancedKeyUsage( pCertCtx, flags, lltype.nullptr(CERT_ENHKEY_USAGE), size_ptr): last_error = rwin32.lastSavedWindowsError() if last_error.winerror == CRYPT_E_NOT_FOUND: return space.w_True raise wrap_windowserror(space, last_error) size = intmask(size_ptr[0]) with lltype.scoped_alloc(rffi.CCHARP.TO, size) as buf: usage = rffi.cast(PCERT_ENHKEY_USAGE, buf) # Now get the actual enhanced usage property if not CertGetEnhancedKeyUsage(pCertCtx, flags, usage, size_ptr): last_error = rwin32.lastSavedWindowsError() if last_error.winerror == CRYPT_E_NOT_FOUND: return space.w_True raise wrap_windowserror(space, last_error) result_w = [None] * usage.c_cUsageIdentifier for i in range(usage.c_cUsageIdentifier): if not usage.c_rgpszUsageIdentifier[i]: continue result_w[i] = space.wrap( rffi.charp2str(usage.c_rgpszUsageIdentifier[i])) return space.newset(result_w)
def enum_crls_w(space, store_name): """enum_crls(store_name) -> [] Retrieve CRLs from Windows' cert store. store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide more cert storages, too. The function returns a list of (bytes, encoding_type) tuples. The encoding_type flag can be interpreted with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING.""" result_w = [] pCrlCtx = lltype.nullptr(CRL_CONTEXT) hStore = CertOpenSystemStore(None, store_name) if not hStore: raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) try: while True: pCrlCtx = CertEnumCRLsInStore(hStore, pCrlCtx) if not pCrlCtx: break w_crl = space.newbytes( rffi.charpsize2str(pCrlCtx.c_pbCrlEncoded, intmask(pCrlCtx.c_cbCrlEncoded))) w_enc = w_certEncodingType(space, pCrlCtx.c_dwCertEncodingType) result_w.append(space.newtuple([w_crl, w_enc])) except: raise finally: if pCrlCtx: # loop ended with an error, need to clean up context manually CertFreeCRLContext(pCrlCtx) if not CertCloseStore(hStore, 0): # This error case might shadow another exception. raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) return space.newlist(result_w)
def do_poll(self, space, timeout): from pypy.module._multiprocessing.interp_win32 import ( _PeekNamedPipe, _GetTickCount, _Sleep) from rpython.rlib import rwin32 from pypy.interpreter.error import wrap_windowserror bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') try: if not _PeekNamedPipe(self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), bytes_ptr, lltype.nullptr(rwin32.LPDWORD.TO)): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) bytes = bytes_ptr[0] finally: lltype.free(bytes_ptr, flavor='raw') if timeout == 0.0: return bytes > 0 block = timeout < 0 if not block: # XXX does not check for overflow deadline = intmask(_GetTickCount()) + int(1000 * timeout + 0.5) else: deadline = 0 _Sleep(0) delay = 1 while True: bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') try: if not _PeekNamedPipe(self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), bytes_ptr, lltype.nullptr(rwin32.LPDWORD.TO)): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) bytes = bytes_ptr[0] finally: lltype.free(bytes_ptr, flavor='raw') if bytes > 0: return True if not block: now = intmask(_GetTickCount()) if now > deadline: return False diff = deadline - now if delay > diff: delay = diff else: delay += 1 if delay >= 20: delay = 20 _Sleep(delay)
def startup(self, space): # Initialize the event handle used to signal Ctrl-C try: globalState.interrupt_event = rwin32.CreateEvent( rffi.NULL, True, False, rffi.NULL) except WindowsError as e: raise wrap_windowserror(space, e) if not _setCtrlHandlerRoutine(globalState.interrupt_event): raise wrap_windowserror(space, rwin32.lastSavedWindowsError("SetConsoleCtrlHandler"))
def do_recv_string(self, space, buflength, maxlength): from pypy.module._multiprocessing.interp_win32 import ( _ReadFile, _PeekNamedPipe, ERROR_BROKEN_PIPE, ERROR_MORE_DATA, ) from pypy.rlib import rwin32 from pypy.interpreter.error import wrap_windowserror read_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor="raw") left_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor="raw") try: result = _ReadFile(self.handle, self.buffer, min(self.BUFFER_SIZE, buflength), read_ptr, rffi.NULL) if result: return read_ptr[0], lltype.nullptr(rffi.CCHARP.TO) err = rwin32.GetLastError() if err == ERROR_BROKEN_PIPE: raise OperationError(space.w_EOFError, space.w_None) elif err != ERROR_MORE_DATA: raise wrap_windowserror(space, WindowsError(err, "_ReadFile")) # More data... if not _PeekNamedPipe( self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), lltype.nullptr(rwin32.LPDWORD.TO), left_ptr, ): raise wrap_windowserror(space, rwin32.lastWindowsError()) length = intmask(read_ptr[0] + left_ptr[0]) if length > maxlength: # bad message, close connection self.flags &= ~READABLE if self.flags == 0: self.close() raise OperationError(space.w_IOError, space.wrap("bad message length")) newbuf = lltype.malloc(rffi.CCHARP.TO, length + 1, flavor="raw") for i in range(read_ptr[0]): newbuf[i] = self.buffer[i] result = _ReadFile(self.handle, rffi.ptradd(newbuf, read_ptr[0]), left_ptr[0], read_ptr, rffi.NULL) if not result: rffi.free_charp(newbuf) raise wrap_windowserror(space, rwin32.lastWindowsError()) assert read_ptr[0] == left_ptr[0] return length, newbuf finally: lltype.free(read_ptr, flavor="raw") lltype.free(left_ptr, flavor="raw")
def do_recv_string(self, space, buflength, maxlength): from _multiprocess.interp_win32 import (_ReadFile, _PeekNamedPipe, ERROR_BROKEN_PIPE, ERROR_MORE_DATA) from rpython.rlib import rwin32 from pypy.interpreter.error import wrap_windowserror read_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') left_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') try: result = _ReadFile(self.handle, self.buffer, min(self.BUFFER_SIZE, buflength), read_ptr, rffi.NULL) if result: return intmask(read_ptr[0]), lltype.nullptr(rffi.CCHARP.TO) err = rwin32.GetLastError_saved() if err == ERROR_BROKEN_PIPE: raise OperationError(space.w_EOFError, space.w_None) elif err != ERROR_MORE_DATA: raise wrap_windowserror(space, WindowsError(err, "_ReadFile")) # More data... if not _PeekNamedPipe(self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), lltype.nullptr(rwin32.LPDWORD.TO), left_ptr): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) length = intmask(read_ptr[0]) + intmask(left_ptr[0]) if length > maxlength: # bad message, close connection self.flags &= ~READABLE if self.flags == 0: self.close() raise oefmt(space.w_IOError, "bad message length") newbuf = lltype.malloc(rffi.CCHARP.TO, length + 1, flavor='raw') length_read = intmask(read_ptr[0]) for i in range(length_read): newbuf[i] = self.buffer[i] result = _ReadFile(self.handle, rffi.ptradd(newbuf, length_read), left_ptr[0], read_ptr, rffi.NULL) if not result: rffi.free_charp(newbuf) raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) assert r_uint(read_ptr[0]) == r_uint(left_ptr[0]) return length, newbuf finally: lltype.free(read_ptr, flavor='raw') lltype.free(left_ptr, flavor='raw')
def startup(self, space): # Initialize the event handle used to signal Ctrl-C try: globalState.interrupt_event = rwin32.CreateEvent( rffi.NULL, True, False, rffi.NULL) except WindowsError as e: raise wrap_windowserror(space, e) if not _setCtrlHandlerRoutine(globalState.interrupt_event): raise wrap_windowserror( space, rwin32.lastSavedWindowsError("SetConsoleCtrlHandler"))
def startup(self, space): # Initialize the event handle used to signal Ctrl-C try: globalState.interrupt_event = rwin32.CreateEvent( rffi.NULL, True, False, rffi.NULL) except WindowsError, e: raise wrap_windowserror(space, e)
def ConnectNamedPipe(space, w_handle, w_overlapped): handle = handle_w(space, w_handle) overlapped = space.int_w(w_overlapped) if overlapped: raise oefmt(space.w_NotImplementedError, "expected a NULL pointer") if not _ConnectNamedPipe(handle, rffi.NULL): raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
def ConnectNamedPipe(space, w_handle, w_overlapped): handle = handle_w(space, w_handle) overlapped = space.int_w(w_overlapped) if overlapped: raise OperationError(space.w_NotImplementedError, space.wrap("expected a NULL pointer")) if not _ConnectNamedPipe(handle, rffi.NULL): raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
def ExpandEnvironmentStrings(space, w_source): "string = ExpandEnvironmentStrings(string) - Expand environment vars." try: source, source_ulen = space.utf8_len_w(w_source) res, res_ulen = rwinreg.ExpandEnvironmentStrings(source, source_ulen) return space.newutf8(res, res_ulen) except WindowsError as e: raise wrap_windowserror(space, e)
def CreateNamedPipe(space, name, openmode, pipemode, maxinstances, outputsize, inputsize, timeout, w_security): security = space.int_w(w_security) if security: raise OperationError(space.w_NotImplementedError, space.wrap("expected a NULL pointer")) handle = _CreateNamedPipe(name, openmode, pipemode, maxinstances, outputsize, inputsize, timeout, rffi.NULL) if handle == rwin32.INVALID_HANDLE_VALUE: raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) return w_handle(space, handle)
def enum_certificates_w(space, store_name): """enum_certificates(store_name) -> [] Retrieve certificates from Windows' cert store. store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide more cert storages, too. The function returns a list of (bytes, encoding_type, trust) tuples. The encoding_type flag can be interpreted with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. The trust setting is either a set of OIDs or the boolean True.""" result_w = [] pCertCtx = lltype.nullptr(CERT_CONTEXT) hStore = CertOpenSystemStore(None, store_name) if not hStore: raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) try: while True: pCertCtx = CertEnumCertificatesInStore(hStore, pCertCtx) if not pCertCtx: break w_cert = space.newbytes( rffi.charpsize2str(pCertCtx.c_pbCertEncoded, intmask(pCertCtx.c_cbCertEncoded))) w_enc = w_certEncodingType(space, pCertCtx.c_dwCertEncodingType) w_keyusage = w_parseKeyUsage( space, pCertCtx, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG) if space.is_w(w_keyusage, space.w_True): w_keyusage = w_parseKeyUsage( space, pCertCtx, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG) result_w.append(space.newtuple([w_cert, w_enc, w_keyusage])) except: raise finally: if pCertCtx: # loop ended with an error, need to clean up context manually CertFreeCertificateContext(pCertCtx) if not CertCloseStore(hStore, 0): # This error case might shadow another exception. raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) return space.newlist(result_w)
def enum_certificates_w(space, store_name): """enum_certificates(store_name) -> [] Retrieve certificates from Windows' cert store. store_name may be one of 'CA', 'ROOT' or 'MY'. The system may provide more cert storages, too. The function returns a list of (bytes, encoding_type, trust) tuples. The encoding_type flag can be interpreted with X509_ASN_ENCODING or PKCS_7_ASN_ENCODING. The trust setting is either a set of OIDs or the boolean True.""" result_w = [] pCertCtx = lltype.nullptr(CERT_CONTEXT) hStore = CertOpenSystemStore(None, store_name) if not hStore: raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) try: while True: pCertCtx = CertEnumCertificatesInStore(hStore, pCertCtx) if not pCertCtx: break w_cert = space.wrapbytes( rffi.charpsize2str(pCertCtx.c_pbCertEncoded, intmask(pCertCtx.c_cbCertEncoded))) w_enc = w_certEncodingType(space, pCertCtx.c_dwCertEncodingType) w_keyusage = w_parseKeyUsage( space, pCertCtx, CERT_FIND_PROP_ONLY_ENHKEY_USAGE_FLAG) if space.is_w(w_keyusage, space.w_True): w_keyusage = w_parseKeyUsage( space, pCertCtx, CERT_FIND_EXT_ONLY_ENHKEY_USAGE_FLAG) result_w.append(space.newtuple([w_cert, w_enc, w_keyusage])) except: raise finally: if pCertCtx: # loop ended with an error, need to clean up context manually CertFreeCertificateContext(pCertCtx) if not CertCloseStore(hStore, 0): # This error case might shadow another exception. raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) return space.newlist(result_w)
def CreateNamedPipe(space, name, openmode, pipemode, maxinstances, outputsize, inputsize, timeout, w_security): security = space.int_w(w_security) if security: raise oefmt(space.w_NotImplementedError, "expected a NULL pointer") handle = _CreateNamedPipe(name, openmode, pipemode, maxinstances, outputsize, inputsize, timeout, rffi.NULL) if handle == rwin32.INVALID_HANDLE_VALUE: raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) return w_handle(space, handle)
def CreateFile(space, filename, access, share, w_security, disposition, flags, w_templatefile): security = space.int_w(w_security) templatefile = space.int_w(w_templatefile) if security or templatefile: raise OperationError(space.w_NotImplementedError, space.wrap("expected a NULL pointer")) handle = _CreateFile(filename, access, share, rffi.NULL, disposition, flags, rwin32.NULL_HANDLE) if handle == rwin32.INVALID_HANDLE_VALUE: raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) return w_handle(space, handle)
def CreateFile(space, filename, access, share, w_security, disposition, flags, w_templatefile): security = space.int_w(w_security) templatefile = space.int_w(w_templatefile) if security or templatefile: raise oefmt(space.w_NotImplementedError, "expected a NULL pointer") handle = _CreateFile(filename, access, share, rffi.NULL, disposition, flags, rwin32.NULL_HANDLE) if handle == rwin32.INVALID_HANDLE_VALUE: raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) return w_handle(space, handle)
class GlobalState: def __init__(self): self.init() def init(self): self.interrupt_event = rwin32.NULL_HANDLE def startup(self, space): # Initialize the event handle used to signal Ctrl-C try: globalState.interrupt_event = rwin32.CreateEvent( rffi.NULL, True, False, rffi.NULL) except WindowsError, e: raise wrap_windowserror(space, e) if not _setCtrlHandlerRoutine(globalState.interrupt_event): raise wrap_windowserror( space, rwin32.lastWindowsError("SetConsoleCtrlHandler"))
def SetNamedPipeHandleState(space, w_handle, w_pipemode, w_maxinstances, w_timeout): handle = handle_w(space, w_handle) state = lltype.malloc(rffi.CArrayPtr(rffi.UINT).TO, 3, flavor="raw") statep = lltype.malloc(rffi.CArrayPtr(rffi.UINTP).TO, 3, flavor="raw", zero=True) try: if not space.is_w(w_pipemode, space.w_None): state[0] = space.uint_w(w_pipemode) statep[0] = rffi.ptradd(state, 0) if not space.is_w(w_maxinstances, space.w_None): state[1] = space.uint_w(w_maxinstances) statep[1] = rffi.ptradd(state, 1) if not space.is_w(w_timeout, space.w_None): state[2] = space.uint_w(w_timeout) statep[2] = rffi.ptradd(state, 2) if not _SetNamedPipeHandleState(handle, statep[0], statep[1], statep[2]): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) finally: lltype.free(state, flavor="raw") lltype.free(statep, flavor="raw")
def SetNamedPipeHandleState(space, w_handle, w_pipemode, w_maxinstances, w_timeout): handle = handle_w(space, w_handle) state = lltype.malloc(rffi.CArrayPtr(rffi.UINT).TO, 3, flavor='raw') statep = lltype.malloc(rffi.CArrayPtr(rffi.UINTP).TO, 3, flavor='raw', zero=True) try: if not space.is_w(w_pipemode, space.w_None): state[0] = space.uint_w(w_pipemode) statep[0] = rffi.ptradd(state, 0) if not space.is_w(w_maxinstances, space.w_None): state[1] = space.uint_w(w_maxinstances) statep[1] = rffi.ptradd(state, 1) if not space.is_w(w_timeout, space.w_None): state[2] = space.uint_w(w_timeout) statep[2] = rffi.ptradd(state, 2) if not _SetNamedPipeHandleState(handle, statep[0], statep[1], statep[2]): raise wrap_windowserror(space, rwin32.lastWindowsError()) finally: lltype.free(state, flavor='raw') lltype.free(statep, flavor='raw')
def ExpandEnvironmentStrings(space, source): "string = ExpandEnvironmentStrings(string) - Expand environment vars." try: return space.wrap(rwinreg.ExpandEnvironmentStrings(source)) except WindowsError, e: raise wrap_windowserror(space, e)
def CloseHandle(space, w_handle): handle = handle_w(space, w_handle) if not rwin32.CloseHandle(handle): raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
def WaitNamedPipe(space, name, timeout): # Careful: zero means "default value specified by CreateNamedPipe()" if not _WaitNamedPipe(name, timeout): raise wrap_windowserror(space, rwin32.lastSavedWindowsError())
def do_poll(self, space, timeout): from pypy.module._multiprocessing.interp_win32 import (_PeekNamedPipe, _GetTickCount, _Sleep) from rpython.rlib import rwin32 from pypy.interpreter.error import wrap_windowserror bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') try: if not _PeekNamedPipe(self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), bytes_ptr, lltype.nullptr(rwin32.LPDWORD.TO)): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) bytes = bytes_ptr[0] finally: lltype.free(bytes_ptr, flavor='raw') if timeout == 0.0: return bytes > 0 block = timeout < 0 if not block: # XXX does not check for overflow deadline = intmask(_GetTickCount()) + int(1000 * timeout + 0.5) else: deadline = 0 _Sleep(0) delay = 1 while True: bytes_ptr = lltype.malloc(rffi.CArrayPtr(rwin32.DWORD).TO, 1, flavor='raw') try: if not _PeekNamedPipe(self.handle, rffi.NULL, 0, lltype.nullptr(rwin32.LPDWORD.TO), bytes_ptr, lltype.nullptr(rwin32.LPDWORD.TO)): raise wrap_windowserror(space, rwin32.lastSavedWindowsError()) bytes = bytes_ptr[0] finally: lltype.free(bytes_ptr, flavor='raw') if bytes > 0: return True if not block: now = intmask(_GetTickCount()) if now > deadline: return False diff = deadline - now if delay > diff: delay = diff else: delay += 1 if delay >= 20: delay = 20 _Sleep(delay)
def ExpandEnvironmentStrings(space, source): "string = ExpandEnvironmentStrings(string) - Expand environment vars." try: return space.newunicode(rwinreg.ExpandEnvironmentStrings(source)) except WindowsError as e: raise wrap_windowserror(space, e)