summaryrefslogtreecommitdiff
path: root/xmlsecurity
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2018-10-24 10:27:26 +0200
committerMiklos Vajna <vmiklos@collabora.co.uk>2018-11-08 15:08:10 +0100
commit025d881e56cdd2bf085a4df8f9f0183817df768f (patch)
treef5c5fb7085f344d949519d30654b6bb1febb5eb2 /xmlsecurity
parent3e57de91fe6af9084809af951f31470e3e987e90 (diff)
fix importing the certificate and private key
If importing the certificate and private key is done separately, they don't associate with each other, so with this you can add the private key to the certificate that it belongs to. If the private key is set in this way, then getPrivateKey() call doesn't look into the database but just returns the private key stored in the member variable. Additionally use CERT_DecodeCertFromPackage to import the DER certificate from the imput as CERT_DecodeDERCertificate doesn't import a complete certificate and doesn't add it into the certificate database. Change-Id: I29876030f167cc5fa6b887f9bfeb0b84622c751e Reviewed-on: https://gerrit.libreoffice.org/62271 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <quikee@gmail.com> (cherry picked from commit 2286137c40a2abb26d36beb906962baffd779312)
Diffstat (limited to 'xmlsecurity')
-rw-r--r--xmlsecurity/source/xmlsec/nss/securityenvironment_nssimpl.cxx83
-rw-r--r--xmlsecurity/source/xmlsec/nss/securityenvironment_nssimpl.hxx8
-rw-r--r--xmlsecurity/source/xmlsec/nss/x509certificate_nssimpl.cxx26
-rw-r--r--xmlsecurity/source/xmlsec/nss/x509certificate_nssimpl.hxx6
4 files changed, 92 insertions, 31 deletions
diff --git a/xmlsecurity/source/xmlsec/nss/securityenvironment_nssimpl.cxx b/xmlsecurity/source/xmlsec/nss/securityenvironment_nssimpl.cxx
index 035896932ed3..a6905b96079f 100644
--- a/xmlsecurity/source/xmlsec/nss/securityenvironment_nssimpl.cxx
+++ b/xmlsecurity/source/xmlsec/nss/securityenvironment_nssimpl.cxx
@@ -527,6 +527,30 @@ Sequence< Reference < XCertificate > > SecurityEnvironment_NssImpl::buildCertifi
return Sequence< Reference < XCertificate > >();
}
+X509Certificate_NssImpl* SecurityEnvironment_NssImpl::createAndAddCertificateFromPackage(
+ const css::uno::Sequence<sal_Int8>& raDERCertificate,
+ OUString const & raString)
+{
+ auto pCertificateBytes = reinterpret_cast<char *>(const_cast<sal_Int8 *>(raDERCertificate.getConstArray()));
+ CERTCertificate* pCERTCertificate = CERT_DecodeCertFromPackage(pCertificateBytes, raDERCertificate.getLength());
+
+ if (!pCERTCertificate)
+ return nullptr;
+
+ OString aTrustString = OUStringToOString(raString, RTL_TEXTENCODING_ASCII_US);
+
+ CERTCertTrust aTrust;
+ if (CERT_DecodeTrustString(&aTrust, aTrustString.getStr()) != SECSuccess)
+ return nullptr;
+
+ if (CERT_ChangeCertTrust(CERT_GetDefaultCertDB(), pCERTCertificate, &aTrust) != SECSuccess)
+ return nullptr;
+
+ X509Certificate_NssImpl* pX509Certificate = new X509Certificate_NssImpl();
+ pX509Certificate->setCert(pCERTCertificate);
+ return pX509Certificate;
+}
+
X509Certificate_NssImpl* SecurityEnvironment_NssImpl::createX509CertificateFromDER(const css::uno::Sequence<sal_Int8>& aDerCertificate)
{
X509Certificate_NssImpl* pX509Certificate = nullptr;
@@ -945,68 +969,71 @@ xmlSecKeysMngrPtr SecurityEnvironment_NssImpl::createKeysManager() {
// Adopt the private key of the signing certificate, if it has any.
if (auto pCertificate = dynamic_cast<X509Certificate_NssImpl*>(m_xSigningCertificate.get()))
{
- if (auto pCERTCertificate = const_cast<CERTCertificate*>(pCertificate->getNssCert()))
+ SECKEYPrivateKey* pPrivateKey = pCertificate->getPrivateKey();
+ if (pPrivateKey)
{
- if (pCERTCertificate && pCERTCertificate->slot)
- {
- SECKEYPrivateKey* pPrivateKey = PK11_FindPrivateKeyFromCert(pCERTCertificate->slot, pCERTCertificate, nullptr);
- xmlSecKeyDataPtr pKeyData = xmlSecNssPKIAdoptKey(pPrivateKey, nullptr);
- xmlSecKeyPtr pKey = xmlSecKeyCreate();
- xmlSecKeySetValue(pKey, pKeyData);
- xmlSecNssAppDefaultKeysMngrAdoptKey(pKeysMngr, pKey);
- }
- else
- {
- SAL_WARN("xmlsecurity.xmlsec", "Can't get the private key from the certificate.");
- }
+ xmlSecKeyDataPtr pKeyData = xmlSecNssPKIAdoptKey(pPrivateKey, nullptr);
+ xmlSecKeyPtr pKey = xmlSecKeyCreate();
+ xmlSecKeySetValue(pKey, pKeyData);
+ xmlSecNssAppDefaultKeysMngrAdoptKey(pKeysMngr, pKey);
+ }
+ else
+ {
+ SAL_WARN("xmlsecurity.xmlsec", "Can't get the private key from the certificate.");
}
}
- return pKeysMngr ;
+ return pKeysMngr;
}
+
void SecurityEnvironment_NssImpl::destroyKeysManager(xmlSecKeysMngrPtr pKeysMngr) {
if( pKeysMngr != nullptr ) {
xmlSecKeysMngrDestroy( pKeysMngr ) ;
}
}
-uno::Reference<security::XCertificate> SecurityEnvironment_NssImpl::createDERCertificateWithPrivateKey(
- Sequence<sal_Int8> const & raDERCertificate, Sequence<sal_Int8> const & raPrivateKey)
+SECKEYPrivateKey* SecurityEnvironment_NssImpl::insertPrivateKey(css::uno::Sequence<sal_Int8> const & raPrivateKey)
{
- SECStatus nStatus = SECSuccess;
-
PK11SlotInfo* pSlot = PK11_GetInternalKeySlot();
+
if (!pSlot)
- return uno::Reference<security::XCertificate>();
+ return nullptr;
SECItem pDerPrivateKeyInfo;
pDerPrivateKeyInfo.data = reinterpret_cast<unsigned char *>(const_cast<sal_Int8 *>(raPrivateKey.getConstArray()));
pDerPrivateKeyInfo.len = raPrivateKey.getLength();
- const unsigned int keyUsage = KU_KEY_ENCIPHERMENT | KU_DATA_ENCIPHERMENT | KU_DIGITAL_SIGNATURE;
+ const unsigned int aKeyUsage = KU_KEY_ENCIPHERMENT | KU_DATA_ENCIPHERMENT | KU_DIGITAL_SIGNATURE;
SECKEYPrivateKey* pPrivateKey = nullptr;
bool bPermanent = false;
bool bSensitive = false;
- nStatus = PK11_ImportDERPrivateKeyInfoAndReturnKey(
+ SECStatus nStatus = PK11_ImportDERPrivateKeyInfoAndReturnKey(
pSlot, &pDerPrivateKeyInfo, nullptr, nullptr, bPermanent, bSensitive,
- keyUsage, &pPrivateKey, nullptr);
+ aKeyUsage, &pPrivateKey, nullptr);
if (nStatus != SECSuccess)
- return uno::Reference<security::XCertificate>();
+ return nullptr;
+
+ return pPrivateKey;
+}
+
+uno::Reference<security::XCertificate> SecurityEnvironment_NssImpl::createDERCertificateWithPrivateKey(
+ Sequence<sal_Int8> const & raDERCertificate, Sequence<sal_Int8> const & raPrivateKey)
+{
+
+ SECKEYPrivateKey* pPrivateKey = insertPrivateKey(raPrivateKey);
if (!pPrivateKey)
return uno::Reference<security::XCertificate>();
- X509Certificate_NssImpl* pX509Certificate = createX509CertificateFromDER(raDERCertificate);
+ X509Certificate_NssImpl* pX509Certificate = createAndAddCertificateFromPackage(raDERCertificate, "TCu,Cu,Tu");
+
if (!pX509Certificate)
return uno::Reference<security::XCertificate>();
- addCryptoSlot(pSlot);
-
- CERTCertificate* pCERTCertificate = const_cast<CERTCertificate*>(pX509Certificate->getNssCert());
- pCERTCertificate->slot = pSlot;
+ pX509Certificate->setCustomPrivateKey(pPrivateKey);
return pX509Certificate;
}
diff --git a/xmlsecurity/source/xmlsec/nss/securityenvironment_nssimpl.hxx b/xmlsecurity/source/xmlsec/nss/securityenvironment_nssimpl.hxx
index becde1168661..94dad6235767 100644
--- a/xmlsecurity/source/xmlsec/nss/securityenvironment_nssimpl.hxx
+++ b/xmlsecurity/source/xmlsec/nss/securityenvironment_nssimpl.hxx
@@ -154,9 +154,15 @@ private:
static void destroyKeysManager(xmlSecKeysMngrPtr pKeysMngr) ;
private:
+
void updateSlots();
- X509Certificate_NssImpl* createX509CertificateFromDER(const css::uno::Sequence<sal_Int8>& aDerCertificate);
+ X509Certificate_NssImpl* createAndAddCertificateFromPackage(
+ const css::uno::Sequence<sal_Int8>& raDerCertificate,
+ OUString const & raString);
+ SECKEYPrivateKey* insertPrivateKey(css::uno::Sequence<sal_Int8> const & raPrivateKey);
+
+ X509Certificate_NssImpl* createX509CertificateFromDER(const css::uno::Sequence<sal_Int8>& raDerCertificate);
/// @throws css::uno::Exception
/// @throws css::uno::RuntimeException
diff --git a/xmlsecurity/source/xmlsec/nss/x509certificate_nssimpl.cxx b/xmlsecurity/source/xmlsec/nss/x509certificate_nssimpl.cxx
index 941f8364f237..1bda73af613b 100644
--- a/xmlsecurity/source/xmlsec/nss/x509certificate_nssimpl.cxx
+++ b/xmlsecurity/source/xmlsec/nss/x509certificate_nssimpl.cxx
@@ -45,7 +45,8 @@ using ::com::sun::star::security::XCertificate ;
using ::com::sun::star::util::DateTime ;
X509Certificate_NssImpl::X509Certificate_NssImpl() :
- m_pCert( nullptr )
+ m_pCert(nullptr),
+ m_pPrivateKey(nullptr)
{
}
@@ -330,6 +331,29 @@ void X509Certificate_NssImpl::setRawCert( const Sequence< sal_Int8 >& rawCert )
m_pCert = cert ;
}
+void X509Certificate_NssImpl::setCustomPrivateKey(SECKEYPrivateKey* pPrivateKey)
+{
+ m_pPrivateKey = pPrivateKey;
+}
+
+SECKEYPrivateKey* X509Certificate_NssImpl::getPrivateKey()
+{
+ if (m_pPrivateKey)
+ {
+ return m_pPrivateKey;
+ }
+ else
+ {
+ if (m_pCert && m_pCert->slot)
+ {
+ SECKEYPrivateKey* pPrivateKey = PK11_FindPrivateKeyFromCert(m_pCert->slot, m_pCert, nullptr);
+ if (pPrivateKey)
+ return pPrivateKey;
+ }
+ }
+ return nullptr;
+}
+
/* XUnoTunnel */
sal_Int64 SAL_CALL X509Certificate_NssImpl::getSomething( const Sequence< sal_Int8 >& aIdentifier ) {
if( aIdentifier.getLength() == 16 && 0 == memcmp( getUnoTunnelId().getConstArray(), aIdentifier.getConstArray(), 16 ) ) {
diff --git a/xmlsecurity/source/xmlsec/nss/x509certificate_nssimpl.hxx b/xmlsecurity/source/xmlsec/nss/x509certificate_nssimpl.hxx
index 5c5794342c62..d537ff7942bb 100644
--- a/xmlsecurity/source/xmlsec/nss/x509certificate_nssimpl.hxx
+++ b/xmlsecurity/source/xmlsec/nss/x509certificate_nssimpl.hxx
@@ -40,7 +40,8 @@ class X509Certificate_NssImpl : public ::cppu::WeakImplHelper<
css::lang::XServiceInfo > , public xmlsecurity::Certificate
{
private:
- CERTCertificate* m_pCert ;
+ CERTCertificate* m_pCert;
+ SECKEYPrivateKey* m_pPrivateKey;
public:
X509Certificate_NssImpl() ;
@@ -90,8 +91,11 @@ class X509Certificate_NssImpl : public ::cppu::WeakImplHelper<
//Helper methods
void setCert( CERTCertificate* cert ) ;
const CERTCertificate* getNssCert() const ;
+
/// @throws css::uno::RuntimeException
void setRawCert( const css::uno::Sequence< sal_Int8 >& rawCert ) ;
+ void setCustomPrivateKey(SECKEYPrivateKey* pPrivateKey);
+ SECKEYPrivateKey* getPrivateKey();
// XServiceInfo
virtual OUString SAL_CALL getImplementationName() override;