dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
22.21k stars 1.75k forks source link

iOS Linker removes support for PCLCrypto.SymmetricKeyAlgorithmProvider.GetAlgorithm #20583

Closed donaghy closed 8 months ago

donaghy commented 8 months ago

Description

When building a MAUI project targeting iOS with MtouchLink = SdkOnly we are seeing the error "Speficied method is not supported" for the execution of PCLCrypto.SymmetricKeyAlgorithmProvider.GetAlgorithm(). When we set the MtouchLink option to None it works fine.

We dont see this issue in our Android release builds of the same project.

Steps to Reproduce

`byte[] keyMaterial = [method to generate key bytes];

var provider = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7); var key = provider.CreateSymmetricKey(keyMaterial);`

Link to public reproduction project repository

No response

Version with bug

8.0.6 SR1

Is this a regression from previous behavior?

Yes, this used to work in Xamarin.Forms

Last version that worked well

Unknown/Other

Affected platforms

iOS

Affected platform versions

iOS 17.0.2

Adding assembly 'System.Secruity.Cryptography' as a TrimmerRootAssembly appears to resolve the issue.

Relevant log output

Message -> "Specified method is not supported." Stacktrace -> "at PCLCrypto.SymmetricKeyAlgorithmProvider.GetAlgorithm() at PCLCryptto.SymmeticKeyAlogorithmProvider.CreateSymmetricKey(Byte[] keyMatrial) at {line of code being excuted}

drasticactions commented 8 months ago

@rolfbjarne Is this something that can be addressed by us? This seems like something at the library level to keep it from being trimmed, or maybe it should't be trimmed at all? I'm not sure if this is a bug.

rolfbjarne commented 8 months ago

@donaghy can you post the full stack trace of the exception?

ghost commented 8 months ago

Hi @donaghy. We have added the "s/needs-info" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

donaghy commented 8 months ago

I've added the stacktrace detail, there is very little to it. Note that we found a workaround by excluding the assembly 'System.Secruity.Cryptography' from the linker. As the Android release build works with the linker process would this indicate that the Assembly is correctly marked as 'Trimmable' were applicable and the issue lays with the iOS Linker? I'm assuming also though that part of that assembly may be platform specific and not have the correct Trimmable attributes where necessary.

rolfbjarne commented 8 months ago

This is an issue with the PCLCrypto library, it's not trimmer-safe.

Referencing it in a net8.0-ios project yields this build warning:

warning NU1701: Package 'PCLCrypto 2.0.147' was restored using '.NETFramework,Version=v4.6.1, .NETFramework,Version=v4.6.2, .NETFramework,Version=v4.7, .NETFramework,Version=v4.7.1, .NETFramework,Version=v4.7.2, .NETFramework,Version=v4.8, .NETFramework,Version=v4.8.1' instead of the project target framework 'net8.0-ios17.2'. This package may not be fully compatible with your project.

Looking at the compatible frameworks here: https://www.nuget.org/packages/PCLCrypto/2.1.40-alpha#supportedframeworks-body-tab

We can deduce that it has a specific implementation for Xamarin.iOS (because "xamarinios10" is darker blue), but it does not have a specific implementation for net8.0-ios (because "net8.0-ios" is a lighter blue).

Looking at the source code, there's an iOS-specific block:

https://github.com/AArnott/PCLCrypto/blob/d0530db9d717175df02e1e1f4ae78910af302942/src/PCLCrypto/SymmetricKeyAlgorithmProvider.cs#L139-L148

but since there's no iOS-specific version of the NuGet for .NET, that code path won't be hit, so this is executed:

https://github.com/AArnott/PCLCrypto/blob/d0530db9d717175df02e1e1f4ae78910af302942/src/PCLCrypto/SymmetricKeyAlgorithmProvider.cs#L147

which is probably not trimmer safe.

donaghy commented 8 months ago

Thanks for investigating the issue and the detailed explanation. Much appreciated.