Skip to content

[KNOWN ISSUE] The SDK does not correctly locate the Yubico.NativeShims.dll on 32 bit processes #159

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

Closed
DennisDyallo opened this issue Nov 8, 2024 · 10 comments · Fixed by #154 or #168 · May be fixed by #211
Closed

[KNOWN ISSUE] The SDK does not correctly locate the Yubico.NativeShims.dll on 32 bit processes #159

DennisDyallo opened this issue Nov 8, 2024 · 10 comments · Fixed by #154 or #168 · May be fixed by #211
Labels
bug Something isn't working known issue Used to indicate known issues by Yubico NETFramework Issues relating to NET Framework (4.x)

Comments

@DennisDyallo
Copy link
Collaborator

DennisDyallo commented Nov 8, 2024

On .NET Framework The SDK does not correctly locate the Yubico.NativeShims.dll on 32 bit processes.

When loading the SDK and trying to enumerate devices using the FindAll or FindByTransport methods. You will get a System.BadImageFormatException HResult=0x8007000B Message=An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) Source=Yubico.Core

NETFramework apps are 32bit processes by default. The workaround is to set your project to be 64 bit by using <Prefer32Bit>false</Prefer32Bit> in your csproj.

Related issues: #151, #65.

The fix will be published with the next release in December Q4, 2024.
Here's the fix #154

This only affects NET Framework and not NET6 and upwards.

Steps To Reproduce

  1. Start new 4.x project
  2. Import Yubikey nuget package
  3. Attempt to locate Yubikeys
  4. Get System.BadImageFormatException

Version

Affects all versions

Version

Affects all versions

@DennisDyallo DennisDyallo added bug Something isn't working known issue Used to indicate known issues by Yubico NETFramework Issues relating to NET Framework (4.x) labels Nov 8, 2024
@DennisDyallo DennisDyallo changed the title [KNOWN ISSUE] [NETFramework4] The SDK does not correctly locate the Yubico.NativeShims.dll on 32 bit processes The SDK does not correctly locate the Yubico.NativeShims.dll on 32 bit processes Nov 8, 2024
@DennisDyallo DennisDyallo pinned this issue Nov 8, 2024
@DennisDyallo DennisDyallo changed the title The SDK does not correctly locate the Yubico.NativeShims.dll on 32 bit processes [KNOWN ISSUE] The SDK does not correctly locate the Yubico.NativeShims.dll on 32 bit processes Nov 12, 2024
@DennisDyallo DennisDyallo linked a pull request Dec 17, 2024 that will close this issue
@DennisDyallo DennisDyallo unpinned this issue Dec 17, 2024
@romerod
Copy link

romerod commented Jan 10, 2025

There is still an issue with this, the files should be copied to x86 or x64 in any case not only in the case of AnyCPU as NativeMethods loads it from there:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <!--
        .NET Framework versions don't seem to include the native runtime files that
        are needed for this package. This makes sure they are included in downlevel projects.
        Since .NET Framework is Windows only, we only need to worry about that platform.
    -->

    <!-- x86 -->
    <ItemGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X86' OR '$(Platform)' == 'x86'">
        <Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x86\native\Yubico.NativeShims.dll">
            <Link>x86\Yubico.NativeShims.dll</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <Visible>false</Visible>
        </Content>
    </ItemGroup>

    <!-- x64 -->
    <ItemGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64' OR '$(Platform)' == 'x64'">
        <Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\Yubico.NativeShims.dll">
            <Link>x64\Yubico.NativeShims.dll</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <Visible>false</Visible>
        </Content>
    </ItemGroup>

    <!-- Arm64 -->
    <ItemGroup Condition="'$(Platform)' == 'arm64'">
        <Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-arm64\native\Yubico.NativeShims.dll">
            <Link>Yubico.NativeShims.dll</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <Visible>false</Visible>
        </Content>
    </ItemGroup>

    <!-- AnyCPU -->
    <ItemGroup Condition="'$(Platform)' == 'AnyCPU'">
        <Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x86\native\Yubico.NativeShims.dll">
            <Link>x86\Yubico.NativeShims.dll</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <Visible>false</Visible>
        </Content>
        <Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\Yubico.NativeShims.dll">
            <Link>x64\Yubico.NativeShims.dll</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <Visible>false</Visible>
        </Content>
    </ItemGroup>
</Project>

The ARM case is not used anymore

@DennisDyallo DennisDyallo reopened this Jan 11, 2025
@DennisDyallo
Copy link
Collaborator Author

DennisDyallo commented Jan 11, 2025

Hi @romerod
It's been a while since I sat with this, so I'm not sure I follow.

These two branches are copying the binaries to either /x86 or /x64 based on Platform or OS Architecture. Or am I missing something? Is is your problem the OR-clause, that it includes OSArchitecture as well?

<!-- x86 -->
<ItemGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X86' OR '$(Platform)' == 'x86'">
<!-- x64 -->
<ItemGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64' OR '$(Platform)' == 'x64'">

@DennisDyallo
Copy link
Collaborator Author

And regarding the ARM-case, I wasn't a part of setting up this targets-file, so my knowledge and experience is limited. But what do you mean it's not used any more? @romerod

@romerod
Copy link

romerod commented Jan 13, 2025

