3 Please read the README file for condition of use, before
6 Maurice Gittens <mgittens@gits.nl> January 1997
11 #include <netinet/in.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>
26 #define PUBFILE "cert.pem"
27 #define PRIVFILE "privkey.pem"
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();
46 else if ((argc
== 2) && !strcmp(argv
[1],"-d"))
59 void main_encrypt(void)
63 unsigned char iv
[EVP_MAX_IV_LENGTH
];
64 unsigned char *ekey
[1];
66 int ekeylen
, net_ekeylen
;
71 memset(iv
, '\0', sizeof(iv
));
73 pubKey
[0] = ReadPublicKey(PUBFILE
);
77 fprintf(stderr
,"Error: can't load public key");
81 ekey
[0] = malloc(EVP_PKEY_size(pubKey
[0]));
84 EVP_PKEY_free(pubKey
[0]);
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
));
104 readlen
= read(STDIN
, buf
, sizeof(buf
));
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]);
127 void main_decrypt(void)
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
);
143 fprintf(stderr
, "Error: can't load private key");
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");
157 encryptKey
= malloc(sizeof(char) * ekeylen
);
160 EVP_PKEY_free(privateKey
);
165 read(STDIN
, encryptKey
, ekeylen
);
166 read(STDIN
, iv
, sizeof(iv
));
176 int readlen
= read(STDIN
, ebuf
, sizeof(ebuf
));
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
);