Skip to content

Commit be185b4

Browse files
Aot compatible: NuGet.Packaging (#7197)
1 parent 460c27e commit be185b4

5 files changed

Lines changed: 17 additions & 46 deletions

File tree

src/NuGet.Core/NuGet.Packaging/NuGet.Packaging.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<IncludeInVSIX>true</IncludeInVSIX>
1111
<XPLATProject>true</XPLATProject>
1212
<UsePublicApiAnalyzer>perTfm</UsePublicApiAnalyzer>
13-
<IsAotCompatible>false</IsAotCompatible>
1413
</PropertyGroup>
1514

1615
<PropertyGroup Condition=" '$(IsVsixBuild)' == 'true' ">

src/NuGet.Core/NuGet.Packaging/Signing/Cms/NativeCms.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Collections.Generic;
88
using System.Runtime.InteropServices;
99
using System.Security.Cryptography.X509Certificates;
10-
using NuGet.Packaging.Signing.Utility;
1110
using System.Linq;
1211
using System.Security.Cryptography;
1312
using System.Security.Cryptography.Pkcs;
@@ -108,16 +107,16 @@ internal byte[] GetRepositoryCountersignatureSignatureValue()
108107
pointer,
109108
ref unsignedAttributeCount));
110109

111-
var unsignedAttributes = MarshalUtility.PtrToStructure<CRYPT_ATTRIBUTES>(pointer);
112-
int sizeOfCryptAttributeString = MarshalUtility.SizeOf<CRYPT_ATTRIBUTE_STRING>();
113-
int sizeOfCryptIntegerBlob = MarshalUtility.SizeOf<CRYPT_INTEGER_BLOB>();
110+
var unsignedAttributes = Marshal.PtrToStructure<CRYPT_ATTRIBUTES>(pointer);
111+
int sizeOfCryptAttributeString = Marshal.SizeOf<CRYPT_ATTRIBUTE_STRING>();
112+
int sizeOfCryptIntegerBlob = Marshal.SizeOf<CRYPT_INTEGER_BLOB>();
114113

