<commit_before>// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "login_manager/nss_util.h"

#include <base/basictypes.h>
#include <base/file_path.h>
#include <base/file_util.h>
#include <base/logging.h>
#include <base/memory/scoped_ptr.h>
#include <crypto/nss_util.h>
#include <crypto/rsa_private_key.h>
#include <crypto/signature_creator.h>
#include <crypto/signature_verifier.h>
#include <cros/chromeos_login.h>

using crypto::RSAPrivateKey;

namespace login_manager {
///////////////////////////////////////////////////////////////////////////
// NssUtil

// static
NssUtil::Factory* NssUtil::factory_ = NULL;

NssUtil::NssUtil() {}

NssUtil::~NssUtil() {}

///////////////////////////////////////////////////////////////////////////
// NssUtilImpl

class NssUtilImpl : public NssUtil {
 public:
  NssUtilImpl();
  virtual ~NssUtilImpl();

  bool MightHaveKeys();

  bool OpenUserDB();

  RSAPrivateKey* GetPrivateKey(const std::vector<uint8>& public_key_der);

  RSAPrivateKey* GenerateKeyPair();

  FilePath GetOwnerKeyFilePath();

  bool Verify(const uint8* algorithm, int algorithm_len,
              const uint8* signature, int signature_len,
              const uint8* data, int data_len,
              const uint8* public_key, int public_key_len);

  bool Sign(const uint8* data, int data_len,
            std::vector<uint8>* OUT_signature,
            RSAPrivateKey* key);
 private:
  static const uint16 kKeySizeInBits;
  // Hardcoded path of the user's NSS key database.
  // TODO(cmasone): get rid of this once http://crosbug.com/14007 is fixed.
  static const char kUserDbPath[];

  DISALLOW_COPY_AND_ASSIGN(NssUtilImpl);
};

// Defined here, instead of up above, because we need NssUtilImpl.
// static
NssUtil* NssUtil::Create() {
  if (!factory_) {
    return new NssUtilImpl;
    crypto::EnsureNSSInit();
  } else {
    return factory_->CreateNssUtil();
  }
}

// static
void NssUtil::BlobFromBuffer(const std::string& buf, std::vector<uint8>* out) {
  out->resize(buf.length());
  if (out->size() == 0)
    return;
  memcpy(&(out->at(0)), buf.c_str(), out->size());
}

// We're generating and using 2048-bit RSA keys.
// static
const uint16 NssUtilImpl::kKeySizeInBits = 2048;
// static
const char NssUtilImpl::kUserDbPath[] = "/home/chronos/user/.pki/nssdb/key4.db";

NssUtilImpl::NssUtilImpl() {}

NssUtilImpl::~NssUtilImpl() {}

bool NssUtilImpl::MightHaveKeys() {
  return file_util::PathExists(FilePath(NssUtilImpl::kUserDbPath));
}

bool NssUtilImpl::OpenUserDB() {
  // TODO(cmasone): If we ever try to keep the session_manager alive across
  // user sessions, we'll need to deal with the fact that we have no way to
  // close this persistent DB.
  crypto::OpenPersistentNSSDB();
  return true;
}

RSAPrivateKey* NssUtilImpl::GetPrivateKey(
    const std::vector<uint8>& public_key_der) {
  if (public_key_der.size() == 0) {
    LOG(ERROR) << "Not checking key because size is 0";
    return false;
  }
  return RSAPrivateKey::FindFromPublicKeyInfo(public_key_der);
}

RSAPrivateKey* NssUtilImpl::GenerateKeyPair() {
  return RSAPrivateKey::CreateSensitive(kKeySizeInBits);
}<commit_msg>[login_manager] Don't return false where a pointer is expected<commit_after>// Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "login_manager/nss_util.h"

#include <base/basictypes.h>
#include <base/file_path.h>
#include <base/file_util.h>
#include <base/logging.h>
#include <base/memory/scoped_ptr.h>
#include <crypto/nss_util.h>
#include <crypto/rsa_private_key.h>
#include <crypto/signature_creator.h>
#include <crypto/signature_verifier.h>
#include <cros/chromeos_login.h>

using crypto::RSAPrivateKey;

namespace login_manager {
///////////////////////////////////////////////////////////////////////////
// NssUtil

// static
NssUtil::Factory* NssUtil::factory_ = NULL;

NssUtil::NssUtil() {}

NssUtil::~NssUtil() {}

///////////////////////////////////////////////////////////////////////////
// NssUtilImpl

class NssUtilImpl : public NssUtil {
 public:
  NssUtilImpl();
  virtual ~NssUtilImpl();

  bool MightHaveKeys();

  bool OpenUserDB();

  RSAPrivateKey* GetPrivateKey(const std::vector<uint8>& public_key_der);

  RSAPrivateKey* GenerateKeyPair();

  FilePath GetOwnerKeyFilePath();

  bool Verify(const uint8* algorithm, int algorithm_len,
              const uint8* signature, int signature_len,
              const uint8* data, int data_len,
              const uint8* public_key, int public_key_len);

  bool Sign(const uint8* data, int data_len,
            std::vector<uint8>* OUT_signature,
            RSAPrivateKey* key);
 private:
  static const uint16 kKeySizeInBits;
  // Hardcoded path of the user's NSS key database.
  // TODO(cmasone): get rid of this once http://crosbug.com/14007 is fixed.
  static const char kUserDbPath[];

  DISALLOW_COPY_AND_ASSIGN(NssUtilImpl);
};

// Defined here, instead of up above, because we need NssUtilImpl.
// static
NssUtil* NssUtil::Create() {
  if (!factory_) {
    return new NssUtilImpl;
    crypto::EnsureNSSInit();
  } else {
    return factory_->CreateNssUtil();
  }
}

// static
void NssUtil::BlobFromBuffer(const std::string& buf, std::vector<uint8>* out) {
  out->resize(buf.length());
  if (out->size() == 0)
    return;
  memcpy(&(out->at(0)), buf.c_str(), out->size());
}

// We're generating and using 2048-bit RSA keys.
// static
const uint16 NssUtilImpl::kKeySizeInBits = 2048;
// static
const char NssUtilImpl::kUserDbPath[] = "/home/chronos/user/.pki/nssdb/key4.db";

NssUtilImpl::NssUtilImpl() {}

NssUtilImpl::~NssUtilImpl() {}

bool NssUtilImpl::MightHaveKeys() {
  return file_util::PathExists(FilePath(NssUtilImpl::kUserDbPath));
}

bool NssUtilImpl::OpenUserDB() {
  // TODO(cmasone): If we ever try to keep the session_manager alive across
  // user sessions, we'll need to deal with the fact that we have no way to
  // close this persistent DB.
  crypto::OpenPersistentNSSDB();
  return true;
}

RSAPrivateKey* NssUtilImpl::GetPrivateKey(
    const std::vector<uint8>& public_key_der) {
  if (public_key_der.size() == 0) {
    LOG(ERROR) << "Not checking key because size is 0";
    return NULL;
  }
  return RSAPrivateKey::FindFromPublicKeyInfo(public_key_der);
}

RSAPrivateKey* NssUtilImpl::GenerateKeyPair() {
  return RSAPrivateKey::CreateSensitive(kKeySizeInBits);
}
{'fraction_non_alphanumeric': 0.16768572302048304, 'fraction_numerical': 0.010700091715071844, 'mean_word_length': 3.460122699386503, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4061960', 'n_tokens_mistral': 2205, 'n_tokens_neox': 2127, 'n_words': 530}