RSA public/private key encryption, private key signing and public key verification in Swift using the Swift Package Manager. Works on iOS, macOS, and Linux (work in progress).

Overview

APIDoc Build Status - Master macOS iOS Linux Apache 2 Slack Status

BlueRSA

Swift cross-platform RSA wrapper library for RSA encryption and signing. Works on supported Apple platforms (using Security framework). Linux (using OpenSSL) is working but is still somewhat of a work in progress.

Contents

  • CryptorRSA: Utility functions for RSA encryption and signing. Pure Swift

Prerequisites

Swift

  • Swift Open Source swift-4.0.0-RELEASE toolchain (Minimum REQUIRED for latest release)
  • Swift Open Source swift-4.2-RELEASE toolchain (Recommended)
  • Swift toolchain included in Xcode Version 10.0 (10A255) or higher.

macOS

  • macOS 10.12.0 (Sierra) or higher
  • Xcode Version 9.0 (9A325) or higher using the included toolchain (Minimum REQUIRED for latest release).
  • Xcode Version 10.0 (10A255) or higher using the included toolchain (Recommended).

iOS

  • iOS 10.3 or higher
  • Xcode Version 9.0 (9A325) or higher using the included toolchain (Minimum REQUIRED for latest release).
  • Xcode Version 10.0 (10A255) or higher using the included toolchain (Recommended).

Linux

  • Ubuntu 16.04 (or 16.10 but only tested on 16.04) and 18.04.
  • One of the Swift Open Source toolchain listed above.
  • OpenSSL is provided by the distribution. Note: 1.0.x, 1.1.x and later releases of OpenSSL are supported.
  • The appropriate libssl-dev package is required to be installed when building.

Build

To build CryptorRSA from the command line:

% cd <path-to-clone>
% swift build

Testing

To run the supplied unit tests for CryptorRSA from the command line:

% cd <path-to-clone>
% swift build
% swift test

Using CryptorRSA

Including in your project

Swift Package Manager

To include BlueRSA into a Swift Package Manager package, add it to the dependencies attribute defined in your Package.swift file. You can select the version using the majorVersion and minor parameters. For example:

	dependencies: [
		.Package(url: "https://github.com/Kitura/BlueRSA", majorVersion: <majorVersion>, minor: <minor>)
	]

Carthage

To include BlueRSA in a project using Carthage, add a line to your Cartfile with the GitHub organization and project names and version. For example:

	github "Kitura/BlueRSA" ~> <majorVersion>.<minor>

Before starting

The first you need to do is import the CryptorRSA framework. This is done by the following:

import CryptorRSA

Data Types

BlueRSA supports the following major data types:

  • Key Handling

    • CryptorRSA.PublicKey - Represents an RSA Public Key.
    • CryptorRSA.PrivateKey - Represents an RSA Private Key.
  • Data Handling

    • CryptorRSA.EncryptedData - Represents encrypted data.
    • CryptorRSA.PlaintextData - Represents plaintext or decrypted data.
    • CryptorRSA.SignedData - Represents signed data.

Key Handling

