Skip to content

Commit 3c65c96

Browse files
committed
Fixes SRP authentication on iOS 14 and macOS 11
The memory layout of ccsrp_ctx changed in iOS 14/macOS 11, causing errors authenticating when using older corecrypto headers. We now dynamically cast ccsrp_ctx to the correct layout when accessing its cnn value to ensure we don’t read invalid memory.
1 parent db6cadf commit 3c65c96

5 files changed

Lines changed: 68 additions & 10 deletions

File tree

AltSign.podspec

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ Pod::Spec.new do |spec|
1616
spec.resources = "AltSign/Resources/apple.pem"
1717
spec.library = "c++"
1818

19+
spec.xcconfig = {
20+
"OTHER_CFLAGS" => "-DCORECRYPTO_DONOT_USE_TRANSPARENT_UNION=1"
21+
}
22+
1923
# Somewhat hacky subspec usage to ensure directory hierarchies match what header includes expect.
2024

2125
spec.subspec 'OpenSSL' do |base|
@@ -49,8 +53,9 @@ Pod::Spec.new do |spec|
4953
end
5054

5155
spec.subspec 'CoreCrypto' do |base|
52-
base.source_files = "Dependencies/corecrypto/*.h"
56+
base.source_files = "Dependencies/corecrypto/*.{h,m}"
5357
base.exclude_files = "Dependencies/corecrypto/ccperf.h"
58+
base.private_header_files = "Dependencies/corecrypto/*.h"
5459
base.header_mappings_dir = "Dependencies"
5560
end
5661

