Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 6e66b25

Browse files
authored
Fix common crypto includes for Xcode 10 (#442)
* Fix common crypto includes for Xcode 10: In theory, the COMMON_DIGEST_FOR_OPENSSL #define should work, which means that we can use the openssl (non-"CC"-prefixed) symbols. For reasons, I still can't get to the bottom of, this breaks on Xcode 10. However, now that compatibility with GNUstep is no longer required, we can just drop the whole openssl thing and use the CommonCrypto ("CC") symbols directly. This also makes the old touchdb code more consistent with later CDTDatastore/Encryption code which doesn't #define COMMON_DIGEST_FOR_OPENSSL. * Update copyrights * Fix a few more missing CC_ prefixed symbols * PR feedback
1 parent e29a01d commit 6e66b25

File tree

9 files changed

+76
-77
lines changed

9 files changed

+76
-77
lines changed

CDTDatastore/touchdb/TDBlobStore.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@
44
//
55
// Created by Jens Alfke on 12/10/11.
66
// Copyright (c) 2011 Couchbase, Inc. All rights reserved.
7+
// Copyright © 2018 IBM Corporation. All rights reserved.
8+
//
9+
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
10+
// except in compliance with the License. You may obtain a copy of the License at
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
// Unless required by applicable law or agreed to in writing, software distributed under the
13+
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14+
// either express or implied. See the License for the specific language governing permissions
15+
// and limitations under the License.
716
//
817

918
#import <Foundation/Foundation.h>
1019

1120
#import <FMDB/FMDB.h>
1221

13-
#ifdef GNUSTEP
14-
#import <openssl/md5.h>
15-
#import <openssl/sha.h>
16-
#else
17-
#define COMMON_DIGEST_FOR_OPENSSL
1822
#import <CommonCrypto/CommonDigest.h>
19-
#endif
2023

2124
#import "CDTEncryptionKeyProvider.h"
2225
#import "CDTBlobReader.h"
@@ -31,7 +34,7 @@ typedef NS_ENUM(NSInteger, CDTBlobStoreError) {
3134
/** Key identifying a data blob. This happens to be a SHA-1 digest. */
3235
typedef struct TDBlobKey
3336
{
34-
uint8_t bytes[SHA_DIGEST_LENGTH];
37+
uint8_t bytes[CC_SHA1_DIGEST_LENGTH];
3538
} TDBlobKey;
3639

3740
/** A persistent content-addressable store for arbitrary-size data blobs.
@@ -109,7 +112,7 @@ typedef struct TDBlobKey
109112

110113
typedef struct
111114
{
112-
uint8_t bytes[MD5_DIGEST_LENGTH];
115+
uint8_t bytes[CC_MD5_DIGEST_LENGTH];
113116
} TDMD5Key;
114117

115118
/** Lets you stream a large attachment to a TDBlobStore asynchronously, e.g. from a network
@@ -120,8 +123,8 @@ typedef struct
120123
NSString* _tempPath;
121124
id<CDTBlobWriter> _blobWriter;
122125
UInt64 _length;
123-
SHA_CTX _shaCtx;
124-
MD5_CTX _md5Ctx;
126+
CC_SHA1_CTX _shaCtx;
127+
CC_MD5_CTX _md5Ctx;
125128
TDBlobKey _blobKey;
126129
TDMD5Key _MD5Digest;
127130
}

CDTDatastore/touchdb/TDBlobStore.m

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Copyright (c) 2011 Couchbase, Inc. All rights reserved.
77
//
88
// Modifications for this distribution by Cloudant, Inc., Copyright (c) 2014 Cloudant, Inc.
9+
// Copyright © 2018 IBM Corporation. All rights reserved.
910
//
1011
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
1112
// except in compliance with the License. You may obtain a copy of the License at
@@ -81,10 +82,10 @@ + (TDBlobKey)keyForBlob:(NSData *)blob
8182
NSCParameterAssert(blob);
8283

8384
TDBlobKey key;
84-
SHA_CTX ctx;
85-
SHA1_Init(&ctx);
86-
SHA1_Update(&ctx, blob.bytes, blob.length);
87-
SHA1_Final(key.bytes, &ctx);
85+
CC_SHA1_CTX ctx;
86+
CC_SHA1_Init(&ctx);
87+
CC_SHA1_Update(&ctx, blob.bytes, blob.length);
88+
CC_SHA1_Final(key.bytes, &ctx);
8889

8990
return key;
9091
}
@@ -289,8 +290,8 @@ - (id)initWithStore:(TDBlobStore*)store
289290
self = [super init];
290291
if (self) {
291292
_store = store;
292-
SHA1_Init(&_shaCtx);
293-
MD5_Init(&_md5Ctx);
293+
CC_SHA1_Init(&_shaCtx);
294+
CC_MD5_Init(&_md5Ctx);
294295

295296
// Open a temporary file in the store's temporary directory:
296297
NSString* filename = [TDCreateUUID() stringByAppendingPathExtension:@"blobtmp"];
@@ -312,8 +313,8 @@ - (void)appendData:(NSData*)data
312313
[_blobWriter appendData:data];
313314
NSUInteger dataLen = data.length;
314315
_length += dataLen;
315-
SHA1_Update(&_shaCtx, data.bytes, dataLen);
316-
MD5_Update(&_md5Ctx, data.bytes, dataLen);
316+
CC_SHA1_Update(&_shaCtx, data.bytes, dataLen);
317+
CC_MD5_Update(&_md5Ctx, data.bytes, dataLen);
317318
}
318319

319320
- (void)closeFile
@@ -326,8 +327,8 @@ - (void)finish
326327
{
327328
Assert(_blobWriter, @"Already finished");
328329
[self closeFile];
329-
SHA1_Final(_blobKey.bytes, &_shaCtx);
330-
MD5_Final(_MD5Digest.bytes, &_md5Ctx);
330+
CC_SHA1_Final(_blobKey.bytes, &_shaCtx);
331+
CC_MD5_Final(_MD5Digest.bytes, &_md5Ctx);
331332
}
332333

333334
- (NSString*)MD5DigestString

CDTDatastore/touchdb/TDMisc.m

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55
// Created by Jens Alfke on 1/13/12.
66
// Copyright (c) 2012 Couchbase, Inc. All rights reserved.
7+
// Copyright © 2018 IBM Corporation. All rights reserved.
78
//
89
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
910
// except in compliance with the License. You may obtain a copy of the License at
@@ -20,14 +21,8 @@
2021
#import "CollectionUtils.h"
2122
#import <errno.h>
2223

23-
#ifdef GNUSTEP
24-
#import <openssl/sha.h>
25-
#import <uuid/uuid.h> // requires installing "uuid-dev" package on Ubuntu
26-
#else
27-
#define COMMON_DIGEST_FOR_OPENSSL
2824
#import <CommonCrypto/CommonDigest.h>
2925
#import <CommonCrypto/CommonHMAC.h>
30-
#endif
3126

3227
NSString* TDCreateUUID()
3328
{
@@ -54,31 +49,31 @@
5449

5550
NSData* TDSHA1Digest(NSData* input)
5651
{
57-
unsigned char digest[SHA_DIGEST_LENGTH];
58-
SHA_CTX ctx;
59-
SHA1_Init(&ctx);
60-
SHA1_Update(&ctx, input.bytes, input.length);
61-
SHA1_Final(digest, &ctx);
52+
unsigned char digest[CC_SHA1_DIGEST_LENGTH];
53+
CC_SHA1_CTX ctx;
54+
CC_SHA1_Init(&ctx);
55+
CC_SHA1_Update(&ctx, input.bytes, input.length);
56+
CC_SHA1_Final(digest, &ctx);
6257
return [NSData dataWithBytes:&digest length:sizeof(digest)];
6358
}
6459

6560
NSData* TDSHA256Digest(NSData* input)
6661
{
67-
unsigned char digest[SHA256_DIGEST_LENGTH];
68-
SHA256_CTX ctx;
69-
SHA256_Init(&ctx);
70-
SHA256_Update(&ctx, input.bytes, input.length);
71-
SHA256_Final(digest, &ctx);
62+
unsigned char digest[CC_SHA256_DIGEST_LENGTH];
63+
CC_SHA256_CTX ctx;
64+
CC_SHA256_Init(&ctx);
65+
CC_SHA256_Update(&ctx, input.bytes, input.length);
66+
CC_SHA256_Final(digest, &ctx);
7267
return [NSData dataWithBytes:&digest length:sizeof(digest)];
7368
}
7469

7570
NSString* TDHexSHA1Digest(NSData* input)
7671
{
77-
unsigned char digest[SHA_DIGEST_LENGTH];
78-
SHA_CTX ctx;
79-
SHA1_Init(&ctx);
80-
SHA1_Update(&ctx, input.bytes, input.length);
81-
SHA1_Final(digest, &ctx);
72+
unsigned char digest[CC_SHA1_DIGEST_LENGTH];
73+
CC_SHA1_CTX ctx;
74+
CC_SHA1_Init(&ctx);
75+
CC_SHA1_Update(&ctx, input.bytes, input.length);
76+
CC_SHA1_Final(digest, &ctx);
8277
return TDHexFromBytes(&digest, sizeof(digest));
8378
}
8479

@@ -93,14 +88,14 @@
9388

9489
NSData* TDHMACSHA1(NSData* key, NSData* data)
9590
{
96-
UInt8 hmac[SHA_DIGEST_LENGTH];
91+
UInt8 hmac[CC_SHA1_DIGEST_LENGTH];
9792
CCHmac(kCCHmacAlgSHA1, key.bytes, key.length, data.bytes, data.length, &hmac);
9893
return [NSData dataWithBytes:hmac length:sizeof(hmac)];
9994
}
10095

10196
NSData* TDHMACSHA256(NSData* key, NSData* data)
10297
{
103-
UInt8 hmac[SHA256_DIGEST_LENGTH];
98+
UInt8 hmac[CC_SHA256_DIGEST_LENGTH];
10499
CCHmac(kCCHmacAlgSHA256, key.bytes, key.length, data.bytes, data.length, &hmac);
105100
return [NSData dataWithBytes:hmac length:sizeof(hmac)];
106101
}

CDTDatastore/touchdb/TD_Database+BlobFilenames.m

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55
// Created by Enrique de la Torre Fernandez on 29/05/2015.
66
// Copyright (c) 2015 IBM Cloudant. All rights reserved.
7+
// Copyright © 2018 IBM Corporation. All rights reserved.
78
//
89
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
910
// except in compliance with the License. You may obtain a copy of the License at
@@ -118,7 +119,7 @@ + (NSArray *)rowsInBlobFilenamesTableInDatabase:(FMDatabase *)db
118119
NSData *keyData = dataFromHexadecimalString(hexKey);
119120

120121
TDBlobKey key;
121-
[keyData getBytes:key.bytes length:SHA_DIGEST_LENGTH];
122+
[keyData getBytes:key.bytes length:CC_SHA1_DIGEST_LENGTH];
122123

123124
NSString *blobFilename = [r stringForColumn:TDDatabaseBlobFilenamesColumnFilename];
124125

@@ -204,8 +205,8 @@ + (BOOL)insertFilename:(NSString *)filename
204205

205206
+ (NSString *)generateRandomBlobFilename
206207
{
207-
uint8_t randBytes[SHA_DIGEST_LENGTH];
208-
arc4random_buf(randBytes, SHA_DIGEST_LENGTH);
208+
uint8_t randBytes[CC_SHA1_DIGEST_LENGTH];
209+
arc4random_buf(randBytes, CC_SHA1_DIGEST_LENGTH);
209210

210211
NSString *randStr = TDHexFromBytes(randBytes, sizeof(randBytes));
211212

@@ -260,4 +261,4 @@ + (instancetype)rowWithKey:(TDBlobKey)key blobFilename:(NSString *)blobFilename
260261
return [[[self class] alloc] initWithKey:key blobFilename:blobFilename];
261262
}
262263

263-
@end
264+
@end

CDTDatastore/touchdb/TD_Database+Insertion.m

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Copyright (c) 2011 Couchbase, Inc. All rights reserved.
77
//
88
// Modifications for this distribution by Cloudant, Inc., Copyright (c) 2014 Cloudant, Inc.
9+
// Copyright © 2018 IBM Corporation. All rights reserved.
910
//
1011
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
1112
// except in compliance with the License. You may obtain a copy of the License at
@@ -33,12 +34,7 @@
3334

3435
#import "CDTLogging.h"
3536

36-
#ifdef GNUSTEP
37-
#import <openssl/sha.h>
38-
#else
39-
#define COMMON_DIGEST_FOR_OPENSSL
4037
#import <CommonCrypto/CommonDigest.h>
41-
#endif
4238

4339
NSString* const TD_DatabaseChangeNotification = @"TD_DatabaseChange";
4440

@@ -91,28 +87,28 @@ - (NSString*)generateIDForRevision:(TD_Revision*)rev
9187
// Generate a digest for this revision based on the previous revision ID, document JSON,
9288
// and attachment digests. This doesn't need to be secure; we just need to ensure that this
9389
// code consistently generates the same ID given equivalent revisions.
94-
MD5_CTX ctx;
95-
unsigned char digestBytes[MD5_DIGEST_LENGTH];
96-
MD5_Init(&ctx);
90+
CC_MD5_CTX ctx;
91+
unsigned char digestBytes[CC_MD5_DIGEST_LENGTH];
92+
CC_MD5_Init(&ctx);
9793

9894
NSData* prevIDUTF8 = [prevID dataUsingEncoding:NSUTF8StringEncoding];
9995
NSUInteger length = prevIDUTF8.length;
10096
if (length > 0xFF) return nil;
10197
uint8_t lengthByte = length & 0xFF;
102-
MD5_Update(&ctx, &lengthByte, 1); // prefix with length byte
103-
if (length > 0) MD5_Update(&ctx, prevIDUTF8.bytes, length);
98+
CC_MD5_Update(&ctx, &lengthByte, 1); // prefix with length byte
99+
if (length > 0) CC_MD5_Update(&ctx, prevIDUTF8.bytes, length);
104100

105101
uint8_t deletedByte = rev.deleted != NO;
106-
MD5_Update(&ctx, &deletedByte, 1);
102+
CC_MD5_Update(&ctx, &deletedByte, 1);
107103

108104
for (NSString* attName in [attachments.allKeys sortedArrayUsingSelector:@selector(compare:)]) {
109105
TD_Attachment* attachment = attachments[attName];
110-
MD5_Update(&ctx, &attachment->blobKey, sizeof(attachment->blobKey));
106+
CC_MD5_Update(&ctx, &attachment->blobKey, sizeof(attachment->blobKey));
111107
}
112108

113-
MD5_Update(&ctx, json.bytes, json.length);
109+
CC_MD5_Update(&ctx, json.bytes, json.length);
114110

115-
MD5_Final(digestBytes, &ctx);
111+
CC_MD5_Final(digestBytes, &ctx);
116112
NSString* digest = TDHexFromBytes(digestBytes, sizeof(digestBytes));
117113
return [NSString stringWithFormat:@"%u-%@", generation + 1, digest];
118114
}

CDTDatastoreTests/Attachments/AttachmentCRUD.m

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ - (void)testCreate
143143
NSData *data = dataFromHexadecimalString(@"D55F9AC778BAF2256FA4DE87AAC61F590EBE66E0");
144144

145145
TDBlobKey key;
146-
[data getBytes:key.bytes length:SHA_DIGEST_LENGTH];
146+
[data getBytes:key.bytes length:CC_SHA1_DIGEST_LENGTH];
147147

148148
filename = [TD_Database filenameForKey:key inBlobFilenamesTableInDatabase:db];
149149
}];
@@ -215,7 +215,7 @@ - (void)testCreateWithMutableBodyAndAttachments
215215
NSData *data = dataFromHexadecimalString(@"D55F9AC778BAF2256FA4DE87AAC61F590EBE66E0");
216216

217217
TDBlobKey key;
218-
[data getBytes:key.bytes length:SHA_DIGEST_LENGTH];
218+
[data getBytes:key.bytes length:CC_SHA1_DIGEST_LENGTH];
219219

220220
filename = [TD_Database filenameForKey:key inBlobFilenamesTableInDatabase:db];
221221
}];
@@ -275,7 +275,7 @@ - (void) testUpdatingDocumentRetainsAttachments
275275
NSData *data = dataFromHexadecimalString(@"D55F9AC778BAF2256FA4DE87AAC61F590EBE66E0");
276276

277277
TDBlobKey key;
278-
[data getBytes:key.bytes length:SHA_DIGEST_LENGTH];
278+
[data getBytes:key.bytes length:CC_SHA1_DIGEST_LENGTH];
279279

280280
filename = [TD_Database filenameForKey:key inBlobFilenamesTableInDatabase:db];
281281
}];
@@ -377,13 +377,13 @@ - (void) testMultipleAttachments
377377
NSData *data = dataFromHexadecimalString(@"D55F9AC778BAF2256FA4DE87AAC61F590EBE66E0");
378378

379379
TDBlobKey key;
380-
[data getBytes:key.bytes length:SHA_DIGEST_LENGTH];
380+
[data getBytes:key.bytes length:CC_SHA1_DIGEST_LENGTH];
381381

382382
filenameImage = [TD_Database filenameForKey:key inBlobFilenamesTableInDatabase:db];
383383

384384
data = dataFromHexadecimalString(@"3FF2989BCCF52150BBA806BAE1DB2E0B06AD6F88");
385385

386-
[data getBytes:key.bytes length:SHA_DIGEST_LENGTH];
386+
[data getBytes:key.bytes length:CC_SHA1_DIGEST_LENGTH];
387387

388388
filenameText = [TD_Database filenameForKey:key inBlobFilenamesTableInDatabase:db];
389389
}];
@@ -475,13 +475,13 @@ - (void) testAddAttachments
475475
NSData *data = dataFromHexadecimalString(@"D55F9AC778BAF2256FA4DE87AAC61F590EBE66E0");
476476

477477
TDBlobKey key;
478-
[data getBytes:key.bytes length:SHA_DIGEST_LENGTH];
478+
[data getBytes:key.bytes length:CC_SHA1_DIGEST_LENGTH];
479479

480480
filenameImage = [TD_Database filenameForKey:key inBlobFilenamesTableInDatabase:db];
481481

482482
data = dataFromHexadecimalString(@"3FF2989BCCF52150BBA806BAE1DB2E0B06AD6F88");
483483

484-
[data getBytes:key.bytes length:SHA_DIGEST_LENGTH];
484+
[data getBytes:key.bytes length:CC_SHA1_DIGEST_LENGTH];
485485

486486
filenameText = [TD_Database filenameForKey:key inBlobFilenamesTableInDatabase:db];
487487
}];
@@ -610,13 +610,13 @@ - (void)testUpdate
610610
NSData *data = dataFromHexadecimalString(@"D55F9AC778BAF2256FA4DE87AAC61F590EBE66E0");
611611

612612
TDBlobKey key;
613-
[data getBytes:key.bytes length:SHA_DIGEST_LENGTH];
613+
[data getBytes:key.bytes length:CC_SHA1_DIGEST_LENGTH];
614614

615615
filenameImage = [TD_Database filenameForKey:key inBlobFilenamesTableInDatabase:db];
616616

617617
data = dataFromHexadecimalString(@"3FF2989BCCF52150BBA806BAE1DB2E0B06AD6F88");
618618

619-
[data getBytes:key.bytes length:SHA_DIGEST_LENGTH];
619+
[data getBytes:key.bytes length:CC_SHA1_DIGEST_LENGTH];
620620

621621
filenameText = [TD_Database filenameForKey:key inBlobFilenamesTableInDatabase:db];
622622
}];
@@ -699,7 +699,7 @@ - (void)testDelete
699699
NSData *data = dataFromHexadecimalString(@"D55F9AC778BAF2256FA4DE87AAC61F590EBE66E0");
700700

701701
TDBlobKey key;
702-
[data getBytes:key.bytes length:SHA_DIGEST_LENGTH];
702+
[data getBytes:key.bytes length:CC_SHA1_DIGEST_LENGTH];
703703

704704
filename = [TD_Database filenameForKey:key inBlobFilenamesTableInDatabase:db];
705705
}];
@@ -774,7 +774,7 @@ - (void) testCDTUnsavedFileAttachment
774774
NSData *data = dataFromHexadecimalString(@"D55F9AC778BAF2256FA4DE87AAC61F590EBE66E0");
775775

776776
TDBlobKey key;
777-
[data getBytes:key.bytes length:SHA_DIGEST_LENGTH];
777+
[data getBytes:key.bytes length:CC_SHA1_DIGEST_LENGTH];
778778

779779
filename = [TD_Database filenameForKey:key inBlobFilenamesTableInDatabase:db];
780780
}];

CDTDatastoreTests/Encryption/TDBlobStoreEncryptionTests.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ - (void)testBlobForKeySucceedsIfKeyIsAMigratedKey
286286
NSData *data = dataFromHexadecimalString(TDBLOBSTOREENCRYPTIONTESTS_LOREM_SHA1DIGEST);
287287

288288
TDBlobKey key;
289-
[data getBytes:key.bytes length:SHA_DIGEST_LENGTH];
289+
[data getBytes:key.bytes length:CC_SHA1_DIGEST_LENGTH];
290290

291291
reader = [_blobStore blobForKey:key withDatabase:db];
292292
}];

0 commit comments

Comments
 (0)