GSS-API のプログラミング

test_import_export_context()

最後に、test_import_export_context()gss_export_sec_context()gss_import_sec_context() がどのように機能するかを示す小さな関数です。この関数は実用性はなく、ここでは上記 2 つの GSS-API 関数がどのように使用されるかだけを示しています。


例 A–15 test_import_export_context()


int test_import_export_context(context)
      gss_ctx_id_t *context;
{
      OM_uint32       min_stat, maj_stat;
      gss_buffer_desc context_token, copied_token;
      struct timeval tm1, tm2;

      /*
       * 保存後、コンテキストを復元する
       */
      gettimeofday(&tm1, (struct timezone *)0);
      maj_stat = gss_export_sec_context(&min_stat, context, &context_token);
      if (maj_stat != GSS_S_COMPLETE) {
            display_status("exporting context", maj_stat, min_stat);
            return 1;
      }
      gettimeofday(&tm2, (struct timezone *)0);
      if (verbose && log)
            fprintf(log, "Exported context: %d bytes, %7.4f seconds\n",
                      context_token.length, timeval_subtract(&tm2, &tm1));
      copied_token.length = context_token.length;
      copied_token.value = malloc(context_token.length);
      if (copied_token.value == 0) {
          fprintf(log, "Couldn't allocate memory to copy context token.\n");
          return 1;
      }
      memcpy(copied_token.value, context_token.value, copied_token.length);
      maj_stat = gss_import_sec_context(&min_stat, &copied_token, context);
      if (maj_stat != GSS_S_COMPLETE) {
              display_status("importing context", maj_stat, min_stat);
              return 1;
      }
      gettimeofday(&tm1, (struct timezone *)0);
      if (verbose && log)
              fprintf(log, "Importing context: %7.4f seconds\n",
                      timeval_subtract(&tm1, &tm2));
      (void) gss_release_buffer(&min_stat, &context_token);
      return 0;
}