Skip to content

Commit

Permalink
Fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
kdubb committed Jun 9, 2023
1 parent 0e64a54 commit 38bb362
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 91 deletions.
2 changes: 1 addition & 1 deletion Sources/ShieldOID/ISO-ITU.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import Foundation

// swiftformat:disable consecutiveSpaces
// swiftlint:disable type_name identifier_name
// swiftlint:disable type_name identifier_name nesting

/// Areas of joint work between ISO/IEC (International Organization for Standardization/International Electrotechnical Commission)
/// and ITU-T (International Telecommunication Union - Telecommunication standardization sector), and other international work
Expand Down
2 changes: 1 addition & 1 deletion Sources/ShieldOID/ISO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Foundation
import PotentASN1

// swiftformat:disable consecutiveSpaces
// swiftlint:disable type_name nesting
// swiftlint:disable type_name identifier_name nesting

/// International Organization for Standardization (ISO)
///
Expand Down
5 changes: 4 additions & 1 deletion Sources/ShieldPKCS/Moved.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//
// Moved.swift
// Shield
//
// Copyright © 2019 Outfox, inc.
//
// Created by Kevin Wooten on 6/8/23.
//
// Distributed under the MIT License, See LICENSE for details.
//

import ShieldX509
Expand Down
51 changes: 25 additions & 26 deletions Sources/ShieldSecurity/SecKeyPair.swift
Original file line number Diff line number Diff line change
Expand Up @@ -396,19 +396,19 @@ public struct SecKeyPair {
///
public static func `import`(fromData data: Data, withPassword password: String) throws -> SecKeyPair {

typealias nist = iso_itu.country.us.organization.gov.csor.nistAlgorithms
typealias rsadsi = iso.memberBody.us.rsadsi
typealias pkcs = rsadsi.pkcs
let supportedEncOids = [nist.aes.aes128_CBC_PAD.oid, nist.aes.aes192_CBC_PAD.oid, nist.aes.aes256_CBC_PAD.oid]
typealias Nist = iso_itu.country.us.organization.gov.csor.nistAlgorithms
typealias RSADSI = iso.memberBody.us.rsadsi
typealias PKCS = RSADSI.pkcs
let supportedEncOids = [Nist.aes.aes128_CBC_PAD.oid, Nist.aes.aes192_CBC_PAD.oid, Nist.aes.aes256_CBC_PAD.oid]

let info = try ASN1.Decoder.decode(EncryptedPrivateKeyInfo.self, from: data)

// Convert and validate requirements (PBKDF2 and AES-CBC-PAD encryption)

guard
info.encryptionAlgorithm.algorithm == rsadsi.pkcs.pkcs5.pbes2.oid,
info.encryptionAlgorithm.algorithm == RSADSI.pkcs.pkcs5.pbes2.oid,
let encAlgParams = try? info.encryptionAlgorithm.parameters.map({ try ASN1.Decoder.decodeTree(PBES2Params.self, from: $0) }),
encAlgParams.keyDerivationFunc.algorithm == pkcs.pkcs5.pbkdf2.oid,
encAlgParams.keyDerivationFunc.algorithm == PKCS.pkcs5.pbkdf2.oid,
let pbkdf2Params = try? encAlgParams.keyDerivationFunc.parameters.map({ try ASN1.Decoder.decodeTree(PBKDF2Params.self, from: $0) }),
supportedEncOids.contains(encAlgParams.encryptionScheme.algorithm),
let aesIV = encAlgParams.encryptionScheme.parameters?.octetStringValue
Expand Down Expand Up @@ -581,37 +581,37 @@ private extension SecKey {
private extension PBKDF.PsuedoRandomAlgorithm {

var prfAlgorithm: iso.memberBody.us.rsadsi.digestAlgorithm {
typealias algs = iso.memberBody.us.rsadsi.digestAlgorithm
typealias Algs = iso.memberBody.us.rsadsi.digestAlgorithm

switch self {
case .hmacSha1:
return algs.hmacWithSHA1
return Algs.hmacWithSHA1
case .hmacSha224:
return algs.hmacWithSHA224
return Algs.hmacWithSHA224
case .hmacSha256:
return algs.hmacWithSHA256
return Algs.hmacWithSHA256
case .hmacSha384:
return algs.hmacWithSHA384
return Algs.hmacWithSHA384
case .hmacSha512:
return algs.hmacWithSHA512
return Algs.hmacWithSHA512
default:
fatalError("Unsupported PBKDF Psuedo Random Algorithm")
}
}

static func from(oid: OID) throws -> Self {
typealias algs = iso.memberBody.us.rsadsi.digestAlgorithm
typealias Algs = iso.memberBody.us.rsadsi.digestAlgorithm

switch oid {
case algs.hmacWithSHA1.oid:
case Algs.hmacWithSHA1.oid:
return .hmacSha1
case algs.hmacWithSHA224.oid:
case Algs.hmacWithSHA224.oid:
return .hmacSha224
case algs.hmacWithSHA256.oid:
case Algs.hmacWithSHA256.oid:
return .hmacSha256
case algs.hmacWithSHA384.oid:
case Algs.hmacWithSHA384.oid:
return .hmacSha384
case algs.hmacWithSHA512.oid:
case Algs.hmacWithSHA512.oid:
return .hmacSha512
default:
throw SecKeyPair.Error.invalidEncodedPrivateKey
Expand All @@ -623,15 +623,15 @@ private extension PBKDF.PsuedoRandomAlgorithm {
private extension SecKeyPair.ExportKeySize {

var aesCBCAlgorithm: iso_itu.country.us.organization.gov.csor.nistAlgorithms.aes {
typealias aes = iso_itu.country.us.organization.gov.csor.nistAlgorithms.aes
typealias AES = iso_itu.country.us.organization.gov.csor.nistAlgorithms.aes

switch self {
case .bits128:
return aes.aes128_CBC_PAD
return AES.aes128_CBC_PAD
case .bits192:
return aes.aes192_CBC_PAD
return AES.aes192_CBC_PAD
case .bits256:
return aes.aes256_CBC_PAD
return AES.aes256_CBC_PAD
}
}

Expand All @@ -648,19 +648,19 @@ private extension EncryptedPrivateKeyInfo {
aesEncryptionScheme: OID,
aesIV: Data
) throws -> EncryptedPrivateKeyInfo {
typealias pkcs5 = iso.memberBody.us.rsadsi.pkcs.pkcs5
typealias PKCS = iso.memberBody.us.rsadsi.pkcs.pkcs5

let pbkdf2Params = PBKDF2Params(salt: pbkdf2Salt,
iterationCount: pbkdf2IterationCount,
keyLength: pbkdf2KeyLength,
prf: .init(algorithm: pbkdf2Prf))

let encAlgParams = PBES2Params(keyDerivationFunc: .init(algorithm: pkcs5.pbkdf2.oid,
let encAlgParams = PBES2Params(keyDerivationFunc: .init(algorithm: PKCS.pbkdf2.oid,
parameters: try ASN1.Encoder.encodeTree(pbkdf2Params)),
encryptionScheme: .init(algorithm: aesEncryptionScheme,
parameters: .octetString(aesIV)))

let encAlgId = AlgorithmIdentifier(algorithm: pkcs5.pbes2.oid,
let encAlgId = AlgorithmIdentifier(algorithm: PKCS.pbes2.oid,
parameters: try ASN1.Encoder.encodeTree(encAlgParams))

return EncryptedPrivateKeyInfo(encryptionAlgorithm: encAlgId,
Expand All @@ -675,4 +675,3 @@ private func printPEM(data: Data, type: String) {

print("-----BEGIN \(type)-----\n\(pemBase64)\n-----END \(type)-----")
}

8 changes: 5 additions & 3 deletions Sources/ShieldX509/ECPrivateKey.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//
// ECPrivateKey.swift
//
// Shield
//
// Created by Kevin Wooten on 6/8/23.
// Copyright © 2019 Outfox, inc.
//
//
// Distributed under the MIT License, See LICENSE for details.
//

import Foundation
Expand Down Expand Up @@ -46,4 +49,3 @@ public extension Schemas {
])

}

9 changes: 6 additions & 3 deletions Sources/ShieldX509/EncryptedPrivateKeyInfo.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//
// EncryptedPrivateKeyInfo.swift
//
// Shield
//
// Created by Kevin Wooten on 6/8/23.
// Copyright © 2019 Outfox, inc.
//
//
// Distributed under the MIT License, See LICENSE for details.
//

import Foundation
Expand Down Expand Up @@ -37,7 +40,7 @@ public extension Schemas {
static let EncryptedPrivateKeyInfo: Schema =
.sequence([
"encryptionAlgorithm": algorithmIdentifier(EncryptedPrivateKeyInfoAlgorithms),
"encryptedData": .octetString()
"encryptedData": .octetString(),
])

}
95 changes: 49 additions & 46 deletions Sources/ShieldX509/PBES2Params.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//
// PBES.swift
//
// Shield
//
// Created by Kevin Wooten on 6/8/23.
// Copyright © 2019 Outfox, inc.
//
//
// Distributed under the MIT License, See LICENSE for details.
//

import BigInt
Expand All @@ -29,61 +32,61 @@ extension PBES2Params: SchemaSpecified {

public extension Schemas {

private typealias rsadisDigAlg = iso.memberBody.us.rsadsi.digestAlgorithm
private typealias rsadisEncAlg = iso.memberBody.us.rsadsi.encryptionAlgorithm
private typealias nistAlgs = iso_itu.country.us.organization.gov.csor.nistAlgorithms.aes
private typealias RSADSIDigAlgs = iso.memberBody.us.rsadsi.digestAlgorithm
private typealias RSADSIEncAlgs = iso.memberBody.us.rsadsi.encryptionAlgorithm
private typealias NISTAlgs = iso_itu.country.us.organization.gov.csor.nistAlgorithms.aes

static let PBES2ParamsKeyDerivationFuncAlgorithms: Schema.DynamicMap = [
iso.memberBody.us.rsadsi.pkcs.pkcs5.pbkdf2.asn1: PBKDF2Params
]

static let PBES2ParamsEncryptionSchemeAlgorithms: Schema.DynamicMap = [
rsadisEncAlg.rc2CBC.asn1: .null,
rsadisEncAlg.rc2ECB.asn1: .null,
rsadisEncAlg.rc4.asn1: .null,
rsadisEncAlg.rc4WithMAC.asn1: .null,
rsadisEncAlg.desxCBC.asn1: .null,
rsadisEncAlg.desEDE3CBC.asn1: .null,
rsadisEncAlg.rc5CBC.asn1: .null,
rsadisEncAlg.rc5CBCPad.asn1: .null,
rsadisEncAlg.desCDMF.asn1: .null,
rsadisEncAlg.desEDE3.asn1: .null,

nistAlgs.aes128_ECB.asn1: .null,
nistAlgs.aes128_CBC_PAD.asn1: .null,
nistAlgs.aes128_OFB.asn1: .null,
nistAlgs.aes128_CFB.asn1: .null,
nistAlgs.aes128_wrap.asn1: .null,
nistAlgs.aes128_GCM.asn1: .null,
nistAlgs.aes128_CCM.asn1: .null,
nistAlgs.aes128_wrap_pad.asn1: .null,
nistAlgs.aes128_GMAC.asn1: .null,

nistAlgs.aes192_ECB.asn1: .null,
nistAlgs.aes192_CBC_PAD.asn1: .null,
nistAlgs.aes192_OFB.asn1: .null,
nistAlgs.aes192_CFB.asn1: .null,
nistAlgs.aes192_wrap.asn1: .null,
nistAlgs.aes192_GCM.asn1: .null,
nistAlgs.aes192_CCM.asn1: .null,
nistAlgs.aes192_wrap_pad.asn1: .null,
nistAlgs.aes192_GMAC.asn1: .null,

nistAlgs.aes256_ECB.asn1: .null,
nistAlgs.aes256_CBC_PAD.asn1: .null,
nistAlgs.aes256_OFB.asn1: .null,
nistAlgs.aes256_CFB.asn1: .null,
nistAlgs.aes256_wrap.asn1: .null,
nistAlgs.aes256_GCM.asn1: .null,
nistAlgs.aes256_CCM.asn1: .null,
nistAlgs.aes256_wrap_pad.asn1: .null,
nistAlgs.aes256_GMAC.asn1: .null,
RSADSIEncAlgs.rc2CBC.asn1: .null,
RSADSIEncAlgs.rc2ECB.asn1: .null,
RSADSIEncAlgs.rc4.asn1: .null,
RSADSIEncAlgs.rc4WithMAC.asn1: .null,
RSADSIEncAlgs.desxCBC.asn1: .null,
RSADSIEncAlgs.desEDE3CBC.asn1: .null,
RSADSIEncAlgs.rc5CBC.asn1: .null,
RSADSIEncAlgs.rc5CBCPad.asn1: .null,
RSADSIEncAlgs.desCDMF.asn1: .null,
RSADSIEncAlgs.desEDE3.asn1: .null,

NISTAlgs.aes128_ECB.asn1: .null,
NISTAlgs.aes128_CBC_PAD.asn1: .null,
NISTAlgs.aes128_OFB.asn1: .null,
NISTAlgs.aes128_CFB.asn1: .null,
NISTAlgs.aes128_wrap.asn1: .null,
NISTAlgs.aes128_GCM.asn1: .null,
NISTAlgs.aes128_CCM.asn1: .null,
NISTAlgs.aes128_wrap_pad.asn1: .null,
NISTAlgs.aes128_GMAC.asn1: .null,

NISTAlgs.aes192_ECB.asn1: .null,
NISTAlgs.aes192_CBC_PAD.asn1: .null,
NISTAlgs.aes192_OFB.asn1: .null,
NISTAlgs.aes192_CFB.asn1: .null,
NISTAlgs.aes192_wrap.asn1: .null,
NISTAlgs.aes192_GCM.asn1: .null,
NISTAlgs.aes192_CCM.asn1: .null,
NISTAlgs.aes192_wrap_pad.asn1: .null,
NISTAlgs.aes192_GMAC.asn1: .null,

NISTAlgs.aes256_ECB.asn1: .null,
NISTAlgs.aes256_CBC_PAD.asn1: .null,
NISTAlgs.aes256_OFB.asn1: .null,
NISTAlgs.aes256_CFB.asn1: .null,
NISTAlgs.aes256_wrap.asn1: .null,
NISTAlgs.aes256_GCM.asn1: .null,
NISTAlgs.aes256_CCM.asn1: .null,
NISTAlgs.aes256_wrap_pad.asn1: .null,
NISTAlgs.aes256_GMAC.asn1: .null,
]

static let PBES2Params: Schema =
.sequence([
"keyDerivationFunc": algorithmIdentifier(PBES2ParamsKeyDerivationFuncAlgorithms),
"encryptionScheme": algorithmIdentifier(PBES2ParamsEncryptionSchemeAlgorithms)
"encryptionScheme": algorithmIdentifier(PBES2ParamsEncryptionSchemeAlgorithms),
])

}
21 changes: 12 additions & 9 deletions Sources/ShieldX509/PBKDF2Params.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//
// PBKDF2Params.swift
//
// Shield
//
// Created by Kevin Wooten on 6/8/23.
// Copyright © 2019 Outfox, inc.
//
//
// Distributed under the MIT License, See LICENSE for details.
//

import Foundation
Expand Down Expand Up @@ -33,22 +36,22 @@ extension PBKDF2Params: SchemaSpecified {

public extension Schemas {

private typealias digAlgs = iso.memberBody.us.rsadsi.digestAlgorithm
private typealias DigAlgs = iso.memberBody.us.rsadsi.digestAlgorithm

private static let PRFAglorithms: Schema.DynamicMap = [
digAlgs.hmacWithSHA1.asn1: .null,
digAlgs.hmacWithSHA224.asn1: .null,
digAlgs.hmacWithSHA256.asn1: .null,
digAlgs.hmacWithSHA384.asn1: .null,
digAlgs.hmacWithSHA512.asn1: .null,
DigAlgs.hmacWithSHA1.asn1: .null,
DigAlgs.hmacWithSHA224.asn1: .null,
DigAlgs.hmacWithSHA256.asn1: .null,
DigAlgs.hmacWithSHA384.asn1: .null,
DigAlgs.hmacWithSHA512.asn1: .null,
]

static let PBKDF2Params: Schema =
.sequence([
"salt": .choiceOf([.octetString(), .objectIdentifier()]),
"iterationCount": .integer(),
"keyLength": .optional(.integer()),
"prf": algorithmIdentifier(PRFAglorithms)
"prf": algorithmIdentifier(PRFAglorithms),
])

}
5 changes: 4 additions & 1 deletion Sources/ShieldX509/PrivateKeyInfo.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//
// PrivateKeyInfo.swift
// Shield
//
// Copyright © 2019 Outfox, inc.
//
// Created by Kevin Wooten on 6/7/23.
//
// Distributed under the MIT License, See LICENSE for details.
//

import Foundation
Expand Down

0 comments on commit 38bb362

Please sign in to comment.