BlueRSA provides seven (7) functions each for creating public and private keys from data. They are as follows (where createXXXX is either createPublicKey or createPrivateKey depending on what you're trying to create):

  • CryptorRSA.createXXXX(with data: Data) throws - This creates either a private or public key containing the data provided. It is assumed that the data being provided is in the proper format.
  • CryptorRSA.createXXXX(withBase64 base64String: String) throws - This creates either a private or public key using the Base64 encoded String provided.
  • CryptorRSA.createXXXX(withPEM pemString: String) throws - This creates either a private or public key using the PEM encoded String provided.
  • CryptorRSA.createXXXX(withPEMNamed pemName: String, onPath path: String) throws - This creates either a private or public key using the PEM encoded file pointed at by the pemName and located on the path specified by path provided.
  • CryptorRSA.createXXXX(withDERNamed derName: String, onPath path: String) throws - This creates either a private or public key using the DER encoded file pointed at by the derName and located on the path specified by path provided.
  • CryptorRSA.createXXXX(withPEMNamed pemName: String, in bundle: Bundle = Bundle.main) throws - This creates either a private or public key using the PEM encoded file pointed at by the pemName and located in the Bundle specified by bundle provided. By default this API will look in the main bundle. Note: Apple Platforms Only
  • CryptorRSA.createXXXX(withDERNamed derName: String, in bundle: Bundle = Bundle.main) throws - This creates either a private or public key using the DER encoded file pointed at by the derName and located in the Bundle specified by bundle provided. By default this API will look in the main bundle. Note: Apple Platforms Only

Additionally, there are three APIs for creating a public key by extracting the key from a PEM formatted certificate: They are:

  • CryptorRSA.createPublicKey(extractingFrom data: Data) throws - This creates either a public key by extracting from the PEM encoded certificate pointed at by the data.
  • CryptorRSA.createPublicKey(extractingFrom certName: String, onPath path: String) throws - This creates a public key by extracting from the PEM encoded certificate pointed at by the certName and located on the path specified by path provided.
  • CryptorRSA.createPublicKey(extractingFrom certName: String, in bundle: Bundle = Bundle.main) throws - This creates a public key using the PEM encoded certificate pointed at by the derName and located in the Bundle specified by bundle provided. By default this API will look in the main bundle. Note: Apple Platforms Only

Example

The following example illustrates creating a public key given PEM encoded file located on a certain path. *Note: Exception handling omitted for brevity.

import Foundation
import CryptorRSA

...

let keyName = ...
let keyPath = ...

let publicKey = try CryptorRSA.createPublicKey(withPEMNamed: keyName, onPath: keyPath)

...

<Do something with the key...>

Data Encryption and Decryption Handling

BlueRSA provides functions for the creation of each of the three (3) data handling types:

Plaintext Data Handling and Signing

There are two class level functions for creating a PlaintextData object. These are:

  • CryptorRSA.createPlaintext(with data: Data) -> PlaintextData - This function creates a PlaintextData containing the specified data.
  • CryptorRSA.createPlaintext(with string: String, using encoding: String.Encoding) throws -> PlaintextData - This function creates a PlaintextData object using the string encoded with the specified encoding as the data.

Once the PlaintextData object is created, there are two instance functions that can be used to manipulate the contained data. These are:

  • encrypted(with key: PublicKey, algorithm: Data.Algorithm) throws -> EncryptedData? - This function allows you to encrypt containing data using the public key and algorithm specified. This function returns an optional EncryptedData object containing the encryped data.
  • signed(with key: PrivateKey, algorithm: Data.Algorithm) throws -> SignedData? - This function allows you to sign the contained data using the private key and algorithm specified. This function returns an optional SignedData object containing the signature of the signed data.

Example

  • Encryption: Note: Exception handling omitted for brevity.
import Foundation
import CryptorRSA

...

let keyName = ...
let keyPath = ...

let myData: Data = <... Data to be encrypted ...>

let publicKey = try CryptorRSA.createPublicKey(withPEMNamed: keyName, onPath: keyPath)
let myPlaintext = CryptorRSA.createPlaintext(with: myData)
let encryptedData = try myPlaintext.encrypt(with: publicKey, algorithm: .sha1)

...

< Do something with the encrypted data...>

  • Signing: Note: Exception handling omitted for brevity.
import Foundation
import CryptorRSA

...

let keyName = ...
let keyPath = ...

let myData: Data = <... Data to be signed ...>

let privateKey = try CryptorRSA.createPrivateKey(withPEMNamed: keyName, onPath: keyPath)
let myPlaintext = CryptorRSA.createPlaintext(with: myData)
let signedData = try myPlaintext.signed(with: privateKey, algorithm: .sha1)

...

< Do something with the signed data...>

Encrypted Data Handling

There are two class level functions for creating a EncryptedData object. These are:

  • CryptorRSA.createEncrypted(with data: Data) -> EncryptedData - This function creates a EncryptedData containing the specified encrypted data.
  • CryptorRSA.createEncrypted(with base64String: String) throws -> EncryptedData - This function creates a EncrpytedData using the Base64 representation of already encrypted data.

Once the EncryptedData object is created, there is an instance function that can be used to decrypt the enclosed data:

  • decrypted(with key: PrivateKey, algorithm: Data.Algorithm) throws -> DecryptedData? - This function allows you to decrypt containing data using the public key and algorithm specified. This function returns an optional DecryptedData object containing the encryped data.

BlueRSA currently supports OAEP padding, which is the recommended padding algorithm.

Example

  • Decryption: Note: Exception handling omitted for brevity.
import Foundation
import CryptorRSA

...

let keyName = ...
let keyPath = ...
let publicKey = try CryptorRSA.createPublicKey(withPEMNamed: keyName, onPath: keyPath)

let pkeyName = ...
let pkeyPath = ...
let privateKey = try CryptorRSA.createPrivateKey(withPEMNamed: pkeyName, onPath: pkeyPath)

let myData: Data = <... Data to be encrypted ...>

let myPlaintext = CryptorRSA.createPlaintext(with: myData)
let encryptedData = try myPlaintext.encrypt(with: publicKey, algorithm: .sha1)

let decryptedData = try encryptedData.decrypt(with: privateKey, algorithm: .sha1)

...

< Do something with the decrypted data...>


Signature Verification Handling

There is a single class level function that can be used to create a SignedData object. It is:

  • CryptorRSA.createSigned(with data: Data) -> SignedData - This function creates a SignedData containing the specified signed data.

Once created or obtained PlaintextData and SignedData, there is an instance function which can be used to verify the signature contained therein:

  • verify(with key: PublicKey, signature: SignedData, algorithm: Data.Algorithm) throws -> Bool - This function is used to verify, using the public key and algorithm, the signature. Returns true if the signature is valid, false otherwise.

  • Verifying: Note: Exception handling omitted for brevity.

import Foundation
import CryptorRSA

...

let keyName = ...
let keyPath = ...
let publicKey = try CryptorRSA.createPublicKey(withPEMNamed: keyName, onPath: keyPath)

let pkeyName = ...
let pkeyPath = ...
let privateKey = try CryptorRSA.createPrivateKey(withPEMNamed: pkeyName, onPath: pkeyPath)

let myData: Data = <... Data to be signed ...>

let myPlaintext = CryptorRSA.createPlaintext(with: myData)
let signedData = try myPlaintext.signed(with: privateKey, algorithm: .sha1)

if try myPlaintext.verify(with: publicKey, signature: signedData, algorithm: .sha1) {

	print("Signature verified")

} else {

	print("Signature Verification Failed")
}

Data Type Utility Functions

All three of the data handling types have two common utility instance functions. These are:

  • digest(using algorithm: Data.Algorithm) throws -> Data - This function returns a Data object containing a digest constructed using the specified algorithm.
  • string(using encoding: String.Encoding) throws -> String - This functions returns a String representation of the data using the specified encoding.

Community

We love to talk server-side Swift and Kitura. Join our Slack to meet the team!

License

This library is licensed under Apache 2.0. Full license text is available in LICENSE.

Comments
  • Use SecCertificateCopyKey API when available, falling back in edge cases

    Use SecCertificateCopyKey API when available, falling back in edge cases

    SecCertificateCopyPublicKey is deprecated in iOS 12 and macOS 10.14 in favor of SecCertificateCopyKey. This change introduces the use of the new API for all platforms that support it.

    Description

    Simply uses the new API when available and falls back to the legacy API when not. Keeps support for pre-4.2 Swift, but wraps everything in compiler directives to allow builds on platforms that have no reference to the deprecated API.

    The changes to the xcode project are from opening the project in a newer version of XCode. Content from 9.3 release notes:

    Xcode 9.3 adds a new IDEWorkspaceChecks.plist file to a workspace's shared data, to store the state of necessary workspace checks. Committing this file to source control will prevent unnecessary rerunning of those checks for each user opening the workspace. (37293167)

    Motivation and Context

    Previous to this patch, BlueRSA did not compile on macOS Catalyst. There is an existing pull request open, but the author has not responded in some time. Moreover, it appears SecCertificateCopyKey is already used for clients that support it, however, the codepath using the deprecated API was not wrapped in a compiler directive and the compiler still failed.

    Resolves #59

    How Has This Been Tested?

    Built and run on macOS and iOS. Ran tests on the macOS target.

    Checklist:

    • [x] I have submitted a CLA form
    • [x] If applicable, I have updated the documentation accordingly.
    • [x] If applicable, I have added tests to cover my changes.
    opened by levi 13
  • Removes unneeded CFString: Hashable extension

    Removes unneeded CFString: Hashable extension

    Starting with starting with Swift 4.0 all CF types already conform to Hashable. Therefore this extension is not needed anymore.

    Description

    Pre Swift 4.0 this extension was required but since the minimal swift version of this package is 4.0 this part of the code is not needed anymore. The corresponding implementation in the swift stdlib can be found here.

    Motivation and Context

    Recent changes in the Swift runtime indicate that the order in which protocol conformances are loaded is not guaranteed and might change with swift 5.4 which will ship with the upcoming Xcode release (12.5). Therefore the CFString extension of this package might produce unwanted side-effects for the application which is using this package.

    How Has This Been Tested?

    Built and run on macOS and iOS. Ran tests on the macOS target.

    opened by stmitt 7
  • fix: Added deallocate functions

    fix: Added deallocate functions

    We had reports of a memory leak when using this repo on Linux. I have tracked down places where we allocate memory and added deallocate functions to fix the leaks.

    opened by Andrew-Lees11 7
  • Compatability changes for Catalyst

    Compatability changes for Catalyst

    In order to use this package in catalyst projects SecCertificateCopyPublicKey was deprecated and needed to change to the supported Method SecCertificateCopyKey(certData) Method.

    Description

    In order to use this package in catalyst projects SecCertificateCopyPublicKey was deprecated and needed to change to the supported Method SecCertificateCopyKey(certData) Method.

    Resources: Deprecation - https://developer.apple.com/documentation/security/1396096-seccertificatecopypublickey New Method - https://developer.apple.com/documentation/security/2963103-seccertificatecopykey

    Motivation and Context

    I'd like to use it in my macOS, iPadOS and iOS project suppored by Catalyst.

    How Has This Been Tested?

    I have a project which has iOS, iPadOS and macOS as deployment targets. I use the deployment version iOS 13.1 and macOS 10.15 (catalina). The dependency management is Swift Package Manager. My dynamic framwork has the SwiftJWT package as dependency and uses BlueRSA in order to create the JWT.

    Building and running the project on iOS works fine but macOS catalyst does not know the deprecated method SecCertificateCopyPublicKey. Since catalyst the new method SecCertificateCopyKey has to be used in order to support catalyst.

    Resources: Deprecation - https://developer.apple.com/documentation/security/1396096-seccertificatecopypublickey New Method - https://developer.apple.com/documentation/security/2963103-seccertificatecopykey

    Checklist:

    • [ ] I have submitted a CLA form (Just changed to the new method and remove the deprecation.)
    • [x] If applicable, I have updated the documentation accordingly. (no changes necessary)
    • [x] If applicable, I have added tests to cover my changes. (see above)
    opened by sn3ek 6
  • iOS not supported in the podspec

    iOS not supported in the podspec

    When trying to use the BlueRSA pod for iOS I get the following error:

    The platform of the target `CocoapodsTest` (iOS 12.0) is not compatible with `BlueRSA (1.0.17)`, which does not support `ios`
    

    when comparing the podspec of BlueRSA to BlueCryptor I saw that:

    s.ios.deployment_target = "10.0"
    

    Was missing. In the readme it states the BlueRSA runs on iOS 10 or higher so I would expect this to work.

    There is not a reference to the BlueRSA pod but I assume it is intended to support this?

    opened by Andrew-Lees11 6
  • feat: Add PSS signing and verifying

    feat: Add PSS signing and verifying

    Description

    This pull request adds usePSS flag to sign and verify. This allows you to use RSA-PSS for signing and verifying. If the flag is set to true the functions will use RSA_PKCS1_PSS_PADDING, a salt the length of the digest and MGF1 mask generation function to sign or verify.

    Motivation and Context

    This change is required because "In general, RSA-PSS should be used as a replacement for RSA-PKCS#1 v1.5".

    It also allows us to support PS256 and PS384 signing/verifying with JWTs as requested by SwiftJWT issue #55

    How Has This Been Tested?

    Tests have been added covering the new algorithms and the produced signatures have been tested against JWT.io both that one we create is valid and we can verify a valid one created by JWT.io.

    Checklist:

    • [x] I have submitted a CLA form
    • [x] If applicable, I have updated the documentation accordingly.
    • [x] If applicable, I have added tests to cover my changes.
    opened by Andrew-Lees11 5
  • Crash while creating private key with Base64 string on Linux

    Crash while creating private key with Base64 string on Linux

    I removed header and footer lines in my private key file and use:

    let privateKey = try CryptorRSA.createPrivateKey(withBase64: base64String)

    it works well on Mac OS but on Linux it crashes with error:

    Fatal error: Unexpectedly found nil while unwrapping an Optional value

    opened by Luzanov 4
  • Encrypted data cannot be shared between MacOS and Linux

    Encrypted data cannot be shared between MacOS and Linux

    When you encrypt some data using a public key on MacOS into an Encrypted data, you can decrypt this data on MacOS, However if you try to decrypt the same data on Linux it doesn't work. This means that is data is encrypted on MacOS it cannot be decrypted on Linux.

    You can replicate this with the following code:

    let publicKey = """
    -----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDpbnF5g5bt9mjweE5nZioLAPKI
    l31qj5/ht4i62gsEg/6R/GMBIAhUEgfr9n3H0jIKRoI0BvVH6+fUB9pKoMUdRu3V
    sAF9ExkuhwazpE1uIfRbf2IJHIAz5zX3emUdymHaPZPucQUF7G/XmOMK86qKl8Zd
    d8Gl8cTFpRVMoSs4lwIDAQAB
    -----END PUBLIC KEY-----
    """
    
    let privateKey = """
    -----BEGIN RSA PRIVATE KEY-----
    MIICXQIBAAKBgQDpbnF5g5bt9mjweE5nZioLAPKIl31qj5/ht4i62gsEg/6R/GMB
    IAhUEgfr9n3H0jIKRoI0BvVH6+fUB9pKoMUdRu3VsAF9ExkuhwazpE1uIfRbf2IJ
    HIAz5zX3emUdymHaPZPucQUF7G/XmOMK86qKl8Zdd8Gl8cTFpRVMoSs4lwIDAQAB
    AoGAcs/ZjDTGxWAPGUdy+LRtNWBP6hLoosLllnVZEN4x0RTC3zbN0z3YGtGLh+mC
    0Ad4iUlIvSI2/hrvuX/rRA1zJRSWqmUi+K/IQtRqH4L29xfDT9yxVd5X61vb5dGs
    nruBHBSdcfsLKrQZClAqGKNElVfLn5+vaf48hsm9de5lSRECQQD+WcQHRSEyuuWl
    dW0qhfRBXjtqnvS1VQ+CmS4nzeAVfNLV8KzJvx/evoQ9KxbnwfN/fQkyniK0RzNs
    kcXNZFsLAkEA6vHzWrQVtiQ5b8itwn3er7FWWy53SFLiy2MJCUBmm0Z2uJXox4Ym
    nH2SoUVMHJOvqgdqcwz53RJIlMPkAMgwJQJAbYTDZon6qHhXR65PSh8RtE/Z76fw
    IGA25HoGqLb6BOaRdfNCwz/bfjK0iA4Ut8gIi92P5062DMAXwWjnLfBHTwJBAM0E
    p2xeO5f+0lQ2lVJkDj/Yi1f0G0j0c04yNL9rAF69RXpb7o62BNmIRr0OQJWrVp4T
    7JNLHnsIqmeO7Va1WjUCQQDcY8s947hfiISXQ9o6auz33WyIRQWt/x0WdujVsxjM
    JNHkkGxCfEjy9Z74EO8MoSddiM4+u7gPZsr5f6AZvMsj
    -----END RSA PRIVATE KEY-----
    """
    
    // Generate encrypted data
    let key = try CryptorRSA.createPublicKey(withPEM: publicKey)
                let plaintext = try CryptorRSA.createPlaintext(with: "Hello World", using: .utf8)
                let encrypted = try plaintext.encrypted(with: key, algorithm: .sha256)
    
    // encrypted.base64String on linux (216 char)
    let linuxEncryptedHelloWorld = "VyG6sYjAAH3FtuOfN5BJWnScjVl5gAXTfThXbFNA0pU50pgPXEffV+T/4e9CchWHp6/8gpKNfuP7J2ly5577zwxnsLTHU3K2VlH2eq4UMEmKi3aZ9sUVzea3I5xvIoBVK0JVluNW4Wjld8Er1dB/1ZDwN94Qq9/TamNhiTD4a10XN5byzBn5D+lOFJ6RTk16Z4svXV3OjjvANYowxvXIeg=="
    
    // encrypted.base64String on mac (208 char)
    let macEncryptedHelloWorld = "JaLeZ4VhVoAENpa7EZZhsUpm2YmzfG9L496m0rtau4vR6wGKZ432l0xcfOkYjZeGN93/Fav82VNQEDonrWCUjEQuiodapbyne2r3HElhPwbbtEBVqeSI3XmJwTE8PH3Q7qGH9DxPxZf0dEhQAyoYNKQHVr4vumYanDNR+rGSvIQqPW37+JIt1zb0wjFaJVRUUsB8ELQzRD0yogM="
    
    if let decryptedString = self.decrypt(base64EncryptedData: linuxEncryptedHelloWorld) {
        print("Successfully decrypted the Linux encrypted word: \(decryptedString)")
    }
    if let decryptedString = self.decrypt(base64EncryptedData: macEncryptedHelloWorld) {
        print("Successfully decrypted the Mac encrypted word: \(decryptedString)")
    }
    // Only the operating system you are currently running will be able to decode the encrypted word
    

    On linux OpenSSL envelopes are used similar to this article On mac Security is used similar to this article

    I did notice that the aes digest is different here. However, when you change to EVP_aes_128_gcm() the test fail.

    bug 
    opened by Andrew-Lees11 4
  • Fix for Ubuntu 14.04 and OpenSSL 1.0.1

    Fix for Ubuntu 14.04 and OpenSSL 1.0.1

    There is an issue where systems using an older version of OpenSSL are not able to build, due to a type mismatch. The PR links to a branch I have made of the OpenSSL module map which now has a wrapper around the method in question (EVP_DigestVerifyFinal) to stop Swift complaining due to a type mismatch.

    When the OpenSSL branch is merged into master, I will update this PR to point to the release that contains the fix, rather than the branch. Opening this now so Travis can do some testing.

    opened by KyeMaloy97 4
  • New Release

    New Release

    Would it be possible to create a new release that contains https://github.com/Kitura/BlueRSA/pull/70.

    Our Project is using https://github.com/Kitura/Swift-JWT which references this repository. Therefore we need a new version of this package to be published in order that the fix will get included.

    Thanks for your work.

    opened by stmitt 3
  • Update iOS available version to 10.3

    Update iOS available version to 10.3

    Description

    A user was trying to build Swfit-JWT using Carthage and it wouldn't compile. They raised issue #64 which shows the compile error that:

    'SecCertificateCopyPublicKey' is only available on iOS 10.3 or newer
    

    This fix updates the @available tag to be:

    @available(macOS 10.12, iOS 10.3, *)
    

    Which allows the project to compile.

    How Has This Been Tested?

    The project was compiled and tested using the iOS simulator.

    opened by Andrew-Lees11 3
  • Update to use SecKeyCreateRandomKey to generate key pairs

    Update to use SecKeyCreateRandomKey to generate key pairs

    SecKeyGeneratePair was deprecated with iOS 15

    Upgraded to support building with Xcode 14 - with no warnings

    Updated minimum supported versions -

    • MacOS - 11.5
    • iOS - 14.5
    • TVOS - 14.5
    • WatchOS - 7.5

    Testing

    • Converted to use xctestplans
    • Added support to do iOS testing
      • Had to add compiler flags to enable successful testing on a simulator

    Updating to switch from the deprecated SecKeyGeneratePair to SecKeyCreateRandomKey

    Description

    SecKeyCreateRandomKey

    • Removed use of the SecKeyGeneratePair API (that was deprecated with iOS 15.0) to now use the SecKeyCreateRandomKey

    Minimum Versions

    • Updated the minimum OS Versions to more recent versions to address additional build warnings

    Unit Testing

    • Added a test target to run unit tests on an iOS device
    • As part of this, had to use some compiler directives to support successfully running tests on the simulator
    • Also converted to using xctestplans - as that is the new standard for Xcode

    Motivation and Context

    It was done to eliminate warnings that we see in our application, and also to just update the repo to more recent standards

    Issue: https://github.com/Kitura/BlueRSA/issues/77

    How Has This Been Tested?

    Tested locally by confirming successful unit tests. Also tested in our application to verify change was passive

    Checklist:

    • [x] If applicable, I have updated the documentation accordingly.
    • [x] If applicable, I have added tests to cover my changes.
    opened by LowAmmo 3
  • Update to use SecKeyCreateRandomKey

    Update to use SecKeyCreateRandomKey

    iOS 15.0 deprecated SecKeyGeneratePair, so would appreciate updating to SecKeyCreateRandomKey to eliminate a warning.

    Pretty easy to change over -

        var privateKey: SecKey?
        var publicKey: SecKey?
    
        do {
            var error: Unmanaged<CFError>?
    
            // The keys are automatically stored in the keychain
            guard let privKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error) else {
                throw error!.takeRetainedValue() as Error
            }
            privateKey = privKey
            publicKey = SecKeyCopyPublicKey(privateKey!)
        } catch {
            ulog.error(error: error, "Error creating public/private keys")
        }
    
    opened by LowAmmo 3
  • creating a symmetric public/private key

    creating a symmetric public/private key

    Hi there,

    can I use this lib also to generate a certificate with a private key that was created? Something like:

    let keypair = try CryptorRSA.makeKeyPair(CryptorRSA.RSAKey.KeySize.bits2048)
    var cert = createCertificate()
    cert.publicKey = keypair.publicKey
    cert.sign(keypair.privateKey, sha256)
    

    is it possible? thanks

    opened by SodaSurfer 0
  • Linux. Crash when decrypting:

    Linux. Crash when decrypting: "Fatal error: Can't form Range with upperBound < lowerBound"

    OS: Linux OpenSSL Version: 1.1.1

    I am using the same exact code on MacOS and on Linux:

    guard let decryptedData = try CryptorRSA.createEncrypted(with: data).decrypted(with: privateKey, algorithm: .sha1)?.data
    

    I am attempting to decrypt data with a private key. It works properly on MacOS, but when running on Linux I get a crash with error:

    Fatal error: Can't form Range with upperBound < lowerBound: file Swift/Range.swift, line 728
    

    I noticed this is coming from line 697 in Sources/CryptorRSA.swift during the step with the comment "Extract encryptedKey, encryptedData, encryptedIV from data"

    opened by ksinghal 1
  • what is error code -50?

    what is error code -50?

    getting this error when trying to decrypt.

    Error Domain=NSOSStatusErrorDomain Code=-50 "RSA-WRAP too short input data" UserInfo={NSDescription=RSA-WRAP too short input data}

    how should I resolve it????

    opened by MohsenMoghimi 3
