예제 #1
0
파일: X509.py 프로젝트: rodrigc/m2crypto
def load_request(file, format=FORMAT_PEM):
    """
    Load certificate request from file.

    @type file: string
    @param file: Name of file containing certificate request in
                 either PEM or DER format.
    @type format: int, either FORMAT_PEM or FORMAT_DER
    @param format: Describes the format of the file to be loaded,
                   either PEM or DER.

    @rtype: M2Crypto.X509.Request
    @return: M2Crypto.X509.Request object.
    """
    f = BIO.openfile(file)
    if format == FORMAT_PEM:
        cptr = m2.x509_req_read_pem(f.bio_ptr())
    elif format == FORMAT_DER:
        cptr = m2.d2i_x509_req(f.bio_ptr())
    else:
        raise ValueError(
            "Unknown filetype. Must be either FORMAT_PEM or FORMAT_DER")
    f.close()
    if cptr is None:
        raise X509Error(Err.get_error())
    return Request(cptr, 1)
예제 #2
0
def load_request(file, format=FORMAT_PEM):
    """
    Load certificate request from file.

    @type file: string
    @param file: Name of file containing certificate request in
                 either PEM or DER format.
    @type format: int, either FORMAT_PEM or FORMAT_DER
    @param format: Describes the format of the file to be loaded,
                   either PEM or DER.

    @rtype: M2Crypto.X509.Request
    @return: M2Crypto.X509.Request object.
    """
    f = BIO.openfile(file)
    if format == FORMAT_PEM:
        cptr = m2.x509_req_read_pem(f.bio_ptr())
    elif format == FORMAT_DER:
        cptr = m2.d2i_x509_req(f.bio_ptr())
    else:
        raise ValueError(
            "Unknown filetype. Must be either FORMAT_PEM or FORMAT_DER")
    f.close()
    if cptr is None:
        raise X509Error(Err.get_error())
    return Request(cptr, 1)
예제 #3
0
    def __init__(self, request=None, path=None, dn=None,
                 keySize=2048, key=None, extensions=None):

        self._signed = False

        # Create public key object
        if key and not request:
            self._key = key
        else:
            self._key = Key(keySize=keySize)

        # Create certificate._request
        if request:
            self._request = request
            if isinstance(request, str):
                if request.startswith("-----BEGIN CERTIFICATE REQUEST-----"):
                    bio = BIO.MemoryBuffer(request)
                    cptr = m2.x509_req_read_pem(bio._ptr())
                    if cptr is None:
                        raise X509.X509Error(Err.get_error())
                    self._request = X509.Request(cptr, _pyfree=1)
                elif ord(request[0]) == 48:
                    bio = BIO.MemoryBuffer(request)
                    cptr = m2.d2i_x509_req(bio._ptr())
                    if cptr is None:
                        raise X509.X509Error(Err.get_error())
                    self._request = X509.Request(cptr, _pyfree=1)
                elif path.exists(request):
                    reqfile = open(request)
                    bio = BIO.File(reqfile)
                    self._request = X509.load_request_bio(bio)
                else:
                    raise ValueError('WFT')
        else:
            self._request = X509.Request()
            self._request.set_pubkey(self._key)
            self._request.set_version(0)

        if dn:
            self.set_dn(dn)

        if extensions:
            self.add_extensions(extensions)
예제 #4
0
def load_request_bio(bio, format=FORMAT_PEM):
    # type: (BIO.BIO, int) -> Request
    """
    Load certificate request from a bio.

    :param bio: BIO pointing at a certificate request in
                either DER or PEM format.
    :param format: Describes the format of the request to be loaded,
                   either PEM or DER. (using constants FORMAT_PEM
                   and FORMAT_DER)
    :return: M2Crypto.X509.Request object.
    """
    if format == FORMAT_PEM:
        cptr = m2.x509_req_read_pem(bio._ptr())
    elif format == FORMAT_DER:
        cptr = m2.d2i_x509_req(bio._ptr())
    else:
        raise ValueError(
            "Unknown format. Must be either FORMAT_DER or FORMAT_PEM")

    return Request(cptr, _pyfree=1)
예제 #5
0
파일: X509.py 프로젝트: mcepl/M2Crypto
def load_request_bio(bio, format=FORMAT_PEM):
    # type: (BIO.BIO, int) -> Request
    """
    Load certificate request from a bio.

    :param bio: BIO pointing at a certificate request in
                either DER or PEM format.
    :param format: Describes the format of the request to be loaded,
                   either PEM or DER. (using constants FORMAT_PEM
                   and FORMAT_DER)
    :return: M2Crypto.X509.Request object.
    """
    if format == FORMAT_PEM:
        cptr = m2.x509_req_read_pem(bio._ptr())
    elif format == FORMAT_DER:
        cptr = m2.d2i_x509_req(bio._ptr())
    else:
        raise ValueError(
            "Unknown format. Must be either FORMAT_DER or FORMAT_PEM")

    return Request(cptr, _pyfree=1)
예제 #6
0
def load_request(file, format=FORMAT_PEM):
    # type: (AnyStr, int) -> Request
    """
    Load certificate request from file.

    :param file: Name of file containing certificate request in
                 either PEM or DER format.
    :param format: Describes the format of the file to be loaded,
                   either PEM or DER. (using constants FORMAT_PEM
                   and FORMAT_DER)
    :return: Request object.
    """
    with BIO.openfile(file) as f:
        if format == FORMAT_PEM:
            cptr = m2.x509_req_read_pem(f.bio_ptr())
        elif format == FORMAT_DER:
            cptr = m2.d2i_x509_req(f.bio_ptr())
        else:
            raise ValueError(
                "Unknown filetype. Must be either FORMAT_PEM or FORMAT_DER")

    return Request(cptr, 1)
