sigtail.c

Go to the documentation of this file.
00001 /***************************************************************************
00002     begin       : Sun Nov 30 2008
00003     copyright   : (C) 2008 by Martin Preuss
00004     email       : martin@libchipcard.de
00005 
00006  ***************************************************************************
00007  *          Please see toplevel file COPYING for license details           *
00008  ***************************************************************************/
00009 
00010 
00011 #ifdef HAVE_CONFIG_H
00012 # include <config.h>
00013 #endif
00014 
00015 
00016 #include "sigtail_p.h"
00017 #include "i18n_l.h"
00018 #include <gwenhywfar/misc.h>
00019 #include <gwenhywfar/debug.h>
00020 #include <gwenhywfar/tag16.h>
00021 
00022 
00023 GWEN_LIST_FUNCTIONS(GWEN_SIGTAIL, GWEN_SigTail)
00024 
00025 
00026 
00027 GWEN_SIGTAIL *GWEN_SigTail_new() {
00028   GWEN_SIGTAIL *st;
00029 
00030   GWEN_NEW_OBJECT(GWEN_SIGTAIL, st);
00031   GWEN_LIST_INIT(GWEN_SIGTAIL, st);
00032 
00033   return st;
00034 }
00035 
00036 
00037 
00038 void GWEN_SigTail_free(GWEN_SIGTAIL *st) {
00039   if (st) {
00040     GWEN_LIST_FINI(GWEN_SIGTAIL, st);
00041     if (st->pSignature && st->lSignature)
00042       free(st->pSignature);
00043 
00044     GWEN_FREE_OBJECT(st);
00045   }
00046 }
00047 
00048 
00049 
00050 GWEN_SIGTAIL *GWEN_SigTail_fromBuffer(const uint8_t *p, uint32_t l) {
00051   if (p==NULL || l<1) {
00052     DBG_INFO(GWEN_LOGDOMAIN, "Bad tag");
00053     return NULL;
00054   }
00055   else {
00056     GWEN_SIGTAIL *st;
00057     const uint8_t *sp;
00058     uint32_t sl;
00059 
00060     st=GWEN_SigTail_new();
00061     sp=p;
00062     sl=l;
00063     while(sl) {
00064       GWEN_TAG16 *subtag;
00065       uint32_t subtagLen;
00066       const char *subtagPtr;
00067       int i;
00068 
00069       subtag=GWEN_Tag16_fromBuffer2(sp, sl, 0);
00070       if (subtag==NULL) {
00071         DBG_INFO(GWEN_LOGDOMAIN, "Bad sub-tag");
00072         GWEN_SigTail_free(st);
00073         return NULL;
00074       }
00075       subtagLen=GWEN_Tag16_GetTagLength(subtag);
00076       subtagPtr=(const char*)GWEN_Tag16_GetTagData(subtag);
00077 
00078       if (subtagLen && subtagPtr) {
00079         switch(GWEN_Tag16_GetTagType(subtag)) {
00080         case GWEN_SIGTAIL_TLV_SIGNATURE:
00081           st->pSignature=(uint8_t*)malloc(subtagLen);
00082           memmove(st->pSignature, subtagPtr, subtagLen);
00083           st->lSignature=subtagLen;
00084           break;
00085 
00086         case GWEN_SIGTAIL_TLV_SIGNUM:
00087           if (sscanf(subtagPtr, "%d", &i)==1)
00088             st->signatureNumber=i;
00089           break;
00090 
00091         default:
00092           DBG_WARN(GWEN_LOGDOMAIN, "Unknown tag %02x", GWEN_Tag16_GetTagType(subtag));
00093         }
00094       }
00095 
00096       sp+=GWEN_Tag16_GetTagSize(subtag);
00097       sl-=GWEN_Tag16_GetTagSize(subtag);
00098       GWEN_Tag16_free(subtag);
00099     } /* while */
00100 
00101     return st;
00102   }
00103 }
00104 
00105 
00106 
00107 int GWEN_SigTail_toBuffer(const GWEN_SIGTAIL *st, GWEN_BUFFER *buf, uint8_t tagType) {
00108   char numbuf[32];
00109   uint32_t pos;
00110   uint8_t *p;
00111   uint32_t l;
00112 
00113   GWEN_Buffer_AppendByte(buf, tagType);
00114   pos=GWEN_Buffer_GetPos(buf);
00115   GWEN_Buffer_AppendByte(buf, 0);
00116   GWEN_Buffer_AppendByte(buf, 0);
00117 
00118   if (st->pSignature && st->lSignature)
00119     GWEN_Tag16_DirectlyToBuffer(GWEN_SIGTAIL_TLV_SIGNATURE,
00120                                 (const char*)st->pSignature,
00121                                 st->lSignature,
00122                                 buf);
00123 
00124   snprintf(numbuf, sizeof(numbuf), "%d", st->signatureNumber);
00125   GWEN_Tag16_DirectlyToBuffer(GWEN_SIGTAIL_TLV_SIGNUM, numbuf, -1, buf);
00126 
00127   /* write size */
00128   l=GWEN_Buffer_GetPos(buf)-pos-2;
00129   p=(uint8_t*)GWEN_Buffer_GetStart(buf)+pos;
00130   *(p++)=l & 0xff;
00131   *p=(l>>8) & 0xff;
00132 
00133   return 0;
00134 }
00135 
00136 
00137 
00138 const uint8_t *GWEN_SigTail_GetSignaturePtr(const GWEN_SIGTAIL *st) {
00139   assert(st);
00140   return st->pSignature;
00141 }
00142 
00143 
00144 
00145 uint32_t GWEN_SigTail_GetSignatureLen(const GWEN_SIGTAIL *st) {
00146   assert(st);
00147   return st->lSignature;
00148 }
00149 
00150 
00151 
00152 void GWEN_SigTail_SetSignature(GWEN_SIGTAIL *st, const uint8_t *p, uint32_t l) {
00153   assert(st);
00154   if (st->pSignature && st->lSignature)
00155     free(st->pSignature);
00156   if (p && l) {
00157     st->pSignature=(uint8_t*)malloc(l);
00158     memmove(st->pSignature, p, l);
00159     st->lSignature=l;
00160   }
00161   else {
00162     st->pSignature=NULL;
00163     st->lSignature=0;
00164   }
00165 }
00166 
00167 
00168 
00169 int GWEN_SigTail_GetSignatureNumber(const GWEN_SIGTAIL *st) {
00170   assert(st);
00171   return st->signatureNumber;
00172 }
00173 
00174 
00175 
00176 void GWEN_SigTail_SetSignatureNumber(GWEN_SIGTAIL *st, int i) {
00177   assert(st);
00178   st->signatureNumber=i;
00179 }
00180 
00181 
00182 
00183 
00184 
00185 

Generated on Mon Jan 25 12:56:02 2010 for gwenhywfar by  doxygen 1.5.6