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#include <unistd.h>
10#include <stdio.h>
11#include <netinet/in.h>
12#include <fcntl.h>
13#include <strings.h>
14#include <stdlib.h>
15
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>
23
24#include "loadkeys.h"
25
26#define PUBFILE   "cert.pem"
27#define PRIVFILE  "privkey.pem"
28
29#define STDIN     0
30#define STDOUT    1
31
32void main_encrypt(void);
33void main_decrypt(void);
34
35static const char *usage = "Usage: example1 [-d]\n";
36
37int main(int argc, char *argv[])
38{
39
40        ERR_load_crypto_strings();
41
42	if ((argc == 1))
43	{
44		main_encrypt();
45	}
46	else if ((argc == 2) && !strcmp(argv[1],"-d"))
47	{
48		main_decrypt();
49	}
50	else
51	{
52		printf("%s",usage);
53		exit(1);
54	}
55
56	return 0;
57}
58
59void main_encrypt(void)
60{
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];
70
71 	memset(iv, '\0', sizeof(iv));
72
73        pubKey[0] = ReadPublicKey(PUBFILE);
74
75	if(!pubKey[0])
76	{
77           fprintf(stderr,"Error: can't load public key");
78           exit(1);
79        }
80
81        ekey[0] = malloc(EVP_PKEY_size(pubKey[0]));
82        if (!ekey[0])
83	{
84	   EVP_PKEY_free(pubKey[0]);
85	   perror("malloc");
86	   exit(1);
87	}
88
89	EVP_SealInit(&ectx,
90                   EVP_des_ede3_cbc(),
91		   ekey,
92		   &ekeylen,
93		   iv,
94		   pubKey,
95		   1);
96
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));
101
102	while(1)
103	{
104		readlen = read(STDIN, buf, sizeof(buf));
105
106		if (readlen <= 0)
107		{
108		   if (readlen < 0)
109			perror("read");
110
111		   break;
112		}
113
114		EVP_SealUpdate(&ectx, ebuf, &ebuflen, buf, readlen);
115
116		write(STDOUT, ebuf, ebuflen);
117	}
118
119        EVP_SealFinal(&ectx, ebuf, &ebuflen);
120
121	write(STDOUT, ebuf, ebuflen);
122
123        EVP_PKEY_free(pubKey[0]);
124	free(ekey[0]);
125}
126
127void main_decrypt(void)
128{
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;
137
138	memset(iv, '\0', sizeof(iv));
139
140	privateKey = ReadPrivateKey(PRIVFILE);
141	if (!privateKey)
142	{
143		fprintf(stderr, "Error: can't load private key");
144		exit(1);
145	}
146
147     	read(STDIN, &ekeylen, sizeof(ekeylen));
148	ekeylen = ntohl(ekeylen);
149
150	if (ekeylen != EVP_PKEY_size(privateKey))
151	{
152        	EVP_PKEY_free(privateKey);
153		fprintf(stderr, "keylength mismatch");
154		exit(1);
155	}
156
157	encryptKey = malloc(sizeof(char) * ekeylen);
158	if (!encryptKey)
159	{
160        	EVP_PKEY_free(privateKey);
161		perror("malloc");
162		exit(1);
163	}
164
165	read(STDIN, encryptKey, ekeylen);
166	read(STDIN, iv, sizeof(iv));
167	EVP_OpenInit(&ectx,
168		   EVP_des_ede3_cbc(),
169		   encryptKey,
170		   ekeylen,
171		   iv,
172		   privateKey);
173
174	while(1)
175	{
176		int readlen = read(STDIN, ebuf, sizeof(ebuf));
177
178		if (readlen <= 0)
179		{
180			if (readlen < 0)
181				perror("read");
182
183			break;
184		}
185
186		EVP_OpenUpdate(&ectx, buf, &buflen, ebuf, readlen);
187		write(STDOUT, buf, buflen);
188	}
189
190        EVP_OpenFinal(&ectx, buf, &buflen);
191
192	write(STDOUT, buf, buflen);
193
194        EVP_PKEY_free(privateKey);
195	free(encryptKey);
196}
197
198
199