Skip to content

Commit

Permalink
Unix cms encryption, csp version update.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fasjeit committed Jun 6, 2022
1 parent e567c7e commit 46f1946
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 53 deletions.
4 changes: 2 additions & 2 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand Down
Binary file added cspTools/linux-amd64_deb.tar
Binary file not shown.
Binary file removed cspTools/linux_amd64_deb.tar
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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<byte> 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));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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<char> 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));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<char> 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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@
<Compile Include="$(CommonPath)\Interop\Windows\Interop.Libraries.cs">
<Link>Common\Interop\Windows\Interop.Libraries.cs</Link>
</Compile>
<Compile Include="Internal\Cryptography\Pal\Windows\HelpersWindows.Windows.cs" />
<Compile Include="$(CommonPath)\Interop\Interop.CPGetLastWin32Error.cs">
<Link>Common\Interop\Interop.CPGetLastWin32Error.cs</Link>
</Compile>
Expand Down Expand Up @@ -386,7 +387,8 @@
<Compile Include="$(CommonPath)\Interop\Interop.CPGetLastWin32Error.Unix.cs">
<Link>Common\Interop\Interop.CPGetLastWin32Error.Unix.cs</Link>
</Compile>
<Compile Include="Interop\Unix\NCryptGetByteProperty.Unix.cs"/>
<Compile Include="Internal\Cryptography\Pal\Windows\HelpersWindows.Unix.cs" />
<Compile Include="Interop\Unix\NCryptGetByteProperty.Unix.cs" />
<Compile Include="Microsoft\Win32\SafeHandles\SafeProvOrNCryptKeyHandleUwp.Unix.cs" />
<Compile Include="Interop\Unix\Interop.Heap.Unix.cs" />
<!-- <Compile Include="Internal\Cryptography\Pal\AnyOS\PkcsPal.AnyOS.cs" /> -->
Expand Down

0 comments on commit 46f1946

Please sign in to comment.