AltSign.xcodeproj/project.pbxproj

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
BF5AB3CA2286269B00DC914B /* ALTDevice.m in Sources */ = {isa = PBXBuildFile; fileRef = BF5AB3C82286269B00DC914B /* ALTDevice.m */; };
6060
BF5AB3CD228645DF00DC914B /* ALTCertificate.h in Headers */ = {isa = PBXBuildFile; fileRef = BF5AB3CB228645DF00DC914B /* ALTCertificate.h */; settings = {ATTRIBUTES = (Public, ); }; };
6161
BF5AB3CE228645DF00DC914B /* ALTCertificate.m in Sources */ = {isa = PBXBuildFile; fileRef = BF5AB3CC228645DF00DC914B /* ALTCertificate.m */; };
62+
BF5C690D24A5205E00C2F854 /* ccsrp.m in Sources */ = {isa = PBXBuildFile; fileRef = BF5C690C24A5205E00C2F854 /* ccsrp.m */; };
63+
BF5C690E24A5205E00C2F854 /* ccsrp.m in Sources */ = {isa = PBXBuildFile; fileRef = BF5C690C24A5205E00C2F854 /* ccsrp.m */; };
6264
BF5D43F6237F53BB00EC8745 /* ALTAppleAPISession.h in Headers */ = {isa = PBXBuildFile; fileRef = BF5D43F4237F53BB00EC8745 /* ALTAppleAPISession.h */; settings = {ATTRIBUTES = (Public, ); }; };
6365
BF5D43F7237F53BB00EC8745 /* ALTAppleAPISession.h in Headers */ = {isa = PBXBuildFile; fileRef = BF5D43F4237F53BB00EC8745 /* ALTAppleAPISession.h */; settings = {ATTRIBUTES = (Public, ); }; };
6466
BF5D43F8237F53BB00EC8745 /* ALTAppleAPISession.m in Sources */ = {isa = PBXBuildFile; fileRef = BF5D43F5237F53BB00EC8745 /* ALTAppleAPISession.m */; };
@@ -240,6 +242,7 @@
240242
BF5AB3C82286269B00DC914B /* ALTDevice.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ALTDevice.m; sourceTree = "<group>"; };
241243
BF5AB3CB228645DF00DC914B /* ALTCertificate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ALTCertificate.h; sourceTree = "<group>"; };
242244
BF5AB3CC228645DF00DC914B /* ALTCertificate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ALTCertificate.m; sourceTree = "<group>"; };
245+
BF5C690C24A5205E00C2F854 /* ccsrp.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ccsrp.m; path = Dependencies/corecrypto/ccsrp.m; sourceTree = SOURCE_ROOT; };
243246
BF5D43F4237F53BB00EC8745 /* ALTAppleAPISession.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ALTAppleAPISession.h; sourceTree = "<group>"; };
244247
BF5D43F5237F53BB00EC8745 /* ALTAppleAPISession.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ALTAppleAPISession.m; sourceTree = "<group>"; };
245248
BF9B633E229DC27C002F0A62 /* ALTModel+Internal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "ALTModel+Internal.h"; sourceTree = "<group>"; };
@@ -392,7 +395,7 @@
392395
BF48CFE0229435CB0004760B /* Dependencies */ = {
393396
isa = PBXGroup;
394397
children = (
395-
BFE20E0D237CAA2500409FF7 /* corecrypto-headers */,
398+
BFE20E0D237CAA2500409FF7 /* corecrypto */,
396399
BF48CFE1229435DB0004760B /* OpenSSL.xcodeproj */,
397400
BF9B63D3229DD8E8002F0A62 /* minizip */,
398401
);
@@ -627,7 +630,7 @@
627630
name = libcnary;
628631
sourceTree = "<group>";
629632
};
630-
BFE20E0D237CAA2500409FF7 /* corecrypto-headers */ = {
633+
BFE20E0D237CAA2500409FF7 /* corecrypto */ = {
631634
isa = PBXGroup;
632635
children = (
633636
BFE20E28237CAA3B00409FF7 /* cc_config.h */,
@@ -699,6 +702,7 @@
699702
BFE20E20237CAA3B00409FF7 /* ccsha2.h */,
700703
BFE20E58237CAA3E00409FF7 /* ccsrp_gp.h */,
701704
BFE20E42237CAA3D00409FF7 /* ccsrp.h */,
705+
BF5C690C24A5205E00C2F854 /* ccsrp.m */,
702706
BFE20E29237CAA3B00409FF7 /* cctest.h */,
703707
BFE20E4F237CAA3E00409FF7 /* ccwrap.h */,
704708
BFE20E27237CAA3B00409FF7 /* ccz_priv.h */,
@@ -707,7 +711,7 @@
707711
BFE20E57237CAA3E00409FF7 /* cczp.h */,
708712
BFE20E3B237CAA3C00409FF7 /* fipspost.h */,
709713
);
710-
name = "corecrypto-headers";
714+
name = corecrypto;
711715
sourceTree = "<group>";
712716
};
713717
/* End PBXGroup section */
@@ -933,6 +937,7 @@
933937
BF9B640A229E0AA0002F0A62 /* unzip.c in Sources */,
934938
BF5AB3CE228645DF00DC914B /* ALTCertificate.m in Sources */,
935939
BF5AB3BA2286024D00DC914B /* ALTAccount.m in Sources */,
940+
BF5C690D24A5205E00C2F854 /* ccsrp.m in Sources */,
936941
BF50E7C122C163DC0070E17B /* ALTApplication.mm in Sources */,
937942
BF9B6402229E0AA0002F0A62 /* ioapi.c in Sources */,
938943
BF9B640C229E0AA0002F0A62 /* zip.c in Sources */,
@@ -961,6 +966,7 @@
961966
BF9B640B229E0AA0002F0A62 /* unzip.c in Sources */,
962967
BF9B6390229DCF3A002F0A62 /* ALTCertificate.m in Sources */,
963968
BF9B6391229DCF3A002F0A62 /* ALTAccount.m in Sources */,
969+
BF5C690E24A5205E00C2F854 /* ccsrp.m in Sources */,
964970
BF50E7C222C163DC0070E17B /* ALTApplication.mm in Sources */,
965971
BF9B6403229E0AA0002F0A62 /* ioapi.c in Sources */,
966972
BF9B640D229E0AA0002F0A62 /* zip.c in Sources */,
@@ -1078,6 +1084,7 @@
10781084
MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE;
10791085
MTL_FAST_MATH = YES;
10801086
ONLY_ACTIVE_ARCH = YES;
1087+
OTHER_CFLAGS = "-DCORECRYPTO_DONOT_USE_TRANSPARENT_UNION";
10811088
SDKROOT = iphoneos;
10821089
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
10831090
VERSIONING_SYSTEM = "apple-generic";
@@ -1138,6 +1145,7 @@
11381145
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
11391146
MTL_ENABLE_DEBUG_INFO = NO;
11401147
MTL_FAST_MATH = YES;
1148+
OTHER_CFLAGS = "-DCORECRYPTO_DONOT_USE_TRANSPARENT_UNION";
11411149
SDKROOT = iphoneos;
11421150
SUPPORTED_PLATFORMS = "iphonesimulator iphoneos";
11431151
VALIDATE_PRODUCT = YES;

AltSign/Apple API/ALTAppleAPI+Authentication.m

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,10 @@ - (void)authenticateWithAppleID:(NSString *)appleID
217217
ccdigest_init(di_info, di_ctx);
218218

219219
const struct ccdigest_info *srp_di = ccsha256_di();
220-
struct ccsrp_ctx_body *srp_ctx = (struct ccsrp_ctx_body *)malloc(ccsrp_sizeof_srp(di_info, gp));
220+
struct ccsrp_ctx *srp_ctx = (struct ccsrp_ctx *)malloc(ccsrp_sizeof_srp(di_info, gp));
221221
ccsrp_ctx_init(srp_ctx, srp_di, gp);
222-
223-
srp_ctx->hdr.blinding_rng = ccrng(NULL);
224-
srp_ctx->hdr.flags.noUsernameInX = true;
222+
ccsrp_client_set_noUsernameInX(srp_ctx, true);
223+
SRP_RNG(srp_ctx) = ccrng(NULL);
225224

226225
NSArray<NSString *> *ps = @[@"s2k", @"s2k_fo"];
227226
ALTDigestUpdateString(di_info, di_ctx, ps[0]);

Dependencies/corecrypto/ccsrp.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ typedef ccdh_gp_t ccsrp_gp_t;
119119
typedef ccdh_const_gp_t ccsrp_const_gp_t;
120120

121121
struct ccsrp_ctx {
122+
const struct ccdigest_info *di;
123+
ccsrp_const_gp_t gp;
124+
struct ccrng_state *blinding_rng;
125+
struct {
126+
unsigned int authenticated:1;
127+
unsigned int noUsernameInX:1;
128+
unsigned int sessionkey:1;
129+
unsigned int variant:16;
130+
} flags;
131+
cc_unit ccn[1];
132+
} CC_ALIGNED(16);
133+
typedef struct ccsrp_ctx *ccsrp_ctx_t;
134+
135+
struct ccsrp_ctx_legacy {
122136
const struct ccdigest_info *di;
123137
ccsrp_const_gp_t gp;
124138
struct ccrng_state *blinding_rng;
@@ -134,7 +148,9 @@ struct ccsrp_ctx {
134148
sizeof(bool))];
135149
cc_unit ccn[1];
136150
} CC_ALIGNED(16);
137-
typedef struct ccsrp_ctx *ccsrp_ctx_t;
151+
typedef struct ccsrp_ctx_legacy *ccsrp_ctx_t_legacy;
152+
153+
cc_unit *srp_ccn(void *srp);
138154

