RAND_pseudo_bytes is good enough for encryption IVs,
[openssl.git] / demos / maurice / loadkeys.c
1 /* NOCW */
2 /*
3         Please read the README file for condition of use, before
4         using this software.
5
6         Maurice Gittens  <mgittens@gits.nl>   January 1997
7
8 */
9
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <netinet/in.h>
13 #include <fcntl.h>
14 #include <strings.h>
15 #include <stdlib.h>
16
17 #include <openssl/rsa.h>
18 #include <openssl/evp.h>
19 #include <openssl/objects.h>
20 #include <openssl/x509.h>
21 #include <openssl/err.h>
22 #include <openssl/pem.h>
23 #include <openssl/ssl.h>
24
25 EVP_PKEY * ReadPublicKey(const char *certfile)
26 {
27   FILE *fp = fopen (certfile, "r");   
28   X509 *x509;
29   EVP_PKEY *pkey;
30
31   if (!fp) 
32      return NULL; 
33
34   x509 = (X509 *)PEM_ASN1_read ((char *(*)())d2i_X509,
35                                    PEM_STRING_X509,
36                                    fp, NULL, NULL);
37
38   if (x509 == NULL) 
39   {  
40      ERR_print_errors_fp (stderr);
41      return NULL;   
42   }
43
44   fclose (fp);
45   
46   pkey=X509_extract_key(x509);
47
48   X509_free(x509);
49
50   if (pkey == NULL) 
51      ERR_print_errors_fp (stderr);
52
53   return pkey; 
54 }
55
56 EVP_PKEY *ReadPrivateKey(const char *keyfile)
57 {
58         FILE *fp = fopen(keyfile, "r");
59         EVP_PKEY *pkey;
60
61         if (!fp)
62                 return NULL;
63
64         pkey = (EVP_PKEY*)PEM_ASN1_read ((char *(*)())d2i_PrivateKey,
65                               PEM_STRING_EVP_PKEY,
66                               fp,
67                               NULL, NULL);
68
69         fclose (fp);
70
71         if (pkey == NULL) 
72                 ERR_print_errors_fp (stderr);   
73
74         return pkey;
75 }
76
77