| 1 |
|
| #include "EXTERN.h"
|
| 2 |
|
| #include "perl.h"
|
| 3 |
|
| #include "XSUB.h"
|
| 4 |
|
|
|
| 5 |
|
| #include "ppport.h"
|
| 6 |
|
| #include "openssl/buffer.h"
|
| 7 |
|
| #include "openssl/bio.h"
|
| 8 |
|
| #include "openssl/sha.h"
|
| 9 |
|
| #include "openssl/rand.h"
|
| 10 |
|
| #include "openssl/err.h"
|
| 11 |
|
| #include "openssl/rsa.h"
|
| 12 |
|
| #include "openssl/evp.h"
|
| 13 |
|
| #include "openssl/x509.h"
|
| 14 |
|
| #include "openssl/x509v3.h"
|
| 15 |
|
| #include "openssl/pkcs7.h"
|
| 16 |
|
| #include "openssl/pem.h"
|
| 17 |
|
|
|
| 18 |
|
| /* The following code comes directly from PayPal's ButtonEncyption.cpp file, and has been
|
| 19 |
|
| modified only to work with C
|
| 20 |
|
| */
|
| 21 |
|
|
|
| 22 |
1 |
| char* sign_and_encrypt(const char *data, RSA *rsa, X509 *x509, X509 *PPx509, bool verbose)
|
| 23 |
|
| {
|
| 24 |
1 |
| char *ret = NULL;
|
| 25 |
1 |
| EVP_PKEY *pkey = NULL;
|
| 26 |
1 |
| PKCS7 *p7 = NULL;
|
| 27 |
1 |
| BIO *memBio = NULL;
|
| 28 |
1 |
| BIO *p7bio = NULL;
|
| 29 |
1 |
| BIO *bio = NULL;
|
| 30 |
1 |
| PKCS7_SIGNER_INFO* si = NULL;
|
| 31 |
1 |
| int len = 0;
|
| 32 |
1 |
| char *str = NULL;
|
| 33 |
|
|
|
| 34 |
1 |
| pkey = EVP_PKEY_new();
|
| 35 |
|
|
|
| 36 |
1 |
▶ | if (EVP_PKEY_set1_RSA(pkey, rsa) == 0)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (EVP_PKEY_set1_RSA(pkey, rsa) == 0)
|
0 |
1 |
1 |
|
| 37 |
|
| {
|
| 38 |
0 |
| printf("Fatal Error: Unable to create EVP_KEY from RSA key\n");
|
| 39 |
0 |
| goto end;
|
| 40 |
1 |
▶ | } else if (verbose) {
|
Branch: 1 branch
| Branch | True | False | Total |
| } else if (verbose) {
|
0 |
1 |
1 |
|
| 41 |
0 |
| printf("Successfully created EVP_KEY from RSA key\n");
|
| 42 |
|
| }
|
| 43 |
|
|
|
| 44 |
|
| // Create a signed and enveloped PKCS7
|
| 45 |
1 |
| p7 = PKCS7_new();
|
| 46 |
1 |
| PKCS7_set_type(p7, NID_pkcs7_signedAndEnveloped);
|
| 47 |
|
|
|
| 48 |
1 |
| si = PKCS7_add_signature(p7, x509, pkey, EVP_sha1());
|
| 49 |
|
|
|
| 50 |
1 |
▶ | if (si) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (si) {
|
1 |
0 |
1 |
|
| 51 |
1 |
▶ | if (PKCS7_add_signed_attribute(si, NID_pkcs9_contentType, V_ASN1_OBJECT,
|
Branch: 1 branch
| Branch | True | False | Total |
| if (PKCS7_add_signed_attribute(si, NID_pkcs9_contentType, V_ASN1_OBJECT,
|
0 |
1 |
1 |
|
| 52 |
1 |
| OBJ_nid2obj(NID_pkcs7_data)) <= 0)
|
| 53 |
|
| {
|
| 54 |
0 |
| printf("Fatal Error: Unable to add signed attribute to certificate\n");
|
| 55 |
0 |
| printf("OpenSSL Error: %s\n", ERR_error_string(ERR_get_error(), NULL));
|
| 56 |
0 |
| goto end;
|
| 57 |
1 |
▶ | } else if (verbose) {
|
Branch: 1 branch
| Branch | True | False | Total |
| } else if (verbose) {
|
0 |
1 |
1 |
|
| 58 |
0 |
| printf("Successfully added signed attribute to certificate\n");
|
| 59 |
|
| }
|
| 60 |
|
|
|
| 61 |
|
| } else {
|
| 62 |
0 |
| printf("Fatal Error: Failed to sign PKCS7\n");
|
| 63 |
0 |
| goto end;
|
| 64 |
|
| }
|
| 65 |
|
|
|
| 66 |
|
| //Encryption
|
| 67 |
1 |
▶ | if (PKCS7_set_cipher(p7, EVP_des_ede3_cbc()) <= 0)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (PKCS7_set_cipher(p7, EVP_des_ede3_cbc()) <= 0)
|
0 |
1 |
1 |
|
| 68 |
|
| {
|
| 69 |
0 |
| printf("Fatal Error: Failed to set encryption algorithm\n");
|
| 70 |
0 |
| printf("OpenSSL Error: %s\n", ERR_error_string(ERR_get_error(), NULL));
|
| 71 |
0 |
| goto end;
|
| 72 |
1 |
▶ | } else if (verbose) {
|
Branch: 1 branch
| Branch | True | False | Total |
| } else if (verbose) {
|
0 |
1 |
1 |
|
| 73 |
0 |
| printf("Successfully added encryption algorithm\n");
|
| 74 |
|
| }
|
| 75 |
|
|
|
| 76 |
1 |
▶ | if (PKCS7_add_recipient(p7, PPx509) <= 0)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (PKCS7_add_recipient(p7, PPx509) <= 0)
|
0 |
1 |
1 |
|
| 77 |
|
| {
|
| 78 |
0 |
| printf("Fatal Error: Failed to add PKCS7 recipient\n");
|
| 79 |
0 |
| printf("OpenSSL Error: %s\n", ERR_error_string(ERR_get_error(), NULL));
|
| 80 |
0 |
| goto end;
|
| 81 |
1 |
▶ | } else if (verbose) {
|
Branch: 1 branch
| Branch | True | False | Total |
| } else if (verbose) {
|
0 |
1 |
1 |
|
| 82 |
0 |
| printf("Successfully added recipient\n");
|
| 83 |
|
| }
|
| 84 |
|
|
|
| 85 |
1 |
▶ | if (PKCS7_add_certificate(p7, x509) <= 0)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (PKCS7_add_certificate(p7, x509) <= 0)
|
0 |
1 |
1 |
|
| 86 |
|
| {
|
| 87 |
0 |
| printf("Fatal Error: Failed to add PKCS7 certificate\n");
|
| 88 |
0 |
| printf("OpenSSL Error: %s\n", ERR_error_string(ERR_get_error(), NULL));
|
| 89 |
0 |
| goto end;
|
| 90 |
1 |
▶ | } else if (verbose) {
|
Branch: 1 branch
| Branch | True | False | Total |
| } else if (verbose) {
|
0 |
1 |
1 |
|
| 91 |
0 |
| printf("Successfully added certificate\n");
|
| 92 |
|
| }
|
| 93 |
|
|
|
| 94 |
1 |
| memBio = BIO_new(BIO_s_mem());
|
| 95 |
1 |
| p7bio = PKCS7_dataInit(p7, memBio);
|
| 96 |
|
|
|
| 97 |
1 |
▶ | if (!p7bio) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (!p7bio) {
|
0 |
1 |
1 |
|
| 98 |
0 |
| printf("OpenSSL Error: %s\n", ERR_error_string(ERR_get_error(), NULL));
|
| 99 |
0 |
| goto end;
|
| 100 |
|
| }
|
| 101 |
|
| /* p7bio now owns memBio, so don't try to free it */
|
| 102 |
1 |
| memBio = NULL; // RT #6150, RT #48877
|
| 103 |
|
|
|
| 104 |
|
| //Pump data to special PKCS7 BIO. This encrypts and signs it.
|
| 105 |
1 |
| BIO_write(p7bio, data, strlen(data));
|
| 106 |
1 |
| BIO_flush(p7bio);
|
| 107 |
1 |
| PKCS7_dataFinal(p7, p7bio);
|
| 108 |
|
|
|
| 109 |
|
| //Write PEM encoded PKCS7
|
| 110 |
1 |
| bio = BIO_new(BIO_s_mem());
|
| 111 |
|
|
|
| 112 |
1 |
▶ | if (!bio || (PEM_write_bio_PKCS7(bio, p7) == 0))
|
Branch: 2 branches
| Branch | True | False | Total |
| if (!bio || (PEM_write_bio_PKCS7(bio, p7) == 0))
|
1 |
0 |
1 |
| if (!bio || (PEM_write_bio_PKCS7(bio, p7) == 0))
|
0 |
1 |
1 |
|
| 113 |
|
| {
|
| 114 |
0 |
| printf("Fatal Error: Failed to create PKCS7 PEM\n");
|
| 115 |
1 |
▶ | } else if (verbose) {
|
Branch: 1 branch
| Branch | True | False | Total |
| } else if (verbose) {
|
0 |
1 |
1 |
|
| 116 |
0 |
| printf("Successfully created PKCS7 PEM\n");
|
| 117 |
|
| }
|
| 118 |
|
|
|
| 119 |
1 |
| BIO_flush(bio);
|
| 120 |
|
|
|
| 121 |
1 |
| len = BIO_get_mem_data(bio, &str);
|
| 122 |
1 |
| Newz(1,ret,sizeof(char)*(len+1),char);
|
| 123 |
1 |
| memcpy(ret, str, len);
|
| 124 |
1 |
| ret[len] = 0;
|
| 125 |
|
|
|
| 126 |
1 |
| end:
|
| 127 |
|
| //Free everything
|
| 128 |
1 |
▶ | if (p7)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (p7)
|
1 |
0 |
1 |
|
| 129 |
1 |
| PKCS7_free(p7);
|
| 130 |
1 |
▶ | if (bio)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (bio)
|
1 |
0 |
1 |
|
| 131 |
1 |
| BIO_free_all(bio);
|
| 132 |
1 |
▶ | if (memBio)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (memBio)
|
0 |
1 |
1 |
|
| 133 |
0 |
| BIO_free_all(memBio);
|
| 134 |
1 |
▶ | if (p7bio)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (p7bio)
|
1 |
0 |
1 |
|
| 135 |
1 |
| BIO_free_all(p7bio);
|
| 136 |
1 |
▶ | if (pkey)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (pkey)
|
1 |
0 |
1 |
|
| 137 |
1 |
| EVP_PKEY_free(pkey);
|
| 138 |
1 |
| return ret;
|
| 139 |
|
| }
|
| 140 |
|
|
|
| 141 |
|
|
|
| 142 |
|
|
|
| 143 |
1 |
| char* SignAndEncryptCImpl(char* sCmdTxt, char* keyPath, char* certPath, char* payPalCertPath, bool verbose)
|
| 144 |
|
| {
|
| 145 |
|
| BIO *bio;
|
| 146 |
1 |
| X509 *x509 = NULL;
|
| 147 |
1 |
| X509 *PPx509 = NULL;
|
| 148 |
1 |
| RSA *rsa = NULL;
|
| 149 |
1 |
| char *enc = NULL;
|
| 150 |
|
|
|
| 151 |
1 |
| ERR_load_crypto_strings();
|
| 152 |
1 |
| OpenSSL_add_all_algorithms();
|
| 153 |
|
|
|
| 154 |
|
| // Load the PayPal Cert File
|
| 155 |
1 |
▶ | if ((bio = BIO_new_file(payPalCertPath, "rt")) == NULL) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ((bio = BIO_new_file(payPalCertPath, "rt")) == NULL) {
|
0 |
1 |
1 |
|
| 156 |
0 |
| printf("Fatal Error: Failed to open %s\n", payPalCertPath);
|
| 157 |
0 |
| goto end;
|
| 158 |
|
| }
|
| 159 |
1 |
▶ | if ((PPx509 = PEM_read_bio_X509(bio, NULL, NULL, NULL)) == NULL) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ((PPx509 = PEM_read_bio_X509(bio, NULL, NULL, NULL)) == NULL) {
|
0 |
1 |
1 |
|
| 160 |
0 |
| printf("Fatal Error: Failed to read Paypal certificate from %s\n", payPalCertPath);
|
| 161 |
0 |
| goto end;
|
| 162 |
|
| }
|
| 163 |
1 |
| BIO_free(bio);
|
| 164 |
|
|
|
| 165 |
|
| // Load the User Cert File
|
| 166 |
1 |
▶ | if ((bio = BIO_new_file(certPath, "rt")) == NULL) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ((bio = BIO_new_file(certPath, "rt")) == NULL) {
|
0 |
1 |
1 |
|
| 167 |
0 |
| printf("Fatal Error: Failed to open %s\n", certPath);
|
| 168 |
0 |
| goto end;
|
| 169 |
|
| }
|
| 170 |
1 |
▶ | if ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL)) == NULL) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ((x509 = PEM_read_bio_X509(bio, NULL, NULL, NULL)) == NULL) {
|
0 |
1 |
1 |
|
| 171 |
0 |
| printf("Fatal Error: Failed to read certificate from %s\n", certPath);
|
| 172 |
0 |
| goto end;
|
| 173 |
|
| }
|
| 174 |
1 |
| BIO_free(bio);
|
| 175 |
|
|
|
| 176 |
|
| // Load the User Key File
|
| 177 |
1 |
▶ | if ((bio = BIO_new_file(keyPath, "rt")) == NULL)
|
Branch: 1 branch
| Branch | True | False | Total |
| if ((bio = BIO_new_file(keyPath, "rt")) == NULL)
|
0 |
1 |
1 |
|
| 178 |
|
| {
|
| 179 |
0 |
| printf("Fatal Error: Failed to open %s\n", keyPath);
|
| 180 |
0 |
| goto end;
|
| 181 |
|
| }
|
| 182 |
1 |
▶ | if ((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)) == NULL)
|
Branch: 1 branch
| Branch | True | False | Total |
| if ((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)) == NULL)
|
0 |
1 |
1 |
|
| 183 |
|
| {
|
| 184 |
0 |
| printf("Fatal Error: Unable to read RSA key %s\n", keyPath);
|
| 185 |
0 |
| goto end;
|
| 186 |
|
| }
|
| 187 |
1 |
| BIO_free(bio);
|
| 188 |
1 |
| bio = NULL;
|
| 189 |
|
|
|
| 190 |
|
| // Process payload into blob.
|
| 191 |
1 |
| enc = sign_and_encrypt(sCmdTxt, rsa, x509, PPx509, verbose);
|
| 192 |
|
|
|
| 193 |
1 |
| end:
|
| 194 |
1 |
▶ | if (bio) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (bio) {
|
0 |
1 |
1 |
|
| 195 |
0 |
| BIO_free_all(bio);
|
| 196 |
|
| }
|
| 197 |
1 |
▶ | if (x509) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (x509) {
|
1 |
0 |
1 |
|
| 198 |
1 |
| X509_free(x509);
|
| 199 |
|
| }
|
| 200 |
1 |
▶ | if (PPx509) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (PPx509) {
|
1 |
0 |
1 |
|
| 201 |
1 |
| X509_free(PPx509);
|
| 202 |
|
| }
|
| 203 |
1 |
▶ | if (rsa) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (rsa) {
|
1 |
0 |
1 |
|
| 204 |
1 |
| RSA_free(rsa);
|
| 205 |
|
| }
|
| 206 |
1 |
| return enc;
|
| 207 |
|
| }
|
| 208 |
|
|
|
| 209 |
|
|
|
| 210 |
|
|
|
| 211 |
|
| MODULE = Business::PayPal::EWP PACKAGE = Business::PayPal::EWP
|
| 212 |
|
| PROTOTYPES: DISABLE
|
| 213 |
|
|
|
| 214 |
|
| char *
|
| 215 |
|
| SignAndEncryptCImpl(sCmdTxt,certPath,keyPath,payPalCertPath,verbose)
|
| 216 |
|
| char* sCmdTxt
|
| 217 |
|
| char* certPath
|
| 218 |
|
| char* keyPath
|
| 219 |
|
| char* payPalCertPath
|
| 220 |
|
| bool verbose
|
| 221 |
|
|
|