Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
:param format_name: (optional)
The format to export the certificate in.
:type format_name: `str`
:returns: `bytes` representing the exported certificate
"""
if self.is_x509:
if format_name == 'rfc4716':
raise KeyExportError('RFC4716 format is not supported for '
'X.509 certificates')
else:
if format_name in ('der', 'pem'):
raise KeyExportError('DER and PEM formats are not supported '
'for OpenSSH certificates')
if format_name == 'der':
return self.public_data
elif format_name == 'pem':
return (b'-----BEGIN CERTIFICATE-----\n' +
_wrap_base64(self.public_data) +
b'-----END CERTIFICATE-----\n')
elif format_name == 'openssh':
if self._comment:
comment = b' ' + self._comment
else:
comment = b''
return (self.algorithm + b' ' +
binascii.b2a_base64(self.public_data)[:-1] +
def encode_pkcs8_public(self):
"""Export parameters associated with a PKCS#8 public key"""
# pylint: disable=no-self-use
raise KeyExportError('PKCS#8 public key export not supported')
def encode_ssh_private(self):
"""Encode an SSH format EC private key"""
if not self._d:
raise KeyExportError('Key is not private')
return b''.join((String(self._alg_id), String(self._Q.encode()),
MPInt(self._d)))
der, pem, openssh, rfc4716
By default, OpenSSH format will be used.
:param format_name: (optional)
The format to export the certificate in.
:type format_name: `str`
:returns: `bytes` representing the exported certificate
"""
if self.is_x509:
if format_name == 'rfc4716':
raise KeyExportError('RFC4716 format is not supported for '
'X.509 certificates')
else:
if format_name in ('der', 'pem'):
raise KeyExportError('DER and PEM formats are not supported '
'for OpenSSH certificates')
if format_name == 'der':
return self.public_data
elif format_name == 'pem':
return (b'-----BEGIN CERTIFICATE-----\n' +
_wrap_base64(self.public_data) +
b'-----END CERTIFICATE-----\n')
elif format_name == 'openssh':
if self._comment:
comment = b' ' + self._comment
else:
def encode_pkcs1_private(self):
"""Export parameters associated with a PKCS#1 private key"""
# pylint: disable=no-self-use
raise KeyExportError('PKCS#1 private key export not supported')
def encode_ssh_private(self):
"""Encode an SSH format Ed25519 private key"""
if self._sk is None:
raise KeyExportError('Key is not private')
return b''.join((String(self._vk), String(self._sk)))
def encode_pkcs8_private(self):
"""Export parameters associated with a PKCS#8 private key"""
# pylint: disable=no-self-use
raise KeyExportError('PKCS#8 private key export not supported')
data = data + bytes(range(1, block_size + 1 - pad))
if cipher:
data, mac = cipher.encrypt_packet(0, b'', data)
else:
mac = b''
data = b''.join((_OPENSSH_KEY_V1, String(alg), String(kdf),
String(kdf_data), UInt32(nkeys),
String(self.public_data), String(data), mac))
return (b'-----BEGIN OPENSSH PRIVATE KEY-----\n' +
_wrap_base64(data, _OPENSSH_WRAP_LEN) +
b'-----END OPENSSH PRIVATE KEY-----\n')
else:
raise KeyExportError('Unknown export format')