Releases(1.0.201)
  • 1.0.201(Mar 9, 2021)

  • 1.0.35(May 14, 2019)

    Pre-Release: Version 1.0

    Swift cross-platform RSA wrapper library for RSA encryption and signing. Works on supported Apple platforms (using Security framework). Linux (using OpenSSL) is working but is still a work in progress.

    • CryptorRSA: Utility functions for RSA encryption and signing. Pure Swift

    Prerequisites

    Swift

    • Swift Open Source swift-4.0.0-RELEASE toolchain (Minimum REQUIRED for latest release)
    • Swift Open Source swift-5.0-RELEASE toolchain (Recommended)
    • Swift toolchain included in Xcode Version 10.2 (10E125) or higher.

    macOS

    • macOS 10.12.0 (Sierra) or higher. ** (Note: to use PSS signing and verification requires macOS 10.13 or higher.)
    • Xcode Version 9.0 (9A325) or higher using one of the above toolchains.
    • Xcode Version 10.2 (10E125) or higher using the included toolchain (Recommended).

    iOS

    • iOS 10.0 or higher ** (Note: to use PSS signing and verification requires iOS 11.0 or higher.)
    • Xcode Version 9.0 (9A325) or higher using one of the above toolchains.
    • Xcode Version 10.2 (10E125) or higher using the included toolchain (Recommended).

    Linux

    • Work in progress
    • Ubuntu 16.04 (or 16.10 but only tested on 16.04 and 18.04).
    • One of the Swift Open Source toolchains listed above
    • OpenSSL is provided by the distribution. Note: 1.0.x, 1.1.x and later releases of OpenSSL are supported.

    Changes since 1.0.0

    • Added prerequisite that the libssl-dev package is required to be installed when building on Linux.
    • Minor license update.
    • Fixes for Linux running older versions of OpenSSL
    • Updates and fixes for Swift 4.1.2, which is now the default version
    • Updates for Xcode 10 to project.
    • Added support for CocoaPods.
    • Make Swift 4.2 the default compiler in project.
    • Swift 4.2 support. PR #25.
    • Make Swift 4.2 the default compiler in the podspec.
    • Fixed warning(s) on Linux as result of change to Swift 4.2.
    • Update recommendations to Swift 4.2 and Xcode 10.
    • Added support for OpenSSL 1.1.x as well as OpenSSL 1.0.x.
    • Added iOS deployment target to podspec. Fixes issue #27. PR #28.
    • Add full support for iOS 10.3 or higher.
    • Added CI support for building with Swift 5. PR #29.
    • Fixed bug in CryptorRSA.decrypted() function.
    • Removed requirement of Swift 4.2 to support OpenSSL 1.1.x. OpenSSL 1.1.x is now supported using Swift 4.0, 4.1 and 4.2.
    • Fixed assorted memory leaks. PR #36.
    • Fixed crash when creating a public key using a base64 encoded string. PR #39.
    • Update CI support to use Swift 4.2.3. PR #43
    • Added functionality to make a key pair and pem string. PR #44.
    • Update to Swift 5.0. PR #45.
    • Added PSS sign/verify support for macOS 10.13 or higher and iOS 11.0 or higher and tvOS 11.0 or higher and watchOS 4.0 or higher. PR #46
    • Changed availability of APIs to require iOS 10.3 at a minimum for iOS builds. PR #48
    • Fixed warning due to new availability checks.
    • Add support for PKCS8 private keys on macOS. PR #49
    • Enable build on Xcode 11. PR #50
    • Enable compiling on MacOS Catalyst. PR #60
    Source code(tar.gz)
    Source code(zip)
  • 1.0.5(Aug 8, 2018)

  • 1.0.2(Feb 27, 2018)

    Pre-Release: Version 1.0

    Swift cross-platform RSA wrapper library for RSA encryption and signing. Works on supported Apple platforms (using Security framework). Linux (using OpenSSL) is working but is still a work in progress.

    Contents

    • CryptorRSA: Utility functions for RSA encryption and signing. Pure Swift

    Prerequisites

    Swift

    • Swift Open Source swift-4.0.0-RELEASE toolchain (Minimum REQUIRED for latest release)
    • Swift Open Source swift-4.0.3-RELEASE toolchain (Recommended)
    • Swift toolchain included in Xcode Version 9.0 (9A325) or higher.

    macOS

    • macOS 10.12.0 (Sierra) or higher
    • Xcode Version 9.0 (9A325) or higher using the included toolchain (Minimum REQUIRED for latest release).
    • Xcode Version 9.2 (9C40b) or higher using the included toolchain (Recommended).

    iOS

    • iOS 10.0 or higher
    • Xcode Version 9.0 (9A325) or higher using the included toolchain (Minimum REQUIRED for latest release).
    • Xcode Version 9.2 (9C40b) or higher using the included toolchain (Recommended).

    Linux

    • Work in progress
    • Ubuntu 16.04 (or 16.10 but only tested on 16.04)
    • One of the Swift Open Source toolchains listed above
    • OpenSSL 1.0.x is provided by the distribution. Note: Only the 1.0.x releases of OpenSSL are currently supported.
    • Added prerequisite that the libssl-dev package is required to be installed when building on Linux.
    • Minor license update.
    Source code(tar.gz)
    Source code(zip)
  • 0.1.18(Jan 24, 2017)

    This is a pre-release. It currently supports macOS/iOS with Linux support still a work in progress. The API contained herein is subject to change as Linux support is further developed.

    • See the README.md for minimum requirements for usage.
    • Added two new APIs that allow creation of a public key by extracting it from a PEM formatted certificate file.
    • Added new API, tests and documentation that allows extraction of a public key from a PEM formatted certificate contained in a Data object.
    • Support for Swift 4.
    • Support for Linux key handling. PR #6.
    • Support for Linux sign/verify handling. PR #10.
    • Support for padding option for RSA signing. PR #11
    • Support for Linux encryption/decryption. PR #12
    • Made the initializers for creating EncryptedData and SignedData public.
    Source code(tar.gz)
    Source code(zip)
