security/manager/ssl/src/nsRandomGenerator.cpp

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:5f43973f853b
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 #include "pk11pub.h"
7 #include "secerr.h"
8 #include "prerror.h"
9 #include "nsNSSComponent.h"
10
11 ////////////////////////////////////////////////////////////////////////////////
12 //// nsRandomGenerator
13
14 NS_IMPL_ISUPPORTS(nsRandomGenerator, nsIRandomGenerator)
15
16 ////////////////////////////////////////////////////////////////////////////////
17 //// nsIRandomGenerator
18
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;
27
28 mozilla::ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
29 if (!slot) {
30 return NS_ERROR_FAILURE;
31 }
32
33 uint8_t *buf = reinterpret_cast<uint8_t *>(NS_Alloc(aLength));
34 if (!buf) {
35 return NS_ERROR_OUT_OF_MEMORY;
36 }
37
38 SECStatus srv = PK11_GenerateRandomOnSlot(slot, buf, aLength);
39
40 if (SECSuccess != srv) {
41 NS_Free(buf);
42 return NS_ERROR_FAILURE;
43 }
44
45 *aBuffer = buf;
46
47 return NS_OK;
48 }

mercurial