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

Swift 5.2 ClearMessage Has Bad Encryption #183

Closed
saeedmozaffari opened this issue Apr 14, 2020 · 14 comments · Fixed by #184
Closed

Swift 5.2 ClearMessage Has Bad Encryption #183

saeedmozaffari opened this issue Apr 14, 2020 · 14 comments · Fixed by #184

Comments

@saeedmozaffari
Copy link

saeedmozaffari commented Apr 14, 2020

After update swift version to 5.2 and update build latest "SwiftyRSA", encryption method at "ClearMessage" class sometime has wrong data.

Test :
I saved a clear message when encryption was OK, and for next time used from that object again
BUT!
for second time encrypted data was not ok. in fact correct encrypted data completely is random after update to swift 5.2.

func generateEncryptedSymmetricKeyData(length :Int,secondaryChunkSize:Int) -> Data {
// TEST AGAIN: repeat 'encrypted' method when encryption was ok
        if isOk { 
            do {
                encryptedSymmetricKeyData = Data()
                for clear in safeClearMessage {
                    let encrypted = try clear.encrypted(with: publicKeyClient, padding: .PKCS1)
                    encryptedSymmetricKeyData.append(contentsOf: encrypted.data)
                }
                return encryptedSymmetricKeyData
            } catch  {
                print(error)
            }
        }
  //===================================================================


        encryptedSymmetricKeyData = Data()
        symmetricKey = IGGlobal.randomString(length: length)
        do {
            let symmetricKeyData = symmetricKey.data(using: .utf8)
            var encSymmetricKeyData = try encrypt(rawData: symmetricKeyData!) 
            let publicKey = try PublicKey(pemEncoded: embeddedPublicKey)
            publicKeyClient = publicKey
            while(0<encSymmetricKeyData.count){
                let chunk = encSymmetricKeyData.subdata(in: 0..<secondaryChunkSize)
                let clear = ClearMessage(data: chunk)

                // STORE: store "ClearMessage" Object for use next time
                safeClearMessage.append(clear)

               // **ERROR LINE** this line sometimes is OK and sometimes is NOT OK
               let encrypted = try clear.encrypted(with: publicKey, padding: .PKCS1)


                encryptedSymmetricKeyData.append(contentsOf: encrypted.data)
                encSymmetricKeyData = encSymmetricKeyData.subdata(in: secondaryChunkSize..<encSymmetricKeyData.count)
            }
        } catch  {
            print(error)
        }
        return encryptedSymmetricKeyData
    }
@Glaubenio
Copy link

@TakeScoop any updates on this?

@phcelos
Copy link

phcelos commented Apr 15, 2020

We're facing the same problem...

@evermeer
Copy link

In our case we had about 10% of encryption failures.
Moving back to Xcode 11.3.1 'solved' the issue for me.

@jannemecek
Copy link
Contributor

jannemecek commented Apr 18, 2020

AFAIK this isn't specifically an issue in SwiftyRSA as it's just a wrapper around the Security APIs. It seems to fail during SecKeyDecrypt with https://developer.apple.com/documentation/security/1542001-security_framework_result_codes/errsecparam?language=objc

I haven't found a way to reproduce this in SwiftyRSA unit tests, though it does happen fairly often in production/our app.

@otsec
Copy link

otsec commented Apr 21, 2020

We are trying to encode credit card data and get an error every time for month, year and cvc.

Looks like this problem can be reproduced more often if you trying to encrypt small strings. Like 2–4 symbols.

@oscar-fung
Copy link

I am experiencing the same issue after migrating my project from Xcode 10.1 to Xcode 11.4. I am asking for Apple for this. Will keep update in this post.

https://forums.developer.apple.com/message/415437#415437

@johnharutyunyan
Copy link

johnharutyunyan commented Apr 24, 2020

We encountered the same problem when trying to decrypt message.

     do {
            debugPrint("\(Date()) Function: \(#function), Line: \(#line)")
            let encrypted = try EncryptedMessage(base64Encoded: message)
            let privateKey = try PrivateKey(pemEncoded: privateKeyString)
            let decrypted = try encrypted.decrypted(with: privateKey, padding: .PKCS1)
            let decryptedMessage = try decrypted.string(encoding: .utf8)
            
            return decryptedMessage
        } catch {
            debugPrint("\(Date()) Function: \(#function), Line: \(#line)")
            debugPrint(error.localizedDescription)
            return "Bad decryption"
        }

Not always, but sometimes we got "Bad decryption"

Xcode 11.4.1

@jannemecek
Copy link
Contributor

Can any of you test with the fork I made recently? I didn't have time to do extensive testing, but the update fixed the warnings (by using proper Data initializer that's available now) and I haven't seen the bug occur. Unit tests also all seem to pass.

pod 'SwiftyRSA', :git => 'https://github.com/jannemecek/SwiftyRSA.git', :branch => 'fix/dangling-pointers'

@saeedmozaffari
Copy link
Author

saeedmozaffari commented Apr 26, 2020

Problem
This problem is because of "ClearMessage" class. after update xCode to the latest version and update carthage, encryption sometimes was unsuccessful.

Solution : just copy "ClearMessage" class from swiftyRSA source in to your project with a new name and use from this class instead of "ClearMessage" at swiftyRSA.
seems to this problem is related to compile time and language version.
after than do this action this problem completely solved.

@evermeer
Copy link

@jannemecek I just did a test run (1000 api calls using RSA encryption) With your fix they all succeeded. With the original SwiftyRSA about the ½ failed. Could you make a PR for this?

@jannemecek
Copy link
Contributor

Great to hear that, just opened #184

@oscar-fung
Copy link

I am experiencing the same issue after migrating my project from Xcode 10.1 to Xcode 11.4. I am asking for Apple for this. Will keep update in this post.

https://forums.developer.apple.com/message/415437#415437

jannemecek's solution solves my issue. Thanks!

@streem-ua
Copy link

Can any of you test with the fork I made recently? I didn't have time to do extensive testing, but the update fixed the warnings (by using proper Data initializer that's available now) and I haven't seen the bug occur. Unit tests also all seem to pass.

pod 'SwiftyRSA', :git => 'https://github.com/jannemecek/SwiftyRSA.git', :branch => 'fix/dangling-pointers'

It works. Thank you! Can you please explain, what was wrong and what your fix do? Thanks

@leoniralves
Copy link
Contributor

I had the same problem, but in version 1.6.0 this has been resolved.
Now with 100% success in decryption.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants