-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Darwin] add API to get public key without leaks (with fixes) #36340
base: master
Are you sure you want to change the base?
Changes from all commits
57423cd
cb67d1e
cbefb5a
456ac90
bc33c7b
84ffc4e
6a50d9d
a23b86e
f66949f
de11a1e
79aaaca
2bf82f0
c134be6
8a17443
8691282
47e264a
8dbbcaa
d328eaf
82379eb
fbbd06a
fdbd53c
33538e4
1cdc2f4
a761b1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,9 +127,13 @@ - (void)testGenerateIntermediateCert | |
__auto_type * intermediateKeys = [[MTRTestKeys alloc] init]; | ||
XCTAssertNotNil(intermediateKeys); | ||
|
||
__auto_type * intermediatePublicKey = [intermediateKeys copyPublicKey]; | ||
XCTAssert(intermediatePublicKey != NULL); | ||
CFAutorelease(intermediatePublicKey); | ||
|
||
__auto_type * intermediateCert = [MTRCertificates createIntermediateCertificate:rootKeys | ||
rootCertificate:rootCert | ||
intermediatePublicKey:intermediateKeys.publicKey | ||
intermediatePublicKey:intermediatePublicKey | ||
issuerID:nil | ||
fabricID:nil | ||
error:nil]; | ||
|
@@ -155,13 +159,16 @@ - (void)testGenerateIntermediateCertWithValidityPeriod | |
|
||
__auto_type * intermediateKeys = [[MTRTestKeys alloc] init]; | ||
XCTAssertNotNil(intermediateKeys); | ||
__auto_type * intermediatePublicKey = intermediateKeys.copyPublicKey; | ||
XCTAssert(intermediatePublicKey != NULL); | ||
CFAutorelease(intermediatePublicKey); | ||
|
||
__auto_type * startDate = [MTRCertificateTests startDateWithTimeIntervalSinceNow:300]; | ||
__auto_type * validityPeriod = [[NSDateInterval alloc] initWithStartDate:startDate duration:400]; | ||
|
||
__auto_type * intermediateCert = [MTRCertificates createIntermediateCertificate:rootKeys | ||
rootCertificate:rootCert | ||
intermediatePublicKey:intermediateKeys.publicKey | ||
intermediatePublicKey:intermediatePublicKey | ||
issuerID:nil | ||
fabricID:nil | ||
validityPeriod:validityPeriod | ||
|
@@ -192,13 +199,16 @@ - (void)testGenerateIntermediateCertWithInfiniteValidity | |
|
||
__auto_type * intermediateKeys = [[MTRTestKeys alloc] init]; | ||
XCTAssertNotNil(intermediateKeys); | ||
__auto_type * intermediatePublicKey = intermediateKeys.copyPublicKey; | ||
XCTAssert(intermediatePublicKey != NULL); | ||
CFAutorelease(intermediatePublicKey); | ||
|
||
__auto_type * startDate = [MTRCertificateTests startDateWithTimeIntervalSinceNow:300]; | ||
__auto_type * validityPeriod = [[NSDateInterval alloc] initWithStartDate:startDate endDate:[NSDate distantFuture]]; | ||
|
||
__auto_type * intermediateCert = [MTRCertificates createIntermediateCertificate:rootKeys | ||
rootCertificate:rootCert | ||
intermediatePublicKey:intermediateKeys.publicKey | ||
intermediatePublicKey:intermediatePublicKey | ||
issuerID:nil | ||
fabricID:nil | ||
validityPeriod:validityPeriod | ||
|
@@ -229,6 +239,9 @@ - (void)testGenerateOperationalCertNoIntermediate | |
|
||
__auto_type * operationalKeys = [[MTRTestKeys alloc] init]; | ||
XCTAssertNotNil(operationalKeys); | ||
__auto_type * operationalPublicKey = [operationalKeys copyPublicKey]; | ||
XCTAssert(operationalPublicKey != NULL); | ||
CFAutorelease(operationalPublicKey); | ||
|
||
__auto_type * cats = [[NSMutableSet alloc] initWithCapacity:3]; | ||
// High bits are identifier, low bits are version. | ||
|
@@ -238,7 +251,7 @@ - (void)testGenerateOperationalCertNoIntermediate | |
|
||
__auto_type * operationalCert = [MTRCertificates createOperationalCertificate:rootKeys | ||
signingCertificate:rootCert | ||
operationalPublicKey:operationalKeys.publicKey | ||
operationalPublicKey:operationalPublicKey | ||
fabricID:@1 | ||
nodeID:@1 | ||
caseAuthenticatedTags:cats | ||
|
@@ -265,6 +278,9 @@ - (void)testGenerateOperationalCertNoIntermediateWithValidityPeriod | |
|
||
__auto_type * operationalKeys = [[MTRTestKeys alloc] init]; | ||
XCTAssertNotNil(operationalKeys); | ||
__auto_type * operationalPublicKey = [operationalKeys copyPublicKey]; | ||
XCTAssert(operationalPublicKey != NULL); | ||
CFAutorelease(operationalPublicKey); | ||
|
||
__auto_type * cats = [[NSMutableSet alloc] initWithCapacity:3]; | ||
// High bits are identifier, low bits are version. | ||
|
@@ -277,7 +293,7 @@ - (void)testGenerateOperationalCertNoIntermediateWithValidityPeriod | |
|
||
__auto_type * operationalCert = [MTRCertificates createOperationalCertificate:rootKeys | ||
signingCertificate:rootCert | ||
operationalPublicKey:operationalKeys.publicKey | ||
operationalPublicKey:operationalPublicKey | ||
fabricID:@1 | ||
nodeID:@1 | ||
caseAuthenticatedTags:cats | ||
|
@@ -309,6 +325,9 @@ - (void)testGenerateOperationalCertNoIntermediateWithInfiniteValidity | |
|
||
__auto_type * operationalKeys = [[MTRTestKeys alloc] init]; | ||
XCTAssertNotNil(operationalKeys); | ||
__auto_type * operationalPublicKey = [operationalKeys copyPublicKey]; | ||
XCTAssert(operationalPublicKey != NULL); | ||
CFAutorelease(operationalPublicKey); | ||
|
||
__auto_type * cats = [[NSMutableSet alloc] initWithCapacity:3]; | ||
// High bits are identifier, low bits are version. | ||
|
@@ -321,7 +340,7 @@ - (void)testGenerateOperationalCertNoIntermediateWithInfiniteValidity | |
|
||
__auto_type * operationalCert = [MTRCertificates createOperationalCertificate:rootKeys | ||
signingCertificate:rootCert | ||
operationalPublicKey:operationalKeys.publicKey | ||
operationalPublicKey:operationalPublicKey | ||
fabricID:@1 | ||
nodeID:@1 | ||
caseAuthenticatedTags:cats | ||
|
@@ -353,21 +372,27 @@ - (void)testGenerateOperationalCertWithIntermediate | |
|
||
__auto_type * intermediateKeys = [[MTRTestKeys alloc] init]; | ||
XCTAssertNotNil(intermediateKeys); | ||
__auto_type * intermediatePublicKey = [intermediateKeys copyPublicKey]; | ||
XCTAssert(intermediatePublicKey != NULL); | ||
CFAutorelease(intermediatePublicKey); | ||
|
||
__auto_type * intermediateCert = [MTRCertificates createIntermediateCertificate:rootKeys | ||
rootCertificate:rootCert | ||
intermediatePublicKey:intermediateKeys.publicKey | ||
intermediatePublicKey:intermediatePublicKey | ||
issuerID:nil | ||
fabricID:nil | ||
error:nil]; | ||
XCTAssertNotNil(intermediateCert); | ||
|
||
__auto_type * operationalKeys = [[MTRTestKeys alloc] init]; | ||
XCTAssertNotNil(operationalKeys); | ||
__auto_type * operationalPublicKey = [operationalKeys copyPublicKey]; | ||
XCTAssert(operationalPublicKey != NULL); | ||
CFAutorelease(operationalPublicKey); | ||
|
||
__auto_type * operationalCert = [MTRCertificates createOperationalCertificate:intermediateKeys | ||
signingCertificate:intermediateCert | ||
operationalPublicKey:operationalKeys.publicKey | ||
operationalPublicKey:operationalPublicKey | ||
fabricID:@1 | ||
nodeID:@1 | ||
caseAuthenticatedTags:nil | ||
|
@@ -394,6 +419,9 @@ - (void)testGenerateOperationalCertErrorCases | |
|
||
__auto_type * operationalKeys = [[MTRTestKeys alloc] init]; | ||
XCTAssertNotNil(operationalKeys); | ||
__auto_type * operationalPublicKey = [operationalKeys copyPublicKey]; | ||
XCTAssert(operationalPublicKey != NULL); | ||
CFAutorelease(operationalPublicKey); | ||
|
||
__auto_type * longCats = [[NSMutableSet alloc] initWithCapacity:4]; | ||
[longCats addObject:@0x00010001]; | ||
|
@@ -415,7 +443,7 @@ - (void)testGenerateOperationalCertErrorCases | |
// Check basic case works | ||
__auto_type * operationalCert = [MTRCertificates createOperationalCertificate:rootKeys | ||
signingCertificate:rootCert | ||
operationalPublicKey:operationalKeys.publicKey | ||
operationalPublicKey:operationalPublicKey | ||
fabricID:@1 | ||
nodeID:@1 | ||
caseAuthenticatedTags:nil | ||
|
@@ -425,7 +453,7 @@ - (void)testGenerateOperationalCertErrorCases | |
// CATs too long | ||
operationalCert = [MTRCertificates createOperationalCertificate:rootKeys | ||
signingCertificate:rootCert | ||
operationalPublicKey:operationalKeys.publicKey | ||
operationalPublicKey:operationalPublicKey | ||
fabricID:@1 | ||
nodeID:@1 | ||
caseAuthenticatedTags:longCats | ||
|
@@ -435,7 +463,7 @@ - (void)testGenerateOperationalCertErrorCases | |
// Multiple CATs with the same identifier but different versions | ||
operationalCert = [MTRCertificates createOperationalCertificate:rootKeys | ||
signingCertificate:rootCert | ||
operationalPublicKey:operationalKeys.publicKey | ||
operationalPublicKey:operationalPublicKey | ||
fabricID:@1 | ||
nodeID:@1 | ||
caseAuthenticatedTags:catsWithSameIdentifier | ||
|
@@ -445,7 +473,7 @@ - (void)testGenerateOperationalCertErrorCases | |
// CAT with invalid version | ||
operationalCert = [MTRCertificates createOperationalCertificate:rootKeys | ||
signingCertificate:rootCert | ||
operationalPublicKey:operationalKeys.publicKey | ||
operationalPublicKey:operationalPublicKey | ||
fabricID:@1 | ||
nodeID:@1 | ||
caseAuthenticatedTags:catsWithInvalidVersion | ||
|
@@ -455,7 +483,7 @@ - (void)testGenerateOperationalCertErrorCases | |
// Signing key mismatch | ||
operationalCert = [MTRCertificates createOperationalCertificate:operationalKeys | ||
signingCertificate:rootCert | ||
operationalPublicKey:operationalKeys.publicKey | ||
operationalPublicKey:operationalPublicKey | ||
fabricID:@1 | ||
nodeID:@1 | ||
caseAuthenticatedTags:nil | ||
|
@@ -465,7 +493,7 @@ - (void)testGenerateOperationalCertErrorCases | |
// Invalid fabric id | ||
operationalCert = [MTRCertificates createOperationalCertificate:rootKeys | ||
signingCertificate:rootCert | ||
operationalPublicKey:operationalKeys.publicKey | ||
operationalPublicKey:operationalPublicKey | ||
fabricID:@0 | ||
nodeID:@1 | ||
caseAuthenticatedTags:nil | ||
|
@@ -475,7 +503,7 @@ - (void)testGenerateOperationalCertErrorCases | |
// Undefined node id | ||
operationalCert = [MTRCertificates createOperationalCertificate:rootKeys | ||
signingCertificate:rootCert | ||
operationalPublicKey:operationalKeys.publicKey | ||
operationalPublicKey:operationalPublicKey | ||
fabricID:@1 | ||
nodeID:@0 | ||
caseAuthenticatedTags:nil | ||
|
@@ -485,7 +513,7 @@ - (void)testGenerateOperationalCertErrorCases | |
// Non-operational node id | ||
operationalCert = [MTRCertificates createOperationalCertificate:rootKeys | ||
signingCertificate:rootCert | ||
operationalPublicKey:operationalKeys.publicKey | ||
operationalPublicKey:operationalPublicKey | ||
fabricID:@1 | ||
nodeID:@(0xFFFFFFFFFFFFFFFFLLU) | ||
caseAuthenticatedTags:nil | ||
|
@@ -504,7 +532,14 @@ - (void)testGenerateCSR | |
__auto_type * publicKey = [MTRCertificates publicKeyFromCSR:csr error:nil]; | ||
XCTAssertNotNil(publicKey); | ||
|
||
SecKeyRef originalKeyRef = [testKeys publicKey]; | ||
SecKeyRef originalKeyRef; | ||
if ([testKeys respondsToSelector:@selector(copyPublicKey)]) { | ||
originalKeyRef = [testKeys copyPublicKey]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This also looks like a leak. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (added There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (specifically: is manual creation of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You also need to nil check CFAutorelease, please just use [object autorelease] per our chat There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Otherwise you have a crash here potentially) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (discussing) |
||
CFAutorelease(originalKeyRef); | ||
} else { | ||
originalKeyRef = [testKeys publicKey]; | ||
} | ||
|
||
XCTAssertTrue(originalKeyRef != NULL); | ||
|
||
NSData * originalPublicKey = (__bridge_transfer NSData *) SecKeyCopyExternalRepresentation(originalKeyRef, nil); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not copy autorelease this? Makes for much easier code...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(discussing)