예제 #1
0
void ERR_load_crypto_strings(void)
{
#ifndef OPENSSL_NO_ERR
    ERR_load_ERR_strings();     /* include error strings for SYSerr */
    ERR_load_BN_strings();
# ifndef OPENSSL_NO_RSA
    ERR_load_RSA_strings();
# endif
# ifndef OPENSSL_NO_DH
    ERR_load_DH_strings();
# endif
    ERR_load_EVP_strings();
    ERR_load_BUF_strings();
    ERR_load_OBJ_strings();
    ERR_load_PEM_strings();
# ifndef OPENSSL_NO_DSA
    ERR_load_DSA_strings();
# endif
    ERR_load_X509_strings();
    ERR_load_ASN1_strings();
    ERR_load_CONF_strings();
    ERR_load_CRYPTO_strings();
# ifndef OPENSSL_NO_COMP
    ERR_load_COMP_strings();
# endif
# ifndef OPENSSL_NO_EC
    ERR_load_EC_strings();
# endif
# ifndef OPENSSL_NO_ECDSA
    ERR_load_ECDSA_strings();
# endif
# ifndef OPENSSL_NO_ECDH
    ERR_load_ECDH_strings();
# endif
    /* skip ERR_load_SSL_strings() because it is not in this library */
    ERR_load_BIO_strings();
    ERR_load_PKCS7_strings();
    ERR_load_X509V3_strings();
    ERR_load_PKCS12_strings();
    ERR_load_RAND_strings();
    ERR_load_DSO_strings();
   // ERR_load_TS_strings();
# ifndef OPENSSL_NO_ENGINE
    ERR_load_ENGINE_strings();
# endif
    ERR_load_OCSP_strings();
    ERR_load_UI_strings();
# ifdef OPENSSL_FIPS
    ERR_load_FIPS_strings();
# endif
/*
# ifndef OPENSSL_NO_CMS
    ERR_load_CMS_strings();
# endif
*/
# ifndef OPENSSL_NO_JPAKE
    ERR_load_JPAKE_strings();
# endif
#endif
}
예제 #2
0
파일: err_all.c 프로젝트: AnClark/openssl
void err_load_crypto_strings_intern(void)
{
#ifdef OPENSSL_FIPS
    FIPS_set_error_callbacks(ERR_put_error, ERR_add_error_vdata);
#endif
#ifndef OPENSSL_NO_ERR
    ERR_load_ERR_strings();     /* include error strings for SYSerr */
    ERR_load_BN_strings();
# ifndef OPENSSL_NO_RSA
    ERR_load_RSA_strings();
# endif
# ifndef OPENSSL_NO_DH
    ERR_load_DH_strings();
# endif
    ERR_load_EVP_strings();
    ERR_load_BUF_strings();
    ERR_load_OBJ_strings();
    ERR_load_PEM_strings();
# ifndef OPENSSL_NO_DSA
    ERR_load_DSA_strings();
# endif
    ERR_load_X509_strings();
    ERR_load_ASN1_strings();
    ERR_load_CONF_strings();
    ERR_load_CRYPTO_strings();
# ifndef OPENSSL_NO_COMP
    ERR_load_COMP_strings();
# endif
# ifndef OPENSSL_NO_EC
    ERR_load_EC_strings();
# endif
    /* skip ERR_load_SSL_strings() because it is not in this library */
    ERR_load_BIO_strings();
    ERR_load_PKCS7_strings();
    ERR_load_X509V3_strings();
    ERR_load_PKCS12_strings();
    ERR_load_RAND_strings();
    ERR_load_DSO_strings();
# ifndef OPENSSL_NO_TS
    ERR_load_TS_strings();
# endif
# ifndef OPENSSL_NO_ENGINE
    ERR_load_ENGINE_strings();
# endif
    ERR_load_OCSP_strings();
    ERR_load_UI_strings();
# ifdef OPENSSL_FIPS
    ERR_load_FIPS_strings();
# endif
# ifndef OPENSSL_NO_CMS
    ERR_load_CMS_strings();
# endif
# ifndef OPENSSL_NO_CT
    ERR_load_CT_strings();
# endif
    ERR_load_ASYNC_strings();
#endif
    ERR_load_KDF_strings();
}
예제 #3
0
		// init
		bool CryptographyEngine::init( void )
		{
			/* Initialise the library */
			ERR_load_CRYPTO_strings();
			OpenSSL_add_all_algorithms();
			OPENSSL_config( NULL );

			return true;
		}
