2017-12-12 23 views
1

バイト配列からX509Certificate2インスタンスを作成すると、Windowsでは動作しますが、Linuxでは "CryptographicException"が失敗します。 WindowsではX509Certificate2はLinuxでは解析できませんが、Windowsでは動作しません

static void Main(string[] args) 
{ 
    var cert = new X509Certificate2(Cert.CertBytes); 
} 

:有効なX509Certificate2インスタンスは、Linuxでは が作成されます。例外がスローされます。

{System.Security.Cryptography.CryptographicException: Cannot find the original signer. at Internal.Cryptography.Pal.PkcsFormatReader.TryReadPkcs7(SafePkcs7Handle pkcs7, Boolean single, ICertificatePal& certPal, List`1& certPals) at Internal.Cryptography.Pal.PkcsFormatReader.TryReadPkcs7Der(Byte[] rawData, Boolean single, ICertificatePal& certPal, List`1& certPals) at Internal.Cryptography.Pal.CertificatePal.FromBlob(Byte[] rawData, SafePasswordHandle password, X509KeyStorageFlags keyStorageFlags) at System.Security.Cryptography.X509Certificates.X509Certificate..ctor(Byte[] data) at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(Byte[] rawData) at CertTest.Program.Main(String[] args) in /home/CertTest/Program.cs:line 14}

私が何か間違ったことをやっていますか?証明書は、それが解析されたOSにかかわらず、証明書であると仮定します。

現在のLinux、Windows上ではなく、解析することができ、有効なX509証明書を見つける:https://gist.github.com/secana/9c13f8fa495681f8a30adb5d8754450e

を私は複数の証明書を試してみましたが、どれもLinux上で働きました。私はMacを持っていないので、そこで動作するかどうかはテストできませんでした。 Ubuntuの16.04は、Ubuntu 17.10、openSUSEのタンブルウィード、Windowsの10

+0

問題のリンク:https://github.com/dotnet/corefx/issues/25828 – secana

答えて

0

new X509Certficate2()は、Windowsの場合のようにLinuxで署名証明書を返さないため、署名証明書を見つけるためにPKCS7のASN.1構造を解析する必要があります。

例:

// Import all certificates in the structure into a collection 
var collection = new X509Certificate2Collection(); 
collection.Import(Cert.CertBytes); 

// Find the signing cert 
var signingCert = collection.Cast<X509Certificate2>().FirstOrDefault(cert => 
string.Equals(cert.SerialNumber, SignerSerialNumber, 
StringComparison.CurrentCultureIgnoreCase)); 

唯一の難点は、署名証明書のシリアル番号を取得することです。そのために、私はASN.1構造を解析しました。シリアル番号は、ASN.1のパス1/0/4/0/1/1にあります。

例:私はMonoプロジェクトからのコードを使用しましたが、Nugetで利用可能ないくつかのパーサがあるASN.1パーサとして

// Get signing cert serial number from ASN.1 
var serialNumber = asn1[1][0][4][0][1][1]; 

+0

私がしなければならなかったループを飛び越したくない人にとって、このプロジェクト(免責事項:それは私のものです)は上記のソリューションを実装しています。 https://github.com/secana/PeNet – secana

2

で.NETコア2.0.2 でテスト

それはX509証明書ではなく、いくつかは(PKCS#7形式)のデータに署名しました。 Windowsは、証明書パス内の他の証明書と共にPKCS#7形式で証明書をエクスポートできます。それがあなたのケースです。名前をp7bに変更して、Windowsで開きます。

  • CN = Microsoft Windowsのシリアル番号: ASN.1 representation of binary data

    は、PKCS#7で2つの証明書がありますMicrosoft Windowsの生産PCAによって発行された330000016143159d469b7f968b000000000161、2011 Signing certificate

  • CN = Microsoft Windowsの生産PCA 2011年、シリアル番号:61077656000000000008、Microsoftルート証明機関発行2010 SubCA that issued the signing certificate

PKCS#7は、マイクロソフトタイムスタンプPCA 2010のタイムスタンプで署名されています。

なぜWindowsでは動作しますが、Linuxでは動作しません。わかりません。問題hereを提出するか、デバッグしてみてください。

+0

PKCS#7パーサーのヒントのThx。私は今働いている! – secana

関連する問題