MCPcopy Create free account
hub / github.com/Keeper-Security/Commander / add_ssh_key

Function add_ssh_key

keepercommander/commands/ssh_agent.py:350–426  ·  view source on GitHub ↗
(private_key_pem, passphrase, key_name)

Source from the content-addressed store, hash-verified

348
349
350def add_ssh_key(private_key_pem, passphrase, key_name): # type: (str, str, str) -> Optional[Callable]
351 private_key = load_private_key(private_key_pem, passphrase)
352 if isinstance(private_key, rsa.RSAPrivateKey):
353 private_numbers = private_key.private_numbers()
354 public_numbers = private_key.public_key().public_numbers()
355
356 store_payload = SSH_AGENTC_ADD_IDENTITY.to_bytes(1, byteorder='big')
357 store_payload += ssh_agent_encode_str('ssh-rsa')
358 store_payload += ssh_agent_encode_long(public_numbers.n)
359 store_payload += ssh_agent_encode_long(public_numbers.e)
360 store_payload += ssh_agent_encode_long(private_numbers.d)
361 store_payload += ssh_agent_encode_long(private_numbers.iqmp)
362 store_payload += ssh_agent_encode_long(private_numbers.p)
363 store_payload += ssh_agent_encode_long(private_numbers.q)
364 store_payload += ssh_agent_encode_str(key_name)
365 # windows ssh implementation does not support constrained identity
366 # store_payload += SSH_AGENT_CONSTRAIN_LIFETIME.to_bytes(1, byteorder='big')
367 # store_payload += int(10).to_bytes(4, byteorder='big')
368
369 remove_payload = ssh_agent_encode_str('ssh-rsa')
370 remove_payload += ssh_agent_encode_long(public_numbers.e)
371 remove_payload += ssh_agent_encode_long(public_numbers.n)
372 remove_payload = SSH_AGENTC_REMOVE_IDENTITY.to_bytes(1, byteorder='big') + ssh_agent_encode_bytes(remove_payload)
373 elif isinstance(private_key, ec.EllipticCurvePrivateKey):
374 curve_name = 'nistp384' if private_key.curve.name == 'secp384r1' else \
375 'nistp521' if private_key.curve.name == 'secp521r1' else \
376 'nistp256' if private_key.curve.name == 'secp256r1' else ''
377 if not curve_name:
378 raise ValueError(f'EC curve is not supported {private_key.curve.name}')
379
380 private_numbers = private_key.private_numbers()
381 store_payload = SSH_AGENTC_ADD_IDENTITY.to_bytes(1, byteorder='big')
382 store_payload += ssh_agent_encode_str(f'ecdsa-sha2-{curve_name}')
383 store_payload += ssh_agent_encode_str(curve_name)
384 public_key_bytes = private_key.public_key().public_bytes(
385 encoding=serialization.Encoding.X962, format=serialization.PublicFormat.UncompressedPoint)
386 store_payload += ssh_agent_encode_bytes(public_key_bytes)
387 store_payload += ssh_agent_encode_long(private_numbers.private_value)
388 store_payload += ssh_agent_encode_str(key_name)
389
390 remove_payload = ssh_agent_encode_str(f'ecdsa-sha2-{curve_name}')
391 remove_payload += ssh_agent_encode_str(curve_name)
392 remove_payload += ssh_agent_encode_bytes(public_key_bytes)
393 remove_payload = SSH_AGENTC_REMOVE_IDENTITY.to_bytes(1, byteorder='big') + ssh_agent_encode_bytes(remove_payload)
394
395 elif isinstance(private_key, ed25519.Ed25519PrivateKey):
396 public_key_bytes = private_key.public_key().public_bytes(
397 encoding=serialization.Encoding.Raw, format=serialization.PublicFormat.Raw)
398 private_key_bytes = private_key.private_bytes(
399 encoding=serialization.Encoding.Raw, format=serialization.PrivateFormat.Raw,
400 encryption_algorithm=serialization.NoEncryption())
401
402 store_payload = SSH_AGENTC_ADD_IDENTITY.to_bytes(1, byteorder='big')
403 store_payload += ssh_agent_encode_str('ssh-ed25519')
404 store_payload += ssh_agent_encode_bytes(public_key_bytes)
405 store_payload += ssh_agent_encode_bytes(private_key_bytes + public_key_bytes)
406 store_payload += ssh_agent_encode_str(key_name)
407

Callers 2

executeMethod · 0.85
add_ssh_keysMethod · 0.85

Calls 7

load_private_keyFunction · 0.85
ssh_agent_encode_strFunction · 0.85
ssh_agent_encode_longFunction · 0.85
ssh_agent_encode_bytesFunction · 0.85
ConnectSshAgentClass · 0.85
public_keyMethod · 0.80
sendMethod · 0.45

Tested by

no test coverage detected