/*
 * Licensed Materials - Property of IBM
 * (C) Copyright IBM Corp. 2022. All Rights Reserved.
 * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
 */

#if !defined(__MVS__)
#error This addon is for zos only
#endif

#ifndef __ZCRYPTO_H_
#define __ZCRYPTO_H_ 1

#include <gskcms.h>
#include <gskssl.h>
#include <napi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>

extern "C" int createKDB_impl( const char* filename, const char* password, int length, int expiration, gsk_handle* handle);
extern "C" int importKey_impl(const char* filename, const char* password, const char* label, gsk_handle* handle);
extern "C" int exportKeyToFile_impl(const char* filename, const char* password, const char* label, gsk_handle* handle);
extern "C" int exportKeyToBuffer_impl(const char* password, const char* label, gsk_buffer* stream, gsk_handle* handle);
extern "C" int exportCertToBuffer_impl(const char* label, gsk_buffer* stream, gsk_handle* handle);
extern "C" int openKDB_impl( const char* filename, const char* password, gsk_handle* handle);
extern "C" int closeKDB_impl( gsk_handle* handle);
extern "C" int openKeyRing_impl( const char* ring_name, gsk_handle* handle);
extern "C" char* errorString_impl( int err, char *errstr, int errstrlen );

class ZCrypto : public Napi::ObjectWrap<ZCrypto> {
 public:
  static Napi::Object Init(Napi::Env env, Napi::Object exports);
  ZCrypto(const Napi::CallbackInfo& info);
  ~ZCrypto();

 private:
  static Napi::FunctionReference constructor;
  
  Napi::Value OpenKeyRing(const Napi::CallbackInfo &info);
  Napi::Value OpenKDB(const Napi::CallbackInfo &info);
  Napi::Value CloseKDB(const Napi::CallbackInfo &info);
  Napi::Value CreateKDB(const Napi::CallbackInfo &info);
  Napi::Value ImportKey(const Napi::CallbackInfo &info);
  Napi::Value GetErrorString(const Napi::CallbackInfo &info);
  Napi::Value ExportKeyToFile(const Napi::CallbackInfo &info);
  Napi::Value ExportKeyToBuffer(const Napi::CallbackInfo &info);
  Napi::Value ExportCertToBuffer(const Napi::CallbackInfo &info);

  bool initialized;
  gsk_handle handle;
};


#ifdef DEBUG
char* getErrStr(int rc);
void dbgPrintf(const char *fname, int linenum, const char *funcname,
               const char *format, ...);
#define DPRINTF(...) dbgPrintf(__FILE__, __LINE__, __FUNCTION__, __VA_ARGS__);
#else
#define DPRINTF(...) (void)0
#endif

#endif
