summaryrefslogtreecommitdiff
path: root/src/orkeygen/orkeygen.c
blob: 8e4c87fff58dd2df6d9be191758f6f1c3edd7087 (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
 * orkeygen.c 
 * Key generation utility. 
 *
 * Matej Pfajfar <mp292@cam.ac.uk>
 */

/*
 * Changes :
 * $Log$
 * Revision 1.2  2002/07/25 08:18:05  badbytes
 * Updated to use crypto.h instead of OpenSSL.
 *
 * Revision 1.1.1.1  2002/06/26 22:45:50  arma
 * initial commit: current code
 *
 * Revision 1.1  2002/01/04 07:19:27  badbytes
 * Key generation utility.
 *
 *
 */

#include <stdlib.h>
#include <stdio.h>

#include "../common/crypto.h"

int main(int argc, char *argv[])
{

  char *file_pr = argv[1];
  char *file_pu = argv[2];
  
  FILE *f_pr = NULL;
  FILE *f_pu = NULL;
  
  crypto_pk_env_t *env;
 
  int retval = 0;
  
  
  if (argc < 3)
  {
    printf("Need two files, for private and public key in that order.\n");
    exit(1);
  }
  
  crypto_global_init();
  
  env = crypto_new_pk_env(CRYPTO_PK_RSA);
  if (!env)
  {
    printf("Could not create a crypto environment.");
    exit(1);
  }
  
  /* generate the key */
  if (crypto_pk_generate_key(env)) /* error has occured */
  {
    printf("%s",crypto_perror());
    exit(1);
  }
  else /* keys generated */
  {
    retval = crypto_pk_check_key(env);
    if (retval == 1)
    {
      printf("Generated key seems to be valid.\n");
      /* open the output files */
      f_pr = fopen(file_pr,"w");
      if (!f_pr)
      {
	perror("fopen");
	crypto_free_pk_env(env);
	exit(1);
      }
      
      f_pu = fopen(file_pu,"w");
      if (!f_pu)
      {
	perror("fopen");
	crypto_free_pk_env(env);
	exit(1);
      }
      
      /* write the private key */
      if (crypto_pk_write_private_key(env, f_pr) == -1)
      {
	printf("%s",crypto_perror());
	fclose(f_pr);
	fclose(f_pu);
        crypto_free_pk_env(env);
	exit(1);
      }
      
      /* write the public key */
      if (crypto_pk_write_public_key(env, f_pu) == -1)
      {
	printf("%s",crypto_perror());
	fclose(f_pr);
	fclose(f_pu);
        crypto_free_pk_env(env);
	exit(1);
      }
      
      printf("Keys written to files %s (public) and %s (private).\n",file_pu,file_pr);
    }
    else if (retval == 0)
    {
      printf("Generated key seems to be invalid. Exiting.\n");
      crypto_free_pk_env(env);
      exit(1);
    }
    else if (retval == -1)
    {
      printf("%s",crypto_perror());
      crypto_free_pk_env(env);
      exit(1);
    }
  }
     
  crypto_free_pk_env(env);
  crypto_global_cleanup();
  fclose(f_pu);
  fclose(f_pr);
  exit(0);
  
 }