Line data Source code
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/. */
4 :
5 : #include "nsRandomGenerator.h"
6 :
7 : #include "ScopedNSSTypes.h"
8 : #include "nsNSSComponent.h"
9 : #include "pk11pub.h"
10 : #include "prerror.h"
11 : #include "secerr.h"
12 :
13 0 : NS_IMPL_ISUPPORTS(nsRandomGenerator, nsIRandomGenerator)
14 :
15 : NS_IMETHODIMP
16 0 : nsRandomGenerator::GenerateRandomBytes(uint32_t aLength,
17 : uint8_t** aBuffer)
18 : {
19 0 : NS_ENSURE_ARG_POINTER(aBuffer);
20 0 : *aBuffer = nullptr;
21 :
22 0 : nsNSSShutDownPreventionLock locker;
23 0 : if (isAlreadyShutDown()) {
24 0 : return NS_ERROR_NOT_AVAILABLE;
25 : }
26 :
27 0 : mozilla::UniquePK11SlotInfo slot(PK11_GetInternalSlot());
28 0 : if (!slot) {
29 0 : return NS_ERROR_FAILURE;
30 : }
31 :
32 0 : auto buf = static_cast<uint8_t*>(moz_xmalloc(aLength));
33 0 : if (!buf) {
34 0 : return NS_ERROR_OUT_OF_MEMORY;
35 : }
36 :
37 0 : SECStatus srv = PK11_GenerateRandomOnSlot(slot.get(), buf, aLength);
38 0 : if (srv != SECSuccess) {
39 0 : free(buf);
40 0 : return NS_ERROR_FAILURE;
41 : }
42 :
43 0 : *aBuffer = buf;
44 :
45 0 : return NS_OK;
46 : }
47 :
48 0 : nsRandomGenerator::~nsRandomGenerator()
49 : {
50 0 : nsNSSShutDownPreventionLock locker;
51 0 : if (isAlreadyShutDown()) {
52 0 : return;
53 : }
54 0 : shutdown(ShutdownCalledFrom::Object);
55 0 : }
|