Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use CommonCrypto instead of the obsolete OpenSSL on Apple platforms. #1

Merged
merged 1 commit into from
Jan 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion demo/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
CC=gcc
CFLAGS=-static -Wall -g
LDFLAGS=-L../src -ldp -lpthread -lcrypto
LDFLAGS=-L../src -ldp

ifneq ($(OS),Windows_NT)
OS = $(shell uname -s)
endif

ifneq ($(OS),Darwin)
LDFLAGS += -lpthread -lcrypto
endif

TARGET=demo

Expand Down
10 changes: 9 additions & 1 deletion src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@

CC=gcc
CFLAGS=-static -Wall -g
LDFLAGS=-lpthread -lcrypto
LDFLAGS=

ifneq ($(OS),Windows_NT)
OS = $(shell uname -s)
endif

ifneq ($(OS),Darwin)
LDFLAGS += -lpthread -lcrypto
endif

SOURCES=$(wildcard *.c)
OBJECTS=$(patsubst %.c, %.o, $(SOURCES))
Expand Down
69 changes: 66 additions & 3 deletions src/dplus.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
#include "dplus.h"
#include "lruhash.h"

#ifdef __APPLE__
#include <CommonCrypto/CommonCrypto.h>
#endif /* __APPLE__ */

#define HTTPDNS_DEFAULT_SERVER "119.29.29.29"
#define HTTPDNS_DEFAULT_PORT 80
Expand Down Expand Up @@ -91,15 +94,40 @@ void dp_set_des_id_key(uint32_t id, const char *key)
*/
char *dp_des_encrypt(const char *domain)
{
#ifdef __APPLE__
size_t blen;
#else /* __APPLE__ */
EVP_CIPHER_CTX ctx;
int blen1, blen2;
#endif /* __APPLE__ */
unsigned char buf[DOMAIN_MAX_SIZE] = { 0 };
char *des_domain;
int blen1, blen2, dlen = strlen(domain), des_len;
int dlen = strlen(domain), des_len;
int i;

if (INVALID_DES_ID == des_id || dlen > DOMAIN_MAX_SIZE)
return NULL;

#ifdef __APPLE__

if (CCCrypt(kCCEncrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
des_key,
strlen(des_key),
NULL,
domain,
dlen,
buf,
sizeof(buf),
&blen) != kCCSuccess) {
return NULL;
}

des_len = (int)blen * 2;

#else /* __APPLE__ */

// 初始化ctx结构,使用des/ecb方式,padding方式默认即可
EVP_CIPHER_CTX_init(&ctx);
EVP_EncryptInit_ex(&ctx, EVP_des_ecb(), NULL, (const unsigned char*)des_key, NULL);
Expand All @@ -111,11 +139,14 @@ char *dp_des_encrypt(const char *domain)
EVP_CIPHER_CTX_cleanup(&ctx);

des_len = (blen1 + blen2) * 2;

#endif /* __APPLE__ */

des_domain = malloc(des_len + 1);
if (NULL == des_domain)
return NULL;

for (i = 0; i < (blen1 + blen2); i++) {
for (i = 0; i < des_len / 2; i++) {
snprintf(des_domain + i * 2, des_len - i * 2 + 1, "%02x", ((u_char *)buf)[i]);
}
des_domain[des_len] = '\0';
Expand All @@ -130,9 +161,14 @@ char *dp_des_encrypt(const char *domain)
*/
char *dp_des_decrypt(const char *des_ip)
{
#ifdef __APPLE__
size_t blen;
#else /* __APPLE__ */
EVP_CIPHER_CTX ctx;
int blen1, blen2;
#endif /* __APPLE__ */
char *buf, *sip;
int blen1, blen2, des_len = strlen(des_ip), iplen;
int des_len = strlen(des_ip), iplen;
int i;

if (INVALID_DES_ID == des_id)
Expand All @@ -156,6 +192,26 @@ char *dp_des_decrypt(const char *des_ip)
}
buf[iplen] = '\0';

#ifdef __APPLE__

if (CCCrypt(kCCDecrypt,
kCCAlgorithmDES,
kCCOptionPKCS7Padding | kCCOptionECBMode,
des_key,
strlen(des_key),
NULL,
buf,
iplen,
sip,
iplen,
&blen) != kCCSuccess) {
return NULL;
}

iplen = (int)blen;

#else /* __APPLE__ */

//初始化ctx结构,使用des/ecb方式,padding方式默认即可
EVP_CIPHER_CTX_init(&ctx);
EVP_DecryptInit_ex(&ctx, EVP_des_ecb(), NULL, (const unsigned char*)des_key, NULL);
Expand All @@ -167,6 +223,9 @@ char *dp_des_decrypt(const char *des_ip)
EVP_CIPHER_CTX_cleanup(&ctx);

iplen = blen1 + blen2;

#endif /* __APPLE__ */

sip[iplen] = '\0';

free(buf);
Expand Down Expand Up @@ -565,12 +624,14 @@ void dp_env_init()
dpe->des_used = des_used;
dpe->des_id = des_id;
dpe->des_key = des_key;
#ifndef __APPLE__
if (dpe->des_used) {
if (!dp_openssl_lock_init()){
fprintf(stderr, "init openssl locks failed\n");
exit(1);
}
}
#endif /* __APPLE__ */
}

void dp_env_destroy()
Expand All @@ -580,9 +641,11 @@ void dp_env_destroy()

lruhash_delete(dpe->cache);
prefetch_list_destroy(dpe->prefetch_list);
#ifndef __APPLE__
if (dpe->des_used) {
dp_openssl_lock_delete();
}
#endif /* __APPLE__ */
free(dpe);
}

Expand Down
2 changes: 2 additions & 0 deletions src/dplus.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
#include <sys/select.h>
#endif

#ifndef __APPLE__
#include <openssl/evp.h>
#endif /* __APPLE__ */

#include "lruhash.h"
#include "locks.h"
Expand Down
4 changes: 4 additions & 0 deletions src/locks.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ void dp_thread_join(dp_thread_t thr)

#endif

#ifndef __APPLE__

/** global lock list for openssl locks */
static lock_basic_t *dp_openssl_locks = NULL;

Expand Down Expand Up @@ -145,3 +147,5 @@ void dp_openssl_lock_delete(void)
}
free(dp_openssl_locks);
}

#endif /* __APPLE__ */
4 changes: 4 additions & 0 deletions src/locks.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,12 @@ typedef pthread_t dp_thread_t;

#endif

#ifndef __APPLE__

#include <openssl/crypto.h>
int dp_openssl_lock_init(void);
void dp_openssl_lock_delete(void);

#endif /* __APPLE__ */

#endif
10 changes: 9 additions & 1 deletion test/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
CC=gcc
CFLAGS=-static -Wall -g
LDFLAGS=-L../src -ldp -lpthread -lcrypto
LDFLAGS=-L../src -ldp

ifneq ($(OS),Windows_NT)
OS = $(shell uname -s)
endif

ifneq ($(OS),Darwin)
LDFLAGS += -lpthread -lcrypto
endif

SOURCES=$(wildcard *.c)
OBJECTS=$(patsubst %.c, %.o, $(SOURCES))
Expand Down