diff --git a/appveyor.yml b/appveyor.yml index e5528b35e1e9..993061056a5c 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -138,8 +138,8 @@ for: - sh: sudo apt-get update - sh: sudo apt install -y lsb lsb-core alien libkrb5-dev - sh: cd ./cspTools - - sh: tar -xf linux_amd64_deb.tar - - sh: sudo ./linux_amd64_deb/install.sh + - sh: tar -xf linux-amd64_deb.tar + - sh: sudo ./linux-amd64_deb/install.sh - sh: sudo /opt/cprocsp/sbin/amd64/cpconfig -license -set "5050C-90030-05B5W-LTHVH-PFPU2" #install container - sh: mkdir -p /var/opt/cprocsp/keys/appveyor/ diff --git a/cspTools/linux-amd64_deb.tar b/cspTools/linux-amd64_deb.tar new file mode 100644 index 000000000000..6d7e7c302d73 Binary files /dev/null and b/cspTools/linux-amd64_deb.tar differ diff --git a/cspTools/linux_amd64_deb.tar b/cspTools/linux_amd64_deb.tar deleted file mode 100644 index 6d263c90f57c..000000000000 Binary files a/cspTools/linux_amd64_deb.tar and /dev/null differ diff --git a/src/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.Unix.cs b/src/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.Unix.cs new file mode 100644 index 000000000000..ab7a9c0c20a1 --- /dev/null +++ b/src/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.Unix.cs @@ -0,0 +1,61 @@ +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Pkcs; +using System.Security.Cryptography.Xml; +using static Interop.Crypt32; + +namespace Internal.Cryptography.Pal.Windows +{ + internal static partial class HelpersWindows + { + const int sizeof_wchar_t = 4; + + public static SubjectIdentifier ToSubjectIdentifier(this CERT_ID certId) + { + switch (certId.dwIdChoice) + { + case CertIdChoice.CERT_ID_ISSUER_SERIAL_NUMBER: + { + const int dwStrType = (int)(CertNameStrTypeAndFlags.CERT_X500_NAME_STR | CertNameStrTypeAndFlags.CERT_NAME_STR_REVERSE_FLAG); + + string issuer; + unsafe + { + DATA_BLOB* dataBlobPtr = &certId.u.IssuerSerialNumber.Issuer; + + int nc = Interop.Crypt32.CertNameToStr((int)MsgEncodingType.All, dataBlobPtr, dwStrType, null, 0); + if (nc <= 1) // The API actually return 1 when it fails; which is not what the documentation says. + { + throw Interop.CPError.GetLastWin32Error().ToCryptographicException(); + } + + Span name = nc <= 256 ? stackalloc byte[nc*sizeof_wchar_t] : new byte[nc*sizeof_wchar_t]; + fixed (byte* namePtr = name) + { + nc = Interop.Crypt32.CertNameToStr((int)MsgEncodingType.All, dataBlobPtr, dwStrType, (char*)namePtr, nc); + if (nc <= 1) // The API actually return 1 when it fails; which is not what the documentation says. + { + throw Interop.CPError.GetLastWin32Error().ToCryptographicException(); + } + + issuer = System.Text.Encoding.UTF32.GetString(name.Slice(0, (nc-1)*sizeof_wchar_t).ToArray()); + } + } + + byte[] serial = certId.u.IssuerSerialNumber.SerialNumber.ToByteArray(); + X509IssuerSerial issuerSerial = new X509IssuerSerial(issuer, serial.ToSerialString()); + return new SubjectIdentifier(SubjectIdentifierType.IssuerAndSerialNumber, issuerSerial); + } + + case CertIdChoice.CERT_ID_KEY_IDENTIFIER: + { + byte[] ski = certId.u.KeyId.ToByteArray(); + return new SubjectIdentifier(SubjectIdentifierType.SubjectKeyIdentifier, ski.ToSkiString()); + } + + default: + throw new CryptographicException(SR.Format(SR.Cryptography_Cms_Invalid_Subject_Identifier_Type, certId.dwIdChoice)); + } + } + } +} diff --git a/src/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.Windows.cs b/src/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.Windows.cs new file mode 100644 index 000000000000..809d08f2460d --- /dev/null +++ b/src/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.Windows.cs @@ -0,0 +1,59 @@ +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Pkcs; +using System.Security.Cryptography.Xml; +using static Interop.Crypt32; + +namespace Internal.Cryptography.Pal.Windows +{ + internal static partial class HelpersWindows + { + public static SubjectIdentifier ToSubjectIdentifier(this CERT_ID certId) + { + switch (certId.dwIdChoice) + { + case CertIdChoice.CERT_ID_ISSUER_SERIAL_NUMBER: + { + const int dwStrType = (int)(CertNameStrTypeAndFlags.CERT_X500_NAME_STR | CertNameStrTypeAndFlags.CERT_NAME_STR_REVERSE_FLAG); + + string issuer; + unsafe + { + DATA_BLOB* dataBlobPtr = &certId.u.IssuerSerialNumber.Issuer; + + int nc = Interop.Crypt32.CertNameToStr((int)MsgEncodingType.All, dataBlobPtr, dwStrType, null, 0); + if (nc <= 1) // The API actually return 1 when it fails; which is not what the documentation says. + { + throw Interop.CPError.GetLastWin32Error().ToCryptographicException(); + } + + Span name = nc <= 128 ? stackalloc char[128] : new char[nc]; + fixed (char* namePtr = name) + { + nc = Interop.Crypt32.CertNameToStr((int)MsgEncodingType.All, dataBlobPtr, dwStrType, namePtr, nc); + if (nc <= 1) // The API actually return 1 when it fails; which is not what the documentation says. + { + throw Interop.CPError.GetLastWin32Error().ToCryptographicException(); + } + + issuer = new string(namePtr); + } + } + + byte[] serial = certId.u.IssuerSerialNumber.SerialNumber.ToByteArray(); + X509IssuerSerial issuerSerial = new X509IssuerSerial(issuer, serial.ToSerialString()); + return new SubjectIdentifier(SubjectIdentifierType.IssuerAndSerialNumber, issuerSerial); + } + + case CertIdChoice.CERT_ID_KEY_IDENTIFIER: + { + byte[] ski = certId.u.KeyId.ToByteArray(); + return new SubjectIdentifier(SubjectIdentifierType.SubjectKeyIdentifier, ski.ToSkiString()); + } + + default: + throw new CryptographicException(SR.Format(SR.Cryptography_Cms_Invalid_Subject_Identifier_Type, certId.dwIdChoice)); + } + } + } +} diff --git a/src/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.cs b/src/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.cs index cb88443ce829..fa708c18b470 100644 --- a/src/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.cs +++ b/src/System.Security.Cryptography.Pkcs/src/Internal/Cryptography/Pal/Windows/HelpersWindows.cs @@ -20,7 +20,7 @@ namespace Internal.Cryptography.Pal.Windows { - internal static class HelpersWindows + internal static partial class HelpersWindows { public static CryptographicException ToCryptographicException(this ErrorCode errorCode) { @@ -160,55 +160,7 @@ public static unsafe byte[] GetSubjectKeyIdentifer(this SafeCertContextHandle hC throw Interop.CPError.GetLastWin32Error().ToCryptographicException(); return ski.Resize(cbData); - } - - public static SubjectIdentifier ToSubjectIdentifier(this CERT_ID certId) - { - switch (certId.dwIdChoice) - { - case CertIdChoice.CERT_ID_ISSUER_SERIAL_NUMBER: - { - const int dwStrType = (int)(CertNameStrTypeAndFlags.CERT_X500_NAME_STR | CertNameStrTypeAndFlags.CERT_NAME_STR_REVERSE_FLAG); - - string issuer; - unsafe - { - DATA_BLOB* dataBlobPtr = &certId.u.IssuerSerialNumber.Issuer; - - int nc = Interop.Crypt32.CertNameToStr((int)MsgEncodingType.All, dataBlobPtr, dwStrType, null, 0); - if (nc <= 1) // The API actually return 1 when it fails; which is not what the documentation says. - { - throw Interop.CPError.GetLastWin32Error().ToCryptographicException(); - } - - Span name = nc <= 128 ? stackalloc char[128] : new char[nc]; - fixed (char* namePtr = name) - { - nc = Interop.Crypt32.CertNameToStr((int)MsgEncodingType.All, dataBlobPtr, dwStrType, namePtr, nc); - if (nc <= 1) // The API actually return 1 when it fails; which is not what the documentation says. - { - throw Interop.CPError.GetLastWin32Error().ToCryptographicException(); - } - - issuer = new string(namePtr); - } - } - - byte[] serial = certId.u.IssuerSerialNumber.SerialNumber.ToByteArray(); - X509IssuerSerial issuerSerial = new X509IssuerSerial(issuer, serial.ToSerialString()); - return new SubjectIdentifier(SubjectIdentifierType.IssuerAndSerialNumber, issuerSerial); - } - - case CertIdChoice.CERT_ID_KEY_IDENTIFIER: - { - byte[] ski = certId.u.KeyId.ToByteArray(); - return new SubjectIdentifier(SubjectIdentifierType.SubjectKeyIdentifier, ski.ToSkiString()); - } - - default: - throw new CryptographicException(SR.Format(SR.Cryptography_Cms_Invalid_Subject_Identifier_Type, certId.dwIdChoice)); - } - } + } public static SubjectIdentifierOrKey ToSubjectIdentifierOrKey(this CERT_ID certId) { diff --git a/src/System.Security.Cryptography.Pkcs/src/System.Security.Cryptography.Pkcs.csproj b/src/System.Security.Cryptography.Pkcs/src/System.Security.Cryptography.Pkcs.csproj index e17431160cab..d08365f1b277 100644 --- a/src/System.Security.Cryptography.Pkcs/src/System.Security.Cryptography.Pkcs.csproj +++ b/src/System.Security.Cryptography.Pkcs/src/System.Security.Cryptography.Pkcs.csproj @@ -354,6 +354,7 @@ Common\Interop\Windows\Interop.Libraries.cs + Common\Interop\Interop.CPGetLastWin32Error.cs @@ -386,7 +387,8 @@ Common\Interop\Interop.CPGetLastWin32Error.Unix.cs - + +