I understood what the branches do, but the original targets file doesn't copy it to x86 (or x64), the xml I've added in the comment was already fixed.

Here it's still copied to the root, I think these should be changed.
https://github.com/Yubico/Yubico.NET.SDK/blob/develop/Yubico.NativeShims/msbuild/Yubico.NativeShims.targets#L12
https://github.com/Yubico/Yubico.NET.SDK/blob/develop/Yubico.NativeShims/msbuild/Yubico.NativeShims.targets#L21

About the arm branch: From what I see in

<file src="msbuild/Yubico.NativeShims.targets" target="build/net47/Yubico.NativeShims.targets" />
<file src="msbuild/Yubico.NativeShims.targets" target="buildTransitive/net47/Yubico.NativeShims.targets" />

the targets file is only used for net47.

And

private static string NativeShimsPath =>
doesn't check for ARM.

From my point of view the code should be:


/// <summary>
	/// Gets the full path to the architecture-specific native library.
	/// </summary>
	/// <remarks>
	/// The path is constructed based on:
	/// - The application's base directory
	/// - The current process architecture (x86/x64)
	/// - The native library filename
	/// </remarks>
	private static string NativeShimsPath =>
		Path.Combine(
			AppDomain.CurrentDomain.BaseDirectory,
			System.Runtime.InteropServices.RuntimeInformation.OSArchitecture switch
			{
				Architecture.X86 => "x86",
				Architecture.X64 => "x64",
				Architecture.Arm64 => "arm64",
				var a => throw new ArgumentOutOfRangeException($"Architecture {a} is not supported!")
			},
			NativeShims);

And the targets file should be:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <!--
        .NET Framework versions don't seem to include the native runtime files that
        are needed for this package. This makes sure they are included in downlevel projects.
        Since .NET Framework is Windows only, we only need to worry about that platform.
    -->

    <!-- x86 -->
    <ItemGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X86' OR '$(Platform)' == 'x86'">
        <Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x86\native\Yubico.NativeShims.dll">
            <Link>x86\Yubico.NativeShims.dll</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <Visible>false</Visible>
        </Content>
    </ItemGroup>

    <!-- x64 -->
    <ItemGroup Condition="'$([System.Runtime.InteropServices.RuntimeInformation]::OSArchitecture)' == 'X64' OR '$(Platform)' == 'x64'">
        <Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\Yubico.NativeShims.dll">
            <Link>x64\Yubico.NativeShims.dll</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <Visible>false</Visible>
        </Content>
    </ItemGroup>

    <!-- Arm64 -->
    <ItemGroup Condition="'$(Platform)' == 'arm64'">
        <Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-arm64\native\Yubico.NativeShims.dll">
            <Link>arm64\Yubico.NativeShims.dll</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <Visible>false</Visible>
        </Content>
    </ItemGroup>

    <!-- AnyCPU -->
    <ItemGroup Condition="'$(Platform)' == 'AnyCPU'">
        <Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x86\native\Yubico.NativeShims.dll">
            <Link>x86\Yubico.NativeShims.dll</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <Visible>false</Visible>
        </Content>
        <Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-x64\native\Yubico.NativeShims.dll">
            <Link>x64\Yubico.NativeShims.dll</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <Visible>false</Visible>
        </Content>
        <Content Include="$(MSBuildThisFileDirectory)..\..\runtimes\win-arm64\native\Yubico.NativeShims.dll">
            <Link>arm64\Yubico.NativeShims.dll</Link>
            <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
            <Visible>false</Visible>
        </Content>
    </ItemGroup>
</Project>

@DennisDyallo
Copy link
Collaborator Author

Aha, now I understand.
Yes, a bit slow after the holidays.
So, the problem then is that if you target x86 or x64 specifically, the SDK wont find the respective Yubico.NativeShims.dll because they are not in the x86 and x64 subdirectories (they are in the root output directory instead, and thats what your fixed .targets-file was addressing)

And the ARM-issue, I haven't taken a lookt at it properly. But what you suggest makes sense to me. @romerod

@romerod
Copy link

romerod commented Jan 16, 2025

@DennisDyallo will this be implemented?

@DennisDyallo
Copy link
Collaborator Author

Our next planned release is Mid April @romerod. How badly is this affecting you guys?

@romerod
Copy link

romerod commented Jan 17, 2025

We can wait until april, no problem. We have a workarround in place. Should I still reopen the issue?

@DennisDyallo
Copy link
Collaborator Author

Hm. The issue is referring to the resolving the dll in the context of 32 bit processes. The issue youre describing here is when we specifically target either x86 or x64. The build doesn't properly output the dll in those cases. How about I create a new issue with the issue specified?

"The SDK does not correctly output Yubico.NativeShims.dll when targeting x86 or x64" and link in this issue. Is that OK for you @romerod ? Im grateful for your input. I don't get to talk to many users of our SDK :P

@DennisDyallo
Copy link
Collaborator Author

DennisDyallo commented Apr 8, 2025

@romerod I regret to inform you that it didn't make the 1.13 release. There is a PR open that has all the code changes, but I've. But I didn't have enough time to test it.

However, I will make sure this fix will make the next mini release so it doesn't have to wait too long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working known issue Used to indicate known issues by Yubico NETFramework Issues relating to NET Framework (4.x)
2 participants