예제 #4
0
Global::Global() {
  SSL_library_init();
  ERR_load_CRYPTO_strings();
  SSL_load_error_strings();
  OpenSSL_add_all_algorithms();
  RAND_poll();
  event_base = event_base_new();
  dns_base = evdns_base_new(event_base, 1);
};
예제 #5
0
Global::Global() {
  SSL_library_init();
  ERR_load_CRYPTO_strings();
  SSL_load_error_strings();
  OpenSSL_add_all_algorithms();
  RAND_poll();
#ifndef _NO_LIBEVENT_THREADS
  evthread_use_pthreads();
#endif
  event_base = event_base_new();
  dns_base = evdns_base_new(event_base, 1);
};
예제 #6
0
void ERR_load_crypto_strings(void)
  {
  static int done=0;

  if (done) return;
  done=1;
#ifndef OPENSSL_NO_ERR
  ERR_load_ERR_strings(); /* include error strings for SYSerr */
  ERR_load_BN_strings();
#ifndef OPENSSL_NO_RSA
  ERR_load_RSA_strings();
#endif
#ifndef OPENSSL_NO_DH
  ERR_load_DH_strings();
#endif
  ERR_load_EVP_strings();
  ERR_load_BUF_strings();
  ERR_load_OBJ_strings();
  ERR_load_PEM_strings();
#ifndef OPENSSL_NO_DSA
  ERR_load_DSA_strings();
#endif
  ERR_load_X509_strings();
  ERR_load_ASN1_strings();
  ERR_load_CONF_strings();
  ERR_load_CRYPTO_strings();
#ifndef OPENSSL_NO_EC
  ERR_load_EC_strings();
#endif
#ifndef OPENSSL_NO_ECDSA
  ERR_load_ECDSA_strings();
#endif
#ifndef OPENSSL_NO_ECDH
  ERR_load_ECDH_strings();
#endif
  /* skip ERR_load_SSL_strings() because it is not in this library */
  ERR_load_BIO_strings();
  ERR_load_PKCS7_strings();  
  ERR_load_X509V3_strings();
  ERR_load_PKCS12_strings();
  ERR_load_RAND_strings();
  ERR_load_DSO_strings();
#ifndef OPENSSL_NO_ENGINE
  ERR_load_ENGINE_strings();
#endif
  ERR_load_OCSP_strings();
  ERR_load_UI_strings();
#endif
  }