115114
for (uint i = 0; i < unsignedAttributes.cAttr; ++i)
116115
{
117116

118117
var attributePointer = new IntPtr(
119118
(long)unsignedAttributes.rgAttr + (i * sizeOfCryptAttributeString));
120-
var attribute = MarshalUtility.PtrToStructure<CRYPT_ATTRIBUTE_STRING>(attributePointer);
119+
var attribute = Marshal.PtrToStructure<CRYPT_ATTRIBUTE_STRING>(attributePointer);
121120

122121
if (!string.Equals(attribute.pszObjId, Oids.Countersignature, StringComparison.Ordinal))
123122
{
@@ -128,7 +127,7 @@ internal byte[] GetRepositoryCountersignatureSignatureValue()
128127
{
129128
var attributeValuePointer = new IntPtr(
130129
(long)attribute.rgValue + (j * sizeOfCryptIntegerBlob));
131-
var attributeValue = MarshalUtility.PtrToStructure<CRYPT_INTEGER_BLOB>(attributeValuePointer);
130+
var attributeValue = Marshal.PtrToStructure<CRYPT_INTEGER_BLOB>(attributeValuePointer);
132131
uint cbSignerInfo = 0;
133132

134133
NativeUtility.ThrowIfFailed(NativeMethods.CryptDecodeObject(
@@ -151,7 +150,7 @@ internal byte[] GetRepositoryCountersignatureSignatureValue()
151150
pvStructInfo: counterSignerInfoPointer,
152151
pcbStructInfo: new IntPtr(&cbSignerInfo)));
153152

154-
var counterSignerInfo = MarshalUtility.PtrToStructure<CMSG_SIGNER_INFO>(counterSignerInfoPointer);
153+
var counterSignerInfo = Marshal.PtrToStructure<CMSG_SIGNER_INFO>(counterSignerInfoPointer);
155154

156155
if (IsRepositoryCounterSignerInfo(counterSignerInfo))
157156
{
@@ -171,12 +170,12 @@ internal byte[] GetRepositoryCountersignatureSignatureValue()
171170
private static bool IsRepositoryCounterSignerInfo(CMSG_SIGNER_INFO counterSignerInfo)
172171
{
173172
var signedAttributes = counterSignerInfo.AuthAttrs;
174-
int sizeOfCryptAttributeString = MarshalUtility.SizeOf<CRYPT_ATTRIBUTE_STRING>();
173+
int sizeOfCryptAttributeString = Marshal.SizeOf<CRYPT_ATTRIBUTE_STRING>();
175174
for (var i = 0; i < signedAttributes.cAttr; ++i)
176175
{
177176
var signedAttributePointer = new IntPtr(
178177
(long)signedAttributes.rgAttr + (i * sizeOfCryptAttributeString));
179-
var signedAttribute = MarshalUtility.PtrToStructure<CRYPT_ATTRIBUTE_STRING>(signedAttributePointer);
178+
var signedAttribute = Marshal.PtrToStructure<CRYPT_ATTRIBUTE_STRING>(signedAttributePointer);
180179
if (string.Equals(signedAttribute.pszObjId, Oids.CommitmentTypeIndication, StringComparison.Ordinal) &&
181180
IsRepositoryCounterSignerInfo(signedAttribute))
182181
{
@@ -189,13 +188,13 @@ private static bool IsRepositoryCounterSignerInfo(CMSG_SIGNER_INFO counterSigner
189188

190189
private static bool IsRepositoryCounterSignerInfo(CRYPT_ATTRIBUTE_STRING commitmentTypeIndicationAttribute)
191190
{
192-
int sizeOfCryptIntegerBlob = MarshalUtility.SizeOf<CRYPT_INTEGER_BLOB>();
191+
int sizeOfCryptIntegerBlob = Marshal.SizeOf<CRYPT_INTEGER_BLOB>();
193192

194193
for (var i = 0; i < commitmentTypeIndicationAttribute.cValue; ++i)
195194
{
196195
var attributeValuePointer = new IntPtr(
197196
(long)commitmentTypeIndicationAttribute.rgValue + (i * sizeOfCryptIntegerBlob));
198-
var attributeValue = MarshalUtility.PtrToStructure<CRYPT_INTEGER_BLOB>(attributeValuePointer);
197+
var attributeValue = Marshal.PtrToStructure<CRYPT_INTEGER_BLOB>(attributeValuePointer);
199198
var bytes = new byte[attributeValue.cbData];
200199

201200
Marshal.Copy(attributeValue.pbData, bytes, startIndex: 0, length: bytes.Length);
@@ -317,7 +316,7 @@ internal unsafe void AddTimestampToRepositoryCountersignature(SignedCms timestam
317316
var signerInfo = repositoryCountersignature.Value.SignerInfo;
318317
var unauthAttrCount = signerInfo.UnauthAttrs.cAttr + 1;
319318

320-
var sizeOfCryptAttribute = MarshalUtility.SizeOf<CRYPT_ATTRIBUTE>();
319+
var sizeOfCryptAttribute = Marshal.SizeOf<CRYPT_ATTRIBUTE>();
321320
var attributesArray = (CRYPT_ATTRIBUTE*)hb.Alloc((int)(sizeOfCryptAttribute * unauthAttrCount));
322321
var currentAttribute = attributesArray;
323322

@@ -326,7 +325,7 @@ internal unsafe void AddTimestampToRepositoryCountersignature(SignedCms timestam
326325
{
327326
var existingAttributePointer = new IntPtr(
328327
(long)signerInfo.UnauthAttrs.rgAttr + (i * sizeOfCryptAttribute));
329-
var existingAttribute = MarshalUtility.PtrToStructure<CRYPT_ATTRIBUTE>(existingAttributePointer);
328+
var existingAttribute = Marshal.PtrToStructure<CRYPT_ATTRIBUTE>(existingAttributePointer);
330329

331330
currentAttribute->pszObjId = existingAttribute.pszObjId;
332331
currentAttribute->cValue = existingAttribute.cValue;

src/NuGet.Core/NuGet.Packaging/Signing/Cms/NativeUtility.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
using System.Runtime.InteropServices;
88
using System.Security.Cryptography;
99
using System.Security.Cryptography.Pkcs;
10-
using NuGet.Packaging.Signing.Utility;
1110

1211
namespace NuGet.Packaging.Signing
1312
{
@@ -38,7 +37,7 @@ internal static SignedCms NativeSign(CmsSigner cmsSigner, byte[] data, CngKey pr
3837
for (var i = 0; i < cmsSigner.Certificates.Count; ++i)
3938
{
4039
var cert = cmsSigner.Certificates[i];
41-
var context = MarshalUtility.PtrToStructure<CERT_CONTEXT>(cert.Handle);
40+
var context = Marshal.PtrToStructure<CERT_CONTEXT>(cert.Handle);
4241

4342
certificateBlobs[i] = new BLOB() { cbData = context.cbCertEncoded, pbData = context.pbCertEncoded };
4443
}
@@ -118,7 +117,7 @@ internal unsafe static CMSG_SIGNER_ENCODE_INFO CreateSignerInfo(
118117
var signerInfo = new CMSG_SIGNER_ENCODE_INFO();
119118

120119
signerInfo.cbSize = (uint)Marshal.SizeOf(signerInfo);
121-
signerInfo.pCertInfo = MarshalUtility.PtrToStructure<CERT_CONTEXT>(cmsSigner.Certificate.Handle).pCertInfo;
120+
signerInfo.pCertInfo = Marshal.PtrToStructure<CERT_CONTEXT>(cmsSigner.Certificate.Handle).pCertInfo;
122121
signerInfo.hCryptProvOrhNCryptKey = privateKey.Handle.DangerousGetHandle();
123122
signerInfo.HashAlgorithm.pszObjId = cmsSigner.DigestAlgorithm.Value;
124123

@@ -169,8 +168,8 @@ internal unsafe static CMSG_SIGNER_ENCODE_INFO CreateSignerInfo(
169168

170169
checked
171170
{
172-
int sizeOfCryptAttribute = MarshalUtility.SizeOf<CRYPT_ATTRIBUTE>();
173-
int sizeOfCryptIntegerBlob = MarshalUtility.SizeOf<CRYPT_INTEGER_BLOB>();
171+
int sizeOfCryptAttribute = Marshal.SizeOf<CRYPT_ATTRIBUTE>();
172+
int sizeOfCryptIntegerBlob = Marshal.SizeOf<CRYPT_INTEGER_BLOB>();
174173
var attributesArray = (CRYPT_ATTRIBUTE*)hb.Alloc(sizeOfCryptAttribute * cmsSigner.SignedAttributes.Count);
175174
var currentAttribute = attributesArray;
176175

src/NuGet.Core/NuGet.Packaging/Signing/TrustStore/FallbackCertificateBundleX509ChainFactory.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ internal static bool TryCreate(
6060

6161
private static string GetThisAssemblyDirectoryPath()
6262
{
63-
string location = typeof(FallbackCertificateBundleX509ChainFactory).Assembly.Location;
64-
FileInfo thisAssembly = new(location);
65-
66-
return thisAssembly.DirectoryName;
63+
return AppContext.BaseDirectory;
6764
}
6865
}
6966
}

src/NuGet.Core/NuGet.Packaging/Signing/Utility/MarshalUtility.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)