Guest User

Untitled

a guest
Oct 30th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.59 KB | None | 0 0
  1. from functools import wraps
  2.  
  3. from OpenSSL import crypto
  4. from OpenSSL._util import (
  5.     ffi as _ffi,
  6.     lib as _lib)
  7.  
  8.  
  9. class _VerifyHelper(object):
  10.     def __init__(self, callback):
  11.         self._problems = []
  12.  
  13.         @wraps(callback)
  14.         def wrapper(ok, store_ctx):
  15.             cert = crypto.X509.__new__(crypto.X509)
  16.             cert._x509 = _lib.X509_STORE_CTX_get_current_cert(store_ctx)
  17.             error_number = _lib.X509_STORE_CTX_get_error(store_ctx)
  18.             error_depth = _lib.X509_STORE_CTX_get_error_depth(store_ctx)
  19.  
  20.             try:
  21.                 result = callback(cert, error_number, error_depth, ok)
  22.             except Exception as e:
  23.                 self._problems.append(e)
  24.                 return 0
  25.             else:
  26.                 if result:
  27.                     _lib.X509_STORE_CTX_set_error(store_ctx, _lib.X509_V_OK)
  28.                     return 1
  29.                 else:
  30.                     return 0
  31.  
  32.         self.callback = _ffi.callback(
  33.             "int (*)(int, X509_STORE_CTX *)", wrapper)
  34.  
  35.     def raise_if_problem(self):
  36.         if self._problems:
  37.             try:
  38.                 crypto._raise_current_error()
  39.             except crypto.Error:
  40.                 pass
  41.             raise self._problems.pop(0)
  42.  
  43.  
  44. class MyX509Store(crypto.X509Store):
  45.     """
  46.    Our extended version of crypto.X509Store. We are adding
  47.    new methods to it.
  48.    """
  49.     def load_locations(self, ca_file=None, ca_dir=None):
  50.         if ca_file is None:
  51.             ca_file = _ffi.NULL
  52.         if ca_dir is None:
  53.             ca_dir = _ffi.NULL
  54.         result = _lib.X509_STORE_load_locations(self._store, ca_file, ca_dir)
  55.         if result == 0:
  56.             crypto._raise_current_error()
  57.  
  58.     def set_default_paths(self):
  59.         result = _lib.X509_STORE_set_default_paths(self._store)
  60.         if result == 0:
  61.             crypto._raise_current_error()
  62.  
  63.  
  64. class MyX509StoreCtx(object):
  65.     """
  66.    Our wrapper for X509_STORE_CTX
  67.    """
  68.     def __init__(self, store):
  69.         """
  70.        store is instance of X509Store class
  71.        """
  72.         store_ctx = _lib.X509_STORE_CTX_new()
  73.         if store_ctx == _ffi.NULL:
  74.             raise MemoryError("Can't create X509_STORE_CTX")
  75.         self._store_ctx = _ffi.gc(store_ctx, _lib.X509_STORE_CTX_free)
  76.         result = _lib.X509_STORE_CTX_init(self._store_ctx, store._store,
  77.                                           _ffi.NULL, _ffi.NULL)
  78.         if result == 0:
  79.             crypto._raise_current_error()
  80.  
  81.     def check_cert(self, cert, cert_chain=[], callback=None):
  82.         uchain = _ffi.NULL
  83.         if len(cert_chain) > 0:
  84.             uchain = _lib.sk_X509_new_null()
  85.             if uchain == _ffi.NULL:
  86.                 raise MemoryError("Can't create STACK_OF(X509)")
  87.             for ucert in cert_chain:
  88.                 result = _lib.sk_X509_push(uchain, ucert.x509._x509)
  89.                 if result == 0:
  90.                     _lib.sk_X509_free(uchain)
  91.                     crypto._raise_current_error()
  92.         _lib.X509_STORE_CTX_set_chain(self._store_ctx, uchain)
  93.         _lib.X509_STORE_CTX_set_cert(self._store_ctx, cert.x509._x509)
  94.         if callback:
  95.             self._verify_helper = _VerifyHelper(callback)
  96.             self._verify_callback = self._verify_helper.callback
  97.             _lib.X509_STORE_CTX_set_verify_cb(self._store_ctx,
  98.                                               self._verify_callback)
  99.         result = _lib.X509_verify_cert(self._store_ctx)
  100.         if uchain == _ffi.NULL:
  101.             _lib.sk_X509_free(uchain)
  102.         self._verify_helper.raise_if_problem()
  103.  
  104.         return result
Advertisement
Add Comment
Please, Sign In to add comment