Skip to content

Commit

Permalink
Merge branch 'da/darwin'
Browse files Browse the repository at this point in the history
* da/darwin:
  OS X: Fix redeclaration of die warning
  Makefile: Fix APPLE_COMMON_CRYPTO with BLK_SHA1
  imap-send: use Apple's Security framework for base64 encoding
  • Loading branch information
gitster committed Sep 4, 2013
2 parents 4aa04a8 + f2be034 commit 9a7eaad
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,9 @@ ifdef NEEDS_SSL_WITH_CRYPTO
else
LIB_4_CRYPTO = $(OPENSSL_LINK) -lcrypto
endif
ifdef APPLE_COMMON_CRYPTO
LIB_4_CRYPTO += -framework Security -framework CoreFoundation
endif
endif
ifdef NEEDS_LIBICONV
ifdef ICONVDIR
Expand Down
86 changes: 86 additions & 0 deletions compat/apple-common-crypto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/* suppress inclusion of conflicting openssl functions */
#define OPENSSL_NO_MD5
#define HEADER_HMAC_H
#define HEADER_SHA_H
#include <CommonCrypto/CommonHMAC.h>
#define HMAC_CTX CCHmacContext
#define HMAC_Init(hmac, key, len, algo) CCHmacInit(hmac, algo, key, len)
#define HMAC_Update CCHmacUpdate
#define HMAC_Final(hmac, hash, ptr) CCHmacFinal(hmac, hash)
#define HMAC_CTX_cleanup(ignore)
#define EVP_md5(...) kCCHmacAlgMD5
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
#define APPLE_LION_OR_NEWER
#include <Security/Security.h>
/* Apple's TYPE_BOOL conflicts with config.c */
#undef TYPE_BOOL
#endif

#ifdef APPLE_LION_OR_NEWER
#define git_CC_error_check(pattern, err) \
do { \
if (err) { \
die(pattern, (long)CFErrorGetCode(err)); \
} \
} while(0)

#define EVP_EncodeBlock git_CC_EVP_EncodeBlock
static inline int git_CC_EVP_EncodeBlock(unsigned char *out,
const unsigned char *in, int inlen)
{
CFErrorRef err;
SecTransformRef encoder;
CFDataRef input, output;
CFIndex length;

encoder = SecEncodeTransformCreate(kSecBase64Encoding, &err);
git_CC_error_check("SecEncodeTransformCreate failed: %ld", err);

input = CFDataCreate(kCFAllocatorDefault, in, inlen);
SecTransformSetAttribute(encoder, kSecTransformInputAttributeName,
input, &err);
git_CC_error_check("SecTransformSetAttribute failed: %ld", err);

output = SecTransformExecute(encoder, &err);
git_CC_error_check("SecTransformExecute failed: %ld", err);

length = CFDataGetLength(output);
CFDataGetBytes(output, CFRangeMake(0, length), out);

CFRelease(output);
CFRelease(input);
CFRelease(encoder);

return (int)strlen((const char *)out);
}

#define EVP_DecodeBlock git_CC_EVP_DecodeBlock
static int inline git_CC_EVP_DecodeBlock(unsigned char *out,
const unsigned char *in, int inlen)
{
CFErrorRef err;
SecTransformRef decoder;
CFDataRef input, output;
CFIndex length;

decoder = SecDecodeTransformCreate(kSecBase64Encoding, &err);
git_CC_error_check("SecEncodeTransformCreate failed: %ld", err);

input = CFDataCreate(kCFAllocatorDefault, in, inlen);
SecTransformSetAttribute(decoder, kSecTransformInputAttributeName,
input, &err);
git_CC_error_check("SecTransformSetAttribute failed: %ld", err);

output = SecTransformExecute(decoder, &err);
git_CC_error_check("SecTransformExecute failed: %ld", err);

length = CFDataGetLength(output);
CFDataGetBytes(output, CFRangeMake(0, length), out);

CFRelease(output);
CFRelease(input);
CFRelease(decoder);

return (int)strlen((const char *)out);
}
#endif /* APPLE_LION_OR_NEWER */
10 changes: 10 additions & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,16 @@ extern NORETURN void die_errno(const char *err, ...) __attribute__((format (prin
extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));

#ifndef NO_OPENSSL
#ifdef APPLE_COMMON_CRYPTO
#include "compat/apple-common-crypto.h"
#else
#include <openssl/evp.h>
#include <openssl/hmac.h>
#endif /* APPLE_COMMON_CRYPTO */
#include <openssl/x509v3.h>
#endif /* NO_OPENSSL */

/*
* Let callers be aware of the constant return value; this can help
* gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,
Expand Down
14 changes: 0 additions & 14 deletions imap-send.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,6 @@
#include "prompt.h"
#ifdef NO_OPENSSL
typedef void *SSL;
#else
#ifdef APPLE_COMMON_CRYPTO
#include <CommonCrypto/CommonHMAC.h>
#define HMAC_CTX CCHmacContext
#define HMAC_Init(hmac, key, len, algo) CCHmacInit(hmac, algo, key, len)
#define HMAC_Update CCHmacUpdate
#define HMAC_Final(hmac, hash, ptr) CCHmacFinal(hmac, hash)
#define HMAC_CTX_cleanup(ignore)
#define EVP_md5() kCCHmacAlgMD5
#else
#include <openssl/evp.h>
#include <openssl/hmac.h>
#endif
#include <openssl/x509v3.h>
#endif

static const char imap_send_usage[] = "git imap-send < <mbox>";
Expand Down

0 comments on commit 9a7eaad

Please sign in to comment.