imap-send: use Apple's Security framework for base64 encoding

Use Apple's supported functions for base64 encoding instead of the deprecated OpenSSL functions. Signed-off-by: Jeremy Huddleston <jeremyhu@apple.com> Signed-off-by: David Aguilar <davvid@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeremy Huddleston committed Jul 29, 2013 at 18:28 UTC 3ef2bcad02efd6219f4ce599754c15e63b6af0dc
4 files changed +98 -14
Makefile
+1
@@ -1398,6 +1398,7 @@ ifdef PPC_SHA1
1398 LIB_H += ppc/sha1.h
1399 else
1400 ifdef APPLE_COMMON_CRYPTO
1401 + LIB_4_CRYPTO += -framework Security -framework CoreFoundation
1402 COMPAT_CFLAGS += -DCOMMON_DIGEST_FOR_OPENSSL
1403 SHA1_HEADER = <CommonCrypto/CommonDigest.h>
1404 else
compat/apple-common-crypto.h new
+86
@@ -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
+11
@@ -127,6 +127,17 @@
127 #else
128 #include <poll.h>
129 #endif
130 +
131 +#ifndef NO_OPENSSL
132 +#ifdef APPLE_COMMON_CRYPTO
133 +#include "compat/apple-common-crypto.h"
134 +#else
135 +#include <openssl/evp.h>
136 +#include <openssl/hmac.h>
137 +#endif /* APPLE_COMMON_CRYPTO */
138 +#include <openssl/x509v3.h>
139 +#endif /* NO_OPENSSL */
140 +
141 #if defined(__MINGW32__)
142 /* pull in Windows compatibility stuff */
143 #include "compat/mingw.h"
imap-send.c
-14
@@ -28,20 +28,6 @@
28 #include "prompt.h"
29 #ifdef NO_OPENSSL
30 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>
31 #endif
32
33 static const char imap_send_usage[] = "git imap-send < <mbox>";