Gtk-WARNING gtktreestore.c:1047: Invalid column number 1 added to iter
[LibreOffice.git] / xmlsecurity / source / xmlsec / nss / ciphercontext.cxx
blob19e4e3ab991f96c1b55de71a413ec9f2151e5088
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <com/sun/star/lang/DisposedException.hpp>
23 #include <rtl/random.h>
24 #include <rtl/ref.hxx>
25 #include <sal/log.hxx>
27 #include "ciphercontext.hxx"
28 #include <nss.h> // for NSS_VMINOR
29 #include <pk11pub.h>
31 constexpr size_t nAESGCMIVSize = 12;
32 constexpr size_t nAESGCMTagSize = 16;
34 using namespace ::com::sun::star;
36 uno::Reference< xml::crypto::XCipherContext > OCipherContext::Create( CK_MECHANISM_TYPE nNSSCipherID, const uno::Sequence< ::sal_Int8 >& aKey, const uno::Sequence< ::sal_Int8 >& aInitializationVector, bool bEncryption, bool bW3CPadding )
38 ::rtl::Reference< OCipherContext > xResult = new OCipherContext;
40 xResult->m_pSlot = PK11_GetBestSlot( nNSSCipherID, nullptr );
41 if (!xResult->m_pSlot)
43 SAL_WARN("xmlsecurity.nss", "PK11_GetBestSlot failed");
44 throw uno::RuntimeException(u"PK11_GetBestSlot failed"_ustr);
47 SECItem aKeyItem = { siBuffer,
48 const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(aKey.getConstArray())),
49 sal::static_int_cast<unsigned>(aKey.getLength()) };
50 xResult->m_pSymKey = PK11_ImportSymKey(xResult->m_pSlot, nNSSCipherID,
51 PK11_OriginDerive, bEncryption ? CKA_ENCRYPT : CKA_DECRYPT, &aKeyItem, nullptr);
52 if (!xResult->m_pSymKey)
54 SAL_WARN("xmlsecurity.nss", "PK11_ImportSymKey failed");
55 throw uno::RuntimeException(u"PK11_ImportSymKey failed"_ustr);
58 if (nNSSCipherID == CKM_AES_GCM)
60 // TODO: when runtime requirements are raised to NSS 3.52,
61 // cleanup according to
62 // https://fedoraproject.org/wiki/Changes/NssGCMParams
63 #if NSS_VMINOR >= 52
64 xResult->m_pSecParam = SECITEM_AllocItem(nullptr, nullptr, sizeof(CK_NSS_GCM_PARAMS));
65 #else
66 xResult->m_pSecParam = SECITEM_AllocItem(nullptr, nullptr, sizeof(CK_GCM_PARAMS));
67 #endif
68 if (!xResult->m_pSecParam)
70 SAL_WARN("xmlsecurity.nss", "SECITEM_AllocItem failed");
71 throw uno::RuntimeException(u"SECITEM_AllocItem failed"_ustr);
73 assert(aInitializationVector.getLength() == nAESGCMIVSize);
74 xResult->m_AESGCMIV = aInitializationVector;
75 #if NSS_VMINOR >= 52
76 auto *const pParams = reinterpret_cast<CK_NSS_GCM_PARAMS*>(xResult->m_pSecParam->data);
77 #else
78 auto *const pParams = reinterpret_cast<CK_GCM_PARAMS*>(xResult->m_pSecParam->data);
79 #endif
80 pParams->pIv = const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(xResult->m_AESGCMIV.getConstArray()));
81 pParams->ulIvLen = sal::static_int_cast<unsigned>(xResult->m_AESGCMIV.getLength());
82 pParams->pAAD = nullptr;
83 pParams->ulAADLen = 0;
84 pParams->ulTagBits = nAESGCMTagSize * 8;
86 else
88 SECItem aIVItem = { siBuffer,
89 const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(aInitializationVector.getConstArray())),
90 sal::static_int_cast<unsigned>(aInitializationVector.getLength()) };
91 xResult->m_pSecParam = PK11_ParamFromIV(nNSSCipherID, &aIVItem);
92 if (!xResult->m_pSecParam)
94 SAL_WARN("xmlsecurity.nss", "PK11_ParamFromIV failed");
95 throw uno::RuntimeException(u"PK11_ParamFromIV failed"_ustr);
98 xResult->m_pContext = PK11_CreateContextBySymKey( nNSSCipherID, bEncryption ? CKA_ENCRYPT : CKA_DECRYPT, xResult->m_pSymKey, xResult->m_pSecParam);
99 if (!xResult->m_pContext)
101 SAL_WARN("xmlsecurity.nss", "PK11_CreateContextBySymKey failed");
102 throw uno::RuntimeException(u"PK11_CreateContextBySymKey failed"_ustr);
106 xResult->m_bEncryption = bEncryption;
107 xResult->m_bW3CPadding = bW3CPadding;
108 xResult->m_bPadding = bW3CPadding || ( PK11_GetPadMechanism( nNSSCipherID ) == nNSSCipherID );
109 // in NSS 3.94, a global default value of 8 is returned for CKM_AES_GCM
110 xResult->m_nBlockSize = nNSSCipherID == CKM_AES_GCM ? 16 : PK11_GetBlockSize(nNSSCipherID, xResult->m_pSecParam);
111 if (SAL_MAX_INT8 < xResult->m_nBlockSize)
113 SAL_WARN("xmlsecurity.nss", "PK11_GetBlockSize unexpected result");
114 throw uno::RuntimeException(u"PK11_GetBlockSize unexpected result"_ustr);
116 return xResult;
119 void OCipherContext::Dispose()
121 if ( m_pContext )
123 PK11_DestroyContext( m_pContext, PR_TRUE );
124 m_pContext = nullptr;
127 if ( m_pSecParam )
129 SECITEM_FreeItem( m_pSecParam, PR_TRUE );
130 m_pSecParam = nullptr;
133 if ( m_pSymKey )
135 PK11_FreeSymKey( m_pSymKey );
136 m_pSymKey = nullptr;
139 if ( m_pSlot )
141 PK11_FreeSlot( m_pSlot );
142 m_pSlot = nullptr;
145 m_bDisposed = true;
148 uno::Sequence< ::sal_Int8 > SAL_CALL OCipherContext::convertWithCipherContext( const uno::Sequence< ::sal_Int8 >& aData )
150 std::unique_lock aGuard( m_aMutex );
152 if ( m_bBroken )
153 throw uno::RuntimeException();
155 if ( m_bDisposed )
156 throw lang::DisposedException();
158 if (m_AESGCMIV.getLength())
160 if (SAL_MAX_INT32 - nAESGCMIVSize - nAESGCMTagSize <= static_cast<size_t>(m_aLastBlock.getLength()) + static_cast<size_t>(aData.getLength()))
162 m_bBroken = true;
163 throw uno::RuntimeException(u"overflow"_ustr);
165 m_aLastBlock.realloc(m_aLastBlock.getLength() + aData.getLength());
166 memcpy(m_aLastBlock.getArray() + m_aLastBlock.getLength() - aData.getLength(), aData.getConstArray(), aData.getLength());
167 return {};
170 uno::Sequence< sal_Int8 > aToConvert;
171 if ( aData.hasElements() )
173 sal_Int32 nOldLastBlockLen = m_aLastBlock.getLength();
175 sal_Int32 nAvailableData = nOldLastBlockLen + aData.getLength();
176 sal_Int32 nToConvertLen;
177 if ( m_bEncryption || !m_bW3CPadding )
179 assert(nOldLastBlockLen < m_nBlockSize);
180 if ( nAvailableData % m_nBlockSize == 0 )
181 nToConvertLen = nAvailableData;
182 else if ( nAvailableData < m_nBlockSize )
183 nToConvertLen = 0;
184 else
185 nToConvertLen = nAvailableData - nAvailableData % m_nBlockSize;
187 else
189 assert(nOldLastBlockLen < m_nBlockSize * 2);
190 // decryption with W3C padding needs at least one block for finalizing
191 if ( nAvailableData < m_nBlockSize * 2 )
192 nToConvertLen = 0;
193 else
194 nToConvertLen = nAvailableData - nAvailableData % m_nBlockSize - m_nBlockSize;
197 aToConvert.realloc( nToConvertLen );
198 if ( nToConvertLen == 0 )
200 m_aLastBlock.realloc( nOldLastBlockLen + aData.getLength() );
201 memcpy( m_aLastBlock.getArray() + nOldLastBlockLen, aData.getConstArray(), aData.getLength() );
202 // aToConvert stays empty
204 else if ( nToConvertLen < nOldLastBlockLen )
206 memcpy( aToConvert.getArray(), m_aLastBlock.getConstArray(), nToConvertLen );
207 memcpy( m_aLastBlock.getArray(), m_aLastBlock.getConstArray() + nToConvertLen, nOldLastBlockLen - nToConvertLen );
208 m_aLastBlock.realloc( nOldLastBlockLen - nToConvertLen + aData.getLength() );
209 memcpy( m_aLastBlock.getArray() + nOldLastBlockLen - nToConvertLen, aData.getConstArray(), aData.getLength() );
211 else
213 memcpy( aToConvert.getArray(), m_aLastBlock.getConstArray(), nOldLastBlockLen );
214 if ( nToConvertLen > nOldLastBlockLen )
215 memcpy( aToConvert.getArray() + nOldLastBlockLen, aData.getConstArray(), nToConvertLen - nOldLastBlockLen );
216 m_aLastBlock.realloc( nAvailableData - nToConvertLen );
217 memcpy( m_aLastBlock.getArray(), aData.getConstArray() + nToConvertLen - nOldLastBlockLen, nAvailableData - nToConvertLen );
221 uno::Sequence< sal_Int8 > aResult;
222 assert(aToConvert.getLength() % m_nBlockSize == 0);
223 if ( aToConvert.hasElements() )
225 int nResultLen = 0;
226 aResult.realloc( aToConvert.getLength() + m_nBlockSize );
227 if ( PK11_CipherOp( m_pContext, reinterpret_cast< unsigned char* >( aResult.getArray() ), &nResultLen, aResult.getLength(), reinterpret_cast< const unsigned char* >( aToConvert.getConstArray() ), aToConvert.getLength() ) != SECSuccess )
229 m_bBroken = true;
230 Dispose();
231 throw uno::RuntimeException(u"PK11_CipherOp failed"_ustr);
234 m_nConverted += aToConvert.getLength();
235 aResult.realloc( nResultLen );
238 return aResult;
241 uno::Sequence< ::sal_Int8 > SAL_CALL OCipherContext::finalizeCipherContextAndDispose()
243 std::unique_lock aGuard( m_aMutex );
245 if ( m_bBroken )
246 throw uno::RuntimeException();
248 if ( m_bDisposed )
249 throw lang::DisposedException();
251 if (m_AESGCMIV.getLength())
253 uno::Sequence<sal_Int8> aResult;
254 unsigned outLen;
255 if (m_bEncryption)
257 assert(sal::static_int_cast<size_t>(m_aLastBlock.getLength()) <= SAL_MAX_INT32 - nAESGCMIVSize - nAESGCMTagSize);
258 // add space for IV and tag
259 aResult.realloc(m_aLastBlock.getLength() + nAESGCMIVSize + nAESGCMTagSize);
260 // W3C xmlenc-core1 requires the IV preceding the ciphertext,
261 // but NSS doesn't do it, so copy it manually
262 memcpy(aResult.getArray(), m_AESGCMIV.getConstArray(), nAESGCMIVSize);
263 if (PK11_Encrypt(m_pSymKey, CKM_AES_GCM, m_pSecParam,
264 reinterpret_cast<unsigned char*>(aResult.getArray() + nAESGCMIVSize),
265 &outLen, aResult.getLength() - nAESGCMIVSize,
266 reinterpret_cast<unsigned char const*>(m_aLastBlock.getConstArray()),
267 m_aLastBlock.getLength()) != SECSuccess)
269 m_bBroken = true;
270 Dispose();
271 throw uno::RuntimeException(u"PK11_Encrypt failed"_ustr);
273 assert(outLen == sal::static_int_cast<unsigned>(aResult.getLength() - nAESGCMIVSize));
275 else if (nAESGCMIVSize + nAESGCMTagSize < sal::static_int_cast<size_t>(m_aLastBlock.getLength()))
277 if (0 != memcmp(m_AESGCMIV.getConstArray(), m_aLastBlock.getConstArray(), nAESGCMIVSize))
279 m_bBroken = true;
280 Dispose();
281 throw uno::RuntimeException(u"inconsistent IV"_ustr);
283 aResult.realloc(m_aLastBlock.getLength() - nAESGCMIVSize - nAESGCMTagSize);
284 if (PK11_Decrypt(m_pSymKey, CKM_AES_GCM, m_pSecParam,
285 reinterpret_cast<unsigned char*>(aResult.getArray()),
286 &outLen, aResult.getLength(),
287 reinterpret_cast<unsigned char const*>(m_aLastBlock.getConstArray() + nAESGCMIVSize),
288 m_aLastBlock.getLength() - nAESGCMIVSize) != SECSuccess)
290 m_bBroken = true;
291 Dispose();
292 throw uno::RuntimeException(u"PK11_Decrypt failed"_ustr);
294 assert(outLen == sal::static_int_cast<unsigned>(aResult.getLength()));
296 else
298 m_bBroken = true;
299 Dispose();
300 throw uno::RuntimeException(u"incorrect size of input"_ustr);
302 Dispose();
303 return aResult;
306 assert(m_nBlockSize <= SAL_MAX_INT8);
307 assert(m_nConverted % m_nBlockSize == 0); // whole blocks are converted
308 sal_Int32 nSizeForPadding = ( m_nConverted + m_aLastBlock.getLength() ) % m_nBlockSize;
310 // if it is decryption, the amount of data should be rounded to the block size even in case of padding
311 if ( ( !m_bPadding || !m_bEncryption ) && nSizeForPadding )
312 throw uno::RuntimeException(u"The data should contain complete blocks only."_ustr );
314 if ( m_bW3CPadding && m_bEncryption )
316 // in this case the last block should be smaller than standard block
317 // it will be increased with the padding
318 assert(m_aLastBlock.getLength() < m_nBlockSize);
320 // W3CPadding handling for encryption
321 sal_Int32 nPaddingSize = m_nBlockSize - nSizeForPadding;
322 sal_Int32 nOldLastBlockLen = m_aLastBlock.getLength();
323 m_aLastBlock.realloc( nOldLastBlockLen + nPaddingSize );
324 auto pLastBlock = m_aLastBlock.getArray();
326 if ( nPaddingSize > 1 )
328 if (rtl_random_getBytes(nullptr, pLastBlock + nOldLastBlockLen, nPaddingSize - 1) != rtl_Random_E_None)
330 throw uno::RuntimeException(u"rtl_random_getBytes failed"_ustr);
333 pLastBlock[m_aLastBlock.getLength() - 1] = static_cast< sal_Int8 >( nPaddingSize );
336 // finally should the last block be smaller than two standard blocks
337 assert(m_aLastBlock.getLength() < m_nBlockSize * 2);
339 uno::Sequence< sal_Int8 > aResult;
340 if ( m_aLastBlock.hasElements() )
342 int nPrefResLen = 0;
343 aResult.realloc( m_aLastBlock.getLength() + m_nBlockSize );
344 if ( PK11_CipherOp( m_pContext, reinterpret_cast< unsigned char* >( aResult.getArray() ), &nPrefResLen, aResult.getLength(), reinterpret_cast< const unsigned char* >( m_aLastBlock.getConstArray() ), m_aLastBlock.getLength() ) != SECSuccess )
346 m_bBroken = true;
347 Dispose();
348 throw uno::RuntimeException(u"PK11_CipherOp failed"_ustr);
351 aResult.realloc( nPrefResLen );
352 m_aLastBlock.realloc( 0 );
355 sal_Int32 nPrefixLen = aResult.getLength();
356 aResult.realloc( nPrefixLen + m_nBlockSize * 2 );
357 unsigned nFinalLen = 0;
358 if ( PK11_DigestFinal( m_pContext, reinterpret_cast< unsigned char* >( aResult.getArray() + nPrefixLen ), &nFinalLen, aResult.getLength() - nPrefixLen ) != SECSuccess )
360 m_bBroken = true;
361 Dispose();
362 throw uno::RuntimeException(u"PK11_DigestFinal failed"_ustr);
365 aResult.realloc( nPrefixLen + nFinalLen );
367 if ( m_bW3CPadding && !m_bEncryption )
369 // W3CPadding handling for decryption
370 // aResult should have enough data, except if the input was completely empty
372 // see https://www.w3.org/TR/xmlenc-core1/#sec-Alg-Block
373 if (aResult.getLength() < m_nBlockSize
374 || aResult[aResult.getLength()-1] <= 0
375 || m_nBlockSize < aResult[aResult.getLength()-1])
377 m_bBroken = true;
378 Dispose();
379 throw uno::RuntimeException(u"incorrect size of padding"_ustr);
382 aResult.realloc(aResult.getLength() - aResult[aResult.getLength()-1]);
385 Dispose();
387 return aResult;
390 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */