pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

C pastebin - collaborative debugging tool View Help


Posted by vchav on Mon 8 Sep 15:32
report abuse | download | new post

  1. #include "openssl/rsa.h"
  2. #include "openssl/pem.h"
  3. #include "openssl/err.h"
  4. #include <string.h>
  5. #include <sys/mman.h>
  6. #include <sys/stat.h>
  7. #include <fcntl.h>
  8.  
  9.  
  10. #define PRIVATE_KEY             0
  11. #define PRIVATE_KEY_DATA "-----BEGIN RSA PRIVATE KEY-----\nMIICXAIBAAKBgQC63DnkIOVCRGA6yZeAERke4rsq6WgmCyDncvew5VUfXO5DT3YY\nEMPm2fkLkApjGiUynyOsPlMBy7pKw1kr1os5hlHaAt6bRbb/7pr1fCBK7M5fY+ol\nQ3onYKFU0VcFBgz2XyF7RJsIPkC7N+3s4do9gmZpAqbiByXxRswo/AbnVQIDAQAB\nAoGBAIJrn4RYaWt9KeUeOz3JsUzbV2O2EVksP0UL+1FeX7FKPYqBdZ2KZhFEZgzp\n9jYBU6EnbdCUl38TYO05t41fa0AC2XWJn+xWzPrba3ipZmzuWncv2eSi5xxDenwc\nPvl1Lcn1EF/YrcKSMuMPRZqLTCP1wx2+7nWEMsCpFY1JIrfBAkEA7eqkmvDR2xyU\nw6y2jHpDJ68WUULmRrSSHC+d7HKSJaiUPm5yOGS+f5PWVDVvi+rIZw/sGbqiAU8e\nRmdhlrsOOQJBAMkQImqNE96f6pmm7vSrEMD5EC0NU09P3mOrSI76d6qXLtXtRL8U\n8psbOtGmrqXfnKzjbKgz2b8p6BoGYmr4of0CQHo8DrwINFmV0pzB9Lwx6KTP4PB5\nJaR4C4VttX6Q0qOEfD2jMw3kPLeBNiHnnlrNko7Y8F27tJZlltFnNg1iJ4kCQEHJ\n6bj7mHjL0rOcD6w3HTBHTqevKIdXFul97iv6gJVtCoItNMVhUVC3RDO9WoAj/twD\nPlZ7QNBwIeYCGMnvuPECQBNdjWiKAKbsYWx4m4W2Kf6GOMI5QXchdEAl3EyNFBHd\nxiqeNlgl+kEEwhWTC7Z/4zOv4rPMJfmKWiVjG1kll30=\n-----END RSA PRIVATE KEY-----"
  12.  
  13. #define PUBLIC_KEY              1
  14. #define PUBLIC_KEY_DATA "-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC63DnkIOVCRGA6yZeAERke4rsq\n6WgmCyDncvew5VUfXO5DT3YYEMPm2fkLkApjGiUynyOsPlMBy7pKw1kr1os5hlHa\nAt6bRbb/7pr1fCBK7M5fY+olQ3onYKFU0VcFBgz2XyF7RJsIPkC7N+3s4do9gmZp\nAqbiByXxRswo/AbnVQIDAQAB\n-----END PUBLIC KEY-----"
  15.  
  16.  
  17. #define PUBLIC_CERTIFICATE      2
  18. #define PUBLIC_CERTIFICATE_DATA "-----BEGIN CERTIFICATE-----\nMIICATCCAWoCCQDz/9kR2AdCuzANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJB\nVTETMBEGA1UECBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0\ncyBQdHkgTHRkMB4XDTA4MDUxNDE3MDE0NVoXDTE4MDUxMjE3MDE0NVowRTELMAkG\nA1UEBhMCQVUxEzARBgNVBAgTClNvbWUtU3RhdGUxITAfBgNVBAoTGEludGVybmV0\nIFdpZGdpdHMgUHR5IEx0ZDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAutw5\n5CDlQkRgOsmXgBEZHuK7KuloJgsg53L3sOVVH1zuQ092GBDD5tn5C5AKYxolMp8j\nrD5TAcu6SsNZK9aLOYZR2gLem0W2/+6a9XwgSuzOX2PqJUN6J2ChVNFXBQYM9l8h\ne0SbCD5Auzft7OHaPYJmaQKm4gcl8UbMKPwG51UCAwEAATANBgkqhkiG9w0BAQUF\nAAOBgQAJ2sY6JDVPQ2jfjrTRrNyJdO4jobk+2AysI1CtsLLczsPNaokEgj71Y1ui\nL2/3Etli1RMT2/9ni4boRCrWmvJTf41fxzX/ETATDOYRGfQZb20rutxLDmXB2ene\nLDE5ha8P/JkJ/fiZYtG2A8ZzpRdC5B16w2rrH5OSDQrIyi+3xA==\n-----END CERTIFICATE-----"
  19.  
  20. #define MAN_OAEP_PADDING        42
  21.  
  22. typedef struct {
  23.   size_t        len;
  24.   u_char        *data;
  25. } ngx_str_t;
  26.  
  27. RSA     *get_RSA_from_str(char *str, int cert_type, char *passphrase)
  28. {
  29.   EVP_PKEY      *pub;
  30.   BIO           *mbio;
  31.  
  32.   mbio = BIO_new_mem_buf((void *)str, strlen(str));
  33.   if (cert_type == PUBLIC_CERTIFICATE) {
  34.     X509 *x509 = PEM_read_bio_X509(mbio, NULL, NULL, NULL);
  35.     if (x509 == NULL) {
  36.       printf("Error reading x509 bio\n");
  37.       ERR_print_errors_fp(stderr);
  38.       return NULL;
  39.     }
  40.     pub = X509_get_pubkey(x509);
  41.     X509_free(x509);
  42.   }
  43.   else if (cert_type == PRIVATE_KEY)
  44.     pub = PEM_read_bio_PrivateKey(mbio, NULL, NULL, passphrase);
  45.   else if (cert_type == PUBLIC_KEY)
  46.     pub = PEM_read_bio_PUBKEY(mbio, NULL, NULL, passphrase);
  47.   BIO_reset(mbio);
  48.   BIO_free(mbio);
  49.   if (pub == NULL) {
  50.     printf("Error reading the %d key \n", cert_type);
  51.     ERR_print_errors_fp (stderr);
  52.     return NULL;
  53.   }
  54.   RSA *tmp = EVP_PKEY_get1_RSA(pub);
  55.   EVP_PKEY_free(pub);
  56.   return (tmp);
  57. }
  58.  
  59. int
  60. public_encrypt(RSA *pkey, unsigned char *data_source, int length,
  61.                ngx_str_t *output)
  62. {
  63.   int size = RSA_size(pkey);
  64.   int block_size = size - MAN_OAEP_PADDING;
  65.   int blocks = length / block_size;
  66.   int rest = length % block_size;
  67.   int i;
  68.   int el;
  69.  
  70.   output->data = malloc((blocks + ((rest == 0) ? 0 : 1)) * size + 1);
  71.   if (output->data == NULL)
  72.     return 0;
  73.   for (i = 0, output->len = 0; i < blocks || (i == blocks && rest != 0); i++) {
  74.     if (blocks == i)
  75.       el = RSA_public_encrypt(rest, data_source + i * block_size,
  76.                               output->data + i * size, pkey,
  77.                               RSA_PKCS1_OAEP_PADDING);
  78.     else
  79.       el = RSA_public_encrypt(block_size, data_source + i * block_size,
  80.                               output->data + i * size, pkey,
  81.                               RSA_PKCS1_OAEP_PADDING);
  82.     if (el < 1)
  83.       return 0;
  84.     output->len += el;
  85.   }
  86.   return 1;
  87. }
  88.  
  89. int
  90. private_decrypt(RSA *pkey, unsigned char *data_source, int length,
  91.                 ngx_str_t *output)
  92. {
  93.   int size = RSA_size(pkey);
  94.   int block_size = size - MAN_OAEP_PADDING;
  95.   int blocks = length / size;
  96.   int i;
  97.   int el;
  98.  
  99.   output->data = malloc(blocks * block_size + 1);
  100.   if (output->data == NULL)
  101.     return 0;
  102.  
  103.   memset(output->data, 0, blocks * block_size);
  104.   for (i = 0, output->len = 0; i < blocks; i++) {
  105.     el = RSA_private_decrypt(size, data_source + i * size, output->data +
  106.                              output->len, pkey, RSA_PKCS1_OAEP_PADDING);
  107.     if (el < 1)
  108.       return 0;
  109.     output->len += el;
  110.   }
  111.   return 1;
  112. }
  113.    
  114. int     main()
  115. {
  116. /*   char               pubkey[] = PUBLIC_KEY_DATA; */
  117.   char          pubcert[] = PUBLIC_CERTIFICATE_DATA;
  118.   char          privkey[] = PRIVATE_KEY_DATA;
  119.   ngx_str_t     out_priv;
  120.   ngx_str_t     out_pub;
  121.   ngx_str_t     data;
  122.  
  123.   RSA *pubk = get_RSA_from_str(pubcert, PUBLIC_CERTIFICATE, NULL);
  124. /*   RSA *pubk = get_RSA_from_str(pubkey, PUBLIC_KEY, NULL); */
  125.   RSA *subk = get_RSA_from_str(privkey, PRIVATE_KEY, NULL);
  126.   if (pubk == NULL || subk == NULL) {
  127.     printf("Error setting key from EVP_PKEY_set1_RSA function\n");
  128.     exit(EXIT_FAILURE);
  129.   }
  130.  
  131.   data.data = (u_char *)"La peur bloque la compréhension intelligente de la vie.";
  132.   data.len = strlen((char *)data.data);
  133.  
  134.   public_encrypt(pubk, data.data, data.len, &out_pub);
  135.   private_decrypt(subk, out_pub.data, out_pub.len, &out_priv);
  136.   printf("%s\n", out_priv.data);
  137.  
  138.   free(out_priv.data);
  139.   free(out_pub.data);
  140.  
  141.   ERR_print_errors_fp(stdout);
  142.   RSA_free(pubk);
  143.   RSA_free(subk);
  144.  
  145.   return (EXIT_SUCCESS);
  146. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post