/*
Module : CryptoWrappersCNGSecret2.h
Purpose: Defines the interface for a C++ wrapper class for a Cryptography Next Generation
         NCRYPT_SECRET_HANDLE
History: PJN / 01-08-2014 1. Initial public release
         PJN / 12-06-2018 1. Updated copyright details.
                          2. Fixed a number of C++ core guidelines compiler warnings. These changes mean
                          that the code will now only compile on VC 2017 or later.
         PJN / 22-08-2018 1. Fixed a number of compiler warnings when using VS 2017 15.8.1

Copyright (c) 2014 - 2018 by PJ Naughter (Web: www.naughter.com, Email: pjna@naughter.com)

All rights reserved.

Copyright / Usage Details:

You are allowed to include the source code in any product (commercial, shareware, freeware or otherwise) 
when your product is released in binary form. You are allowed to modify the source code in any way you want 
except you cannot modify the copyright details at the top of each module. If you want to distribute source 
code with your application, then you are only allowed to distribute versions released by the author. This is 
to maintain a single distribution point for the source code.

*/


////////////////////////// Macros / Defines ///////////////////////////////////

#pragma once

#ifndef __CRYPTOWRAPPERSCNGSECRET2_H__
#define __CRYPTOWRAPPERSCNGSECRET2_H__

#pragma comment(lib, "Ncrypt.lib") //Automatically link in the CNG dll


////////////////////////// Includes ///////////////////////////////////////////

#ifndef __NCRYPT_H__
#pragma message("To avoid this message, please put ncrypt.h in your pre compiled header (normally stdafx.h)")
#include <ncrypt.h>
#endif //#ifndef __NCRYPT_H__


////////////////////////// Classes ////////////////////////////////////////////

namespace CryptoWrappers
{


//Encapsulates a CNG NCRYPT_SECRET_HANDLE
class CCNGSecret2
{
public:
//Constructors / Destructors
  CCNGSecret2() noexcept : m_hSecret(NULL)
  {
  }

  CCNGSecret2(_In_ const CCNGSecret2&) = delete;

  CCNGSecret2(_In_ CCNGSecret2&& secret) noexcept : m_hSecret(NULL)
  {
    Attach(secret.Detach());
  }
  
  explicit CCNGSecret2(_In_opt_ NCRYPT_SECRET_HANDLE hSecret) noexcept : m_hSecret(hSecret)
  {
  }

  ~CCNGSecret2() noexcept
  {
    if (m_hSecret != NULL)
      Free();
  }
  
//Methods
  _Must_inspect_result_
  SECURITY_STATUS Create(_In_ NCRYPT_KEY_HANDLE hPrivKey, _In_ NCRYPT_KEY_HANDLE hPubKey, _In_ DWORD dwFlags) noexcept
  {
    //Validate our parameters
#pragma warning(suppress: 26477)
    ATLASSERT(m_hSecret == NULL);

    return NCryptSecretAgreement(hPrivKey, hPubKey, &m_hSecret, dwFlags);
  }

  CCNGSecret2& operator=(_In_ const CCNGSecret2&) = delete;

  CCNGSecret2& operator=(_In_ CCNGSecret2&& secret) noexcept
  {
    if (m_hSecret != NULL)
      Free();
    Attach(secret.Detach());
    return *this;
  }

  SECURITY_STATUS Free() noexcept
  {
    //Validate our parameters
#pragma warning(suppress: 26477)
    ATLASSERT(m_hSecret != NULL);

    const SECURITY_STATUS status = NCryptFreeObject(m_hSecret);
    m_hSecret = NULL;
    return status;
  }

  NCRYPT_SECRET_HANDLE Handle() const noexcept
  {
    return m_hSecret;
  }

  void Attach(_In_opt_ NCRYPT_SECRET_HANDLE hSecret) noexcept
  {
    //Validate our parameters
#pragma warning(suppress: 26477)
    ATLASSERT(m_hSecret == NULL);

    m_hSecret = hSecret;
  }

  NCRYPT_SECRET_HANDLE Detach() noexcept
  {
    const NCRYPT_SECRET_HANDLE hSecret = m_hSecret;
    m_hSecret = NULL;
    return hSecret;
  }

  _Check_return_
  NTSTATUS DeriveKey(_In_ LPCWSTR pwszKDF, _In_opt_ NCryptBufferDesc* pParameterList, _Out_writes_bytes_to_opt_(cbDerivedKey, *pcbResult) PBYTE pbDerivedKey,
                     _In_ DWORD cbDerivedKey,_Out_ DWORD* pcbResult, _In_ ULONG dwFlags) noexcept
  {
    //Validate our parameters
#pragma warning(suppress: 26477)
    ATLASSERT(m_hSecret != NULL);

    return NCryptDeriveKey(m_hSecret, pwszKDF, pParameterList, pbDerivedKey, cbDerivedKey, pcbResult, dwFlags);
  }

//Member variables
  NCRYPT_SECRET_HANDLE m_hSecret;
};


}; //namespace CryptoWrappers

#endif //#ifndef __CRYPTOWRAPPERSCNGSECRET2_H__