Owner
Kitura
Kitura - Server Side framework written in Swift
Kitura
A customizable verification code textField. Can be used for phone verification codes, passwords etc

KKPinCodeTextField A customizable verification code textField. Can be used for phone verification codes, passwords etc. Настраиваемое текстовое поле д

Kolesa Group 66 Jul 5, 2022
Swift RSA Key Loader

Swift RSA Key Loader Requirements iOS 9.0+ Installation See the subsections below for details about the different installation methods. Swift Package

Dmytrii Golovanov 1 Jun 13, 2022
Private Password Manager developped with Swift for iOS project.

Private Password Manager developped with Swift for iOS project. This manager can syncronize secret data to Azure Blob Storage. To specify user account, tSecret use Azure Active Directory authentication.

Manabu Tonosaki 0 Dec 3, 2021
RSA encrypt and decrypt in Swift

MZRSA_Swift MZRSA_Swift是一个轻量级框架,框架功能包含RSA加密/解密Data、RSA加密/解密String,支持字符串密钥和证书密钥 公钥加密&私钥解密(字符串密钥) 代码示例 let PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8

null 8 Jan 5, 2023
Swift HybridCrypto is simple customizable implementation of hybrid cryptography (AES+RSA+Hash) recommended by OWASP.

HybridCrypto (Swift) HybridCrypto is simple implementation of hybrid cryptography following recommendations by OWASP. Prerequisites: Minimum iOS SDK:

