<commit_before>/*
 * This file is part of tpm2-pk11.
 * Copyright (C) 2017 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "sessions.h"

#include <stdlib.h>

#include <tcti/tcti_socket.h>
#include <tcti/tcti_device.h>
#include <tcti/tcti-tabrmd.h>

#define DEFAULT_DEVICE "/dev/tpm0"
#define DEFAULT_HOSTNAME "127.0.0.1"
#define DEFAULT_PORT 2323

int session_init(struct session* session, struct config *config) {
  session->context = NULL;

  size_t size = 0;
  TSS2_TCTI_CONTEXT *tcti_ctx = NULL;
  TSS2_RC rc;
  TCTI_SOCKET_CONF socket_conf = {
    .hostname = config->hostname != NULL ? config->hostname : DEFAULT_HOSTNAME,
    .port = config->port > 0 ? config->port : DEFAULT_PORT,
    .logCallback = NULL,
    .logBufferCallback = NULL,
    .logData = NULL,
  };

  if (config->type == TPM_TYPE_SOCKET)
    rc = InitSocketTcti(NULL, &size, &socket_conf, 0);
  else if (config->type == TPM_TYPE_DEVICE)
    rc = InitDeviceTcti(tcti_ctx, &size, 0);
  else
    rc = tss2_tcti_tabrmd_init(tcti_ctx, &size);

  if (rc != TSS2_RC_SUCCESS)
    goto cleanup;

  tcti_ctx = (TSS2_TCTI_CONTEXT*) malloc(size);
  if (tcti_ctx == NULL)
    goto cleanup;

  if (config->type == TPM_TYPE_SOCKET)
    rc = InitSocketTcti(tcti_ctx, &size, &socket_conf, 0);
  else if (config->type == TPM_TYPE_DEVICE) {
    TCTI_DEVICE_CONF conf = {
      .device_path = config->device != NULL ? config->device : DEFAULT_DEVICE,
      .logCallback = NULL,
      .logData = NULL,
    };
    rc = InitDeviceTcti(tcti_ctx, &size, &conf);
  } else
    rc = tss2_tcti_tabrmd_init(tcti_ctx, &size);

  if (rc != TSS2_RC_SUCCESS)
    goto cleanup;
  
  size = Tss2_Sys_GetContextSize(0);
  session->context = (TSS2_SYS_CONTEXT*) malloc(size);
  if (session->context == NULL)
    goto cleanup;

  TSS2_ABI_VERSION abi_version = {
    .tssCreator = TSSWG_INTEROP,
    .tssFamily = TSS_SAPI_FIRST_FAMILY,
    .tssLevel = TSS_SAPI_FIRST_LEVEL,
    .tssVersion = TSS_SAPI_FIRST_VERSION,
  };
  rc = Tss2_Sys_Initialize(session->context, size, tcti_ctx, &abi_version);

  session->objects = object_load(session->context);

  return 0;

  cleanup:
  if (tcti_ctx != NULL)
    free(tcti_ctx);

  if (session->context != NULL)
    free(session->context);

  return -1;
}

void session_close(struct session* session) {
  object_free(session->objects);
  Tss2_Sys_Finalize(session->context);
}
<commit_msg>Clear TPM context allocated memory<commit_after>/*
 * This file is part of tpm2-pk11.
 * Copyright (C) 2017 <NAME>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "sessions.h"

#include <stdlib.h>

#include <tcti/tcti_socket.h>
#include <tcti/tcti_device.h>
#include <tcti/tcti-tabrmd.h>

#define DEFAULT_DEVICE "/dev/tpm0"
#define DEFAULT_HOSTNAME "127.0.0.1"
#define DEFAULT_PORT 2323

int session_init(struct session* session, struct config *config) {
  session->context = NULL;

  size_t size = 0;
  TSS2_TCTI_CONTEXT *tcti_ctx = NULL;
  TSS2_RC rc;
  TCTI_SOCKET_CONF socket_conf = {
    .hostname = config->hostname != NULL ? config->hostname : DEFAULT_HOSTNAME,
    .port = config->port > 0 ? config->port : DEFAULT_PORT,
    .logCallback = NULL,
    .logBufferCallback = NULL,
    .logData = NULL,
  };

  if (config->type == TPM_TYPE_SOCKET)
    rc = InitSocketTcti(NULL, &size, &socket_conf, 0);
  else if (config->type == TPM_TYPE_DEVICE)
    rc = InitDeviceTcti(NULL, &size, 0);
  else
    rc = tss2_tcti_tabrmd_init(NULL, &size);

  if (rc != TSS2_RC_SUCCESS)
    goto cleanup;

  tcti_ctx = (TSS2_TCTI_CONTEXT*) calloc(1, size);
  if (tcti_ctx == NULL)
    goto cleanup;

  if (config->type == TPM_TYPE_SOCKET)
    rc = InitSocketTcti(tcti_ctx, &size, &socket_conf, 0);
  else if (config->type == TPM_TYPE_DEVICE) {
    TCTI_DEVICE_CONF conf = {
      .device_path = config->device != NULL ? config->device : DEFAULT_DEVICE,
      .logCallback = NULL,
      .logData = NULL,
    };
    rc = InitDeviceTcti(tcti_ctx, &size, &conf);
  } else
    rc = tss2_tcti_tabrmd_init(tcti_ctx, &size);

  if (rc != TSS2_RC_SUCCESS)
    goto cleanup;
  
  size = Tss2_Sys_GetContextSize(0);
  session->context = (TSS2_SYS_CONTEXT*) calloc(1, size);
  if (session->context == NULL)
    goto cleanup;

  TSS2_ABI_VERSION abi_version = {
    .tssCreator = TSSWG_INTEROP,
    .tssFamily = TSS_SAPI_FIRST_FAMILY,
    .tssLevel = TSS_SAPI_FIRST_LEVEL,
    .tssVersion = TSS_SAPI_FIRST_VERSION,
  };
  rc = Tss2_Sys_Initialize(session->context, size, tcti_ctx, &abi_version);

  session->objects = object_load(session->context);

  return 0;

  cleanup:
  if (tcti_ctx != NULL)
    free(tcti_ctx);

  if (session->context != NULL)
    free(session->context);

  return -1;
}

void session_close(struct session* session) {
  object_free(session->objects);
  Tss2_Sys_Finalize(session->context);
}

{'fraction_non_alphanumeric': 0.11102120084156013, 'fraction_numerical': 0.016831202459944974, 'mean_word_length': 3.7869868319132456, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '5166575', 'n_tokens_mistral': 2266, 'n_tokens_neox': 2193, 'n_words': 598}