예제 #7
0
파일: X509.py 프로젝트: mcepl/M2Crypto
def load_request(file, format=FORMAT_PEM):
    # type: (AnyStr, int) -> Request
    """
    Load certificate request from file.

    :param file: Name of file containing certificate request in
                 either PEM or DER format.
    :param format: Describes the format of the file to be loaded,
                   either PEM or DER. (using constants FORMAT_PEM
                   and FORMAT_DER)
    :return: Request object.
    """
    with BIO.openfile(file) as f:
        if format == FORMAT_PEM:
            cptr = m2.x509_req_read_pem(f.bio_ptr())
        elif format == FORMAT_DER:
            cptr = m2.d2i_x509_req(f.bio_ptr())
        else:
            raise ValueError(
                "Unknown filetype. Must be either FORMAT_PEM or FORMAT_DER")

    return Request(cptr, 1)
예제 #8
0
def load_request_bio(bio, format=FORMAT_PEM):
    """
    Load certificate request from a bio.

    @type bio: M2Crypto.BIO.BIO
    @param bio: BIO pointing at a certificate request in
                either DER or PEM format.
    @type format: int, either FORMAT_PEM or FORMAT_DER
    @param format: Describes the format of the request to be loaded,
                   either PEM or DER.

    @rtype: M2Crypto.X509.Request
    @return: M2Crypto.X509.Request object.
    """
    if format == FORMAT_PEM:
        cptr = m2.x509_req_read_pem(bio._ptr())
    elif format == FORMAT_DER:
        cptr = m2.d2i_x509_req(bio._ptr())
    else:
        raise ValueError(
            "Unknown format. Must be either FORMAT_DER or FORMAT_PEM")
    if cptr is None:
        raise X509Error(Err.get_error())
    return Request(cptr, _pyfree=1)
예제 #9
0
파일: X509.py 프로젝트: rodrigc/m2crypto
def load_request_bio(bio, format=FORMAT_PEM):
    """
    Load certificate request from a bio.

    @type bio: M2Crypto.BIO.BIO
    @param bio: BIO pointing at a certificate request in
                either DER or PEM format.
    @type format: int, either FORMAT_PEM or FORMAT_DER
    @param format: Describes the format of the request to be loaded,
                   either PEM or DER.

    @rtype: M2Crypto.X509.Request
    @return: M2Crypto.X509.Request object.
    """
    if format == FORMAT_PEM:
        cptr = m2.x509_req_read_pem(bio._ptr())
    elif format == FORMAT_DER:
        cptr = m2.d2i_x509_req(bio._ptr())
    else:
        raise ValueError(
            "Unknown format. Must be either FORMAT_DER or FORMAT_PEM")
    if cptr is None:
        raise X509Error(Err.get_error())
    return Request(cptr, _pyfree=1)
예제 #10
0
파일: certificate.py 프로젝트: grith/gsindl
def generate_request(dn=None, request=None, path=None,
                     keySize=2048, key=None, extensions=None):
    """This funciton is for certificate request generation.

    :param request: either a PEM :class:`str` a DER :class:`str`
    :param path: the path to the certificate request file
    :param key: if sepecified this will be wrapped in
       a :class:`~gsindl.key.Key`
    :param keySize: The size of the key to be generated (default 2048)
    :param dn: the DN string of M2Crypto X509_Name
    :param extensions: a :class:`list` of :class:`dict` objects
       containing extensions

    """

    # Create public key object
    if key and not request:
        _key = key
    else:
        _key = generate_key(keySize=keySize)

    # Create certificate._request
    if request:
        _request = request
        if isinstance(request, str):
            if request.startswith("-----BEGIN CERTIFICATE REQUEST-----"):
                bio = BIO.MemoryBuffer(request)
                cptr = m2.x509_req_read_pem(bio._ptr())
                if cptr is None:
                    raise X509.X509Error(Err.get_error())
                _request = X509.Request(cptr, _pyfree=1)
            elif ord(request[0]) == 48:
                bio = BIO.MemoryBuffer(request)
                cptr = m2.d2i_x509_req(bio._ptr())
                if cptr is None:
                    raise X509.X509Error(Err.get_error())
                _request = X509.Request(cptr, _pyfree=1)
            elif path.exists(request):
                reqfile = open(request)
                bio = BIO.File(reqfile)
                _request = X509.load_request_bio(bio)
            else:
                raise ValueError('WFT')
    else:
        _request = X509.Request()
        _request.set_pubkey(_key)
        _request.set_version(0)

    if dn:
        if isinstance(dn, X509.X509_Name):
            _request.set_subject_name(dn)
        elif isinstance(dn, str):
            _request.set_subject_name(_build_name_from_string(dn))
        else:
            raise ValueError("the DN passes in isn't either a "
                             "X509_Name or string")

    if extensions:
        extstack = X509.X509_Extension_Stack()

        sslower = lambda s: s.lower().replace(' ', '')

        for e in extensions:
            name = e['name']
            key = sslower(name)
            critical = e['critical']
            if key in multi_attrs:
                e['value'] = ', '.join([multi_attrs[key][sslower(v)]
                               for v in e['value'].split(',')])
            extstack.push(X509.new_extension(Att_map[key],
                                             e['value'],
                                             critical=int(critical)))
        _request.add_extensions(extstack)

    return _request, _key