示例#1
0
int logFile(AB_BANKING *ab,
            GWEN_DB_NODE *dbArgs,
            int argc,
            char **argv) {
  int rv;
  GWEN_DB_NODE *db;
  GWEN_DB_NODE *dbMessages;
  const char *s;
  GWEN_MSGENGINE *e;
  GWEN_SYNCIO *sioOut=NULL;
  GWEN_SYNCIO *sioDb=NULL;
  const char *inFile;
  const char *outFile;
  const char *dbOutFile;
  int i;
  GWEN_DB_NODE *dbT;
  int trustLevel;
  const GWEN_ARGS args[]={
  {
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
    GWEN_ArgsType_Char,           /* type */
    "xmlfile",                    /* name */
    0,                            /* minnum */
    99,                           /* maxnum */
    "x",                          /* short option */
    "xmlfile",                    /* long option */
    "Specify XML files to load",  /* short description */
    "Specify XML files to load"   /* long description */
  },
  {
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
    GWEN_ArgsType_Char,           /* type */
    "infile",                     /* name */
    1,                            /* minnum */
    1,                            /* maxnum */
    "i",                          /* short option */
    "infile",                     /* long option */
    "Specify input file",         /* short description */
    "Specify input file"          /* long description */
  },
  {
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
    GWEN_ArgsType_Char,           /* type */
    "outfile",                    /* name */
    0,                            /* minnum */
    1,                            /* maxnum */
    "o",                          /* short option */
    "outfile",                    /* long option */
    "Specify output file",        /* short description */
    "Specify output file"         /* long description */
  },
  {
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
    GWEN_ArgsType_Char,           /* type */
    "dboutfile",                  /* name */
    0,                            /* minnum */
    1,                            /* maxnum */
    "d",                          /* short option */
    "dbfile",                     /* long option */
    "Specify DB output file",     /* short description */
    "Specify DB output file"      /* long description */
  },
  {
    GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
    GWEN_ArgsType_Int,            /* type */
    "trustLevel",                 /* name */
    0,                            /* minnum */
    1,                            /* maxnum */
    "L",                          /* short option */
    "trustlevel",                 /* long option */
    "Specify the trust level",    /* short description */
    "Specify the trust level"     /* long description */
  },
  {
    GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
    GWEN_ArgsType_Int,            /* type */
    "help",                       /* name */
    0,                            /* minnum */
    0,                            /* maxnum */
    "h",                          /* short option */
    "help",                       /* long option */
    "Show this help screen",      /* short description */
    "Show this help screen"       /* long description */
  }
  };

  db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
  rv=GWEN_Args_Check(argc, argv, 1,
                     0 /*GWEN_ARGS_MODE_ALLOW_FREEPARAM*/,
                     args,
                     db);
  if (rv==GWEN_ARGS_RESULT_ERROR) {
    fprintf(stderr, "ERROR: Could not parse arguments\n");
    return 1;
  }
  else if (rv==GWEN_ARGS_RESULT_HELP) {
    GWEN_BUFFER *ubuf;

    ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
    if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
      fprintf(stderr, "ERROR: Could not create help string\n");
      return 1;
    }
    fprintf(stderr, "%s\n", GWEN_Buffer_GetStart(ubuf));
    GWEN_Buffer_free(ubuf);
    return 0;
  }

  trustLevel=GWEN_DB_GetIntValue(db, "trustLevel", 0, 0);
  outFile=GWEN_DB_GetCharValue(db, "outFile", 0, NULL);
  dbOutFile=GWEN_DB_GetCharValue(db, "dbOutFile", 0, NULL);
  inFile=GWEN_DB_GetCharValue(db, "inFile", 0, NULL);
  assert(inFile);

  /* do it */
  dbMessages=GWEN_DB_Group_new("Messages");
  rv=_readLogFile(inFile, dbMessages);
  if (rv<0) {
    DBG_ERROR(0, "Error reading message (%d)", rv);
    return 2;
  }

  /* create message engine, read XML definitions */
  e=AH_MsgEngine_new();
  for (i=0; i<99; i++) {
    s=GWEN_DB_GetCharValue(dbArgs, "xmlfile", i, NULL);
    if (s && *s) {
      GWEN_XMLNODE *defs;

      defs=GWEN_XMLNode_new(GWEN_XMLNodeTypeTag, "defs");
      if (GWEN_XML_ReadFile(defs, s, GWEN_XML_FLAGS_DEFAULT)){
	fprintf(stderr, "Error parsing.\n");
	GWEN_MsgEngine_free(e);
	return 2;
      }
      GWEN_MsgEngine_AddDefinitions(e, defs);
      GWEN_XMLNode_free(defs);
    }
    else {
      if (i==0) {
	GWEN_XMLNODE *defs;

	defs=GWEN_XMLNode_new(GWEN_XMLNodeTypeTag, "defs");
	if (GWEN_XML_ReadFile(defs, XMLDATA_DIR "/hbci.xml", GWEN_XML_FLAGS_DEFAULT)){
	  fprintf(stderr, "Error parsing.\n");
	  GWEN_MsgEngine_free(e);
	  return 2;
	}
	GWEN_MsgEngine_AddDefinitions(e, defs);
	GWEN_XMLNode_free(defs);
      }
      break;
    }
  }

  if (outFile) {
    sioOut=GWEN_SyncIo_File_new(outFile, GWEN_SyncIo_File_CreationMode_CreateAlways);
    GWEN_SyncIo_AddFlags(sioOut,
			 GWEN_SYNCIO_FILE_FLAGS_READ |
			 GWEN_SYNCIO_FILE_FLAGS_WRITE |
			 GWEN_SYNCIO_FILE_FLAGS_UREAD |
			 GWEN_SYNCIO_FILE_FLAGS_UWRITE |
			 GWEN_SYNCIO_FILE_FLAGS_APPEND);
    rv=GWEN_SyncIo_Connect(sioOut);
    if (rv<0) {
      DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
      GWEN_SyncIo_free(sioOut);
      return 2;
    }
  }

  if (dbOutFile) {
    sioDb=GWEN_SyncIo_File_new(dbOutFile, GWEN_SyncIo_File_CreationMode_CreateAlways);
    GWEN_SyncIo_AddFlags(sioDb,
			 GWEN_SYNCIO_FILE_FLAGS_READ |
			 GWEN_SYNCIO_FILE_FLAGS_WRITE |
			 GWEN_SYNCIO_FILE_FLAGS_UREAD |
			 GWEN_SYNCIO_FILE_FLAGS_UWRITE |
			 GWEN_SYNCIO_FILE_FLAGS_APPEND);
    rv=GWEN_SyncIo_Connect(sioDb);
    if (rv<0) {
      DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
      GWEN_SyncIo_free(sioDb);
      return 2;
    }
  }

  dbT=GWEN_DB_GetFirstGroup(dbMessages);
  while(dbT) {
    const uint8_t *p;
    uint32_t len;
    GWEN_DB_NODE *dbHeader;

    dbHeader=GWEN_DB_GetGroup(dbT, GWEN_PATH_FLAGS_NAMEMUSTEXIST, "header");
    assert(dbHeader);

    s=GWEN_DB_GetCharValue(dbHeader, "mode", 0, "PINTAN");
    GWEN_MsgEngine_SetMode(e, s);

    i=GWEN_DB_GetIntValue(dbHeader, "hbciVersion", 0, 220);
    GWEN_MsgEngine_SetProtocolVersion(e, i);

    p=GWEN_DB_GetBinValue(dbT, "body", 0, NULL, 0, &len);
    if (p && len) {
      GWEN_BUFFER *tbuf;
      GWEN_DB_NODE *gr;
      GWEN_MSGENGINE_TRUSTEDDATA *trustedData;
      GWEN_MSGENGINE_TRUSTEDDATA *ntd;
      GWEN_DB_NODE *repl;

      gr=GWEN_DB_Group_new("message");
      tbuf=GWEN_Buffer_new((char*) p, len, len, 0);
      rv=GWEN_MsgEngine_ReadMessage(e, "SEG", tbuf, gr,
				    GWEN_MSGENGINE_READ_FLAGS_TRUSTINFO);
      if (rv) {
	fprintf(stderr, "ERROR.\n");
	GWEN_Buffer_Dump(tbuf, 2);
	return 2;
      }

      /* work on trust data */
      trustedData=GWEN_MsgEngine_TakeTrustInfo(e);
      if (trustedData) {
	if (GWEN_MsgEngine_TrustedData_CreateReplacements(trustedData)) {
	  fprintf(stderr, "Could not anonymize log (createReplacements)\n");
	  GWEN_MsgEngine_TrustedData_free(trustedData);
	  GWEN_MsgEngine_free(e);
	  return 2;
	}
      }

      /* anonymize file */
      ntd=trustedData;
      repl=GWEN_DB_GetGroup(dbHeader, GWEN_DB_FLAGS_OVERWRITE_GROUPS, "replacements");
      assert(repl);
      while(ntd) {
	if (GWEN_MsgEngine_TrustedData_GetTrustLevel(ntd)>trustLevel) {
	  int pos;
	  unsigned int size;
	  char rbuffer[3];
	  const char *rpstr;

	  rpstr=GWEN_MsgEngine_TrustedData_GetReplacement(ntd);
	  assert(rpstr);
	  assert(*rpstr);
	  size=strlen(rpstr);
	  if (size==1) {
	    rbuffer[0]=rpstr[0];
	    rbuffer[1]=0;
	  }
	  else {
	    rbuffer[0]=rpstr[0];
	    rbuffer[1]=rpstr[1];
	    rbuffer[2]=0;
	  }
	  GWEN_DB_SetCharValue(repl,
			       GWEN_DB_FLAGS_DEFAULT |
			       GWEN_PATH_FLAGS_CREATE_VAR,
			       rbuffer,
			       GWEN_MsgEngine_TrustedData_GetDescription(ntd));
	  size=GWEN_MsgEngine_TrustedData_GetSize(ntd);
	  pos=GWEN_MsgEngine_TrustedData_GetFirstPos(ntd);
	  while(pos>=0) {
	    DBG_INFO(0, "Replacing %d bytes at %d", size, pos);
	    GWEN_Buffer_SetPos(tbuf, pos);
	    GWEN_Buffer_ReplaceBytes(tbuf,
				     size,
				     GWEN_MsgEngine_TrustedData_GetReplacement(ntd),
				     size);
	    pos=GWEN_MsgEngine_TrustedData_GetNextPos(ntd);
	  } // while pos
	}
	ntd=GWEN_MsgEngine_TrustedData_GetNext(ntd);
      } // while ntd

      GWEN_DB_SetIntValue(dbHeader, GWEN_DB_FLAGS_OVERWRITE_VARS, "size", GWEN_Buffer_GetUsedBytes(tbuf));
      if (outFile) {
	rv=dumpMsg(sioOut,
		   dbHeader,
		   (const uint8_t*)GWEN_Buffer_GetStart(tbuf),
		   GWEN_Buffer_GetUsedBytes(tbuf));
	if (rv<0) {
	  fprintf(stderr, "Could not anonymize log (dumpMsg)\n");
	  GWEN_MsgEngine_TrustedData_free(trustedData);
	  GWEN_MsgEngine_free(e);
	  return 2;
	}
      }

      if (dbOutFile) {
	GWEN_BUFFER *xbuf;
	GWEN_DB_NODE *dbOut;

	xbuf=GWEN_Buffer_new(0, 256, 0, 1);
	GWEN_Buffer_AppendString(xbuf, "# ========== Message ( ");
	s=GWEN_DB_GetCharValue(dbHeader, "sender", 0, "UNK");
	if (s && *s) {
	  GWEN_Buffer_AppendString(xbuf, "sender=");
	  GWEN_Buffer_AppendString(xbuf, s);
	  GWEN_Buffer_AppendString(xbuf, " ");
	}
	s=GWEN_DB_GetCharValue(dbHeader, "crypt", 0, "UNK");
	if (s && *s) {
	  GWEN_Buffer_AppendString(xbuf, "crypt=");
	  GWEN_Buffer_AppendString(xbuf, s);
	  GWEN_Buffer_AppendString(xbuf, " ");
	}
	GWEN_Buffer_AppendString(xbuf, ") ==========\n");

	dbOut=GWEN_DB_Group_new("Message");
	GWEN_Buffer_Rewind(tbuf);
	rv=GWEN_MsgEngine_ReadMessage(e, "SEG", tbuf, dbOut, 0);
	if (rv) {
	  fprintf(stderr, "ERROR.\n");
	  GWEN_Buffer_Dump(tbuf, 2);
	  return 2;
	}
  
	rv=GWEN_SyncIo_WriteForced(sioDb,
				   (const uint8_t*) GWEN_Buffer_GetStart(xbuf),
				   GWEN_Buffer_GetUsedBytes(xbuf));
	GWEN_Buffer_free(xbuf);
	if (rv<0) {
	  DBG_INFO(0, "here (%d)", rv);
	  return rv;
	}
	rv=GWEN_DB_WriteToIo(dbOut, sioDb, GWEN_DB_FLAGS_DEFAULT);
	if (rv<0) {
	  DBG_INFO(0, "here (%d)", rv);
	  return 2;
	}
  
	/* append empty line to separate header from data */
	rv=GWEN_SyncIo_WriteForced(sioDb, (const uint8_t*) "\n", 1);
	if (rv<0) {
	  DBG_INFO(0, "here (%d)", rv);
	  return rv;
	}
      }

      GWEN_Buffer_free(tbuf);
    }

    dbT=GWEN_DB_GetNextGroup(dbT);
  }

  /* close output layer */
  if (outFile) {
    rv=GWEN_SyncIo_Disconnect(sioOut);
    if (rv<0) {
      DBG_INFO(AQHBCI_LOGDOMAIN, "here (%d)", rv);
      GWEN_SyncIo_free(sioOut);
      return 2;
    }
    GWEN_SyncIo_free(sioOut);
  }

  if (dbOutFile) {
    rv=GWEN_SyncIo_Disconnect(sioDb);
    if (rv<0) {
      DBG_INFO(AQHBCI_LOGDOMAIN, "here (%d)", rv);
      GWEN_SyncIo_free(sioDb);
      return 2;
    }
    GWEN_SyncIo_free(sioDb);
  }

  return 0;
}
示例#2
0
int testPcsc6(int argc, char **argv) {
  LC_CLIENT *cl;
  LC_CLIENT_RESULT res;
  LC_CARD *card;
  GWEN_BUFFER *mbuf;
  GWEN_DB_NODE *dbRecord;

  cl=LC_Client_new("test", "0.1");
  if (!cl) {
    fprintf(stderr, "ERROR: Could not create client.\n");
    return 1;
  }

  res=LC_Client_Init(cl);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Could not init client (%d).\n",
            res);
    return 2;
  }

  res=LC_Client_Start(cl);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Could not start using cards (%d).\n",
            res);
    return 2;
  }

  res=LC_Client_GetNextCard(cl, &card, 20);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: No card found (%d).\n",
            res);
    return 2;
  }

  fprintf(stderr, "Got this card:\n");
  LC_Card_Dump(card, 2);


  if (LC_DDVCard_ExtendCard(card)) {
    fprintf(stderr, "ERROR: Could not extend card as DDV card\n");
    return 2;
  }

  fprintf(stderr, "INFO: Opening card\n");
  res=LC_Card_Open(card);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Unable to open card (%d).\n", res);
    return 2;
  }
  fprintf(stderr, "Response was %d\n", res);

  fprintf(stderr, "Selecting EF...\n");
  res=LC_Card_SelectEf(card, "EF_BNK");
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Wait timed out.\n");
    return 2;
  }
  fprintf(stderr, "Response was %d\n", res);

  fprintf(stderr, "Reading record...\n");
  mbuf=GWEN_Buffer_new(0, 256, 0, 1);
  res=LC_Card_IsoReadRecord(card, LC_CARD_ISO_FLAGS_RECSEL_GIVEN,
                            1, mbuf);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Wait timed out.\n");
    return 2;
  }
  fprintf(stderr, "Response was %d\n", res);
  GWEN_Text_DumpString(GWEN_Buffer_GetStart(mbuf),
                       GWEN_Buffer_GetUsedBytes(mbuf), 2);
  GWEN_Buffer_Rewind(mbuf);

  dbRecord=GWEN_DB_Group_new("record");
  if (LC_Card_ParseRecord(card, 1, mbuf, dbRecord)) {
    fprintf(stderr, "Error parsing record.\n");
  }
  else {
    fprintf(stderr, "Parsed record is:\n");
    GWEN_DB_Dump(dbRecord, 2);
  }

  GWEN_Buffer_Reset(mbuf);
  if (LC_Card_CreateRecord(card, 1, mbuf, dbRecord)) {
    fprintf(stderr, "Error creating record.\n");
  }
  else {
    fprintf(stderr, "Created record is:\n");
    GWEN_Buffer_Dump(mbuf, 2);
  }


  res=LC_Client_ReleaseCard(cl, card);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Unable to release card (%d).\n",
            res);
    return 2;
  }
  LC_Card_free(card);


  res=LC_Client_Stop(cl);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Could not stop using cards (%d).\n",
            res);
    return 2;
  }

  res=LC_Client_Fini(cl);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Could not fini client (%d).\n",
            res);
    return 2;
  }

  return 0;
}
示例#3
0
int testPcsc9(int argc, char **argv) {
  LC_CLIENT *cl;
  LC_CLIENT_RESULT res;
  LC_CARD *card;
  GWEN_BUFFER *mbuf;
  LC_PININFO *pi;
  int triesLeft=-1;

  fprintf(stderr, "\n");
  fprintf(stderr, "====================================================\n");
  fprintf(stderr, "I   This test will verify the PIN of the card and  I\n");
  fprintf(stderr, "I   dump the first bank information                I\n");
  fprintf(stderr, "====================================================\n");
  fprintf(stderr, "\n");

  cl=LC_Client_new("test", "0.1");
  if (!cl) {
    fprintf(stderr, "ERROR: Could not create client.\n");
    return 1;
  }

  res=LC_Client_Init(cl);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Could not init client (%d).\n",
            res);
    return 2;
  }

  res=LC_Client_Start(cl);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Could not start using cards (%d).\n",
            res);
    return 2;
  }

  res=LC_Client_GetNextCard(cl, &card, 20);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: No card found (%d).\n",
            res);
    return 2;
  }

  fprintf(stderr, "Got this card:\n");
  LC_Card_Dump(card, 2);


  if (LC_ZkaCard_ExtendCard(card)) {
    fprintf(stderr, "ERROR: Could not extend card as ZKA card\n");
    return 2;
  }

  fprintf(stderr, "INFO: Opening card\n");
  res=LC_Card_Open(card);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Unable to open card (%d).\n", res);
    return 2;
  }
  fprintf(stderr, "Response was %d\n", res);

  fprintf(stderr, "Selecting MF...\n");
  res=LC_Card_SelectMf(card);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Wait timed out.\n");
    return 2;
  }
  fprintf(stderr, "Response was %d\n", res);

  fprintf(stderr, "Selecting DF_NOTEPAD...\n");
  res=LC_Card_SelectDf(card, "DF_NOTEPAD");
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Wait timed out.\n");
    return 2;
  }
  fprintf(stderr, "Response was %d\n", res);

  pi=LC_Card_GetPinInfoByName(card, "ch_pin");
  if (pi==NULL) {
    fprintf(stderr, "ERROR: Pin Info not found.\n");
    return 2;
  }

