예제 #1
0
def save_key(private_key: RSAPrivateKeyWithSerialization, path: str):
    private_key_pem = private_key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.PKCS8,
        encryption_algorithm=serialization.NoEncryption(),
    )
    with open(path, "wb") as f:
        f.write(private_key_pem)
예제 #2
0
def store_key(private_key: RSAPrivateKeyWithSerialization, where: str):
    pem = private_key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=serialization.NoEncryption(),
    )
    with open(where, "wb") as key_file:
        os.chmod(where, 0o0600)
        key_file.write(pem)
예제 #3
0
def store_key(private_key: RSAPrivateKeyWithSerialization, where: str):
    pem = private_key.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=serialization.NoEncryption(),
    )
    with open(
        os.open(where, os.O_CREAT | os.O_EXCL | os.O_WRONLY | os.O_TRUNC, 0o0600), "wb"
    ) as key_file:
        key_file.write(pem)
예제 #4
0
    def from_crypto(cls, private_key: rsa.RSAPrivateKeyWithSerialization):
        """Convert a cryptography RSAPrivateKey object to an SQLAlchemy model."""
        # type: (type, rsa.RSAPrivateKeyWithSerialization) -> RSAPrivateKey
        m = cls()
        m.pem_data = private_key.private_bytes(
            encoding=serialization.Encoding.PEM,
            format=serialization.PrivateFormat.PKCS8,
            encryption_algorithm=serialization.NoEncryption(),
        )

        return m
예제 #5
0
파일: utils.py 프로젝트: yopiti/authserver
def _create_key(pkey: RSAPrivateKeyWithSerialization) -> Key:
    privpem = pkey.private_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PrivateFormat.TraditionalOpenSSL,
        encryption_algorithm=serialization.NoEncryption(),
    ).decode("utf-8")

    pub = pkey.public_key()  # type: RSAPublicKeyWithSerialization
    pubpem = pub.public_bytes(
        encoding=serialization.Encoding.PEM,
        format=serialization.PublicFormat.SubjectPublicKeyInfo).decode("utf-8")
    return Key(public_key=pubpem, private_key=privpem, key=pkey)
예제 #6
0
def _save_cert_chain(
    path_pem: Path,
    certificate_chain: Iterable[Certificate],
    key: RSAPrivateKeyWithSerialization,
) -> None:
    path_pem.parent.mkdir(mode=0o770, parents=True, exist_ok=True)
    with path_pem.open(mode="wb") as f:
        f.write(
            key.private_bytes(Encoding.PEM, PrivateFormat.PKCS8,
                              NoEncryption()))
        for cert in certificate_chain:
            f.write(cert.public_bytes(Encoding.PEM))
    path_pem.chmod(mode=0o660)
예제 #7
0
 def process_bind_param(self, value: rsa.RSAPrivateKeyWithSerialization,
                        dialect):
     return value.private_bytes(
         encoding=serialization.Encoding.DER,
         format=serialization.PrivateFormat.PKCS8,
         encryption_algorithm=serialization.NoEncryption())
예제 #8
0
def dump_key(key: rsa.RSAPrivateKeyWithSerialization) -> bytes:
    return key.private_bytes(
        serialization.Encoding.PEM,
        serialization.PrivateFormat.TraditionalOpenSSL,
        serialization.NoEncryption(),
    )
예제 #9
0
 def _serialize_private_key(private_key: RSAPrivateKeyWithSerialization) -> bytes:
     return private_key.private_bytes(
         Encoding.PEM,
         PrivateFormat.PKCS8,
         NoEncryption(),
     )
예제 #10
0
def rsa_to_pem(key: rsa.RSAPrivateKeyWithSerialization) -> str:
    return key.private_bytes(encoding=serialization.Encoding.PEM,
                             format=serialization.PrivateFormat.PKCS8,
                             encryption_algorithm=serialization.NoEncryption())