/usr/lib/python3/dist-packages/asyncssh/ecdsa.py is in python3-asyncssh 1.11.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 | # Copyright (c) 2013-2017 by Ron Frederick <ronf@timeheart.net>.
# All rights reserved.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v1.0 which accompanies this
# distribution and is available at:
#
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors:
# Ron Frederick - initial implementation, API, and documentation
"""ECDSA public key encryption handler"""
from .asn1 import ASN1DecodeError, BitString, ObjectIdentifier, TaggedDERObject
from .asn1 import der_encode, der_decode
from .crypto import lookup_ec_curve_by_params
from .crypto import ECDSAPrivateKey, ECDSAPublicKey
from .packet import MPInt, String, SSHPacket
from .public_key import SSHKey, SSHOpenSSHCertificateV01
from .public_key import KeyImportError, KeyExportError
from .public_key import register_public_key_alg, register_certificate_alg
from .public_key import register_x509_certificate_alg
# OID for EC prime fields
PRIME_FIELD = ObjectIdentifier('1.2.840.10045.1.1')
# Short variable names are used here, matching names in the spec
# pylint: disable=invalid-name
_alg_oids = {}
_alg_oid_map = {}
class _ECKey(SSHKey):
"""Handler for elliptic curve public key encryption"""
pem_name = b'EC'
pkcs8_oid = ObjectIdentifier('1.2.840.10045.2.1')
def __init__(self, key):
super().__init__(key)
self.algorithm = b'ecdsa-sha2-' + key.curve_id
self.sig_algorithms = (self.algorithm,)
self.x509_algorithms = (b'x509v3-' + self.algorithm,)
self.all_sig_algorithms = set(self.sig_algorithms)
self._alg_oid = _alg_oids[key.curve_id]
def __eq__(self, other):
# This isn't protected access - both objects are _ECKey instances
# pylint: disable=protected-access
return (isinstance(other, type(self)) and
self._key.curve_id == other._key.curve_id and
self._key.x == other._key.x and
self._key.y == other._key.y and
self._key.d == other._key.d)
def __hash__(self):
return hash((self._key.curve_id, self._key.x,
self._key.y, self._key.d))
@classmethod
def _lookup_curve(cls, alg_params):
"""Look up an EC curve matching the specified parameters"""
if isinstance(alg_params, ObjectIdentifier):
try:
curve_id = _alg_oid_map[alg_params]
except KeyError:
raise KeyImportError('Unknown elliptic curve OID %s',
alg_params) from None
elif (isinstance(alg_params, tuple) and len(alg_params) >= 5 and
alg_params[0] == 1 and isinstance(alg_params[1], tuple) and
len(alg_params[1]) == 2 and alg_params[1][0] == PRIME_FIELD and
isinstance(alg_params[2], tuple) and len(alg_params[2]) >= 2 and
isinstance(alg_params[3], bytes) and
isinstance(alg_params[2][0], bytes) and
isinstance(alg_params[2][1], bytes) and
isinstance(alg_params[4], int)):
p = alg_params[1][1]
a = int.from_bytes(alg_params[2][0], 'big')
b = int.from_bytes(alg_params[2][1], 'big')
point = alg_params[3]
n = alg_params[4]
try:
curve_id = lookup_ec_curve_by_params(p, a, b, point, n)
except ValueError as exc:
raise KeyImportError(str(exc)) from None
else:
raise KeyImportError('Invalid EC curve parameters')
return curve_id
@classmethod
def generate(cls, algorithm):
"""Generate a new EC private key"""
# Strip 'ecdsa-sha2-' prefix of algorithm to get curve_id
return cls(ECDSAPrivateKey.generate(algorithm[11:]))
@classmethod
def make_private(cls, curve_id, private_key, public_key):
"""Construct an EC private key"""
if isinstance(private_key, bytes):
private_key = int.from_bytes(private_key, 'big')
return cls(ECDSAPrivateKey.construct(curve_id, public_key,
private_key))
@classmethod
def make_public(cls, curve_id, public_key):
"""Construct an EC public key"""
return cls(ECDSAPublicKey.construct(curve_id, public_key))
@classmethod
def decode_pkcs1_private(cls, key_data):
"""Decode a PKCS#1 format EC private key"""
if (isinstance(key_data, tuple) and len(key_data) > 2 and
key_data[0] == 1 and isinstance(key_data[1], bytes) and
isinstance(key_data[2], TaggedDERObject) and
key_data[2].tag == 0):
alg_params = key_data[2].value
private_key = key_data[1]
if (len(key_data) > 3 and
isinstance(key_data[3], TaggedDERObject) and
key_data[3].tag == 1 and
isinstance(key_data[3].value, BitString) and
key_data[3].value.unused == 0):
public_key = key_data[3].value.value
else:
public_key = None
return cls._lookup_curve(alg_params), private_key, public_key
else:
return None
@classmethod
def decode_pkcs1_public(cls, key_data):
"""Decode a PKCS#1 format EC public key"""
# pylint: disable=unused-argument
raise KeyImportError('PKCS#1 not supported for EC public keys')
@classmethod
def decode_pkcs8_private(cls, alg_params, data):
"""Decode a PKCS#8 format EC private key"""
try:
key_data = der_decode(data)
except ASN1DecodeError:
key_data = None
if (isinstance(key_data, tuple) and len(key_data) > 1 and
key_data[0] == 1 and isinstance(key_data[1], bytes)):
private_key = key_data[1]
if (len(key_data) > 2 and
isinstance(key_data[2], TaggedDERObject) and
key_data[2].tag == 1 and
isinstance(key_data[2].value, BitString) and
key_data[2].value.unused == 0):
public_key = key_data[2].value.value
else:
public_key = None
return cls._lookup_curve(alg_params), private_key, public_key
else:
return None
@classmethod
def decode_pkcs8_public(cls, alg_params, key_data):
"""Decode a PKCS#8 format EC public key"""
if isinstance(alg_params, ObjectIdentifier):
return cls._lookup_curve(alg_params), key_data
else:
return None
@classmethod
def decode_ssh_private(cls, packet):
"""Decode an SSH format EC private key"""
curve_id = packet.get_string()
public_key = packet.get_string()
private_key = packet.get_mpint()
return curve_id, private_key, public_key
@classmethod
def decode_ssh_public(cls, packet):
"""Decode an SSH format EC public key"""
curve_id = packet.get_string()
public_key = packet.get_string()
return curve_id, public_key
def encode_public_tagged(self):
"""Encode an EC public key blob as a tagged bitstring"""
return TaggedDERObject(1, BitString(self._key.public_value))
def encode_pkcs1_private(self):
"""Encode a PKCS#1 format EC private key"""
if not self._key.private_value:
raise KeyExportError('Key is not private')
return (1, self._key.private_value,
TaggedDERObject(0, self._alg_oid),
self.encode_public_tagged())
def encode_pkcs1_public(self):
"""Encode a PKCS#1 format EC public key"""
raise KeyExportError('PKCS#1 is not supported for EC public keys')
def encode_pkcs8_private(self):
"""Encode a PKCS#8 format EC private key"""
if not self._key.private_value:
raise KeyExportError('Key is not private')
return self._alg_oid, der_encode((1, self._key.private_value,
self.encode_public_tagged()))
def encode_pkcs8_public(self):
"""Encode a PKCS#8 format EC public key"""
return self._alg_oid, self._key.public_value
def encode_ssh_private(self):
"""Encode an SSH format EC private key"""
if not self._key.d:
raise KeyExportError('Key is not private')
return b''.join((String(self._key.curve_id),
String(self._key.public_value),
MPInt(self._key.d)))
def encode_ssh_public(self):
"""Encode an SSH format EC public key"""
return b''.join((String(self._key.curve_id),
String(self._key.public_value)))
def encode_agent_cert_private(self):
"""Encode ECDSA certificate private key data for agent"""
if not self._key.d:
raise KeyExportError('Key is not private')
return MPInt(self._key.d)
def sign_der(self, data, sig_algorithm):
"""Compute a DER-encoded signature of the specified data"""
# pylint: disable=unused-argument
if not self._key.private_value:
raise ValueError('Private key needed for signing')
return self._key.sign(data)
def verify_der(self, data, sig_algorithm, sig):
"""Verify a DER-encoded signature of the specified data"""
# pylint: disable=unused-argument
return self._key.verify(data, sig)
def sign_ssh(self, data, sig_algorithm):
"""Compute an SSH-encoded signature of the specified data"""
r, s = der_decode(self.sign_der(data, sig_algorithm))
return MPInt(r) + MPInt(s)
def verify_ssh(self, data, sig_algorithm, sig):
"""Verify an SSH-encoded signature of the specified data"""
packet = SSHPacket(sig)
r = packet.get_mpint()
s = packet.get_mpint()
packet.check_end()
return self.verify_der(data, sig_algorithm, der_encode((r, s)))
for _curve_id, _oid in ((b'nistp521', '1.3.132.0.35'),
(b'nistp384', '1.3.132.0.34'),
(b'nistp256', '1.2.840.10045.3.1.7')):
_algorithm = b'ecdsa-sha2-' + _curve_id
_cert_algorithm = _algorithm + b'-cert-v01@openssh.com'
_x509_algorithm = b'x509v3-' + _algorithm
_oid = ObjectIdentifier(_oid)
_alg_oids[_curve_id] = _oid
_alg_oid_map[_oid] = _curve_id
register_public_key_alg(_algorithm, _ECKey, (_algorithm,))
register_certificate_alg(1, _algorithm, _cert_algorithm,
_ECKey, SSHOpenSSHCertificateV01)
register_x509_certificate_alg(_x509_algorithm)
|