#if 0
  if (pi) {
    GWEN_DB_NODE *dbDEBUG;

    dbDEBUG=GWEN_DB_Group_new("PinInfo");
    LC_PinInfo_toDb(pi, dbDEBUG);
    GWEN_DB_Dump(dbDEBUG, 2);
    GWEN_DB_Group_free(dbDEBUG);
  }
#endif


  fprintf(stderr, "Verifying pin...\n");
  res=LC_Card_IsoPerformVerification(card, 0, pi, &triesLeft);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Wait timed out.\n");
    return 2;
  }
  fprintf(stderr, "Response was %d\n", res);

  fprintf(stderr, "Selecting EF_NOTEPAD...\n");
  res=LC_Card_SelectEf(card, "EF_NOTEPAD");
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Wait timed out.\n");
    return 2;
  }
  fprintf(stderr, "Response was %d\n", res);

  fprintf(stderr, "Reading record...\n");
  mbuf=GWEN_Buffer_new(0, 256, 0, 1);
  res=LC_Card_IsoReadRecord(card, LC_CARD_ISO_FLAGS_RECSEL_GIVEN,
                            1, mbuf);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Wait timed out.\n");
    return 2;
  }
  fprintf(stderr, "Response was %d\n", res);
  GWEN_Text_DumpString(GWEN_Buffer_GetStart(mbuf),
                       GWEN_Buffer_GetUsedBytes(mbuf), 2);
  GWEN_Buffer_Rewind(mbuf);