예제 #7
0
void capi_read_key_Engine(EVP_PKEY** key, char* id, ENGINE *e, char* storename) {
	if(!ENGINE_ctrl(e, CAPI_CMD_STORE_NAME, 0, (void*)storename, NULL)) {
		ERR_load_CRYPTO_strings();
		fprintf(stderr, "Executing CAPI_CMD_STORE_NAME did not succeed: %s\n", ERR_error_string(ERR_peek_last_error(), NULL));
		ERR_free_strings();
		exit(SCEP_PKISTATUS_ERROR);
	}
	//loading the key
	*key = ENGINE_load_private_key(e, id, NULL, NULL);
	if(!key) {
		printf("%s: Could not load key %s from storename %s\n", pname, id, storename);
		exit(SCEP_PKISTATUS_FILE);
	}

	//ENGINE_ctrl(e, CAPI_CMD_STORE_NAME, 0, (void*)"MY", NULL);
}
예제 #8
0
void SWU_CryptoInit_PEM(char *configuration_dir, char *pem_file) {
	OpenSSL_add_all_digests();
	ERR_load_CRYPTO_strings();
	char pem_file_name[1024] = "";
	strcat(pem_file_name, configuration_dir);
	strcat(pem_file_name, "/");
	strcat(pem_file_name, pem_file);
	FILE *pubKeyFile = fopen(pem_file_name, "r");
	if (pubKeyFile == NULL) {
		printf("Error: Can't open PEM file %s\n", pem_file);
		exit(1);
	}
	EVP_PKEY *gpPubKey = PEM_read_PUBKEY(pubKeyFile, NULL, NULL, NULL);
	_gpPubKey = gpPubKey;
	if (_gpPubKey == NULL) {
		printf("Error: Can't read PEM signature from file %s\n", pem_file);
		fclose(pubKeyFile);
		exit(1);
	}
	fclose(pubKeyFile);
	ERR_clear_error();
}
예제 #9
0
bool BaseSSLProtocol::Initialize(Variant &parameters) {
	//1. Initialize the SSL library
	if (!_libraryInitialized) {
		//3. This is the first time we use the library. So we have to
		//initialize it first
		SSL_library_init();

		//init readable error messages
		SSL_load_error_strings();
		ERR_load_SSL_strings();
		ERR_load_CRYPTO_strings();
		ERR_load_crypto_strings();
		OpenSSL_add_all_algorithms();
		OpenSSL_add_all_ciphers();
		OpenSSL_add_all_digests();

		//initialize the random numbers generator
		InitRandGenerator();
		_libraryInitialized = true;
	}

	//2. Initialize the global context
	if (!InitGlobalContext(parameters)) {
		FATAL("Unable to initialize global context");
		return false;
	}

	//3. create connection SSL context
	_pSSL = SSL_new(_pGlobalSSLContext);
	if (_pSSL == NULL) {
		FATAL("Unable to create SSL connection context");
		return false;
	}

	//4. setup the I/O buffers
	SSL_set_bio(_pSSL, BIO_new(BIO_s_mem()), BIO_new(BIO_s_mem()));

	return DoHandshake();
}
예제 #10
0
파일: js_rsa.c 프로젝트: feilerr/SecretDemo
char *js_public_encrypt(const char *plain_text, const char *public_key_path) {
    RSA *rsa_publicKey = NULL;
    FILE *fp_publicKey;
    int rsa_public_len;

    if ((fp_publicKey = fopen(public_key_path, "r")) == NULL) {
        printf("Could not open %s\n", public_key_path);
        return '\0';
    }


    if ((rsa_publicKey = PEM_read_RSA_PUBKEY(fp_publicKey, NULL, NULL, NULL)) == NULL) {
        fclose(fp_publicKey);
        printf("Error loading RSA Public Key File.");
        return '\0';
    }
    fclose(fp_publicKey);

    rsa_public_len = RSA_size(rsa_publicKey);
    printf("RSA public length: %d\n", rsa_public_len);

    // 11 bytes is overhead required for encryption
    int chunk_length = rsa_public_len - 11;
    // plain text length
    int plain_char_len = (int)strlen(plain_text);
    // calculate the number of chunks
    int num_of_chunks = (int)(strlen(plain_text) / chunk_length) + 1;

    int total_cipher_length = 0;

    // the output size is (total number of chunks) x (the key length)
    int encrypted_size = (num_of_chunks * rsa_public_len);
    unsigned char *cipher_data = malloc(encrypted_size + 1);

    char *err = NULL;
    for (int i = 0; i < plain_char_len; i += chunk_length) {

        // get the remaining character count from the plain text
        int remaining_char_count = plain_char_len - i;

        // this len is the number of characters to encrypt, thus take the minimum between the chunk count & the remaining characters
        // this must less than rsa_public_len - 11
        int len = JSMIN(remaining_char_count, chunk_length);
        unsigned char *plain_chunk = malloc(len + 1);
        // take out chunk of plain text
        memcpy(&plain_chunk[0], &plain_text[i], len);

        printf("Plain chunk: %s\n", plain_chunk);

        unsigned char *result_chunk = malloc(rsa_public_len + 1);

        int result_length = RSA_public_encrypt(len, plain_chunk, result_chunk, rsa_publicKey, RSA_PKCS1_PADDING);
        printf("Plain char len: %d\n", i);
        printf("Encrypted Result chunk: %s\nEncrypted Chunk length: %d\n", result_chunk, result_length);

        free(plain_chunk);

        if (result_length == -1) {
            ERR_load_CRYPTO_strings();
            fprintf(stderr, "Error %s\n", ERR_error_string(ERR_get_error(), err));
            fprintf(stderr, "Error %s\n", err);
        }

        memcpy(&cipher_data[total_cipher_length], &result_chunk[0], result_length);

        total_cipher_length += result_length;

        free(result_chunk);
    }
    printf("Total cipher length: %d\n", total_cipher_length);

    RSA_free(rsa_publicKey);
    size_t total_len = 0;
    char *encrypted = base64_encode(cipher_data, encrypted_size, &total_len);
    printf("Final result: %s\n Final result length: %zu\n", encrypted, total_len);

    free(cipher_data);

    return encrypted;
}
예제 #11
0
파일: js_rsa.c 프로젝트: feilerr/SecretDemo
char *js_public_decrypt(const char *cipher_text, const char *public_key_path) {
    RSA *rsa_publicKey = NULL;
    FILE *fp_publicKey;
    int rsa_public_len;

    if ((fp_publicKey = fopen(public_key_path, "r")) == NULL) {
        printf("Could not open %s\n", public_key_path);
        return '\0';
    }

    if ((rsa_publicKey = PEM_read_RSA_PUBKEY(fp_publicKey, NULL, NULL, NULL)) == NULL) {
        fclose(fp_publicKey);
        printf("Error loading RSA Public Key File.");
        return '\0';
    }
    fclose(fp_publicKey);

    printf("Cipher text: %s\n", cipher_text);

    rsa_public_len = RSA_size(rsa_publicKey);
    printf("RSA public length: %d\n", rsa_public_len);

    size_t crypt_len = 0;

    unsigned char *crypt = base64_decode(cipher_text, strlen(cipher_text), &crypt_len);

    printf("Decoded cipher: %s\nCrypt length: %ld\n", crypt, crypt_len);

    // If no static, it will cause "address of stack memory associated with local variable ...", which mean the variable will released from memory after the end of this function
    char *plain_char = malloc(crypt_len);
    // initialize
    strcpy(plain_char, "");

    char *err = NULL;
    for (int i = 0; i < crypt_len; i += rsa_public_len) {
        unsigned char *crypt_chunk = malloc(rsa_public_len + 1);
        memcpy(&crypt_chunk[0], &crypt[i], rsa_public_len);

        printf("Crypt chunk: %s\n", crypt_chunk);

        unsigned char *result_chunk = malloc(crypt_len + 1);
        int result_length = RSA_public_decrypt(rsa_public_len, crypt_chunk, result_chunk, rsa_publicKey, RSA_PKCS1_PADDING);
        // chunk length should be the size of publickey (in bytes) minus 11 (overhead during encryption)
        printf("Result chunk: %s\nChunk length: %d\n", result_chunk, result_length);

        free(crypt_chunk);

        // this is to omit the dummy character behind
        // i.e. Result chunk: ABC-1234567-201308101427371250-abcdefghijklmnopqrstuv\240Z
        //      Chunk length: 53
        //      New chunk: ABC-1234567-201308101427371250-abcdefghijklmnopqrstuv
        //
        // by copying the chunk to a temporary variable with an extra length (i.e. in this case is 54)
        // and then set the last character of temporary variable to NULL
        char tmp_result[result_length + 1];
        memcpy(tmp_result, result_chunk, result_length);
        tmp_result[result_length] = '\0';
        printf("New chunk: %s\n", tmp_result);

        free(result_chunk);

        if (result_length == -1) {
            ERR_load_CRYPTO_strings();
            fprintf(stderr, "Error %s\n", ERR_error_string(ERR_get_error(), err));
            fprintf(stderr, "Error %s\n", err);
        }

        strcat(plain_char, tmp_result);
    }

    RSA_free(rsa_publicKey);
    free(crypt);
    printf("Final result: %s\n", plain_char);

    return plain_char;
}
예제 #12
0
int err_load_crypto_strings_int(void)
{
    if (
#ifndef OPENSSL_NO_ERR
        ERR_load_ERR_strings() == 0 ||    /* include error strings for SYSerr */
        ERR_load_BN_strings() == 0 ||
# ifndef OPENSSL_NO_RSA
        ERR_load_RSA_strings() == 0 ||
# endif
# ifndef OPENSSL_NO_DH
        ERR_load_DH_strings() == 0 ||
# endif
        ERR_load_EVP_strings() == 0 ||
        ERR_load_BUF_strings() == 0 ||
        ERR_load_OBJ_strings() == 0 ||
        ERR_load_PEM_strings() == 0 ||
# ifndef OPENSSL_NO_DSA
        ERR_load_DSA_strings() == 0 ||
# endif
        ERR_load_X509_strings() == 0 ||
        ERR_load_ASN1_strings() == 0 ||
        ERR_load_CONF_strings() == 0 ||
        ERR_load_CRYPTO_strings() == 0 ||
# ifndef OPENSSL_NO_COMP
        ERR_load_COMP_strings() == 0 ||
# endif
# ifndef OPENSSL_NO_EC
        ERR_load_EC_strings() == 0 ||
# endif
        /* skip ERR_load_SSL_strings() because it is not in this library */
        ERR_load_BIO_strings() == 0 ||
        ERR_load_PKCS7_strings() == 0 ||
        ERR_load_X509V3_strings() == 0 ||
        ERR_load_PKCS12_strings() == 0 ||
        ERR_load_RAND_strings() == 0 ||
        ERR_load_DSO_strings() == 0 ||
# ifndef OPENSSL_NO_TS
        ERR_load_TS_strings() == 0 ||
# endif
# ifndef OPENSSL_NO_ENGINE
        ERR_load_ENGINE_strings() == 0 ||
# endif
# ifndef OPENSSL_NO_OCSP
        ERR_load_OCSP_strings() == 0 ||
# endif
#ifndef OPENSSL_NO_UI
        ERR_load_UI_strings() == 0 ||
#endif
# ifndef OPENSSL_NO_CMS
        ERR_load_CMS_strings() == 0 ||
# endif
# ifndef OPENSSL_NO_CT
        ERR_load_CT_strings() == 0 ||
# endif
        ERR_load_ASYNC_strings() == 0 ||
#endif
        ERR_load_KDF_strings() == 0 ||
        ERR_load_OSSL_STORE_strings() == 0)
        return 0;

    return 1;
}
예제 #13
0
int main(void) {
	EVP_CIPHER_CTX *ctx = NULL;
	unsigned char key[16];
	unsigned char iv[12];
	unsigned char tag[16];
	unsigned char data[128];
	unsigned char ori_msg[128];
	unsigned char enc_msg[128+16];
	unsigned char dec_msg[128];
	int r, len, enc_msg_len, dec_msg_len;
	const EVP_CIPHER* cipher = NULL;

	ERR_load_CRYPTO_strings();
	OPENSSL_add_all_algorithms_noconf();

	r = RAND_bytes(key, sizeof(key));
	assert(r == 1);
	r = RAND_bytes(iv, sizeof(iv));
	assert(r == 1);
	r = RAND_pseudo_bytes(data, sizeof(data));
	assert(r == 1);
	r = RAND_pseudo_bytes(ori_msg, sizeof(ori_msg));
	assert(r == 1);
	r = RAND_pseudo_bytes(enc_msg, sizeof(enc_msg));
	assert(r == 1);

	cipher = EVP_aes_128_gcm();

	ctx = EVP_CIPHER_CTX_new();
	assert(ctx);

	EVP_CIPHER_CTX_init(ctx);

	len = EVP_CIPHER_key_length(cipher);
	assert(len == sizeof(key));

	len = EVP_CIPHER_iv_length(cipher);
	assert(len == sizeof(iv));


	r = EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv);
	assert(r == 1);

	r = EVP_EncryptUpdate(ctx, NULL, &enc_msg_len, data, sizeof(data));
	assert(r == 1);

	r = EVP_EncryptUpdate(ctx, enc_msg, &enc_msg_len, ori_msg, sizeof(ori_msg));
	assert(r == 1);
	assert(enc_msg_len == sizeof(ori_msg));

	r = EVP_EncryptFinal_ex(ctx, enc_msg + enc_msg_len, &len);
	assert(r == 1);
	assert(len == 0);

	r = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, sizeof(tag), tag);
	assert(r == 1);


	r = EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv);
	assert(r == 1);

	r = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, sizeof(tag), tag);
	assert(r == 1);

	r = EVP_DecryptUpdate(ctx, NULL, &dec_msg_len, data, sizeof(data));
	assert(r == 1);

	r = EVP_DecryptUpdate(ctx, dec_msg, &dec_msg_len, enc_msg, enc_msg_len);
	assert(r == 1);
	assert(dec_msg_len == enc_msg_len);

	r = EVP_DecryptFinal_ex(ctx, dec_msg + dec_msg_len, &len);
	assert(r == 1);
	assert(len == 0);

	assert(memcmp(ori_msg, dec_msg, dec_msg_len) == 0);


	EVP_CIPHER_CTX_free(ctx);

	puts("OK!");
	return 0;
}
예제 #14
0
파일: acs_rsa.c 프로젝트: whlzdy/rastyle
/*
 * js private key encrypt
*/
char * js_private_encrypt(const char *plain_text,int * encode_len ,char *private_key)
{
	RSA *rsa_privateKey = NULL;
	int rsa_private_len;
	int i;
	// private_key = rsa_key_seliaze(private_key_str);
	//BIO* p_bio = BIO_new_mem_buf(PRIVATE_KEY, -1);
	BIO* p_bio = BIO_new_mem_buf(private_key, -1);
    rsa_privateKey = PEM_read_bio_RSAPrivateKey(p_bio, NULL, 0, NULL); //
	if ( rsa_privateKey == NULL ) {
		   printf("RSA is NULL\n");
		   return NULL;
	}
    rsa_private_len = RSA_size(rsa_privateKey);
   // printf("RSA private length: %d\n", rsa_private_len);
    // 11 bytes is overhead required for encryption
    int chunk_length = rsa_private_len - 11;
    // plain text length
    int plain_char_len = (int)strlen(plain_text);
    // calculate the number of chunks
    int num_of_chunks = (int)(strlen(plain_text) / chunk_length) + 1;
    int total_cipher_length = 0;
    // the output size is (total number of chunks) x (the key length)
    int encrypted_size = (num_of_chunks * rsa_private_len);
    unsigned char *cipher_data = malloc(encrypted_size + 1);
    char *err = NULL;
    for ( i = 0; i < plain_char_len; i += chunk_length) {
        // get the remaining character count from the plain text
        int remaining_char_count = plain_char_len - i;
        // this len is the number of characters to encrypt, thus take the minimum between the chunk count & the remaining characters
        // this must less than rsa_private_len - 11
        int len = JSMIN(remaining_char_count, chunk_length);
        unsigned char *plain_chunk = malloc(len + 1);
        memset(plain_chunk,0,len + 1);
        // take out chunk of plain text
        memcpy(&plain_chunk[0], &plain_text[i], len);
       // printf("Plain chunk: %s\n", plain_chunk);
        unsigned char *result_chunk = malloc(rsa_private_len + 1);
        memset(result_chunk,0,rsa_private_len + 1);
        int result_length = RSA_private_encrypt(len, plain_chunk, result_chunk, rsa_privateKey, RSA_PKCS1_PADDING); // RSA_NO_PADDING RSA_PKCS1_PADDING
       // printf("Encrypted Result chunk: %s\nEncrypted Chunk length: %d\n", result_chunk, result_length);
        free(plain_chunk);
        if (result_length == -1) {
            ERR_load_CRYPTO_strings();
            fprintf(stderr, "Error %s\n", ERR_error_string(ERR_get_error(), err));
            fprintf(stderr, "Error %s\n", err);
        }
        memcpy(&cipher_data[total_cipher_length], &result_chunk[0], result_length);
        total_cipher_length += result_length;
        free(result_chunk);
    }
    //printf("Total cipher length: %d\n", total_cipher_length);

    RSA_free(rsa_privateKey);
    // size_t total_len = 0;
    // char *encrypted = base64_encode(cipher_data, encrypted_size, &total_len);
    // printf("Final result: %s\n Final result length: %zu\n", encrypted, total_len);
     *encode_len = encrypted_size;
    //free(cipher_data);

    return cipher_data;
}
예제 #15
0
파일: acs_rsa.c 프로젝트: whlzdy/rastyle
/*
 * js public decrypt
*/
char * js_public_decrypt(const char *cipher_text, int encode_len, char *public_key)
{
    RSA *rsa_publicKey = NULL;
    int rsa_public_len;
    int i;
    //printf("js_public_decrypt public_key is %s \n",public_key);
    BIO* p_bio = BIO_new_mem_buf(public_key, -1);
    //BIO* p_bio = BIO_new_mem_buf(public_key, strlen(public_key));
    // printf("rsa_encrypt is %p \n",p_bio);
    //rsa_publicKey = PEM_read_bio_RSAPublicKey(p_bio, NULL, NULL, NULL); //PEM_read_bio_RSAPrivateKey PEM_read_RSA_PUBKEY()
     //PEM_read_bio_RSAPublicKey(p_bio, &rsa_publicKey, 0, NULL); //PEM_read_bio_RSAPrivateKey PEM_read_RSA_PUBKEY()
    //printf("111\n");
    PEM_read_bio_RSA_PUBKEY(p_bio, &rsa_publicKey, NULL, NULL);
    if ( rsa_publicKey == NULL ) {
	   printf("RSA is NULL\n");
	   return NULL;
    }
    //printf("222\n");
    // printf("Cipher text: %s\n", cipher_text);
    rsa_public_len = RSA_size(rsa_publicKey);
    //printf("RSA public length: %d\n", rsa_public_len);
    size_t crypt_len = (size_t)encode_len;
    char *crypt =(char *) cipher_text;
   // unsigned char *crypt = base64_decode(cipher_text, strlen(cipher_text), &crypt_len);
   // printf("Decoded cipher: %s\nCrypt length: %ld\n", crypt, crypt_len);
    // If no static, it will cause "address of stack memory associated with local variable ...", which mean the variable will released from memory after the end of this function
    char *plain_char = malloc(crypt_len);
    //printf("plain_char is %p,crypt_len is %ld",plain_char,(int)crypt_len);
    // initialize
    //printf("333\n");
    strcpy(plain_char, "");
    char *err = NULL;
    for ( i = 0; i < crypt_len; i += rsa_public_len) {
        unsigned char *crypt_chunk = malloc(rsa_public_len + 1);
       // printf("444\n");
        memcpy(&crypt_chunk[0], &crypt[i], rsa_public_len);
        //printf("Crypt chunk: %s\n", crypt_chunk);
        unsigned char *result_chunk = malloc(crypt_len + 1);
       // printf("555\n");
        int result_length = RSA_public_decrypt(rsa_public_len, crypt_chunk, result_chunk, rsa_publicKey, RSA_PKCS1_PADDING);//RSA_NO_PADDING RSA_PKCS1_PADDING
        // chunk length should be the size of publickey (in bytes) minus 11 (overhead during encryption)
       // printf("Result chunk: %s\nChunk length: %d\n", result_chunk, result_length);

        free(crypt_chunk);

        // this is to omit the dummy character behind
        // i.e. Result chunk: ABC-1234567-201308101427371250-abcdefghijklmnopqrstuv\240Z
        //      Chunk length: 53
        //      New chunk: ABC-1234567-201308101427371250-abcdefghijklmnopqrstuv
        //
        // by copying the chunk to a temporary variable with an extra length (i.e. in this case is 54)
        // and then set the last character of temporary variable to NULL
        char tmp_result[result_length + 1];
        //printf("666\n");
        memcpy(tmp_result, result_chunk, result_length);
        tmp_result[result_length] = '\0';
        //printf("New chunk: %s\n", tmp_result);

        free(result_chunk);

        if (result_length == -1) {
            ERR_load_CRYPTO_strings();
            fprintf(stderr, "Error %s\n", ERR_error_string(ERR_get_error(), err));
            fprintf(stderr, "Error %s\n", err);
        }

        strcat(plain_char, tmp_result);
    }
    //printf("777\n");
    RSA_free(rsa_publicKey);
   //free(crypt);
   // printf("Final result: %s\n", plain_char);

    return plain_char;
}
예제 #16
0
파일: acs_rsa.c 프로젝트: whlzdy/rastyle
/*
 * js pubilc key encrypt
*/
char * js_public_encrypt(const char *plain_text,int * length, char *public_key)
{
    RSA *rsa_publicKey = NULL;
    int rsa_public_len;
    int i;
    //public_key = rsa_key_seliaze(public_key_str);
    BIO* p_bio = BIO_new_mem_buf(public_key, -1);
   // printf("rsa_encrypt is %p \n",p_bio);
    //rsa_publicKey = PEM_read_bio_RSAPublicKey(p_bio, NULL, NULL, NULL); //PEM_read_bio_RSAPrivateKey
    PEM_read_bio_RSA_PUBKEY(p_bio, &rsa_publicKey, NULL, NULL);
    if ( rsa_publicKey == NULL ) {
        printf("RSA is NULL\n");
        return NULL;
    }
    rsa_public_len = RSA_size(rsa_publicKey);
   // printf("RSA public length: %d\n", rsa_public_len);
    // 11 bytes is overhead required for encryption
    int chunk_length = rsa_public_len - 11;
    // plain text length
    int plain_char_len = (int)strlen(plain_text);
    // calculate the number of chunks
    int num_of_chunks = (int)(strlen(plain_text) / chunk_length) + 1;
    int total_cipher_length = 0;
    // the output size is (total number of chunks) x (the key length)
    int encrypted_size = (num_of_chunks * rsa_public_len);
    char *cipher_data = malloc(encrypted_size + 1);
    if (cipher_data) {
        memset(cipher_data,0,encrypted_size + 1);
    }else{
        return NULL;
    }
    char *err = NULL;
    for ( i = 0; i < plain_char_len; i += chunk_length) {

        // get the remaining character count from the plain text
        int remaining_char_count = plain_char_len - i;

        // this len is the number of characters to encrypt, thus take the minimum between the chunk count & the remaining characters
        // this must less than rsa_public_len - 11
        int len = JSMIN(remaining_char_count, chunk_length);
        unsigned char *plain_chunk = calloc(len + 1,1);
        memset(plain_chunk,0,len+1);
        // take out chunk of plain text
        memcpy(&plain_chunk[0], &plain_text[i], len);
//        printf("Plain chunk: %s\n", plain_chunk);
        unsigned char *result_chunk = calloc(rsa_public_len + 1,1);
        memset(result_chunk,0,rsa_public_len + 1);
        int result_length = RSA_public_encrypt(len, plain_chunk,result_chunk, rsa_publicKey, RSA_PKCS1_PADDING);

//        printf("Plain char len: %d\n", i);

      //  printf("\n\n%ld,鍔犲瘑鍓嶆暟鎹�s \n", strlen((char *)plain_chunk), plain_chunk);
       // printf("\鍔犲瘑缁撴灉闀垮害: %d\n", result_length);
       // printf("*********%d*****************\n", strlen(result_chunk));
        free(plain_chunk);
        if (result_length == -1) {
            ERR_load_CRYPTO_strings();
            fprintf(stderr, "Error %s\n", ERR_error_string(ERR_get_error(), err));
            fprintf(stderr, "Error %s\n", err);
            break;
        }
        memcpy(&cipher_data[total_cipher_length], &result_chunk[0], result_length);
    //    strcat(cipher_data, (const char *)result_chunk);
        total_cipher_length += result_length;
        free(result_chunk);
    }
   // printf("\n 鍔犲瘑鎬婚暱搴�d\n", total_cipher_length);
    cipher_data[total_cipher_length] = '\0';
    *length = encrypted_size;
    //printf("\n鍔犲瘑杩斿洖缁撴灉闀垮害%d\n", strlen(cipher_data));
    RSA_free(rsa_publicKey);
    return cipher_data;
}