139155
#endif
140156

@@ -175,7 +191,7 @@ typedef struct ccsrp_ctx *ccsrp_ctx_t;
175191
#define ccsrp_ctx_gp_l(KEY) (ccdh_gp_l(ccsrp_ctx_gp(KEY)))
176192
#define ccsrp_ctx_n(KEY) (ccdh_gp_n(ccsrp_ctx_gp(KEY)))
177193
#define ccsrp_ctx_prime(KEY) (ccdh_gp_prime(ccsrp_ctx_gp(KEY)))
178-
#define ccsrp_ctx_ccn(KEY) SRP_CCN((ccsrp_ctx_t)(KEY))
194+
#define ccsrp_ctx_ccn(KEY) srp_ccn(KEY)
179195
#define ccsrp_ctx_pki_key(KEY,_N_) (ccsrp_ctx_ccn(KEY) + ccsrp_ctx_n(KEY) * _N_)
180196
#define ccsrp_ctx_public(KEY) (ccsrp_ctx_pki_key(KEY,0))
181197
#define ccsrp_ctx_private(KEY) (ccsrp_ctx_pki_key(KEY,1))

Dependencies/corecrypto/ccsrp.m

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// ccsrp.c
3+
// AltSign
4+
//
5+
// Created by Riley Testut on 6/25/20.
6+
// Copyright © 2020 Riley Testut. All rights reserved.
7+
//
8+
9+
#include "ccsrp.h"
10+
11+
#import <Foundation/Foundation.h>
12+
13+
cc_unit *srp_ccn(void *srp)
14+
{
15+
// Memory layout of ccsrp_ctx changed between iOS 13/macOS 10.15 and iOS 14/macOS 11.
16+
// Dynamically cast to correct memory layout to ensure we access valid memory.
17+
18+
#if TARGET_OS_IPHONE
19+
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ 14, 0, 0 }])
20+
#else
21+
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){ 11, 0, 0 }])
22+
#endif
23+
{
24+
return SRP_CCN((ccsrp_ctx_t)(srp));
25+
}
26+
else
27+
{
28+
return SRP_CCN((ccsrp_ctx_t_legacy)(srp));
29+
}
30+
}

0 commit comments

Comments
 (0)