#if 0
  dbRecord=GWEN_DB_Group_new("record");
  if (LC_Card_ParseRecord(card, 1, mbuf, dbRecord)) {
    fprintf(stderr, "Error parsing record.\n");
  }
  else {
    fprintf(stderr, "Parsed record is:\n");
    GWEN_DB_Dump(dbRecord, 2);
  }

  GWEN_Buffer_Reset(mbuf);
  if (LC_Card_CreateRecord(card, 1, mbuf, dbRecord)) {
    fprintf(stderr, "Error creating record.\n");
  }
  else {
    fprintf(stderr, "Created record is:\n");
    GWEN_Buffer_Dump(mbuf, 2);
  }
#endif

  res=LC_Client_ReleaseCard(cl, card);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Unable to release card (%d).\n",
            res);
    return 2;
  }
  LC_Card_free(card);


  res=LC_Client_Stop(cl);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Could not stop using cards (%d).\n",
            res);
    return 2;
  }

  res=LC_Client_Fini(cl);
  if (res!=LC_Client_ResultOk) {
    fprintf(stderr, "ERROR: Could not fini client (%d).\n",
            res);
    return 2;
  }

  return 0;
}
示例#4
0
int check2() {
  AB_BANKING *ab;
  AB_PROVIDER *pro;
  int rv;
  AH_MEDIUM *medium=0;
  GWEN_CRYPTKEY *localCryptKey=0;
  GWEN_CRYPTKEY *msgKey=0;
  int err;
  char keybuffer[16];
  unsigned int bsize;
  AH_MEDIUM_RESULT res;
  GWEN_BUFFER *plainKeyBuf;
  GWEN_BUFFER *encKeyBuf;
  GWEN_BUFFER *decKeyBuf;
  const char *p1, *p2;
  int i;
  GWEN_GUI *gui;

  fprintf(stderr, "Check2:\n");

  gui=GWEN_Gui_CGui_new();
  GWEN_Gui_SetGetPasswordFn(gui, _getPin);

  unlink("check2.medium");

  ab=AB_Banking_new("hbci-check1", 0, 0);

  rv=AB_Banking_Init(ab);
  if (rv) {
    DBG_ERROR(0, "Error on init (%d)", rv);
    return 2;
  }

  pro=AB_Banking_GetProvider(ab, "aqhbci");
  assert(pro);

  medium=AH_Provider_MediumFactory(pro, "ohbci", 0, "check2.medium");

  if (!medium) {
    DBG_ERROR(0, "Could not create medium object");
    AB_Banking_Fini(ab);
    return 3;
  }

  fprintf(stderr, "  Creating medium ...\n");
  rv=AH_Medium_Create(medium);
  if (rv) {
    DBG_ERROR(0, "Could not create medium (%d)", rv);
    return 3;
  }

  fprintf(stderr, "  Mounting medium ...\n");
  rv=AH_Medium_Mount(medium);
  if (rv) {
    DBG_ERROR(0, "Could not mount medium (%d)", rv);
    return 3;
  }

  fprintf(stderr, "  Selecting context ...\n");
  rv=AH_Medium_SelectContext(medium, 0);
  if (rv) {
    DBG_ERROR(0, "Could not select context (%d)", rv);
    return 3;
  }

  fprintf(stderr, "  Creating keys ...\n");
  rv=AH_Medium_CreateKeys(medium);
  if (rv) {
    DBG_ERROR(0, "Could not create keys (%d)", rv);
    return 3;
  }

  localCryptKey=AH_Medium_GetLocalPubCryptKey(medium);
  if (!localCryptKey) {
    DBG_ERROR(0, "No local crypt key.");
    return 3;
  }

  fprintf(stderr, "  Storing remote public key ...\n");
  rv=AH_Medium_SetPubCryptKey(medium, localCryptKey);
  if (rv) {
    DBG_ERROR(0, "Could not store remote crypt key (%d)", rv);
    return 3;
  }

  fprintf(stderr, "  Creating DES key object ...\n");
  msgKey=GWEN_CryptKey_Factory("DES");
  if (!msgKey) {
    DBG_ERROR(0, "Could not create message key (%d)", rv);
    return 3;
  }

  fprintf(stderr, "  Generating DES message key ...\n");
  err=GWEN_CryptKey_Generate(msgKey, 16);
  if (err) {
    DBG_ERROR_ERR(0, err);
    return 3;
  }

  fprintf(stderr, "  Getting data of message key ...\n");
  bsize=sizeof(keybuffer);
  err=GWEN_CryptKey_GetData(msgKey, keybuffer, &bsize);
  if (err) {
    DBG_ERROR_ERR(0, err);
    return 3;
  }

  fprintf(stderr, "  Encrypting message key ...\n");
  plainKeyBuf=GWEN_Buffer_new(0, 256, 0, 1);
  GWEN_Buffer_AppendBytes(plainKeyBuf, keybuffer, bsize);
  encKeyBuf=GWEN_Buffer_new(0, 256, 0, 1);
  res=AH_Medium_EncryptKey(medium, plainKeyBuf, encKeyBuf, 1);
  if (res!=AH_MediumResultOk) {
    DBG_ERROR(0, "Error %d", res);
    return 3;
  }

  fprintf(stderr, "  Decrypting message key ...\n");
  decKeyBuf=GWEN_Buffer_new(0, 256, 0, 1);
  res=AH_Medium_DecryptKey(medium, encKeyBuf, decKeyBuf, 1);
  if (res!=AH_MediumResultOk) {
    DBG_ERROR(0, "Error %d", res);
    return 3;
  }

  fprintf(stderr, "  Comparing message key ...\n");
  p1=GWEN_Buffer_GetStart(plainKeyBuf);
  p2=GWEN_Buffer_GetStart(decKeyBuf);
  rv=0;
  for (i=0; i<GWEN_Buffer_GetUsedBytes(plainKeyBuf); i++) {
    if (p1[i]!=p2[i]) {
      fprintf(stderr, "Buffer1:\n");
      GWEN_Buffer_Dump(plainKeyBuf, stderr, 2);

      fprintf(stderr, "Buffer2:\n");
      GWEN_Buffer_Dump(decKeyBuf, stderr, 2);

      fprintf(stderr, "Differ at %d (%04x)\n", i, i);
      rv=-1;
      break;
    }
  }
  if (rv) {
    fprintf(stderr, "Data differs in content\n");
    return 3;
  }


  fprintf(stderr, "  Unmounting medium ...\n");
  rv=AH_Medium_Unmount(medium, 1);
  if (rv) {
    DBG_ERROR(0, "Could not unmount medium (%d)", rv);
    return 3;
  }

  rv=AB_Banking_Fini(ab);
  if (rv) {
    DBG_ERROR(0, "Could not deinit banking (%d)", rv);
    return 3;
  }

  unlink("check2.medium");

  fprintf(stderr, "Check2: PASSED\n");
  return 0;
}