Skip to content
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

Add support for PKCS8 private key format #73

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let (privateKey, publicKey) = try! CC.RSA.generateKeyPair(2048)
### Convert them to PEM format
```
let privateKeyPEM = try SwKeyConvert.PrivateKey.derToPKCS1PEM(privateKey)
let privateKeyPEMPKCS8 = try SwKeyConvert.PrivateKey.derToPKCS8PEM(privateKey)
let publicKeyPEM = SwKeyConvert.PublicKey.derToPKCS8PEM(publicKey)
```
### Or read them from strings with PEM data
Expand Down
40 changes: 32 additions & 8 deletions SwCrypt/SwCrypt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ open class SwKeyConvert {
public static func derToPKCS1PEM(_ derKey: Data) -> String {
return PEM.PrivateKey.toPEM(derKey)
}

public static func derToPKCS8PEM(_ derKey: Data) -> String {
let pkcs8Key = PKCS8.PrivateKey.addHeader(derKey)
return PEM.PrivateKey.toPEM(pkcs8Key)
}

public typealias EncMode = PEM.EncryptedPrivateKey.EncMode

Expand Down Expand Up @@ -177,6 +182,19 @@ open class SwKeyConvert {
open class PKCS8 {

open class PrivateKey {
private static let RSAVersion: [UInt8] = [0x02, 0x01, 0x00] // version 0
private static let RSAOID: [UInt8] = [0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00]

public static func addHeader(_ derKey: Data) -> Data {
let octetString: [UInt8] = [0x04] + encodeLength(derKey.count) + derKey.bytesView

// assemble the PKCS#8 header
let sequence: [UInt8] = [0x30] + encodeLength(RSAVersion.count + RSAOID.count + octetString.count) + RSAVersion + RSAOID + octetString

return Data(sequence)
}


// https://lapo.it/asn1js/
public static func getPKCS1DEROffset(_ derKey: Data) -> Int? {
Expand Down Expand Up @@ -205,15 +223,12 @@ open class PKCS8 {
return 0
}

let OID: [UInt8] = [0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00]
guard bytes.length > offset + RSAOID.count else { return nil }
let slice = derKey.bytesViewRange(NSRange(location: offset, length: RSAOID.count))

guard bytes.length > offset + OID.count else { return nil }
let slice = derKey.bytesViewRange(NSRange(location: offset, length: OID.count))
guard RSAOID.elementsEqual(slice) else { return nil }

guard OID.elementsEqual(slice) else { return nil }

offset += OID.count
offset += RSAOID.count

guard bytes.length > offset else { return nil }
guard bytes[offset] == 0x04 else { return nil }
Expand Down Expand Up @@ -242,7 +257,16 @@ open class PKCS8 {
public static func hasCorrectHeader(_ derKey: Data) -> Bool {
return getPKCS1DEROffset(derKey) != nil
}


// Helper function to encode length in DER
private static func encodeLength(_ length: Int) -> [UInt8] {
if length < 128 {
return [UInt8(length)]
} else {
let lengthBytes = withUnsafeBytes(of: length.bigEndian, Array.init).drop { $0 == 0 }
return [0x80 | UInt8(lengthBytes.count)] + lengthBytes
}
}
}

open class PublicKey {
Expand Down
11 changes: 11 additions & 0 deletions SwCryptTests/SwCryptTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ class SwCryptTest: XCTestCase {
XCTAssert($0 as? SwKeyConvert.SwError == SwKeyConvert.SwError.badPassphrase)
}
}

func testPKCS8KeyPair() {
let (priv, pub) = keyPair!

let privKeyPem = SwKeyConvert.PrivateKey.derToPKCS8PEM(priv)

let privKey = try? SwKeyConvert.PrivateKey.pemToPKCS1DER(privKeyPem)
let genPubKey = try? CC.RSA.getPublicKeyFromPrivateKey(privKey!)

XCTAssert(pub == genPubKey)
}

func testOpenSSLKeyPair() {
let bundle = Bundle(for: type(of: self))
Expand Down