Wed, 31 Dec 2014 07:16:47 +0100
Revert simplistic fix pending revisit of Mozilla integration attempt.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "nsRandomGenerator.h"
6 #include "pk11pub.h"
7 #include "secerr.h"
8 #include "prerror.h"
9 #include "nsNSSComponent.h"
11 ////////////////////////////////////////////////////////////////////////////////
12 //// nsRandomGenerator
14 NS_IMPL_ISUPPORTS(nsRandomGenerator, nsIRandomGenerator)
16 ////////////////////////////////////////////////////////////////////////////////
17 //// nsIRandomGenerator
19 /* void generateRandomBytes(in unsigned long aLength,
20 [retval, array, size_is(aLength)] out octet aBuffer) */
21 NS_IMETHODIMP
22 nsRandomGenerator::GenerateRandomBytes(uint32_t aLength,
23 uint8_t **aBuffer)
24 {
25 NS_ENSURE_ARG_POINTER(aBuffer);
26 *aBuffer = nullptr;
28 mozilla::ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
29 if (!slot) {
30 return NS_ERROR_FAILURE;
31 }
33 uint8_t *buf = reinterpret_cast<uint8_t *>(NS_Alloc(aLength));
34 if (!buf) {
35 return NS_ERROR_OUT_OF_MEMORY;
36 }
38 SECStatus srv = PK11_GenerateRandomOnSlot(slot, buf, aLength);
40 if (SECSuccess != srv) {
41 NS_Free(buf);
42 return NS_ERROR_FAILURE;
43 }
45 *aBuffer = buf;
47 return NS_OK;
48 }