11#!/usr/bin/swift sh
22
33import Foundation
4- import Security
4+ import CryptoKit
55
66// MARK: - Settings Configuration
77
@@ -26,8 +26,8 @@ class SettingsConfiguration {
2626
2727 log ( " 1 / 4 - Downloading configuration data... " )
2828 let configData = try fetchData ( from: " \( configBaseUrl) /config.json " )
29- let publicKey = try fetchData ( from: " \( configBaseUrl) /config.pub " )
30- let signature = try fetchData ( from: " \( configBaseUrl) /config.rsa " )
29+ let publicKey = try fetchData ( from: " \( configBaseUrl) /config.ecpub " )
30+ let signature = try fetchData ( from: " \( configBaseUrl) /config.ecc " )
3131
3232 log ( " 2 / 4 - Verifying signature... " )
3333 try verifySignature ( configData: configData, publicKey: publicKey, signature: signature)
@@ -38,8 +38,8 @@ class SettingsConfiguration {
3838
3939 log ( " 4 / 4 - Saving and moving files... " )
4040 try saveFile ( named: " config.json " , content: configData)
41- try saveFile ( named: " publicKey.pub " , content: publicKey)
42- try saveFile ( named: " signature.rsa " , content: signature)
41+ try saveFile ( named: " publicKey.ecpub " , content: publicKey)
42+ try saveFile ( named: " signature.ecc " , content: signature)
4343 try saveFile ( named: " defaultConfiguration.json " , content: defaultConfiguration)
4444
4545 log ( " Default configuration initialized successfully! " )
@@ -64,27 +64,35 @@ extension SettingsConfiguration {
6464 throw ConfigurationError . signatureVerificationFailed
6565 }
6666 guard let pubKey = Data ( base64Encoded: publicKey
67- . replacingOccurrences ( of: " -----BEGIN RSA PUBLIC KEY----- " , with: " " )
68- . replacingOccurrences ( of: " -----END RSA PUBLIC KEY----- " , with: " " ) , options: . ignoreUnknownCharacters) else {
67+ . replacingOccurrences ( of: " -----BEGIN PUBLIC KEY----- " , with: " " )
68+ . replacingOccurrences ( of: " -----END PUBLIC KEY----- " , with: " " ) , options: . ignoreUnknownCharacters) else {
69+ log ( " Failed to parse key " )
6970 throw ConfigurationError . signatureVerificationFailed
7071 }
7172 guard let sigData = Data ( base64Encoded: signature, options: . ignoreUnknownCharacters) else {
7273 throw ConfigurationError . signatureVerificationFailed
7374 }
74- let parameters : [ CFString : Any ] = [
75- kSecAttrKeyType: kSecAttrKeyTypeRSA,
76- kSecAttrKeyClass: kSecAttrKeyClassPublic,
77- kSecReturnPersistentRef: false
78- ]
79- var error : Unmanaged < CFError > ?
80- guard let key = SecKeyCreateWithData ( pubKey as CFData , parameters as CFDictionary , & error) else {
81- log ( " Key importing failed \( error? . takeRetainedValue ( ) . localizedDescription ?? " " ) " )
75+
76+ let result : Bool
77+ switch pubKey. count {
78+ case 91 :
79+ let key = try P256 . Signing. PublicKey ( derRepresentation: pubKey)
80+ let sig = try P256 . Signing. ECDSASignature ( derRepresentation: sigData)
81+ result = key. isValidSignature ( sig, for: Data ( configData. utf8) )
82+ case 120 :
83+ let key = try P384 . Signing. PublicKey ( derRepresentation: pubKey)
84+ let sig = try P384 . Signing. ECDSASignature ( derRepresentation: sigData)
85+ result = key. isValidSignature ( sig, for: Data ( configData. utf8) )
86+ case 158 :
87+ let key = try P521 . Signing. PublicKey ( derRepresentation: pubKey)
88+ let sig = try P521 . Signing. ECDSASignature ( derRepresentation: sigData)
89+ result = key. isValidSignature ( sig, for: Data ( configData. utf8) )
90+ default :
91+ log ( " Unknown key size " )
8292 throw ConfigurationError . signatureVerificationFailed
8393 }
84- let algorithm : SecKeyAlgorithm = . rsaSignatureMessagePKCS1v15SHA512
85- let result = SecKeyVerifySignature ( key, algorithm, Data ( configData. utf8) as CFData , sigData as CFData , & error)
8694 if !result {
87- log ( " Signature verifying failed \( error ? . takeRetainedValue ( ) . localizedDescription ?? " " ) " )
95+ log ( " Signature verifying failed " )
8896 throw ConfigurationError . signatureVerificationFailed
8997 }
9098 log ( " Signature verified successfully! " )
0 commit comments