Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(frozenset(), '3100'),
(frozenset({1}), '3103020101'),
(frozenset({1, 2}), '3106020101020102'),
(frozenset({-128, 127}), '310602017f020180'),
(BitString(b''), '030100'),
(BitString(b'\0', 7), '03020700'),
(BitString(b'\x80', 7), '03020780'),
(BitString(b'\x80', named=True), '03020780'),
(BitString(b'\x81', named=True), '03020081'),
(BitString(b'\x81\x00', named=True), '03020081'),
(BitString(b'\x80', 6), '03020680'),
(BitString(b'\x80'), '03020080'),
(BitString(b'\x80\x00', 7), '0303078000'),
(BitString(''), '030100'),
(BitString('0'), '03020700'),
(BitString('1'), '03020780'),
(BitString('10'), '03020680'),
(BitString('10000000'), '03020080'),
(BitString('10000001'), '03020081'),
(BitString('100000000'), '0303078000'),
(IA5String(b''), '1600'),
(IA5String(b'\0'), '160100'),
(IA5String(b'abc'), '1603616263'),
(ObjectIdentifier('0.0'), '060100'),
(ObjectIdentifier('1.2'), '06012a'),
(ObjectIdentifier('1.2.840'), '06032a8648'),
(ObjectIdentifier('2.5'), '060155'),
(ObjectIdentifier('2.40'), '060178'),
(frozenset({1, 2}), '3106020101020102'),
(frozenset({-128, 127}), '310602017f020180'),
(BitString(b''), '030100'),
(BitString(b'\0', 7), '03020700'),
(BitString(b'\x80', 7), '03020780'),
(BitString(b'\x80', named=True), '03020780'),
(BitString(b'\x81', named=True), '03020081'),
(BitString(b'\x81\x00', named=True), '03020081'),
(BitString(b'\x80', 6), '03020680'),
(BitString(b'\x80'), '03020080'),
(BitString(b'\x80\x00', 7), '0303078000'),
(BitString(''), '030100'),
(BitString('0'), '03020700'),
(BitString('1'), '03020780'),
(BitString('10'), '03020680'),
(BitString('10000000'), '03020080'),
(BitString('10000001'), '03020081'),
(BitString('100000000'), '0303078000'),
(IA5String(b''), '1600'),
(IA5String(b'\0'), '160100'),
(IA5String(b'abc'), '1603616263'),
(ObjectIdentifier('0.0'), '060100'),
(ObjectIdentifier('1.2'), '06012a'),
(ObjectIdentifier('1.2.840'), '06032a8648'),
(ObjectIdentifier('2.5'), '060155'),
(ObjectIdentifier('2.40'), '060178'),
(TaggedDERObject(0, None), 'a0020500'),
(TaggedDERObject(1, None), 'a1020500'),
('abc', '0c03616263'),
((), '3000'),
((1,), '3003020101'),
((1, 2), '3006020101020102'),
(frozenset(), '3100'),
(frozenset({1}), '3103020101'),
(frozenset({1, 2}), '3106020101020102'),
(frozenset({-128, 127}), '310602017f020180'),
(BitString(b''), '030100'),
(BitString(b'\0', 7), '03020700'),
(BitString(b'\x80', 7), '03020780'),
(BitString(b'\x80', named=True), '03020780'),
(BitString(b'\x81', named=True), '03020081'),
(BitString(b'\x81\x00', named=True), '03020081'),
(BitString(b'\x80', 6), '03020680'),
(BitString(b'\x80'), '03020080'),
(BitString(b'\x80\x00', 7), '0303078000'),
(BitString(''), '030100'),
(BitString('0'), '03020700'),
(BitString('1'), '03020780'),
(BitString('10'), '03020680'),
(BitString('10000000'), '03020080'),
(BitString('10000001'), '03020081'),
(BitString('100000000'), '0303078000'),
(IA5String(b''), '1600'),
(IA5String(b'\0'), '160100'),
(IA5String(b'abc'), '1603616263'),
b'-----END OPENSSH PRIVATE KEY-----'),
('Unexpected OpenSSH trailing data',
b'-----BEGIN OPENSSH PRIVATE KEY-----\n' +
binascii.b2a_base64(b''.join(
(b'openssh-key-v1\0', String('aes256-cbc'), String('bcrypt'),
String(b''.join((String(16*b'\0'), UInt32(128)))), UInt32(1),
String(''), String(''), String('xxx')))) +
b'-----END OPENSSH PRIVATE KEY-----')
]
public_errors = [
('Non-ASCII', '\xff'),
('Invalid ASN.1', b'\x30'),
('Invalid PKCS#1', der_encode(None)),
('Invalid PKCS#8', der_encode(((self.pubkey.pkcs8_oid, ()),
BitString(der_encode(None))))),
('Unknown PKCS#8 algorithm', der_encode(((ObjectIdentifier('1.1'),
None), BitString(b'')))),
('Invalid PKCS#8 ASN.1', der_encode(((self.pubkey.pkcs8_oid,
None), BitString(b'')))),
('Invalid PEM header', b'-----BEGIN XXX-----\n'),
('Missing PEM footer', b'-----BEGIN PUBLIC KEY-----\n'),
('Invalid PEM key type',
b'-----BEGIN XXX PUBLIC KEY-----\n' +
binascii.b2a_base64(der_encode(None)) +
b'-----END XXX PUBLIC KEY-----'),
('Invalid PEM Base64',
b'-----BEGIN PUBLIC KEY-----\n'
b'X\n'
b'-----END PUBLIC KEY-----'),
('Incomplete PEM ASN.1',
b'-----BEGIN PUBLIC KEY-----\n'
(frozenset(), '3100'),
(frozenset({1}), '3103020101'),
(frozenset({1, 2}), '3106020101020102'),
(frozenset({-128, 127}), '310602017f020180'),
(BitString(b''), '030100'),
(BitString(b'\0', 7), '03020700'),
(BitString(b'\x80', 7), '03020780'),
(BitString(b'\x80', named=True), '03020780'),
(BitString(b'\x81', named=True), '03020081'),
(BitString(b'\x81\x00', named=True), '03020081'),
(BitString(b'\x80', 6), '03020680'),
(BitString(b'\x80'), '03020080'),
(BitString(b'\x80\x00', 7), '0303078000'),
(BitString(''), '030100'),
(BitString('0'), '03020700'),
(BitString('1'), '03020780'),
(BitString('10'), '03020680'),
(BitString('10000000'), '03020080'),
(BitString('10000001'), '03020081'),
(BitString('100000000'), '0303078000'),
(IA5String(b''), '1600'),
(IA5String(b'\0'), '160100'),
(IA5String(b'abc'), '1603616263'),
(ObjectIdentifier('0.0'), '060100'),
(ObjectIdentifier('1.2'), '06012a'),
(ObjectIdentifier('1.2.840'), '06032a8648'),
(ObjectIdentifier('2.5'), '060155'),
(ObjectIdentifier('2.40'), '060178'),
('', '0c00'),
('\0', '0c0100'),
('abc', '0c03616263'),
((), '3000'),
((1,), '3003020101'),
((1, 2), '3006020101020102'),
(frozenset(), '3100'),
(frozenset({1}), '3103020101'),
(frozenset({1, 2}), '3106020101020102'),
(frozenset({-128, 127}), '310602017f020180'),
(BitString(b''), '030100'),
(BitString(b'\0', 7), '03020700'),
(BitString(b'\x80', 7), '03020780'),
(BitString(b'\x80', named=True), '03020780'),
(BitString(b'\x81', named=True), '03020081'),
(BitString(b'\x81\x00', named=True), '03020081'),
(BitString(b'\x80', 6), '03020680'),
(BitString(b'\x80'), '03020080'),
(BitString(b'\x80\x00', 7), '0303078000'),
(BitString(''), '030100'),
(BitString('0'), '03020700'),
(BitString('1'), '03020780'),
(BitString('10'), '03020680'),
(BitString('10000000'), '03020080'),
(BitString('10000001'), '03020081'),
(BitString('100000000'), '0303078000'),
(IA5String(b''), '1600'),
def _decode_pkcs8_public(key_data):
"""Decode a PKCS#8 format public key"""
if (isinstance(key_data, tuple) and len(key_data) == 2 and
isinstance(key_data[0], tuple) and 1 <= len(key_data[0]) <= 2 and
isinstance(key_data[1], BitString) and key_data[1].unused == 0):
if len(key_data[0]) == 2:
alg, alg_params = key_data[0]
else:
alg, alg_params = key_data[0][0], OMIT
handler = _pkcs8_oid_map.get(alg)
if handler is None:
raise KeyImportError('Unknown PKCS#8 algorithm')
key_params = handler.decode_pkcs8_public(alg_params, key_data[1].value)
if key_params is None:
raise KeyImportError('Invalid %s public key' %
handler.pem_name.decode('ascii')
if handler.pem_name else 'PKCS#8')
return handler.make_public(*key_params)
def encode_public(self):
"""Encode an EC public key blob"""
return TaggedDERObject(1, BitString(self._Q.encode()))
"""
if format_name in ('pkcs1-der', 'pkcs1-pem'):
data = der_encode(self.encode_pkcs1_public())
if format_name == 'pkcs1-pem':
keytype = self.pem_name + b' PUBLIC KEY'
data = (b'-----BEGIN ' + keytype + b'-----\n' +
_wrap_base64(data) +
b'-----END ' + keytype + b'-----\n')
return data
elif format_name in ('pkcs8-der', 'pkcs8-pem'):
alg_params, data = self.encode_pkcs8_public()
data = BitString(data)
if alg_params is OMIT:
data = der_encode(((self.pkcs8_oid,), data))
else:
data = der_encode(((self.pkcs8_oid, alg_params), data))
if format_name == 'pkcs8-pem':
data = (b'-----BEGIN PUBLIC KEY-----\n' +
_wrap_base64(data) +
b'-----END PUBLIC KEY-----\n')
return data
elif format_name == 'openssh':
if self._comment:
comment = b' ' + self._comment
else: