Skip to content

Commit

Permalink
Add rsa-pkcs-openssl test. (#322)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmds authored Dec 13, 2023
1 parent a865d83 commit 354d765
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
98 changes: 98 additions & 0 deletions rsa-pkcs-openssl/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/* This test validates .NET preserves the OpenSSL 3.2 behavior introduced in
https://github.com/openssl/openssl/pull/13817.
This change is backported to earlier OpenSSL versions in the Red Hat/Fedora distributions. */

using System.Security.Cryptography;

/* The data used in this test was generated using https://github.com/tomato42/marvin-toolkit/.
The data has PKCS padding errors. Instead of returning an error code for the padding errors,
OpenSSL returns success with data. */

string key = @"
-----BEGIN PRIVATE KEY-----
MIIEugIBADANBgkqhkiG9w0BAQEFAASCBKQwggSgAgEAAoIBAQCYLqFvySu5mYTS
j4yveQz0PAM4AQOdtCLriXUk6KVq0Q+sb0CxZ/L6S+KxNHIV46idUnNnihWRLdlU
MWG6Q0b7vKx58c8KGVZT51AUHaXyqbNFbh60ho8lPHIkrJxYw9Q22t2MIgb8pEgh
8yF6Z1aelSuZ8SC9rojhqUykaBG/oVwqaT6QG1lrS6gQx/glVbPv88ubUd6lSwMz
R2RwvhCTGMZFZDL3FP5ZmU2A6tQreLcC+3OloLoFDwzHkvrGBxhouEDuhmB91JHk
Erf+SUxY6gOV1hTuM6txnwZeOrJvGmrUaOsjT2cnvwDCun1KV+6tiQQlGmah7zMu
ST/km7RPAgMBAAECggEABs3sjVuz1xhS2yCGNe3vxj3Z5DFB0NVvy3ms1La+qrOC
HWYnp0CfTLfqGp1Ezc8myDEz4SwutOKVbJFjer+svVXDna8FRsDSRNPuly+qLo4n
t163BCbEelg+peTxfeCOE3PR/LfxLKIV0MeW6betvk9FXbYHDj273hP2GtKSZ8lR
qbP5UX+iRdPj2GThni4OWcN+lgszUyYb+yEAI8/JnNcISZjNFghjBuE8/Il3/I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,
0xbd, 0xec, 0x9b, 0xab, 0x55, 0xc6, 0x0b, 0x29, 0xa8, 0xc6, 0x72, 0xbf, 0xcb, 0x58, 0xda, 0x4d,
0x0e, 0x1a, 0x34, 0xd4, 0xc9, 0x93, 0x97, 0xce, 0xbc, 0x19, 0x50, 0xb3, 0x4b, 0xf2, 0xf9, 0x1f,
0x2e, 0x62, 0xbf, 0xf2, 0x5f, 0x50, 0xce, 0x89, 0x64, 0xd5, 0x54, 0xcc, 0xc4, 0x49, 0x57, 0xfa,
0x48, 0x7f, 0x19, 0x82, 0x17, 0x2b, 0xeb, 0x7e, 0x22, 0x6b, 0xd9, 0xf7, 0x77, 0x3e, 0xce, 0x72,
}.Select(i => (byte)i).ToArray();

byte[] expected = new[]
{
0x38, 0xd2, 0x12, 0xd1, 0xef, 0xd2, 0x4d, 0x28, 0x99, 0x3e, 0x07, 0x0d, 0x7b, 0xb2, 0x8b, 0xe7,
0x4f, 0x8c, 0xbe, 0x68, 0x9e, 0x29, 0x01, 0x14, 0xd6, 0x08, 0x73, 0x67, 0x34, 0x3e, 0xed, 0x03,
0x6c, 0x08, 0x01, 0x75, 0x4e, 0xea, 0xcf, 0x1f, 0x98, 0xdd, 0x3b, 0x82, 0x78, 0x3c, 0xaf, 0x74,
0x56, 0xf2, 0xab, 0xd8, 0x2c, 0xe9, 0xe6, 0xcd, 0x1d, 0x61, 0xc9, 0x77, 0xfc, 0x71, 0x14, 0x9c,
0x9d, 0x53, 0xfc, 0xf4, 0xfc, 0x4a, 0x4d, 0xff, 0x93, 0x96, 0x08, 0xb8, 0x90, 0x83, 0xad, 0xfa,
0x73, 0x87, 0xeb, 0xbe, 0x28, 0x21, 0xae, 0x8d, 0xa8, 0xca, 0x79, 0xff, 0x38, 0xa2, 0x26, 0x67,
0xd4, 0x91, 0x05, 0x34, 0x1b, 0x02, 0xea, 0x11, 0x02, 0x60, 0xe9, 0x4d, 0x5c, 0xfd, 0x14, 0x7a,
0xbf, 0xfc, 0xff, 0xe9, 0x5b, 0x08, 0x6e, 0x10, 0x9a, 0x1d, 0xdf,
}.Select(i => (byte)i).ToArray();

using RSA rsa = RSA.Create();
rsa.ImportFromPem(key);

byte[] decrypted;
try
{
decrypted = rsa.Decrypt(data, RSAEncryptionPadding.Pkcs1);
}
catch (Exception ex)
{
Console.WriteLine("FAIL: RSA decryption threw an exception:");
Console.WriteLine(ex);
return 1;
}

if (!expected.AsSpan().SequenceEqual(decrypted))
{
Console.WriteLine("FAIL: decrypted data does not match the expected value from OpenSSL:");
Console.WriteLine($"Actual: {string.Join("", decrypted.Select(b => b.ToString("x2")))}");
Console.WriteLine($"Expected: {string.Join("", expected.Select(b => b.ToString("x2")))}");
return 1;
}


Console.WriteLine("PASS: .NET RSA+PKCS API matches OpenSSL behavior.");
return 0;
10 changes: 10 additions & 0 deletions rsa-pkcs-openssl/rsa-pkcs-openssl.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>$(TestTargetFramework)</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
15 changes: 15 additions & 0 deletions rsa-pkcs-openssl/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "rsa-pkcs-openssl",
"enabled": true,
"requiresSdk": true,
"version": "6.0",
"versionSpecific": false,
"type": "bash",
"cleanup": true,
"skipWhen": [
"vmr-ci,version=9", // upstream main branch opts out of the OpenSSL change.
"os=alpine" // test validates behavior for Fedora/RHEL.
],
"ignoredRIDs":[
]
}
6 changes: 6 additions & 0 deletions rsa-pkcs-openssl/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash

set -euo pipefail
IFS=$'\n\t'

dotnet run

0 comments on commit 354d765

Please sign in to comment.