@@ -1742,16 +1742,13 @@ std::optional<X509NameInspection> InspectName(const X509_NAME* name) { |
1742 | 1742 | return output; |
1743 | 1743 | } |
1744 | 1744 |
|
| 1745 | +template <typename GetExtension> |
1745 | 1746 | std::optional<std::vector<X509RawExtension>> InspectExtensions( |
1746 | | - const X509* certificate) { |
1747 | | - if (certificate == nullptr) return std::nullopt; |
1748 | | - const int count = X509_get_ext_count(certificate); |
1749 | | - if (count < 0) return std::nullopt; |
1750 | | - |
| 1747 | + OPENSSL_SIZE_T count, GetExtension get_extension) { |
1751 | 1748 | std::vector<X509RawExtension> output; |
1752 | 1749 | output.reserve(static_cast<size_t>(count)); |
1753 | | - for (int i = 0; i < count; i++) { |
1754 | | - const X509_EXTENSION* extension = X509_get_ext(certificate, i); |
| 1750 | + for (OPENSSL_SIZE_T i = 0; i < count; i++) { |
| 1751 | + const X509_EXTENSION* extension = get_extension(i); |
1755 | 1752 | if (extension == nullptr) return std::nullopt; |
1756 | 1753 |
|
1757 | 1754 | auto* mutable_extension = const_cast<X509_EXTENSION*>(extension); |
@@ -1770,6 +1767,15 @@ std::optional<std::vector<X509RawExtension>> InspectExtensions( |
1770 | 1767 | } |
1771 | 1768 | } // namespace |
1772 | 1769 |
|
| 1770 | +std::optional<std::vector<X509RawExtension>> inspectX509Extensions( |
| 1771 | + const STACK_OF(X509_EXTENSION) * extensions) { |
| 1772 | + if (extensions == nullptr) return std::nullopt; |
| 1773 | + return InspectExtensions(sk_X509_EXTENSION_num(extensions), |
| 1774 | + [extensions](OPENSSL_SIZE_T index) { |
| 1775 | + return sk_X509_EXTENSION_value(extensions, index); |
| 1776 | + }); |
| 1777 | +} |
| 1778 | + |
1773 | 1779 | std::optional<int64_t> X509View::getVersion() const { |
1774 | 1780 | MarkPopErrorOnReturn mark_pop_error_on_return; |
1775 | 1781 | if (cert_ == nullptr) return std::nullopt; |
@@ -1793,7 +1799,13 @@ std::optional<X509NameInspection> X509View::inspectIssuerName() const { |
1793 | 1799 | std::optional<std::vector<X509RawExtension>> X509View::inspectExtensions() |
1794 | 1800 | const { |
1795 | 1801 | MarkPopErrorOnReturn mark_pop_error_on_return; |
1796 | | - return InspectExtensions(cert_); |
| 1802 | + if (cert_ == nullptr) return std::nullopt; |
| 1803 | + const int count = X509_get_ext_count(cert_); |
| 1804 | + if (count < 0) return std::nullopt; |
| 1805 | + return InspectExtensions( |
| 1806 | + static_cast<OPENSSL_SIZE_T>(count), [this](OPENSSL_SIZE_T index) { |
| 1807 | + return X509_get_ext(cert_, static_cast<int>(index)); |
| 1808 | + }); |
1797 | 1809 | } |
1798 | 1810 |
|
1799 | 1811 | bool X509View::isIssuedBy(const X509View& issuer) const { |
@@ -1803,10 +1815,39 @@ bool X509View::isIssuedBy(const X509View& issuer) const { |
1803 | 1815 | const_cast<X509*>(cert_)) == X509_V_OK; |
1804 | 1816 | } |
1805 | 1817 |
|
1806 | | -bool X509View::checkPrivateKey(const EVPKeyPointer& pkey) const { |
1807 | | - ClearErrorOnReturn clearErrorOnReturn; |
1808 | | - if (cert_ == nullptr || pkey == nullptr) return false; |
1809 | | - return X509_check_private_key(const_cast<X509*>(cert_), pkey.get()) == 1; |
| 1818 | +X509View::CheckKeyResult X509View::checkPrivateKey( |
| 1819 | + const EVPKeyPointer& pkey) const { |
| 1820 | + MarkPopErrorOnReturn mark_pop_error_on_return; |
| 1821 | + if (cert_ == nullptr || pkey == nullptr) { |
| 1822 | + return CheckKeyResult::OPERATION_FAILED; |
| 1823 | + } |
| 1824 | + |
| 1825 | + EVPKeyPointer certificate_key(X509_get_pubkey(const_cast<X509*>(cert_))); |
| 1826 | + if (!certificate_key) return CheckKeyResult::OPERATION_FAILED; |
| 1827 | +#if NCRYPTO_USE_BORINGSSL |
| 1828 | + if (EVP_PKEY_is_opaque(certificate_key.get()) || |
| 1829 | + EVP_PKEY_is_opaque(pkey.get())) { |
| 1830 | + return CheckKeyResult::UNSUPPORTED_OPERATION; |
| 1831 | + } |
| 1832 | +#endif |
| 1833 | +#if NCRYPTO_USE_OPENSSL_PROVIDER |
| 1834 | + const int result = EVP_PKEY_eq(certificate_key.get(), pkey.get()); |
| 1835 | +#elif NCRYPTO_USE_BORINGSSL |
| 1836 | + const int result = EVP_PKEY_cmp(certificate_key.get(), pkey.get()); |
| 1837 | +#else |
| 1838 | +#error "Unsupported crypto backend" |
| 1839 | +#endif |
| 1840 | + switch (result) { |
| 1841 | + case 1: |
| 1842 | + return CheckKeyResult::MATCH; |
| 1843 | + case 0: |
| 1844 | + case -1: |
| 1845 | + return CheckKeyResult::NO_MATCH; |
| 1846 | + case -2: |
| 1847 | + return CheckKeyResult::UNSUPPORTED_OPERATION; |
| 1848 | + default: |
| 1849 | + return CheckKeyResult::OPERATION_FAILED; |
| 1850 | + } |
1810 | 1851 | } |
1811 | 1852 |
|
1812 | 1853 | bool X509View::checkPublicKey(const EVPKeyPointer& pkey) const { |
|