UTNGY Pisal 2 Sep 6, 2022
Wrapper class for handling all tasks related to RSA cryptography

RSAWrapper Wrapper class for handling all tasks related to RSA cryptography USAG

null 1 Dec 24, 2021
CCCryptor (AES encryption) wrappers for iOS and Mac in Swift. -- For ObjC, see RNCryptor/RNCryptor-objc

RNCryptor Cross-language AES Encryptor/Decryptor data format. The primary targets are Swift and Objective-C, but implementations are available in C, C

null 3.3k Dec 30, 2022
Simple Swift wrapper for Keychain that works on iOS, watchOS, tvOS and macOS.

KeychainAccess KeychainAccess is a simple Swift wrapper for Keychain that works on iOS and OS X. Makes using Keychain APIs extremely easy and much mor

Kishikawa Katsumi 7.2k Dec 30, 2022
Valet lets you securely store data in the iOS, tvOS, or macOS Keychain without knowing a thing about how the Keychain works.

Valet Valet lets you securely store data in the iOS, tvOS, watchOS, or macOS Keychain without knowing a thing about how the Keychain works. It’s easy.

Square 3.8k Jan 4, 2023
A wrapper to make it really easy to deal with iOS, macOS, watchOS and Linux Keychain and store your user's credentials securely.

