forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d577450
commit 55b738c
Showing
11 changed files
with
190 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
examples/tv-casting-app/darwin/MatterTvCastingBridge/MatterTvCastingBridge/MTRCryptoUtils.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* | ||
* Copyright (c) 2023 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#ifndef MTRCryptoUtils_h | ||
#define MTRCryptoUtils_h | ||
|
||
@interface MTRCryptoUtils : NSObject | ||
|
||
/** | ||
* @brief Convert an ASN.1 DER signature (per X9.62) as used by TLS libraries to SEC1 raw format | ||
* | ||
* Errors are: | ||
* - CHIP_ERROR_INVALID_ARGUMENT on any argument being invalid (e.g. nullptr), wrong sizes, | ||
* wrong or unsupported format, | ||
* - CHIP_ERROR_BUFFER_TOO_SMALL on running out of space at runtime. | ||
* - CHIP_ERROR_INTERNAL on any unexpected processing error. | ||
* | ||
* @param[in] feLengthBytes Field Element length in bytes (e.g. 32 for P256 curve) | ||
* @param[in] asn1Signature ASN.1 DER signature input | ||
* @param[out] outRawSignature Raw signature of <r,s> concatenated format output buffer. Size must be at | ||
* least >= `2 * fe_length_bytes`. On success, the outRawSignature buffer will be re-assigned | ||
* to have the correct size (2 * feLengthBytes). | ||
* @return Returns an NSError on error, nil otherwise | ||
*/ | ||
+ (NSError * _Nullable)ecdsaAsn1SignatureToRawWithFeLengthBytes:(NSUInteger)feLengthBytes asn1Signature:(CFDataRef _Nonnull)asn1Signature outRawSignature:(NSData *_Nullable* _Nullable)outRawSignature; | ||
|
||
@end | ||
|
||
#endif /* MTRCryptoUtils_h */ |
53 changes: 53 additions & 0 deletions
53
examples/tv-casting-app/darwin/MatterTvCastingBridge/MatterTvCastingBridge/MTRCryptoUtils.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* | ||
* Copyright (c) 2020-2022 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#import "MTRCryptoUtils.h" | ||
|
||
#include <crypto/CHIPCryptoPAL.h> | ||
#include <lib/support/logging/CHIPLogging.h> | ||
#include <lib/support/Span.h> | ||
#include <lib/core/CHIPError.h> | ||
|
||
#include <Security/Security.h> | ||
|
||
@implementation MTRCryptoUtils | ||
|
||
+ (NSError *)ecdsaAsn1SignatureToRawWithFeLengthBytes:(NSUInteger)feLengthBytes asn1Signature:(CFDataRef)asn1Signature outRawSignature:(NSData **)outRawSignature | ||
{ | ||
// convert asn1Signature from CFDataRef to MutableByteSpan (asn1SignatureByteSpan) | ||
uint8_t mAsn1SignatureBytes[256]; | ||
chip::MutableByteSpan asn1SignatureByteSpan = chip::MutableByteSpan(mAsn1SignatureBytes, sizeof(mAsn1SignatureBytes)); | ||
size_t signatureLen = CFDataGetLength(asn1Signature); | ||
CFDataGetBytes(asn1Signature, CFRangeMake(0, signatureLen), asn1SignatureByteSpan.data()); | ||
asn1SignatureByteSpan.reduce_size(signatureLen); | ||
|
||
uint8_t *outRawSignatureBytes = new uint8_t[(*outRawSignature).length]; | ||
chip::MutableByteSpan outRawSignatureMutableByteSpan = chip::MutableByteSpan(outRawSignatureBytes, (*outRawSignature).length); | ||
|
||
CHIP_ERROR conversionError = chip::Crypto::EcdsaAsn1SignatureToRaw( | ||
feLengthBytes, chip::ByteSpan(asn1SignatureByteSpan.data(), asn1SignatureByteSpan.size()), outRawSignatureMutableByteSpan); | ||
if (CHIP_NO_ERROR != conversionError) { | ||
ChipLogError(AppServer, | ||
"DeviceAttestationCredentialsProviderImpl::SignWithDeviceAttestationKey failed to convert to raw signature."); | ||
|
||
//break; | ||
} | ||
*outRawSignature = [NSData dataWithBytes:outRawSignatureMutableByteSpan.data() length:outRawSignatureMutableByteSpan.size()]; | ||
return nil; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...s/tv-casting-app/darwin/TvCastingDarwin.xcworkspace/xcshareddata/swiftpm/Package.resolved
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"pins" : [ | ||
{ | ||
"identity" : "swift-asn1", | ||
"kind" : "remoteSourceControl", | ||
"location" : "https://github.com/apple/swift-asn1.git", | ||
"state" : { | ||
"revision" : "c7e239b5c1492ffc3ebd7fbcc7a92548ce4e78f0", | ||
"version" : "1.1.0" | ||
} | ||
} | ||
], | ||
"version" : 2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters