For openstack apparently I need the key-output like ssh-keygen -t ecdsa generates it, but I am not getting similar output when using the ecdsa modul in python.
def createECDSAKeyPairLocally(projectName="current"):
sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
vk = sk.verifying_key
with open(f"privateKey_bibi2_{projectName}.pem", "wb") as f:
f.write(sk.to_pem())
with open(f"publicKey_bibi2_{projectName}.pem", "wb") as f:
f.write(vk.to_pem())
return vk
vk will be something like:
b'-----BEGIN PUBLIC KEY-----[...]
while the ssh-keygen will be like:
ecdsa-sha2-nistp256[...]
While openstack accepts ssh-keygen's output it does not accept python exdsa's. I think I am misunderstanding something regarding the .pem files created. How can I generate similar output or what am I understanding wrong?
EDIT1
I tried
def createECDSAKeyPairLocally(projectName="current", comment = "no comment"):
sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
vk = sk.verifying_key
with open(f"privateKey_bibi2_{projectName}.pem", "wb") as f:
f.write(sk.to_pem())
first = "ecdsa-sha2-nistp256"
second = base64.b64encode(vk.to_string()).decode("utf-8")
third = comment
sshKeygenFormat = " ".join([first, second, third])
with open(f"publicKey_bibi2_{projectName}.pub", "w") as f:
f.write(sshKeygenFormat)
return sshKeygenFormat
But openstack says that it is invalid. In the output I noticed that the python ecdsa version includes "" which I haven't seen yet in ssh.keygen's keys. Do they maybe use a different alphabet?
EDIT2
I attempted using
def createECDSAKeyPairLocally(projectName="current", comment = "no comment"):
sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
vk = sk.verifying_key
with open(f"privateKey_bibi2_{projectName}.pem", "wb") as f:
f.write(sk.to_pem())
with open(f"publicKey_bibi2_{projectName}.pub", "w") as f:
first = "ecdsa-sha2-nistp256"
prefix = b"\x00\x00\x00\x13ecdsa-sha2-nistp256\x00\x00\x00\x08nistp256\x00\x00\x00A"
second = base64.b64encode(prefix+vk.to_string()).decode("utf-8")
third = comment
sshkeygen = " ".join([first, second, third])
f.write(sshkeygen)
return sshkeygen
But the response is also that it's invalid.
EDIT3 I now tried:
def createECDSAKeyPairLocally(projectName="current", comment = "no comment"):
sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
vk = sk.verifying_key
with open(f"privateKey_bibi2_{projectName}", "wb") as f:
f.write(sk.to_pem())
with open(f"publicKey_bibi2_{projectName}.pub", "w") as f:
first = "ecdsa-sha2-nistp256"
prefix = b"\x00\x00\x00\x13ecdsa-sha2-nistp256\x00\x00\x00\x08nistp256\x00\x00\x00A"
second = base64.b64encode(
prefix+vk.to_string(encoding="uncompressed")
).decode("utf-8")
third = comment
keygen = " ".join([first, second, third])
f.write(keygen)
return keygen
But the generated key is invalid (by openstack standards) as well. Someone outside of StackOverflow suggested that the ecdsa modul might use an older standard, but I don't know how to verify that.
EDIT 4.2
def createECDSAKeyPairLocally(projectName="current", comment = "no"):
sk = ecdsa.SigningKey.generate(curve=ecdsa.SECP256k1)
vk = sk.verifying_key
with open(f"privateKey_bibi2_{projectName}", "wb") as f:
f.write(sk.to_pem())
with open(f"publicKey_bibi2_{projectName}.pub", "w") as f:
first = "ecdsa-sha2-nistp256"
prefix = b"\x00\x00\x00\x13ecdsa-sha2-nistp256\x00\x00\x00\x08nistp256\x00\x00\x00A"
all_bytes = vk.to_string(encoding="uncompressed")
prepending_byte = all_bytes[:1] # necessary to get bytes instead of int
first_key_part = all_bytes[1:33]
second_key_part = all_bytes[33:]
second = base64.b64encode(
b''.join([prefix,prepending_byte,second_key_part,first_key_part])
).decode("utf-8")
third = comment
keygen = " ".join([first, second, third])
f.write(keygen)
return keygen
Fixed the byte slicing by using b''.join. Still not accepted by openstack.
[0],[1:33]and[33:].