A wrapper (written only in Swift) to make it really easy to deal with iOS, macOS, watchOS and Linux Keychain and store your user's credentials securely.

Ezequiel Aceto 2 Mar 29, 2022
Native and encrypted password manager for iOS and macOS.

Open Sesame Native and encrypted password manager for iOS and macOS. What is it? OpenSesame is a free and powerful password manager that lets you mana

OpenSesame 432 Jan 7, 2023
iOS library for device fingerprinting. Does not require server APIs to work, fully client-side operation.

Lightweight iOS library for local device fingerprinting Installation (CocoaPods) # Podfile pod 'FingerprintJS' Note: If you've never used CocoaPods fo

FingerprintJS 45 Dec 17, 2022
Simple Objective-C wrapper for the keychain that works on Mac and iOS

SAMKeychain SAMKeychain is a simple wrapper for accessing accounts, getting passwords, setting passwords, and deleting passwords using the system Keyc

Sam Soffes 5.4k Dec 29, 2022
Apple's iOS Private Frameworks

iOS Private Framework This repo contains reversed-engendered private frameworks used by Apple in iOS. Private frameworks are frameworks which you are

Reels Research, Inc. 8 Oct 25, 2022
Send key events to any running macOS application.

KeySender An extremely simple micro package that enables you to send key events to any running application. Install Add the following to your Package.

Jordan Baird 5 Jul 31, 2022
Swift OpenSSL for OS X and Linux

OpenSSL OpenSSL for Swift 3.0. Installation import PackageDescription let package = Package( dependencies: [ .Package(url: "https://github.com/Zew

Zewo Graveyard 38 Sep 9, 2022
KeePassium is a KeePass-compatible password manager for iOS

KeePassium is a KeePass-compatible password manager for iOS. It offers automatic database synchronization, respect to privacy and premium user experience.

KeePassium 839 Jan 8, 2023
A key value store for storing per-developer environment and application keys

A key value store for enviroment and application keys. Its good security practice to keep production keys out of developer hands. CocoaPods-keys makes

Orta Therox 1.5k Dec 20, 2022
Swift Key Derivation

KeyDerivation This package implements key derivation system based of 5 levels: m / purpose' / coin_type' / account' / role / index As described in CIP

Arnot 1 Mar 5, 2022