Skip to content

Commit 70b5c84

Browse files
committed
ECC key support
IB-8381 Signed-off-by: Raul Metsma <raul@metsma.ee>
1 parent 7bbf07d commit 70b5c84

3 files changed

Lines changed: 33 additions & 17 deletions

File tree

CMakeLists.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ set(VERSION ${PROJECT_VERSION}.${BUILD_NUMBER})
1414
set_env( CONFIG_URL "https://id.eesti.ee/config.json" CACHE STRING "Set Config URL" )
1515
set_env( SIGNCERT "" CACHE STRING "Common name of certificate to used sign binaries, empty skip signing" )
1616
add_definitions( -DCONFIG_URL="${CONFIG_URL}" )
17-
string( REPLACE ".json" ".pub" PUB_URL ${CONFIG_URL} )
18-
file( DOWNLOAD ${PUB_URL} ${CMAKE_CURRENT_BINARY_DIR}/config.pub )
17+
string( REPLACE ".json" ".ecpub" PUB_URL ${CONFIG_URL} )
18+
message("Fetching pub key: ${PUB_URL}")
19+
file(DOWNLOAD ${PUB_URL} ${CMAKE_CURRENT_BINARY_DIR}/config.ecpub)
1920

2021
if( APPLE )
2122
add_custom_command( OUTPUT config.h
22-
COMMAND xxd -i config.pub config.h
23+
COMMAND xxd -i config.ecpub config.h
2324
COMMENT "Generating config.h"
2425
)
2526
include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
@@ -93,9 +94,9 @@ else()
9394
if(NOT EXISTS ${CMAKE_SOURCE_DIR}/common/CMakeLists.txt)
9495
message(FATAL_ERROR "cmake submodule directory empty, did you 'git clone --recursive'?")
9596
endif()
96-
file( DOWNLOAD ${CONFIG_URL} ${CMAKE_CURRENT_BINARY_DIR}/config.json )
97-
string( REPLACE ".json" ".rsa" RSA_URL ${CONFIG_URL} )
98-
file( DOWNLOAD ${RSA_URL} ${CMAKE_CURRENT_BINARY_DIR}/config.rsa )
97+
file(DOWNLOAD ${CONFIG_URL} ${CMAKE_CURRENT_BINARY_DIR}/config.json)
98+
string(REPLACE ".json" ".ecc" ECC_URL ${CONFIG_URL})
99+
file(DOWNLOAD ${ECC_URL} ${CMAKE_CURRENT_BINARY_DIR}/config.ecc)
99100
set(CONFIG_DIR ${CMAKE_CURRENT_BINARY_DIR})
100101

101102
find_package(OpenSSL 3.0.0 REQUIRED)
@@ -142,7 +143,7 @@ else()
142143
)
143144
qt_add_resources(${PROJECT_NAME} icon FILES appicon.png)
144145
qt_add_resources(${PROJECT_NAME} config BASE ${CONFIG_DIR} PREFIX / FILES
145-
${CONFIG_DIR}/config.json ${CONFIG_DIR}/config.rsa ${CONFIG_DIR}/config.pub
146+
${CONFIG_DIR}/config.json ${CONFIG_DIR}/config.ecc ${CONFIG_DIR}/config.ecpub
146147
)
147148

148149
if(OPENSSL_ROOT_DIR)

common

prefPane/update.m

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919

2020
#import "update.h"
21-
#include <CoreFoundation/CoreFoundation.h>
2221

