turns printfs back on
[freebsd-src/fkvm-freebsd.git] / crypto / openssl / demos / maurice / example1.c
blob1ef829990065103d7573c6132747f845f50471cd
1 /* NOCW */
2 /*
3 Please read the README file for condition of use, before
4 using this software.
6 Maurice Gittens <mgittens@gits.nl> January 1997
7 */
9 #include <unistd.h>
10 #include <stdio.h>
11 #include <netinet/in.h>
12 #include <fcntl.h>
13 #include <strings.h>
14 #include <stdlib.h>
16 #include <openssl/rsa.h>
17 #include <openssl/evp.h>
18 #include <openssl/objects.h>
19 #include <openssl/x509.h>
20 #include <openssl/err.h>
21 #include <openssl/pem.h>
22 #include <openssl/ssl.h>
24 #include "loadkeys.h"
26 #define PUBFILE "cert.pem"
27 #define PRIVFILE "privkey.pem"
29 #define STDIN 0
30 #define STDOUT 1
32 void main_encrypt(void);
33 void main_decrypt(void);
35 static const char *usage = "Usage: example1 [-d]\n";
37 int main(int argc, char *argv[])
40 ERR_load_crypto_strings();
42 if ((argc == 1))
44 main_encrypt();
46 else if ((argc == 2) && !strcmp(argv[1],"-d"))
48 main_decrypt();
50 else
52 printf("%s",usage);
53 exit(1);
56 return 0;
59 void main_encrypt(void)
61 unsigned int ebuflen;
62 EVP_CIPHER_CTX ectx;
63 unsigned char iv[EVP_MAX_IV_LENGTH];
64 unsigned char *ekey[1];
65 int readlen;
66 int ekeylen, net_ekeylen;
67 EVP_PKEY *pubKey[1];
68 char buf[512];
69 char ebuf[512];
71 memset(iv, '\0', sizeof(iv));
73 pubKey[0] = ReadPublicKey(PUBFILE);
75 if(!pubKey[0])
77 fprintf(stderr,"Error: can't load public key");
78 exit(1);
81 ekey[0] = malloc(EVP_PKEY_size(pubKey[0]));
82 if (!ekey[0])
84 EVP_PKEY_free(pubKey[0]);
85 perror("malloc");
86 exit(1);
89 EVP_SealInit(&ectx,
90 EVP_des_ede3_cbc(),
91 ekey,
92 &ekeylen,
93 iv,
94 pubKey,
95 1);
97 net_ekeylen = htonl(ekeylen);
98 write(STDOUT, (char*)&net_ekeylen, sizeof(net_ekeylen));
99 write(STDOUT, ekey[0], ekeylen);
100 write(STDOUT, iv, sizeof(iv));
102 while(1)
104 readlen = read(STDIN, buf, sizeof(buf));
106 if (readlen <= 0)
108 if (readlen < 0)
109 perror("read");
111 break;
114 EVP_SealUpdate(&ectx, ebuf, &ebuflen, buf, readlen);
116 write(STDOUT, ebuf, ebuflen);
119 EVP_SealFinal(&ectx, ebuf, &ebuflen);
121 write(STDOUT, ebuf, ebuflen);
123 EVP_PKEY_free(pubKey[0]);
124 free(ekey[0]);
127 void main_decrypt(void)
129 char buf[520];
130 char ebuf[512];
131 unsigned int buflen;
132 EVP_CIPHER_CTX ectx;
133 unsigned char iv[EVP_MAX_IV_LENGTH];
134 unsigned char *encryptKey;
135 unsigned int ekeylen;
136 EVP_PKEY *privateKey;
138 memset(iv, '\0', sizeof(iv));
140 privateKey = ReadPrivateKey(PRIVFILE);
141 if (!privateKey)
143 fprintf(stderr, "Error: can't load private key");
144 exit(1);
147 read(STDIN, &ekeylen, sizeof(ekeylen));
148 ekeylen = ntohl(ekeylen);
150 if (ekeylen != EVP_PKEY_size(privateKey))
152 EVP_PKEY_free(privateKey);
153 fprintf(stderr, "keylength mismatch");
154 exit(1);
157 encryptKey = malloc(sizeof(char) * ekeylen);
158 if (!encryptKey)
160 EVP_PKEY_free(privateKey);
161 perror("malloc");
162 exit(1);
165 read(STDIN, encryptKey, ekeylen);
166 read(STDIN, iv, sizeof(iv));
167 EVP_OpenInit(&ectx,
168 EVP_des_ede3_cbc(),
169 encryptKey,
170 ekeylen,
172 privateKey);
174 while(1)
176 int readlen = read(STDIN, ebuf, sizeof(ebuf));
178 if (readlen <= 0)
180 if (readlen < 0)
181 perror("read");
183 break;
186 EVP_OpenUpdate(&ectx, buf, &buflen, ebuf, readlen);
187 write(STDOUT, buf, buflen);
190 EVP_OpenFinal(&ectx, buf, &buflen);
192 write(STDOUT, buf, buflen);
194 EVP_PKEY_free(privateKey);
195 free(encryptKey);