Exemplo n.º 1
0
def mk_temporary_cacert():
    """
	Create a temporary CA cert.
	Returns a tuple of NamedTemporaryFiles holding the CA cert and private key.
	"""
    cacert, pk1, pkey = mk_cacert()
    cacertf = namedtmp()
    cacertf.write(cacert.as_pem())
    cacertf.flush()

    pk1f = namedtmp()
    pk1f.write(pk1.as_pem(None))
    pk1f.flush()

    return cacertf, pk1f
Exemplo n.º 2
0
def mk_temporary_cacert():
    """
    Create a temporary CA cert.
    Returns a tuple of NamedTemporaryFiles holding the CA cert and private key.
    """
    cacert, pk1, pkey = mk_cacert()
    cacertf = namedtmp()
    cacertf.write(cacert.as_pem())
    cacertf.flush()

    pk1f = namedtmp()
    pk1f.write(pk1.as_pem(None))
    pk1f.flush()

    return cacert,pk1, pk1f
Exemplo n.º 3
0
    def _split_files(self):
        """
        Splits input file into subfiles based on their "key", or first
         key_length bases, to allow for a memory efficient sort
        """
        for read in self.reader:
            key = read[1][:self.key_length]
            if key in self.tmp_file_names:
                fh = open(self.tmp_file_names[key], "ab")
            else:
                self.keys.append(key)
                # If in keys, file handle should exist
                fh = namedtmp(
                        mode="wb",
                        dir=self.tmp_dir,
                        prefix=key + "_",
                        delete=False
                        )
                file_name = fh.name
                self.tmp_file_names[key] = file_name
            read_str = "\n".join(read)
            fh.write(read_str + "\n")
            fh.close()

        # get file size
        for key in self.tmp_file_names:
            fh = open(self.tmp_file_names[key], "rb")
            fh.seek(0, 2)  #go the end of the file
            this_file_size = fh.tell()  # and get its size
            self.file_sizes[key] = this_file_size
            fh.close
def mk_temporary_cert(cacert_file, ca_key_file, cn):
    """
    Create a temporary certificate signed by the given CA, and with the given common name.

    If cacert_file and ca_key_file is None, the certificate will be self-signed.

    Args:
      cacert_file -- file containing the CA certificate
      ca_key_file -- file containing the CA private key
      cn -- desired common name
    Returns a namedtemporary file with the certificate and private key
    """
    cert_req, pk2 = mk_request(1024, cn=cn)
    if cacert_file and ca_key_file:
        cacert = X509.load_cert(cacert_file)
        pk1 = EVP.load_key(ca_key_file)
    else:
        cacert = None
        pk1 = None

    certificate = mk_cert()
    certificate.set_subject(cert_req.get_subject())
    certificate.set_pubkey(cert_req.get_pubkey())

    if cacert and pk1:
        certificate.set_issuer(cacert.get_issuer())
        certificate.sign(pk1, 'sha1')
    else:
        certificate.set_issuer(certificate.get_subject())
        certificate.sign(pk2, 'sha1')

    cert_file = namedtmp()
    key_file = namedtmp()
    cert_file.write(certificate.as_pem())
    key_file.write(pk2.as_pem(None))
    cert_file.flush()
    key_file.flush()
    return cert_file, key_file
Exemplo n.º 5
0
def mk_temporary_cert(cacert_file, ca_key_file, cn, o , ou):
    """
    Create a temporary certificate signed by the given CA, and with the given common name.
    If cacert_file and ca_key_file is None, the certificate will be self-signed.
    Args:
      cacert_file -- file containing the CA certificate
      ca_key_file -- file containing the CA private key
      cn -- desired common name
    Returns a namedtemporary file with the certificate and private key
    """
    cert_req, pk2 = mk_request(1024, cn=cn, o = o , ou = ou)
    if cacert_file and ca_key_file:
        if isinstance(ca_key_file,EVP.PKey):
            pk1 = ca_key_file
        else:
            pk1 = EVP.load_key(ca_key_file)
        if  isinstance(cacert_file,X509.X509):
            cacert = cacert_file
        else:
            cacert = X509.load_cert(cacert_file)
        
    else:
        cacert = None
        pk1 = None

    cert = mk_cert()
    cert.set_subject(cert_req.get_subject())
    cert.set_pubkey(cert_req.get_pubkey())

    if cacert and pk1:
        cert.set_issuer(cacert.get_issuer())
        cert.sign(pk1, 'sha1')
    else:
        cert.set_issuer(cert.get_subject())
        cert.sign(pk2, 'sha1')

    certf = namedtmp()
    certf.write(cert.as_pem())
    certf.write(pk2.as_pem(None))
    certf.flush()

    return cert , pk2