chilkatchilkat-sshkey

Chilkat load SSHKey without knowing the type


I'm wondering if its possible to load a SSHKey via the Chilkat library without directly knowing the type. Currently I need to do something like this:

BOOL success = false;
if([privateKey FromOpenSshPublicKey: privateKeyString])
{
    NSLog(@"key is FromOpenSshPublicKey");
    success = true;
}
else if([privateKey FromPuttyPrivateKey: privateKeyString])
{
    NSLog(@"key is FromPuttyPrivateKey");
    success = true;
}
else if([privateKey FromRfc4716PublicKey: privateKeyString])
{
    NSLog(@"key is FromRfc4716PublicKey");
    success = true;
}

But it makes logging a bit difficult if I want to use something like LastErrorText.


Solution

  • The methods were originally written to load a key of a specific type. In actuality, all of the methods auto-recognize the type and will still load the key successfully. For example, you may call FromOpenSshPrivate key with a PuTTY private key, and it still works. (you can even pass PEM to it).

    However... you do need to know if you have a public or private key. A call to FromPrivateKey will fail if you pass a public key to it. The reason is that the private parts of the key are simply not present, and if you wanted to load a private key it must be that you're doing something that requires a private key.

    On the other hand, a public key is just a portion of the full private key. If you have a private key, by definition you also have the public key. (For example, an RSA public key is composed of the modulus and exponent, but an RSA private key contains modulus, exponent, plus other parts. If you have the private key, then you have the modulus and exponent and by definition you also have the public key.) Thus.. passing a private key to a method that loads a public key will still work. You're just passing more than what's necessary.