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