|
3 | 3 | #include "Microsoft.Windows.Management.Deployment.PackageCertificateEkuValidator.g.cpp" |
4 | 4 | #include <TerminalVelocityFeatures-PackageManager.h> |
5 | 5 |
|
| 6 | +#include <AppxPackaging.h> |
| 7 | + |
6 | 8 | namespace winrt::Microsoft::Windows::Management::Deployment::implementation |
7 | 9 | { |
8 | 10 | PackageCertificateEkuValidator::PackageCertificateEkuValidator(hstring const& expectedCertificateEku) |
9 | 11 | { |
10 | 12 | THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Management::Deployment::Feature_PackageValidator::IsEnabled()); |
11 | 13 |
|
12 | | - UNREFERENCED_PARAMETER(expectedCertificateEku); |
13 | | - |
14 | | - throw hresult_not_implemented(); |
| 14 | + m_expectedEku = expectedCertificateEku; |
15 | 15 | } |
16 | 16 |
|
17 | 17 | bool PackageCertificateEkuValidator::IsPackageValid(winrt::Windows::Foundation::IInspectable const& appxPackagingObject) |
18 | 18 | { |
19 | 19 | THROW_HR_IF(E_NOTIMPL, !::Microsoft::Windows::Management::Deployment::Feature_PackageValidator::IsEnabled()); |
20 | 20 |
|
21 | | - UNREFERENCED_PARAMETER(appxPackagingObject); |
| 21 | + winrt::com_ptr<IAppxPackageReader> packageReader; |
| 22 | + winrt::com_ptr<IAppxBundleReader> bundleReader; |
22 | 23 |
|
23 | | - throw hresult_not_implemented(); |
| 24 | + if (SUCCEEDED(appxPackagingObject.as(IID_PPV_ARGS(&packageReader)))) |
| 25 | + { |
| 26 | + winrt::com_ptr<IAppxFile> signatureFile; |
| 27 | + if (SUCCEEDED(packageReader->GetFootprintFile(APPX_FOOTPRINT_FILE_TYPE_SIGNATURE, signatureFile.put()))) |
| 28 | + { |
| 29 | + return CheckSignature(signatureFile.get()); |
| 30 | + } |
| 31 | + else |
| 32 | + { |
| 33 | + return false; // package is not valid because there is no signature present |
| 34 | + } |
| 35 | + } |
| 36 | + else if (SUCCEEDED(appxPackagingObject.as(IID_PPV_ARGS(&bundleReader)))) |
| 37 | + { |
| 38 | + winrt::com_ptr<IAppxFile> signatureFile; |
| 39 | + if (SUCCEEDED(bundleReader->GetFootprintFile(APPX_BUNDLE_FOOTPRINT_FILE_TYPE_SIGNATURE, signatureFile.put()))) |
| 40 | + { |
| 41 | + return CheckSignature(signatureFile.get()); |
| 42 | + } |
| 43 | + else |
| 44 | + { |
| 45 | + return false; // package is not valid because there is no signature present |
| 46 | + } |
| 47 | + } |
| 48 | + else |
| 49 | + { |
| 50 | + THROW_WIN32(ERROR_NOT_SUPPORTED); |
| 51 | + } |
24 | 52 | } |
| 53 | + |
| 54 | + bool PackageCertificateEkuValidator::CheckSignature(IAppxFile* signatureFile) |
| 55 | + { |
| 56 | + winrt::com_ptr<IStream> signatureStream; |
| 57 | + THROW_IF_FAILED(signatureFile->GetStream(signatureStream.put())); |
| 58 | + |
| 59 | + // The p7x signature should have a leading 4-byte PKCX header |
| 60 | + static const DWORD P7xFileId = 0x58434b50; // PKCX |
| 61 | + static const DWORD P7xFileIdSize = sizeof(P7xFileId); |
| 62 | + |
| 63 | + STATSTG streamStats{}; |
| 64 | + THROW_IF_FAILED(signatureStream->Stat(&streamStats, STATFLAG_NONAME)); |
| 65 | + if (streamStats.cbSize.HighPart > 0 || streamStats.cbSize.LowPart < P7xFileIdSize) |
| 66 | + { |
| 67 | + return false; // The signature has unexpected size |
| 68 | + } |
| 69 | + |
| 70 | + DWORD streamSize = streamStats.cbSize.LowPart; |
| 71 | + auto streamBuffer{ wil::make_unique_nothrow<BYTE[]>(streamSize) }; |
| 72 | + THROW_IF_NULL_ALLOC(streamBuffer); |
| 73 | + |
| 74 | + ULONG bytesRead{}; |
| 75 | + THROW_IF_FAILED(signatureStream->Read(streamBuffer.get(), streamSize, &bytesRead)); |
| 76 | + THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_BAD_FORMAT), bytesRead != streamSize); |
| 77 | + |
| 78 | + if (*reinterpret_cast<DWORD*>(streamBuffer.get()) != P7xFileId) |
| 79 | + { |
| 80 | + return false; // The signature does not have expected header |
| 81 | + } |
| 82 | + |
| 83 | + CRYPT_DATA_BLOB signatureBlob{}; |
| 84 | + signatureBlob.cbData = streamSize - P7xFileIdSize; |
| 85 | + signatureBlob.pbData = streamBuffer.get() + P7xFileIdSize; |
| 86 | + |
| 87 | + wil::unique_hcertstore certStore; |
| 88 | + wil::unique_hcryptmsg signedMessage; |
| 89 | + DWORD queryContentType = 0; |
| 90 | + DWORD queryFormatType = 0; |
| 91 | + if (!CryptQueryObject( |
| 92 | + CERT_QUERY_OBJECT_BLOB, |
| 93 | + &signatureBlob, |
| 94 | + CERT_QUERY_CONTENT_FLAG_PKCS7_SIGNED, |
| 95 | + CERT_QUERY_FORMAT_FLAG_BINARY, |
| 96 | + 0, // Reserved parameter |
| 97 | + NULL, // No encoding info needed |
| 98 | + &queryContentType, |
| 99 | + &queryFormatType, |
| 100 | + &certStore, |
| 101 | + &signedMessage, |
| 102 | + NULL // No additional params for signed message output |
| 103 | + )) |
| 104 | + { |
| 105 | + return false; // CryptQueryObject could not read the signature |
| 106 | + } |
| 107 | + |
| 108 | + if (queryContentType != CERT_QUERY_CONTENT_PKCS7_SIGNED || queryFormatType != CERT_QUERY_FORMAT_BINARY || !certStore || !signedMessage) |
| 109 | + { |
| 110 | + return false; // CryptQueryObject returned unexpected data |
| 111 | + } |
| 112 | + |
| 113 | + CMSG_SIGNER_INFO* signerInfo = NULL; |
| 114 | + DWORD signerInfoSize = 0; |
| 115 | + if (!CryptMsgGetParam(signedMessage.get(), CMSG_SIGNER_INFO_PARAM, 0, NULL, &signerInfoSize)) |
| 116 | + { |
| 117 | + return false; // CryptMsgGetParam could not read the signature |
| 118 | + } |
| 119 | + |
| 120 | + if (signerInfoSize == 0 || signerInfoSize >= STRSAFE_MAX_CCH) |
| 121 | + { |
| 122 | + return false; // Signer info has unexpected size |
| 123 | + } |
| 124 | + |
| 125 | + auto signerInfoBuffer{ wil::make_unique_nothrow<BYTE[]>(signerInfoSize) }; |
| 126 | + THROW_IF_NULL_ALLOC(signerInfoBuffer); |
| 127 | + signerInfo = reinterpret_cast<CMSG_SIGNER_INFO*>(signerInfoBuffer.get()); |
| 128 | + if (!CryptMsgGetParam(signedMessage.get(), CMSG_SIGNER_INFO_PARAM, 0, signerInfo, &signerInfoSize)) |
| 129 | + { |
| 130 | + return false; // CryptMsgGetParam could not read the signature |
| 131 | + } |
| 132 | + |
| 133 | + // Get the signing certificate from the certificate store based on the issuer and serial number of the signer info |
| 134 | + CERT_INFO certInfo; |
| 135 | + certInfo.Issuer = signerInfo->Issuer; |
| 136 | + certInfo.SerialNumber = signerInfo->SerialNumber; |
| 137 | + |
| 138 | + wil::unique_cert_context certContext{ |
| 139 | + CertGetSubjectCertificateFromStore( |
| 140 | + certStore.get(), |
| 141 | + X509_ASN_ENCODING | PKCS_7_ASN_ENCODING, |
| 142 | + &certInfo) |
| 143 | + }; |
| 144 | + if (!certContext) |
| 145 | + { |
| 146 | + return false; // CertGetSubjectCertificateFromStore could not find a cert context |
| 147 | + } |
| 148 | + |
| 149 | + DWORD ekuBufferSize = 0; |
| 150 | + if (!CertGetEnhancedKeyUsage( |
| 151 | + certContext.get(), |
| 152 | + 0, // Accept EKU from EKU extension or EKU extended properties |
| 153 | + NULL, |
| 154 | + &ekuBufferSize)) |
| 155 | + { |
| 156 | + return false; // CertGetEnhancedKeyUsage could not read EKUs |
| 157 | + } |
| 158 | + |
| 159 | + if (ekuBufferSize < sizeof(CERT_ENHKEY_USAGE)) |
| 160 | + { |
| 161 | + return false; // No EKUs are found in the signature |
| 162 | + } |
| 163 | + |
| 164 | + auto ekuBuffer{ wil::make_unique_nothrow<BYTE[]>(ekuBufferSize)}; |
| 165 | + THROW_IF_NULL_ALLOC(ekuBuffer); |
| 166 | + PCERT_ENHKEY_USAGE ekusFound = reinterpret_cast<PCERT_ENHKEY_USAGE>(ekuBuffer.get()); |
| 167 | + if (!CertGetEnhancedKeyUsage( |
| 168 | + certContext.get(), |
| 169 | + 0, // Accept EKU from EKU extension or EKU extended properties |
| 170 | + ekusFound, |
| 171 | + &ekuBufferSize)) |
| 172 | + { |
| 173 | + return false; // CertGetEnhancedKeyUsage could not read EKUs |
| 174 | + } |
| 175 | + |
| 176 | + for (DWORD i = 0; i < ekusFound->cUsageIdentifier; i++) |
| 177 | + { |
| 178 | + auto eku{ ekusFound->rgpszUsageIdentifier[i] }; |
| 179 | + |
| 180 | + int length = MultiByteToWideChar(CP_ACP, 0, eku, -1, nullptr, 0); |
| 181 | + auto ekuWcharBuffer{ wil::make_unique_nothrow<WCHAR[]>(length) }; |
| 182 | + THROW_IF_NULL_ALLOC(ekuWcharBuffer); |
| 183 | + |
| 184 | + MultiByteToWideChar(CP_ACP, 0, eku, -1, ekuWcharBuffer.get(), length); |
| 185 | + |
| 186 | + if (wcscmp(ekuWcharBuffer.get(), m_expectedEku.c_str()) == 0) |
| 187 | + { |
| 188 | + return true; // Found expected EKU |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + return false; // Did not find expected EKU |
| 193 | + } |
| 194 | + |
25 | 195 | } |
0 commit comments