crypthead.c

Go to the documentation of this file.
00001 /***************************************************************************
00002     begin       : Mon Dec 01 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 #ifdef HAVE_CONFIG_H
00011 # include <config.h>
00012 #endif
00013 
00014 
00015 #include "crypthead_p.h"
00016 #include "i18n_l.h"
00017 #include <gwenhywfar/misc.h>
00018 #include <gwenhywfar/debug.h>
00019 #include <gwenhywfar/tag16.h>
00020 
00021 
00022 GWEN_LIST_FUNCTIONS(GWEN_CRYPTHEAD, GWEN_CryptHead)
00023 
00024 
00025 GWEN_CRYPTHEAD *GWEN_CryptHead_new() {
00026   GWEN_CRYPTHEAD *ch;
00027 
00028   GWEN_NEW_OBJECT(GWEN_CRYPTHEAD, ch);
00029 
00030   return ch;
00031 }
00032 
00033 
00034 
00035 void GWEN_CryptHead_free(GWEN_CRYPTHEAD *ch) {
00036   if (ch) {
00037     free(ch->keyName);
00038     if (ch->pKey &&ch->lKey)
00039       free(ch->pKey);
00040     GWEN_FREE_OBJECT(ch);
00041   }
00042 }
00043 
00044 
00045 
00046 GWEN_CRYPTHEAD *GWEN_CryptHead_fromBuffer(const uint8_t *p, uint32_t l) {
00047   if (p==NULL || l<1) {
00048     DBG_INFO(GWEN_LOGDOMAIN, "Bad tag");
00049     return NULL;
00050   }
00051   else {
00052     GWEN_CRYPTHEAD *ch;
00053     const uint8_t *sp;
00054     uint32_t sl;
00055 
00056     ch=GWEN_CryptHead_new();
00057     sp=p;
00058     sl=l;
00059     while(sl) {
00060       GWEN_TAG16 *subtag;
00061       uint32_t subtagLen;
00062       const char *subtagPtr;
00063       int i;
00064 
00065       subtag=GWEN_Tag16_fromBuffer2(sp, sl, 0);
00066       if (subtag==NULL) {
00067         DBG_INFO(GWEN_LOGDOMAIN, "Bad sub-tag");
00068         GWEN_CryptHead_free(ch);
00069         return NULL;
00070       }
00071       subtagLen=GWEN_Tag16_GetTagLength(subtag);
00072       subtagPtr=(const char*)GWEN_Tag16_GetTagData(subtag);
00073 
00074       if (subtagLen && subtagPtr) {
00075         switch(GWEN_Tag16_GetTagType(subtag)) {
00076 
00077         case GWEN_CRYPTHEAD_TLV_KEYNAME:
00078           ch->keyName=(char*)malloc(subtagLen+1);
00079           memmove(ch->keyName, subtagPtr, subtagLen);
00080           ch->keyName[subtagLen]=0;
00081           break;
00082 
00083         case GWEN_CRYPTHEAD_TLV_KEYNUM:
00084           if (sscanf(subtagPtr, "%d", &i)==1)
00085             ch->keyNumber=i;
00086           break;
00087 
00088         case GWEN_CRYPTHEAD_TLV_KEYVER:
00089           if (sscanf(subtagPtr, "%d", &i)==1)
00090             ch->keyVersion=i;
00091           break;
00092 
00093         case GWEN_CRYPTHEAD_TLV_KEY:
00094           ch->pKey=(uint8_t*)malloc(subtagLen);
00095           assert(ch->pKey);
00096           memmove(ch->pKey, subtagPtr, subtagLen);
00097           ch->lKey=subtagLen;
00098           break;
00099 
00100         case GWEN_CRYPTHEAD_TLV_CRYPTPROFILE:
00101           if (sscanf(subtagPtr, "%d", &i)==1)
00102             ch->cryptProfile=i;
00103           break;
00104 
00105         default:
00106           DBG_WARN(GWEN_LOGDOMAIN, "Unknown tag %02x", GWEN_Tag16_GetTagType(subtag));
00107         }
00108       }
00109 
00110       sp+=GWEN_Tag16_GetTagSize(subtag);
00111       sl-=GWEN_Tag16_GetTagSize(subtag);
00112       GWEN_Tag16_free(subtag);
00113     } /* while */
00114 
00115     return ch;
00116   }
00117 
00118 }
00119 
00120 
00121 
00122 int GWEN_CryptHead_toBuffer(const GWEN_CRYPTHEAD *ch, GWEN_BUFFER *buf, uint8_t tagType) {
00123   char numbuf[32];
00124   uint32_t pos;
00125   uint8_t *p;
00126   uint32_t l;
00127 
00128   GWEN_Buffer_AppendByte(buf, tagType);
00129   pos=GWEN_Buffer_GetPos(buf);
00130   GWEN_Buffer_AppendByte(buf, 0);
00131   GWEN_Buffer_AppendByte(buf, 0);
00132 
00133   if (ch->keyName)
00134     GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_KEYNAME, ch->keyName, -1, buf);
00135 
00136   snprintf(numbuf, sizeof(numbuf), "%d", ch->keyNumber);
00137   GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_KEYNUM, numbuf, -1, buf);
00138 
00139   snprintf(numbuf, sizeof(numbuf), "%d", ch->keyVersion);
00140   GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_KEYVER, numbuf, -1, buf);
00141   if (ch->pKey && ch->lKey)
00142     GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_KEY,
00143                                 (const char*)ch->pKey,
00144                                 ch->lKey,
00145                                 buf);
00146 
00147   snprintf(numbuf, sizeof(numbuf), "%d", ch->cryptProfile);
00148   GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTHEAD_TLV_CRYPTPROFILE, numbuf, -1, buf);
00149 
00150   /* write size */
00151   l=GWEN_Buffer_GetPos(buf)-pos-2;
00152   p=(uint8_t*)GWEN_Buffer_GetStart(buf)+pos;
00153   *(p++)=l & 0xff;
00154   *p=(l>>8) & 0xff;
00155 
00156   return 0;
00157 }
00158 
00159 
00160 
00161 const char *GWEN_CryptHead_GetKeyName(const GWEN_CRYPTHEAD *ch) {
00162   assert(ch);
00163   return ch->keyName;
00164 }
00165 
00166 
00167 
00168 void GWEN_CryptHead_SetKeyName(GWEN_CRYPTHEAD *ch, const char *s) {
00169   assert(ch);
00170   free(ch->keyName);
00171   if (s) ch->keyName=strdup(s);
00172   else ch->keyName=NULL;
00173 }
00174 
00175 
00176 
00177 int GWEN_CryptHead_GetKeyNumber(const GWEN_CRYPTHEAD *ch) {
00178   assert(ch);
00179   return ch->keyNumber;
00180 }
00181 
00182 
00183 
00184 void GWEN_CryptHead_SetKeyNumber(GWEN_CRYPTHEAD *ch, int i) {
00185   assert(ch);
00186   ch->keyNumber=i;
00187 }
00188 
00189 
00190 
00191 int GWEN_CryptHead_GetKeyVersion(const GWEN_CRYPTHEAD *ch) {
00192   assert(ch);
00193   return ch->keyVersion;
00194 }
00195 
00196 
00197 
00198 void GWEN_CryptHead_SetKeyVersion(GWEN_CRYPTHEAD *ch, int i) {
00199   assert(ch);
00200   ch->keyVersion=i;
00201 }
00202 
00203 
00204 
00205 int GWEN_CryptHead_GetCryptProfile(const GWEN_CRYPTHEAD *ch) {
00206   assert(ch);
00207   return ch->cryptProfile;
00208 }
00209 
00210 
00211 
00212 void GWEN_CryptHead_SetCryptProfile(GWEN_CRYPTHEAD *ch, int i) {
00213   assert(ch);
00214   ch->cryptProfile=i;
00215 }
00216 
00217 
00218 
00219 const uint8_t *GWEN_CryptHead_GetKeyPtr(const GWEN_CRYPTHEAD *ch) {
00220   assert(ch);
00221   return ch->pKey;
00222 }
00223 
00224 
00225 
00226 uint32_t GWEN_CryptHead_GetKeyLen(const GWEN_CRYPTHEAD *ch) {
00227   assert(ch);
00228   return ch->lKey;
00229 }
00230 
00231 
00232 
00233 void GWEN_CryptHead_SetKey(GWEN_CRYPTHEAD *ch, const uint8_t *p, uint32_t l) {
00234   assert(ch);
00235   if (ch->pKey && ch->lKey)
00236     free(ch->pKey);
00237   if (p && l) {
00238     ch->pKey=(uint8_t*)malloc(l);
00239     assert(ch->pKey);
00240     memmove(ch->pKey, p, l);
00241     ch->lKey=l;
00242   }
00243   else {
00244     ch->pKey=NULL;
00245     ch->lKey=0;
00246   }
00247 }
00248 
00249 
00250 
00251 

Generated on Thu Aug 20 13:54:37 2009 for gwenhywfar by  doxygen 1.5.9