example1.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* NOCW */
  2. /*
  3. Please read the README file for condition of use, before
  4. using this software.
  5. Maurice Gittens <mgittens@gits.nl> January 1997
  6. */
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <netinet/in.h>
  10. #include <fcntl.h>
  11. #include <strings.h>
  12. #include <stdlib.h>
  13. #include <openssl/rsa.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/objects.h>
  16. #include <openssl/x509.h>
  17. #include <openssl/err.h>
  18. #include <openssl/pem.h>
  19. #include <openssl/ssl.h>
  20. #include "loadkeys.h"
  21. #define PUBFILE "cert.pem"
  22. #define PRIVFILE "privkey.pem"
  23. #define STDIN 0
  24. #define STDOUT 1
  25. void main_encrypt(void);
  26. void main_decrypt(void);
  27. static const char *usage = "Usage: example1 [-d]\n";
  28. int main(int argc, char *argv[])
  29. {
  30. ERR_load_crypto_strings();
  31. if ((argc == 1))
  32. {
  33. main_encrypt();
  34. }
  35. else if ((argc == 2) && !strcmp(argv[1],"-d"))
  36. {
  37. main_decrypt();
  38. }
  39. else
  40. {
  41. printf("%s",usage);
  42. exit(1);
  43. }
  44. return 0;
  45. }
  46. void main_encrypt(void)
  47. {
  48. unsigned int ebuflen;
  49. EVP_CIPHER_CTX ectx;
  50. unsigned char iv[EVP_MAX_IV_LENGTH];
  51. unsigned char *ekey[1];
  52. int readlen;
  53. int ekeylen, net_ekeylen;
  54. EVP_PKEY *pubKey[1];
  55. char buf[512];
  56. char ebuf[512];
  57. memset(iv, '\0', sizeof(iv));
  58. pubKey[0] = ReadPublicKey(PUBFILE);
  59. if(!pubKey[0])
  60. {
  61. fprintf(stderr,"Error: can't load public key");
  62. exit(1);
  63. }
  64. ekey[0] = malloc(EVP_PKEY_size(pubKey[0]));
  65. if (!ekey[0])
  66. {
  67. EVP_PKEY_free(pubKey[0]);
  68. perror("malloc");
  69. exit(1);
  70. }
  71. EVP_SealInit(&ectx,
  72. EVP_des_ede3_cbc(),
  73. ekey,
  74. &ekeylen,
  75. iv,
  76. pubKey,
  77. 1);
  78. net_ekeylen = htonl(ekeylen);
  79. write(STDOUT, (char*)&net_ekeylen, sizeof(net_ekeylen));
  80. write(STDOUT, ekey[0], ekeylen);
  81. write(STDOUT, iv, sizeof(iv));
  82. while(1)
  83. {
  84. readlen = read(STDIN, buf, sizeof(buf));
  85. if (readlen <= 0)
  86. {
  87. if (readlen < 0)
  88. perror("read");
  89. break;
  90. }
  91. EVP_SealUpdate(&ectx, ebuf, &ebuflen, buf, readlen);
  92. write(STDOUT, ebuf, ebuflen);
  93. }
  94. EVP_SealFinal(&ectx, ebuf, &ebuflen);
  95. write(STDOUT, ebuf, ebuflen);
  96. EVP_PKEY_free(pubKey[0]);
  97. free(ekey[0]);
  98. }
  99. void main_decrypt(void)
  100. {
  101. char buf[520];
  102. char ebuf[512];
  103. unsigned int buflen;
  104. EVP_CIPHER_CTX ectx;
  105. unsigned char iv[EVP_MAX_IV_LENGTH];
  106. unsigned char *encryptKey;
  107. unsigned int ekeylen;
  108. EVP_PKEY *privateKey;
  109. memset(iv, '\0', sizeof(iv));
  110. privateKey = ReadPrivateKey(PRIVFILE);
  111. if (!privateKey)
  112. {
  113. fprintf(stderr, "Error: can't load private key");
  114. exit(1);
  115. }
  116. read(STDIN, &ekeylen, sizeof(ekeylen));
  117. ekeylen = ntohl(ekeylen);
  118. if (ekeylen != EVP_PKEY_size(privateKey))
  119. {
  120. EVP_PKEY_free(privateKey);
  121. fprintf(stderr, "keylength mismatch");
  122. exit(1);
  123. }
  124. encryptKey = malloc(sizeof(char) * ekeylen);
  125. if (!encryptKey)
  126. {
  127. EVP_PKEY_free(privateKey);
  128. perror("malloc");
  129. exit(1);
  130. }
  131. read(STDIN, encryptKey, ekeylen);
  132. read(STDIN, iv, sizeof(iv));
  133. EVP_OpenInit(&ectx,
  134. EVP_des_ede3_cbc(),
  135. encryptKey,
  136. ekeylen,
  137. iv,
  138. privateKey);
  139. while(1)
  140. {
  141. int readlen = read(STDIN, ebuf, sizeof(ebuf));
  142. if (readlen <= 0)
  143. {
  144. if (readlen < 0)
  145. perror("read");
  146. break;
  147. }
  148. EVP_OpenUpdate(&ectx, buf, &buflen, ebuf, readlen);
  149. write(STDOUT, buf, buflen);
  150. }
  151. EVP_OpenFinal(&ectx, buf, &buflen);
  152. write(STDOUT, buf, buflen);
  153. EVP_PKEY_free(privateKey);
  154. free(encryptKey);
  155. }