2322
#import <CryptoTokenKit/CryptoTokenKit.h>
2423
#import <Security/Security.h>
@@ -35,15 +34,23 @@ @implementation Update {
3534

3635
- (instancetype)init {
3736
NSString *pem = @((char*)config_pub);
38-
pem = [pem stringByReplacingOccurrencesOfString:@"-----BEGIN RSA PUBLIC KEY-----" withString:@""];
39-
pem = [pem stringByReplacingOccurrencesOfString:@"-----END RSA PUBLIC KEY-----" withString:@""];
37+
pem = [pem stringByReplacingOccurrencesOfString:@"-----BEGIN PUBLIC KEY-----" withString:@""];
38+
pem = [pem stringByReplacingOccurrencesOfString:@"-----END PUBLIC KEY-----" withString:@""];
4039
NSData *keyData = [[NSData alloc] initWithBase64EncodedString:pem options:NSDataBase64DecodingIgnoreUnknownCharacters];
40+
TKTLVRecord *record = [TKBERTLVRecord recordFromData:keyData];
41+
NSData *eckey;
42+
for (TKTLVRecord *nestedRecord in [TKBERTLVRecord sequenceOfRecordsFromData:record.value]) {
43+
if (nestedRecord.tag == 0x03) {
44+
eckey = nestedRecord.value;
45+
eckey = [eckey subdataWithRange:NSMakeRange(1, eckey.length - 1)];
46+
}
47+
}
4148
NSDictionary *parameters = @{
42-
(__bridge id)kSecAttrKeyType: (__bridge id)kSecAttrKeyTypeRSA,
43-
(__bridge id)kSecAttrKeyClass: (__bridge id)kSecAttrKeyClassPublic
49+
(__bridge id)kSecAttrKeyType: (__bridge id)kSecAttrKeyTypeEC,
50+
(__bridge id)kSecAttrKeyClass: (__bridge id)kSecAttrKeyClassPublic,
4451
};
4552
CFErrorRef err = nil;
46-
key = CFBridgingRelease(SecKeyCreateWithData((__bridge CFDataRef)keyData, (__bridge CFDictionaryRef)parameters, &err));
53+
key = CFBridgingRelease(SecKeyCreateWithData((__bridge CFDataRef)eckey, (__bridge CFDictionaryRef)parameters, &err));
4754
if (key == nil) {
4855
NSLog(@"Failed to create key: %@", err);
4956
CFRelease(err);
@@ -90,7 +97,7 @@ - (BOOL)checkCertificatePinning:(NSURLAuthenticationChallenge *)challenge {
9097

9198
- (void)request {
9299
NSURL *url = [NSURL URLWithString:@CONFIG_URL];
93-
url = [url.URLByDeletingLastPathComponent URLByAppendingPathComponent:@"config.rsa"];
100+
url = [url.URLByDeletingLastPathComponent URLByAppendingPathComponent:@"config.ecc"];
94101
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
95102
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];
96103
[request addValue:[self userAgent:YES] forHTTPHeaderField:@"User-Agent"];
@@ -191,9 +198,17 @@ - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticat
191198
}
192199

193200
- (void)receivedData:(NSData *)data withSignature:(NSData *)signature {
201+
NSDictionary<NSString*, id> *attributes = CFBridgingRelease(SecKeyCopyAttributes((__bridge SecKeyRef)key));
202+
NSNumber *keySize = attributes[(__bridge NSString*)kSecAttrKeySizeInBits];
203+
SecKeyAlgorithm algorithm = kSecKeyAlgorithmECDSASignatureMessageX962SHA512;
204+
switch (keySize.unsignedIntValue) {
205+
case 256: algorithm = kSecKeyAlgorithmECDSASignatureMessageX962SHA256; break;
206+
case 384: algorithm = kSecKeyAlgorithmECDSASignatureMessageX962SHA384; break;
207+
default: break;
208+
}
194209
CFErrorRef err = nil;
195-
BOOL isValid = SecKeyVerifySignature((__bridge SecKeyRef)key, kSecKeyAlgorithmRSASignatureMessagePKCS1v15SHA512,
196-
(__bridge CFDataRef)data, (__bridge CFDataRef)signature, &err);
210+
BOOL isValid = SecKeyVerifySignature((__bridge SecKeyRef)key, algorithm, (__bridge CFDataRef)data,
211+
(__bridge CFDataRef)signature, &err);
197212
if (!isValid) {
198213
NSLog(@"Verify error: %@", err);
199214
CFRelease(err);

0 commit comments

Comments
 (0)