[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: ssh_pki_export_privkey_file for SSH_KEYTYPE_RSA key returns SSH_ERROR - libssh-0.9.3/src/pki_gcrypt:931 return always NULL


On Tue, 17 Dec 2019 09:25:18 +0100
Jakub Jelen <jjelen@xxxxxxxxxx> wrote:

> On Mon, 2019-12-16 at 23:39 +0100, Torsten Kuehnel wrote:
> > I'm new to programming using libssh. 
> > 
> > When i take the keygen.c example file in the examples directory, it
> > fails during execution when i change the keytype.
> > 
> > [tdkuehnel@ multiboot examples]$ ./keygen 
> > Failed to write private key file[tdkuehnel@ multiboot examples]$ 
> > 
> > /*    rv = ssh_pki_generate(SSH_KEYTYPE_ED25519, 0, &key);*/
> >     rv = ssh_pki_generate(SSH_KEYTYPE_RSA, 1024, &key);
> > 
> > Do i miss additional steps in preparing the rsa key to be exported ?
> > 
> > Complete code:
> > 
> > #include <libssh/libssh.h>
> > #include <stdio.h>
> > 
> > int main(void)
> > {
> >     ssh_key key = NULL;
> >     int rv;
> > 
> >     /* Generate a new ED25519 private key file */
> > /*    rv = ssh_pki_generate(SSH_KEYTYPE_ED25519, 0, &key);*/
> >     rv = ssh_pki_generate(SSH_KEYTYPE_RSA, 1024, &key);
> >     if (rv != SSH_OK) {
> >         fprintf(stderr, "Failed to generate private key");
> > 	return -1;
> >     }
> > 
> >     /* Write it to a file testkey in the current dirrectory */
> >     rv = ssh_pki_export_privkey_file(key, NULL, NULL, NULL,
> > "testkey");
> >     if (rv != SSH_OK) {
> >         fprintf(stderr, "Failed to write private key file");
> > 	return -1;
> >     }
> > 
> >     return 0;
> > }
> 
> This exact code works for me just fine and generates the testkey file
> without any problem (with libssh-0.9.2-1.fc31.x86_64). Are you getting
> the error from the key generation or export function?
> 
> $ gcc -lssh keygen.c -o keygen
> $ ./keygen 
> $ echo $?
> 0
> $ cat testkey 
> -----BEGIN PRIVATE KEY-----
> [...]
> 
> 
> Regards,
> -- 
> Jakub Jelen
> Senior Software Engineer
> Security Technologies
> Red Hat, Inc.
> 

I narrowed down the error down to the follwing:

ssh_pki_export_privkey_file calls pki_private_key_to_pem which contains (libssh-0.9.3/src/pki_gcrypt:931):

ssh_string pki_private_key_to_pem(const ssh_key key,
                                  const char *passphrase,
                                  ssh_auth_callback auth_fn,
                                  void *auth_data)
{
    (void) key;                   
    (void) passphrase;
    (void) auth_fn;
    (void) auth_data;

    return NULL;                 <<------------  always returns null (which lets the calling function return -1)
}


-- 
Torsten Kuehnel <tdkuehnel@xxxxxxxxxxxxxxxxxxxxx>

References:
ssh_pki_export_privkey_file for SSH_KEYTYPE_RSA key returns SSH_ERRORTorsten Kuehnel <tdkuehnel@xxxxxxxxxxxxxxxxxxxxx>
Re: ssh_pki_export_privkey_file for SSH_KEYTYPE_RSA key returns SSH_ERRORJakub Jelen <jjelen@xxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org