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