security/manager/ssl/src/nsRandomGenerator.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/manager/ssl/src/nsRandomGenerator.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,48 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "nsRandomGenerator.h"
     1.9 +#include "pk11pub.h"
    1.10 +#include "secerr.h"
    1.11 +#include "prerror.h"
    1.12 +#include "nsNSSComponent.h"
    1.13 +
    1.14 +////////////////////////////////////////////////////////////////////////////////
    1.15 +//// nsRandomGenerator
    1.16 +
    1.17 +NS_IMPL_ISUPPORTS(nsRandomGenerator, nsIRandomGenerator)
    1.18 +
    1.19 +////////////////////////////////////////////////////////////////////////////////
    1.20 +//// nsIRandomGenerator
    1.21 +
    1.22 +/* void generateRandomBytes(in unsigned long aLength,
    1.23 +                            [retval, array, size_is(aLength)] out octet aBuffer) */
    1.24 +NS_IMETHODIMP
    1.25 +nsRandomGenerator::GenerateRandomBytes(uint32_t aLength,
    1.26 +                                       uint8_t **aBuffer)
    1.27 +{
    1.28 +  NS_ENSURE_ARG_POINTER(aBuffer);
    1.29 +  *aBuffer = nullptr;
    1.30 +
    1.31 +  mozilla::ScopedPK11SlotInfo slot(PK11_GetInternalSlot());
    1.32 +  if (!slot) {
    1.33 +    return NS_ERROR_FAILURE;
    1.34 +  }
    1.35 +
    1.36 +  uint8_t *buf = reinterpret_cast<uint8_t *>(NS_Alloc(aLength));
    1.37 +  if (!buf) {
    1.38 +    return NS_ERROR_OUT_OF_MEMORY;
    1.39 +  }
    1.40 +
    1.41 +  SECStatus srv = PK11_GenerateRandomOnSlot(slot, buf, aLength);
    1.42 +
    1.43 +  if (SECSuccess != srv) {
    1.44 +    NS_Free(buf);
    1.45 +    return NS_ERROR_FAILURE;
    1.46 +  }
    1.47 +
    1.48 +  *aBuffer = buf;
    1.49 +
    1.50 +  return NS_OK;
    1.51 +}

mercurial