'Return serial number of the certificate
:return: Serial number as a Python integer'

def get_serial_number(self):

    asn1_serial = _lib.X509_get_serialNumber(self._x509)
    bignum_serial = _lib.ASN1_INTEGER_to_BN(asn1_serial, _ffi.NULL)
    try:
        hex_serial = _lib.BN_bn2hex(bignum_serial)
        try:
            hexstring_serial = _ffi.string(hex_serial)
            serial = int(hexstring_serial, 16)
            return serial
        finally:
            _lib.OPENSSL_free(hex_serial)
    finally:
        _lib.BN_free(bignum_serial)
