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