1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
|
/*-------------------------------------------------------------*/
/***************************************************************
* A netwib_buf is the standard memory storage used in netwib. *
* *
* A netwib_buf points to an array (static or malloced) which *
* starts at 'first' and finishes at 'last'. *
* The memory contains user data between 'begin' and 'end'. *
* --------------------------------------------------- *
* | | data | | *
* |F| |B| |E| |L *
* --------------------------------------------------- *
* First Begin End Last *
* 0 x y totalsize *
* *
* Data between 'first and begin', and data between 'end and *
* last' may be corrupted when working on the buffer. To avoid *
* this, use netwib_buf_init_ext_buf and work on the new buffer*
***************************************************************/
/*-------------------------------------------------------------*/
typedef struct {
netwib_uint32 flags; /* see below */
netwib_data totalptr; /* ptr to first */
netwib_uint32 totalsize; /* last - first */
netwib_uint32 beginoffset; /* begin - first */
netwib_uint32 endoffset; /* end - first */
} netwib_buf;
typedef const netwib_buf netwib_constbuf;
/*-------------------------------------------------------------*/
/***************************************************************
* Field "flags" is a bit field indicating some information *
* about a netwib_buf. *
***************************************************************/
/* If totalptr points :
0x0001 : to an allocated memory
~0x0001 : to an external array (stack array or array which
does not need to be freed)
Those two modes corresponds to the two ways to initialize a
netwib_buf :
- netwib_buf_init_malloc : it internally allocates memory
and eventually reallocate it if more is needed. At the
end, function netwib_buf_close must be called to free
memory.
- netwib_buf_init_ext_xyz : buffer does not contain memory,
but points on an external array. At the end, function
netwib_buf_close does not need to be called to free
memory (no closing function is needed).
This flag should not be directly modified by user.
*/
#define NETWIB_BUF_FLAGS_ALLOC 0x00000001u
/* In the following case :
- flag NETWIB_BUF_FLAGS_ALLOC is unset, and
- there is no sufficient space in the array
we can :
0x0002 : allocate memory and store array content in it
(the array is no more used)
~0x0002 : return a space limitation error
This flag is very useful to optimize code. When we know in
most cases our code will need 80 bytes, we use an array
of 80 bytes. In the rare cases where it is not sufficient,
an allocated pointer is created. Like this for most
frequently encountered cases, there is no need to allocate.
Once allocated, it works like if it was allocated from beginning.
Once allocated, the external array is no more used.
Once allocated, flag NETWIB_BUF_FLAGS_ALLOC is automatically
set.
If array size is greater than 2k, it's not really advantageous
to use this flag, because copying 2k needs almost the same time
as a malloc and free.
This is generally used with netwib_buf_init_ext_arrayempty.
This flag defaults to false. It has to be explicitly set
by user.
*/
#define NETWIB_BUF_FLAGS_CANALLOC 0x00000002u
/* If functions are :
0x0004 : allowed to slide data (shrunk space between First
and Begin), when there is no sufficient space
between End and Last for appending
~0x0004 : not allowed to slide
This flag defaults to false. It has to be explicitly set
by user.
*/
#define NETWIB_BUF_FLAGS_CANSLIDE 0x00000004u
/* Sometimes, a buffer contains sensitive data such as a password,
so we want to do a memset on this data when it is no more needed.
This flags says that buffer contains sensitive data. Buffer will
be wiped with memset(.0.) during:
netwib_buf_close(),
netwib__buf_reinit(),
netwib__buf_erase(),
netwib_bufpool_buf_close(),
netwib_bufpool_close(),
and all functions using above ones.
It is developer's task to set this flag each time a buffer may
contain sensitive data.
Netwib supports a feature to transfer this flag (also known as
tainting in other languages). For example, when a sensitive
buffer is copied to another buffer, this one also becomes
sensitive.
Once buffer is closed, the flag is unset, so user has to set
it each time sensitive data is initialized. The closing
functions are:
netwib_buf_close(),
netwib_bufpool_buf_close(),
netwib_bufpool_close().
WARNING:
This feature was conceived for PASSWORDS or cryptographic KEYS
stored in a netwib_buf. It cannot help for other types such
as a netwib_string, a netwib_ring or a netwib_ip.
This feature was conceived for buffer manipulation functions,
such as netwib_buf_append_buf, and not in functions unrelated
to passwords or keys, such as netwib_pkt_... or
netwib_sniff_... When a function supports this feature, it is
indicated in its help comment before the prototype (.h file).
Perhaps one day, I will expand this feature to erase other kind
of data, but this is not the case currently.
To sum up, don't expect this feature to be the Solution for
your sensitive data management, but only a small step towards it.
END OF WARNING.
This flag defaults to false. It has to be explicitly set
by user.
*/
#define NETWIB_BUF_FLAGS_SENSITIVE 0x00000008u
/* data is sensitive, but readonly (such as a password stored
in a static string which cannot be wiped, but can be
transfered when buffer is copied)
*/
#define NETWIB_BUF_FLAGS_SENSITIVE_READONLY 0x00000010u
/* to transfer the sensitive flag to the second buffer */
#define netwib__buf_transfersensitive(pbuf1,pbuf2) { if ((pbuf1) != NULL && (pbuf2) != NULL) { if ((pbuf1)->flags & NETWIB_BUF_FLAGS_SENSITIVE) { (pbuf2)->flags |= NETWIB_BUF_FLAGS_SENSITIVE; } } }
/* to wipe a local array used in parallel of a buffer */
#define netwib__localarray_wipe(arr) netwib_c_memset((arr),0,sizeof(arr))
#define netwib__localarray_ifbuf_wipe(pbuf,arr) { if ((pbuf) != NULL && ((pbuf)->flags & NETWIB_BUF_FLAGS_SENSITIVE)) netwib__localarray_wipe(arr); }
/* number of used bits */
#define NETWIB_BUF_FLAGS_USEDBITS 5
/*-------------------------------------------------------------*/
/***************************************************************
* Type netwib_bufext is exactly the same as netwib_buf. It *
* permits to easily determine which kind of buffer is needed *
* by a function : *
* - Functions having an output parameter of type netwib_buf *
* must be called with the buffer previously initialized : *
* they will append data to it *
* - Functions having an output parameter of type *
* netwib_bufext will initialize it. *
* Example : *
* An IP4 packet might contain an IP4 option. There is no *
* need to allocate/copy data for this option, because it is *
* simply contained in the input packet (at offset *
* 20==sizeof(ip4hdr)). So in this case a netwib_bufext is *
* used. *
***************************************************************/
typedef netwib_buf netwib_bufext;
/*-------------------------------------------------------------*/
/* Name : netwib_buf_init_malloc
Description :
Initialize a buf. Its memory dynamically grows.
Input parameter(s) :
allocsize : allocated size. If 0, a
default value is used.
Input/output parameter(s) :
Output parameter(s) :
*pbuf : netwib_buf initialized
Normal return values :
NETWIB_ERR_OK : ok
*/
netwib_err netwib_buf_init_malloc(netwib_uint32 allocs,
netwib_buf *pbuf);
#define netwib_buf_init_mallocdefault(pbuf) netwib_buf_init_malloc(1024,pbuf)
/*-------------------------------------------------------------*/
/* Name : netwib_buf_init_ext_array
Description :
Initialize a buf. Its memory corresponds to an external
fixed size array.
Input parameter(s) :
array : external array
arraysize : external array size
beginoffset : offset of begin in this array
endoffset : offset of end in this array
Input/output parameter(s) :
Output parameter(s) :
*pbuf : netwib_bufext initialized
Normal return values :
NETWIB_ERR_OK : ok
*/
netwib_err netwib_buf_init_ext_array(netwib_constptr array,
netwib_uint32 arraysize,
netwib_uint32 beginoffset,
netwib_uint32 endoffset,
netwib_bufext *pbuf);
#define netwib_buf_init_ext_arrayempty(array,arraysize,pbuf) netwib_buf_init_ext_array(array,arraysize,0,0,pbuf)
#define netwib_buf_init_ext_arrayfilled(array,arraysize,pbuf) netwib_buf_init_ext_array(array,arraysize,0,arraysize,pbuf)
#define netwib_buf_init_ext_arraysizeofempty(array,pbuf) netwib_buf_init_ext_arrayempty(array,sizeof(array),pbuf)
#define netwib_buf_init_ext_arraysizeoffilled(array,pbuf) netwib_buf_init_ext_arrayfilled(array,sizeof(array),pbuf)
/* A buffer containing no data */
#define netwib_buf_init_ext_empty(pbuf) netwib_buf_init_ext_array(NULL,0,0,0,pbuf)
/*-------------------------------------------------------------*/
/* Name : netwib_buf_init_ext_storagearray
Description :
Initialize a buf. Its memory corresponds to an external
fixed size array or to nothing. When data is added, it may
be allocated. A call to netwib_buf_close() will have to be done.
Input parameter(s) :
array : external array
arraysize : external array size
beginoffset : offset of begin in this array
endoffset : offset of end in this array
Input/output parameter(s) :
Output parameter(s) :
*pbuf : netwib_bufext initialized
Normal return values :
NETWIB_ERR_OK : ok
*/
netwib_err netwib_buf_init_ext_storagearray(netwib_constptr array,
netwib_uint32 arraysize,
netwib_bufext *pbuf);
/* An empty buffer, allocated on first append */
#define netwib_buf_init_ext_storage(pbuf) netwib_buf_init_ext_storagearray(NULL,0,pbuf)
/* An empty array, allocated if not sufficiently big */
#define netwib_buf_init_ext_storagearraysizeof(array,pbuf) netwib_buf_init_ext_storagearray(array,sizeof(array),pbuf)
/*-------------------------------------------------------------*/
/* Name : netwib_buf_init_ext_buf
Description :
Initialize a buf. Its memory corresponds to an external
buffer.
Input parameter(s) :
*pbuf : netwib_buf containing data
Input/output parameter(s) :
Output parameter(s) :
*pbuf : netwib_bufext initialized
Normal return values :
NETWIB_ERR_OK : ok
This function supports NETWIB_BUF_FLAGS_SENSITIVE.
*/
netwib_err netwib_buf_init_ext_buf(netwib_constbuf *pbufin,
netwib_bufext *pbuf);
/*-------------------------------------------------------------*/
/* Name : netwib_buf_init_ext_string
Description :
Initialize a buf. Its memory corresponds to an external
preset string.
Input parameter(s) :
str : external string
Input/output parameter(s) :
Output parameter(s) :
*pbuf : netwib_bufext initialized
Normal return values :
NETWIB_ERR_OK : ok
*/
netwib_err netwib_buf_init_ext_string(netwib_conststring str,
netwib_bufext *pbuf);
/*-------------------------------------------------------------*/
/* Name : netwib_buf_append_xyz
Description :
Add data to a buf.
Input parameter(s) :
data : data to add
datasize : size of data to add
str : string to add
c : character to add
Input/output parameter(s) :
*pbuf : netwib_buf updated
Output parameter(s) :
Normal return values :
NETWIB_ERR_OK : ok
The netwib_buf_append_buf function supports NETWIB_BUF_FLAGS_SENSITIVE.
*/
netwib_err netwib_buf_append_data(netwib_constdata data,
netwib_uint32 datasize,
netwib_buf *pbuf);
netwib_err netwib_buf_append_string(netwib_conststring str,
netwib_buf *pbuf);
netwib_err netwib_buf_append_buf(netwib_constbuf *pbuftoappend,
netwib_buf *pbuf);
netwib_err netwib_buf_append_byte(netwib_byte b,
netwib_buf *pbuf);
/*-------------------------------------------------------------*/
/* Name : netwib_buf_prepend_buf
Description :
Prepend data to a buf.
Input parameter(s) :
Input/output parameter(s) :
*pbuf : netwib_buf updated
Output parameter(s) :
Normal return values :
NETWIB_ERR_OK : ok
Note :
Parameter canslide has no effect on this function (this is
logical).
This function supports NETWIB_BUF_FLAGS_SENSITIVE.
*/
netwib_err netwib_buf_prepend_buf(netwib_constbuf *pbuftoprepend,
netwib_buf *pbuf);
/*-------------------------------------------------------------*/
/* Name : netwib_buf_ref_string
Description :
Get pointers to data stored in buf (between begin and end).
If buffer internal storage does not end with '\0',
a new one is added. So, we can have NETWIB_ERR_DATANOSPACE
if there is no room for the '\0'.
Input parameter(s) :
*pbuf : buffer
Input/output parameter(s) :
Output parameter(s) :
*pstr : pointer to start of string
Normal return values :
NETWIB_ERR_OK : ok
NETWIB_ERR_DATANOSPACE : no room for the '\0'
*/
netwib_err netwib_buf_ref_string(netwib_buf *pbuf,
netwib_string *pstr);
/*-------------------------------------------------------------*/
/* Name : netwib_constbuf_ref_string
Description :
Some as netwib_buf_ref_string except it does not modify
the buffer to add the '\0'.
Input parameter(s) :
*pbuf : buffer
Input/output parameter(s) :
Output parameter(s) :
*pstr : pointer to start of string
Normal return values :
NETWIB_ERR_OK : ok
NETWIB_ERR_DATANOSPACE : the char after endoffset is not a '\0'
*/
netwib_err netwib_constbuf_ref_string(netwib_constbuf *pbuf,
netwib_string *pstr);
/* to use at begin of a function to manage a local buffer */
#define netwib__constbuf_ref_string(pbuf, str, bufstorage, func) { netwib_err bufstorageret; bufstorageret = netwib_constbuf_ref_string(pbuf, &str); if (bufstorageret != NETWIB_ERR_OK) { if (bufstorageret == NETWIB_ERR_DATANOSPACE) { netwib_data bufstoragearray[512]; netwib_buf bufstorage; netwib_er(netwib_buf_init_ext_storagearraysizeof(bufstoragearray, &bufstorage)); netwib_er(netwib_buf_append_buf(pbuf, &bufstorage)); netwib_er(netwib_buf_append_byte(0, &bufstorage)); bufstorage.endoffset--; bufstorageret = func; netwib_er(netwib_buf_close(&bufstorage)); } return(bufstorageret); } }
/*-------------------------------------------------------------*/
/* Name : netwib_buf_wantspace
Description :
Request space in a buffer (from end to last).
When buffer is initialized as malloced memory, it is
possible to obtain unlimited space. Otherwise, we cannot
obtain more space than array size (unless flag
NETWIB_BUF_FLAGS_CANALLOC is set).
Input parameter(s) :
wantedspace : wanted space
Input/output parameter(s) :
*pbuf : buffer
Output parameter(s) :
*pdata : pointer to end (endoffset)
Normal return values :
NETWIB_ERR_OK : ok
NETWIB_ERR_DATANOSPACE : there is less than wantedspace
*/
netwib_err netwib_buf_wantspace(netwib_buf *pbuf,
netwib_uint32 wantedspace,
netwib_data *pdata);
/*-------------------------------------------------------------*/
/* Name : netwib_buf_wishspace
Description :
Request space in a buffer (from end to last).
When buffer is initialized as malloced memory, it is
possible to obtain unlimited space. Otherwise, we cannot
obtain more space than array size (unless flag
NETWIB_BUF_FLAGS_CANALLOC is set).
Input parameter(s) :
wantedspace : wanted space
Input/output parameter(s) :
*pbuf : buffer
Output parameter(s) :
*pdata : pointer to end (endoffset)
*pobtainedspace : obtained space (from end to last)
Normal return values :
NETWIB_ERR_OK : ok
*/
netwib_err netwib_buf_wishspace(netwib_buf *pbuf,
netwib_uint32 wantedspace,
netwib_data *pdata,
netwib_uint32 *pobtainedspace);
/*-------------------------------------------------------------*/
/* Name : netwib_buf_cmp
Description :
Compare two netwib_buf.
Input parameter(s) :
*pbuf1 : netwib_buf to compare with buf2
*pbuf2 : netwib_buf to compare with buf1
Input/output parameter(s) :
Output parameter(s) :
*pcmp :
NETWIB_CMP_LT : buf1<buf2
NETWIB_CMP_EQ : if buf1 and buf2 are equal
NETWIB_CMP_GT : buf1>buf2
Normal return values :
NETWIB_ERR_OK : ok
*/
netwib_err netwib_buf_cmp(netwib_constbuf *pbuf1,
netwib_constbuf *pbuf2,
netwib_cmp *pcmp);
/* ignore case */
netwib_err netwib_buf_casecmp(netwib_constbuf *pbuf1,
netwib_constbuf *pbuf2,
netwib_cmp *pcmp);
/* compare to a string */
netwib_err netwib_buf_cmp_string(netwib_constbuf *pbuf1,
netwib_conststring string2,
netwib_cmp *pcmp);
netwib_err netwib_buf_casecmp_string(netwib_constbuf *pbuf1,
netwib_conststring string2,
netwib_cmp *pcmp);
/*-------------------------------------------------------------*/
/* Name : netwib_buf_shift
Description :
Shift data in a buf.
Input parameter(s) :
offset : offset
truncbegend : truncate on begin/end edges (otherwise,
truncate only on first/last edges)
Input/output parameter(s) :
*pbuf : netwib_buf updated
Output parameter(s) :
Normal return values :
NETWIB_ERR_OK : ok
Examples :
buf contains " data "
shift(buf,-1,0) => " data "
shift(buf,-1,1) => " ata " (d is truncated)
shift(buf,+1,0) => " data "
shift(buf,+4,0) => " data" (if buffer is malloced)
shift(buf,+4,0) => " da" (if buffer is external)
shift(buf,+1,1) => " dat " (a is truncated)
Note :
Flag NETWIB_BUF_FLAGS_CANSLIDE has no effect on this
function (this is logical).
*/
netwib_err netwib_buf_shift(netwib_buf *pbuf,
netwib_int32 offset,
netwib_bool truncbegend);
/*-------------------------------------------------------------*/
/* Name : netwib_buf_close
Description :
Close buf, eventually freeing data it contains.
Input parameter(s) :
Input/output parameter(s) :
*pbuf : buffer closed
Output parameter(s) :
Normal return values :
NETWIB_ERR_OK : ok
Note :
This function is only needed for a buffer initialized
with netwib_buf_init_malloc, or if flag
NETWIB_BUF_FLAGS_CANALLOC is set.
*/
netwib_err netwib_buf_close(netwib_buf *pbuf);
/*-------------------------------------------------------------*/
/* Frequently needed defines */
#define netwib__buf_reinit(pbuf) { (pbuf)->beginoffset = 0; (pbuf)->endoffset = 0; if (((pbuf)->flags & NETWIB_BUF_FLAGS_SENSITIVE) && !((pbuf)->flags & NETWIB_BUF_FLAGS_SENSITIVE_READONLY)) { netwib_c_memset((pbuf)->totalptr, 0, (pbuf)->totalsize); } }
#define netwib__buf_erase(pbuf) { if (((pbuf)->flags & NETWIB_BUF_FLAGS_SENSITIVE) && !((pbuf)->flags & NETWIB_BUF_FLAGS_SENSITIVE_READONLY)) { netwib_c_memset((pbuf)->totalptr+(pbuf)->beginoffset, 0, (pbuf)->endoffset-(pbuf)->beginoffset); } (pbuf)->endoffset = (pbuf)->beginoffset; }
#define netwib__buf_ref_data_ptr(pbuf) ((pbuf)->totalptr + (pbuf)->beginoffset)
#define netwib__buf_ref_data_size(pbuf) ((pbuf)->endoffset -(pbuf)->beginoffset)
#define netwib__buf_ref_data_sizenull(pbuf) ((pbuf)!=NULL?netwib__buf_ref_data_size(pbuf):0)
|