Skip to content

Commit 9a7eaad

Browse files
committed
Merge branch 'da/darwin'
* 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
2 parents 4aa04a8 + f2be034 commit 9a7eaad

File tree

4 files changed

+99
-14
lines changed

4 files changed

+99
-14
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,6 +1182,9 @@ ifdef NEEDS_SSL_WITH_CRYPTO
11821182
else
11831183
LIB_4_CRYPTO = $(OPENSSL_LINK) -lcrypto
11841184
endif
1185+
ifdef APPLE_COMMON_CRYPTO
1186+
LIB_4_CRYPTO += -framework Security -framework CoreFoundation
1187+
endif
11851188
endif
11861189
ifdef NEEDS_LIBICONV
11871190
ifdef ICONVDIR

compat/apple-common-crypto.h

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* suppress inclusion of conflicting openssl functions */
2+
#define OPENSSL_NO_MD5
3+
#define HEADER_HMAC_H
4+
#define HEADER_SHA_H
5+
#include <CommonCrypto/CommonHMAC.h>
6+
#define HMAC_CTX CCHmacContext
7+
#define HMAC_Init(hmac, key, len, algo) CCHmacInit(hmac, algo, key, len)
8+
#define HMAC_Update CCHmacUpdate
9+
#define HMAC_Final(hmac, hash, ptr) CCHmacFinal(hmac, hash)
10+
#define HMAC_CTX_cleanup(ignore)
11+
#define EVP_md5(...) kCCHmacAlgMD5
12+
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
13+
#define APPLE_LION_OR_NEWER
14+
#include <Security/Security.h>
15+
/* Apple's TYPE_BOOL conflicts with config.c */
16+
#undef TYPE_BOOL
17+
#endif
18+
19+
#ifdef APPLE_LION_OR_NEWER
20+
#define git_CC_error_check(pattern, err) \
21+
do { \
22+
if (err) { \
23+
die(pattern, (long)CFErrorGetCode(err)); \
24+
} \
25+
} while(0)
26+
27+
#define EVP_EncodeBlock git_CC_EVP_EncodeBlock
28+
static inline int git_CC_EVP_EncodeBlock(unsigned char *out,
29+
const unsigned char *in, int inlen)
30+
{
31+
CFErrorRef err;
32+
SecTransformRef encoder;
33+
CFDataRef input, output;
34+
CFIndex length;
35+
36+
encoder = SecEncodeTransformCreate(kSecBase64Encoding, &err);
37+
git_CC_error_check("SecEncodeTransformCreate failed: %ld", err);
38+
39+
input = CFDataCreate(kCFAllocatorDefault, in, inlen);
40+
SecTransformSetAttribute(encoder, kSecTransformInputAttributeName,
41+
input, &err);
42+
git_CC_error_check("SecTransformSetAttribute failed: %ld", err);
43+
44+
output = SecTransformExecute(encoder, &err);
45+
git_CC_error_check("SecTransformExecute failed: %ld", err);
46+
47+
length = CFDataGetLength(output);
48+
CFDataGetBytes(output, CFRangeMake(0, length), out);
49+
50+
CFRelease(output);
51+
CFRelease(input);
52+
CFRelease(encoder);
53+
54+
return (int)strlen((const char *)out);
55+
}
56+
57+
#define EVP_DecodeBlock git_CC_EVP_DecodeBlock
58+
static int inline git_CC_EVP_DecodeBlock(unsigned char *out,
59+
const unsigned char *in, int inlen)
60+
{
61+
CFErrorRef err;
62+
SecTransformRef decoder;
63+
CFDataRef input, output;
64+
CFIndex length;
65+
66+
decoder = SecDecodeTransformCreate(kSecBase64Encoding, &err);
67+
git_CC_error_check("SecEncodeTransformCreate failed: %ld", err);
68+
69+
input = CFDataCreate(kCFAllocatorDefault, in, inlen);
70+
SecTransformSetAttribute(decoder, kSecTransformInputAttributeName,
71+
input, &err);
72+
git_CC_error_check("SecTransformSetAttribute failed: %ld", err);
73+
74+
output = SecTransformExecute(decoder, &err);
75+
git_CC_error_check("SecTransformExecute failed: %ld", err);
76+
77+
length = CFDataGetLength(output);
78+
CFDataGetBytes(output, CFRangeMake(0, length), out);
79+
80+
CFRelease(output);
81+
CFRelease(input);
82+
CFRelease(decoder);
83+
84+
return (int)strlen((const char *)out);
85+
}
86+
#endif /* APPLE_LION_OR_NEWER */

git-compat-util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,16 @@ extern NORETURN void die_errno(const char *err, ...) __attribute__((format (prin
330330
extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
331331
extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
332332

333+
#ifndef NO_OPENSSL
334+
#ifdef APPLE_COMMON_CRYPTO
335+
#include "compat/apple-common-crypto.h"
336+
#else
337+
#include <openssl/evp.h>
338+
#include <openssl/hmac.h>
339+
#endif /* APPLE_COMMON_CRYPTO */
340+
#include <openssl/x509v3.h>
341+
#endif /* NO_OPENSSL */
342+
333343
/*
334344
* Let callers be aware of the constant return value; this can help
335345
* gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,

imap-send.c

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@
2828
#include "prompt.h"
2929
#ifdef NO_OPENSSL
3030
typedef void *SSL;
31-
#else
32-
#ifdef APPLE_COMMON_CRYPTO
33-
#include <CommonCrypto/CommonHMAC.h>
34-
#define HMAC_CTX CCHmacContext
35-
#define HMAC_Init(hmac, key, len, algo) CCHmacInit(hmac, algo, key, len)
36-
#define HMAC_Update CCHmacUpdate
37-
#define HMAC_Final(hmac, hash, ptr) CCHmacFinal(hmac, hash)
38-
#define HMAC_CTX_cleanup(ignore)
39-
#define EVP_md5() kCCHmacAlgMD5
40-
#else
41-
#include <openssl/evp.h>
42-
#include <openssl/hmac.h>
43-
#endif
44-
#include <openssl/x509v3.h>
4531
#endif
4632

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

0 commit comments

Comments
 (0)