aboutsummaryrefslogtreecommitdiff
path: root/examples/ssh-keygen.c
blob: 8d2a42c460db66b3ee852c57c6b2472af07d890a (plain)
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
/*
 * This file is part of the SSH Library
 *
 * Copyright (c) 2011 by Aris Adamantiadis <aris@0xbadc0de.be>
 *
 * The SSH Library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or (at your
 * option) any later version.
 *
 * The SSH Library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with the SSH Library; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
 * MA 02111-1307, USA.
 */

#include <libssh/libssh.h>
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv){
  ssh_key key;
  int rc;
  char path[256];
  char passphrase[256];
  char *ptr;

  printf("ssh-keygen, using libssh %s\n", ssh_version(0));
  printf("Generating rsa keypair\n");
  rc = ssh_pki_generate(SSH_KEYTYPE_RSA,2048,&key);
  if (rc != SSH_OK){
    printf("error\n");
    return 1;
  }
  printf("Enter the filename for private key (id_rsa):");
  fflush(stdout);
  ptr = fgets(path,sizeof(path),stdin);
  if (!ptr)
    return 1;
  ptr = strchr(path,'\n');
  if(ptr)
    *ptr=0;
  ptr = strchr(path,'\r');
  if(ptr)
    *ptr=0;
  rc = ssh_getpass("Enter passphrase (empty for no passphrase):",
      passphrase,sizeof(passphrase),0,1);
  if (rc != SSH_OK)
    return 1;
  rc = ssh_pki_export_pubkey_file(key, path);
  if (rc != SSH_OK){
    printf("Error exporting pubkey\n");
    return 1;
  }
  return 0;
}