• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stefanberger / libtpms / 2698

pending completion
2698

Pull #353

travis-ci-com

web-flow
Merge a8cae3f5d into d08b929de
Pull Request #353: Travis: Update from bionic to focal and from xenial to bionic

33875 of 43923 relevant lines covered (77.12%)

93992.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.67
/src/tpm12/tpm_sizedbuffer.c
1
/********************************************************************************/
2
/*                                                                              */
3
/*                        TPM Sized Buffer Handler                              */
4
/*                           Written by Ken Goldman                             */
5
/*                     IBM Thomas J. Watson Research Center                     */
6
/*            $Id: tpm_sizedbuffer.c 4071 2010-04-29 19:26:45Z kgoldman $       */
7
/*                                                                              */
8
/* (c) Copyright IBM Corporation 2006, 2010.                                        */
9
/*                                                                                */
10
/* All rights reserved.                                                                */
11
/*                                                                                 */
12
/* Redistribution and use in source and binary forms, with or without                */
13
/* modification, are permitted provided that the following conditions are        */
14
/* met:                                                                                */
15
/*                                                                                 */
16
/* Redistributions of source code must retain the above copyright notice,        */
17
/* this list of conditions and the following disclaimer.                        */
18
/*                                                                                 */
19
/* Redistributions in binary form must reproduce the above copyright                */
20
/* notice, this list of conditions and the following disclaimer in the                */
21
/* documentation and/or other materials provided with the distribution.                */
22
/*                                                                                 */
23
/* Neither the names of the IBM Corporation nor the names of its                */
24
/* contributors may be used to endorse or promote products derived from                */
25
/* this software without specific prior written permission.                        */
26
/*                                                                                 */
27
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS                */
28
/* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT                */
29
/* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR        */
30
/* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT                */
31
/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,        */
32
/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT                */
33
/* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,        */
34
/* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY        */
35
/* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT                */
36
/* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE        */
37
/* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                */
38
/********************************************************************************/
39

40
#include <stdio.h>
41
#include <stdlib.h>
42
#include <string.h>
43

44
#include "tpm_cryptoh.h"
45
#include "tpm_debug.h"
46
#include "tpm_error.h"
47
#include "tpm_memory.h"
48
#include "tpm_process.h"
49
#include "tpm_types.h"
50

51
#include "tpm_sizedbuffer.h"
52

53
void TPM_SizedBuffer_Init(TPM_SIZED_BUFFER *tpm_sized_buffer)
68,205✔
54
{
55
    tpm_sized_buffer->size = 0;
68,205✔
56
    tpm_sized_buffer->buffer = NULL;
68,205✔
57
    return;
68,205✔
58
}
59

60
/* TPM_SizedBuffer_Load() allocates and sets a sized buffer from a stream.  A sized buffer structure
61
   has two members:
62

63
   - 4 bytes size
64
   - pointer to array of 'size'
65

66
   This structure is typically a cast from a subset of a larger TPM structure.  Two members - a 4
67
   bytes size followed by a 4 bytes pointer to the data is a common TPM structure idiom.
68

69
   This function correctly handles a 'size' of 0.
70

71
   Call TPM_SizedBuffer_Init() before first use
72
   Call TPM_SizedBuffer_Delete() after use
73
*/
74

75
TPM_RESULT TPM_SizedBuffer_Load(TPM_SIZED_BUFFER *tpm_sized_buffer,     /* result */
12,449✔
76
                                unsigned char **stream,                /* pointer to next parameter */
77
                                uint32_t *stream_size)                /* stream size left */
78
{
79
    TPM_RESULT  rc = 0;
12,449✔
80
    
81
    printf("  TPM_SizedBuffer_Load:\n");
12,449✔
82
    if (rc == 0) {
12,449✔
83
        rc = TPM_Load32(&(tpm_sized_buffer->size), stream, stream_size);
12,449✔
84
    }
85
    /* if the size is not 0 */
86
    if ((rc == 0) && (tpm_sized_buffer->size > 0)) {
12,449✔
87
        /* allocate memory for the buffer */
88
        if (rc == 0) {
8,682✔
89
            rc = TPM_Malloc(&(tpm_sized_buffer->buffer), tpm_sized_buffer->size);
8,682✔
90
        }
91
        /* copy the buffer */
92
        if (rc == 0) {
8,682✔
93
            rc = TPM_Loadn(tpm_sized_buffer->buffer, tpm_sized_buffer->size, stream, stream_size);
8,682✔
94
        }
95
    }
96
    return rc;
12,449✔
97
}
98

99
/* TPM_SizedBuffer_Set() reallocs a sized buffer and copies 'size' bytes of 'data' into it.
100

101
   If the sized buffer already has data, the buffer is realloc'ed.
102

103
   This function correctly handles a 'size' of 0.
104
   
105
   Call TPM_SizedBuffer_Delete() to free the buffer
106
*/
107

108
TPM_RESULT TPM_SizedBuffer_Set(TPM_SIZED_BUFFER *tpm_sized_buffer,
10,660✔
109
                               uint32_t size,
110
                               const unsigned char *data)
111
{
112
    TPM_RESULT  rc = 0;
10,660✔
113
    
114
    printf("  TPM_SizedBuffer_Set:\n");
10,660✔
115
    /* allocate memory for the buffer, and copy the buffer */
116
    if (rc == 0) {
10,660✔
117
        if (size > 0) {
10,660✔
118
            rc = TPM_Realloc(&(tpm_sized_buffer->buffer),
7,990✔
119
                             size);
120
            if (rc == 0) {
7,990✔
121
                tpm_sized_buffer->size = size;
7,990✔
122
                memcpy(tpm_sized_buffer->buffer, data, size);
7,990✔
123
            }
124
        }
125
        /* if size is zero */
126
        else {
127
            TPM_SizedBuffer_Delete(tpm_sized_buffer);
2,670✔
128
        }
129
    }
130
    return rc;
10,660✔
131
}
132

133
/* TPM_SizedBuffer_SetFromStore() reallocs a sized buffer and copies 'sbuffer" data into it.
134

135
   This function correctly handles an 'sbuffer' of 0 length.
136
 */
137

138
TPM_RESULT TPM_SizedBuffer_SetFromStore(TPM_SIZED_BUFFER *tpm_sized_buffer,
6,013✔
139
                                        TPM_STORE_BUFFER *sbuffer)
140
{
141
    TPM_RESULT          rc = 0;
6,013✔
142
    const unsigned char *data;
6,013✔
143
    uint32_t              size;
6,013✔
144
    
145
    if (rc == 0) {
6,013✔
146
        /* get the stream and its size from the TPM_STORE_BUFFER */
147
        TPM_Sbuffer_Get(sbuffer, &data, &size);
6,013✔
148
        rc = TPM_SizedBuffer_Set(tpm_sized_buffer, size, data);
6,013✔
149
    }
150
    return rc;
6,013✔
151
}
152

153
/* TPM_SizedBuffer_SetStructure() serializes the structure 'tpmStructure' using the function
154
   'storeFunction', storing the result in a TPM_SIZED_BUFFER.
155
*/
156

157
TPM_RESULT TPM_SizedBuffer_SetStructure(TPM_SIZED_BUFFER *tpm_sized_buffer,
5,034✔
158
                                        void *tpmStructure,
159
                                        TPM_STORE_FUNCTION_T storeFunction)
160
{
161
    TPM_RESULT          rc = 0;
5,034✔
162
    TPM_STORE_BUFFER    sbuffer;        /* serialized tpmStructure */
5,034✔
163

164
    printf("  TPM_SizedBuffer_SetStructure:\n");
5,034✔
165
    TPM_Sbuffer_Init(&sbuffer);                         /* freed @1 */
5,034✔
166
    /* serialize the structure */
167
    if (rc == 0) {
5,034✔
168
        if (tpmStructure != NULL) {
5,034✔
169
            rc = storeFunction(&sbuffer, tpmStructure);
2,800✔
170
        }
171
    }
172
    /* copy to TPM_SIZED_BUFFER */
173
    if (rc == 0) {
2,800✔
174
        rc = TPM_SizedBuffer_SetFromStore(tpm_sized_buffer, &sbuffer);
5,034✔
175
    }
176
    TPM_Sbuffer_Delete(&sbuffer);                       /* @1 */
5,034✔
177
    return rc;
5,034✔
178
}
179

180
TPM_RESULT TPM_SizedBuffer_Copy(TPM_SIZED_BUFFER *tpm_sized_buffer_dest,
1,087✔
181
                                TPM_SIZED_BUFFER *tpm_sized_buffer_src)
182
{
183
    TPM_RESULT  rc = 0;
1,087✔
184
    rc = TPM_SizedBuffer_Set(tpm_sized_buffer_dest,
2,174✔
185
                             tpm_sized_buffer_src->size,
186
                             tpm_sized_buffer_src->buffer);
1,087✔
187
    return rc;
1,087✔
188
}
189

190

191
/* TPM_SizedBuffer_Store() serializes a TPM_SIZED_BUFFER into a TPM_STORE_BUFFER
192
 */
193

194
TPM_RESULT TPM_SizedBuffer_Store(TPM_STORE_BUFFER *sbuffer,
17,506✔
195
                                 const TPM_SIZED_BUFFER *tpm_sized_buffer)
196
{
197
    TPM_RESULT  rc = 0;
17,506✔
198

199
    printf("  TPM_SizedBuffer_Store:\n");
17,506✔
200
    /* append the size */
201
    if (rc == 0) {
17,506✔
202
        rc = TPM_Sbuffer_Append32(sbuffer, tpm_sized_buffer->size);
17,506✔
203
    }
204
    /* append the data */
205
    if (rc == 0) {
17,506✔
206
        rc = TPM_Sbuffer_Append(sbuffer, tpm_sized_buffer->buffer, tpm_sized_buffer->size);
17,506✔
207
    }
208
    return rc;
17,506✔
209
}
210

211
void TPM_SizedBuffer_Delete(TPM_SIZED_BUFFER *tpm_sized_buffer)
24,330✔
212
{
213
    printf("  TPM_SizedBuffer_Delete:\n");
24,330✔
214
    if (tpm_sized_buffer != NULL) {
24,330✔
215
        free(tpm_sized_buffer->buffer);
24,330✔
216
        TPM_SizedBuffer_Init(tpm_sized_buffer);
24,330✔
217
    }
218
    return;
24,330✔
219
}
220

221
/* TPM_SizedBuffer_Allocate() allocates 'size' bytes of memory and sets the TPM_SIZED_BUFFER
222
   members.
223

224
   The buffer data is not initialized.
225
*/
226

227
TPM_RESULT TPM_SizedBuffer_Allocate(TPM_SIZED_BUFFER *tpm_sized_buffer,
177✔
228
                                    uint32_t size)
229
{
230
    TPM_RESULT  rc = 0;
177✔
231

232
    printf("  TPM_SizedBuffer_Allocate: Size %u\n", size);
177✔
233
    tpm_sized_buffer->size = size;
177✔
234
    rc = TPM_Malloc(&(tpm_sized_buffer->buffer), size);
177✔
235
    return rc;
177✔
236
}
237

238
/* TPM_SizedBuffer_GetBool() converts from a TPM_SIZED_BUFFER to a TPM_BOOL.
239

240
   If the size does not indicate a TPM_BOOL, an error is returned.
241
*/
242

243
TPM_RESULT TPM_SizedBuffer_GetBool(TPM_BOOL *tpm_bool,
17✔
244
                                   TPM_SIZED_BUFFER *tpm_sized_buffer)
245
{
246
    TPM_RESULT rc = 0;
17✔
247
    
248
    if (tpm_sized_buffer->size == sizeof(TPM_BOOL)) {
17✔
249
        *tpm_bool = *(TPM_BOOL *)tpm_sized_buffer->buffer;
17✔
250
        printf("  TPM_SizedBuffer_GetBool: bool %02x\n", *tpm_bool);
17✔
251
    }
252
    else {
253
        printf("TPM_SizedBuffer_GetBool: Error, buffer size %08x is not a BOOL\n",
×
254
               tpm_sized_buffer->size);
×
255
        rc = TPM_BAD_PARAMETER;
×
256
    }
257
    return rc;
17✔
258
}
259

260
/* TPM_SizedBuffer_GetUint32() converts from a TPM_SIZED_BUFFER to a uint32_t.
261

262
   If the size does not indicate a uint32_t, an error is returned.
263
*/
264

265
TPM_RESULT TPM_SizedBuffer_GetUint32(uint32_t *uint32,
3✔
266
                                     TPM_SIZED_BUFFER *tpm_sized_buffer)
267
{
268
    TPM_RESULT rc = 0;
3✔
269
    unsigned char *stream;
3✔
270
    uint32_t stream_size;
3✔
271
    
272
    if (rc == 0) {
3✔
273
        if (tpm_sized_buffer->size != sizeof(uint32_t)) {
3✔
274
            printf("TPM_GetUint32: Error, buffer size %08x is not a uint32_t\n",
×
275
                   tpm_sized_buffer->size);
×
276
            rc = TPM_BAD_PARAMETER;
×
277
        }
278
    }
279
    if (rc == 0) {
×
280
        stream = tpm_sized_buffer->buffer;
3✔
281
        stream_size = tpm_sized_buffer->size;
3✔
282
        rc = TPM_Load32(uint32, &stream, &stream_size);
3✔
283
    }
284
    return rc;
3✔
285
}
286

287
/* TPM_SizedBuffer_Append32() appends a uint32_t to a TPM_SIZED_BUFFER
288

289
*/
290

291
TPM_RESULT TPM_SizedBuffer_Append32(TPM_SIZED_BUFFER *tpm_sized_buffer,
×
292
                                    uint32_t uint32)
293
{
294
    TPM_RESULT rc = 0;
×
295

296
    printf("  TPM_SizedBuffer_Append32: Current size %u uint32 %08x\n",
×
297
           tpm_sized_buffer->size, uint32);
×
298
    /* allocate space for another uint32_t */
299
    if (rc == 0) {
×
300
        rc = TPM_Realloc(&(tpm_sized_buffer->buffer),
×
301
                         tpm_sized_buffer->size + sizeof(uint32_t));
×
302
    }
303
    if (rc == 0) {
×
304
        uint32_t ndata = htonl(uint32);           /* convert to network byte order */
×
305
        memcpy(tpm_sized_buffer->buffer + tpm_sized_buffer->size, /* append at end */
×
306
               (char *)&ndata,                  /* cast safe after conversion */
307
               sizeof(uint32_t));
308
        tpm_sized_buffer->size += sizeof(uint32_t);
×
309
    }    
310
    return rc;
×
311
}
312

313
/* TPM_SizedBuffer_Remove32() removes the uint32_t with value from a TPM_SIZED_BUFFER
314

315
*/
316

317
TPM_RESULT TPM_SizedBuffer_Remove32(TPM_SIZED_BUFFER *tpm_sized_buffer,
×
318
                                    uint32_t uint32)
319
{
320
    TPM_RESULT                rc = 0;
×
321
    unsigned char        *stream;
×
322
    uint32_t                stream_size;
×
323
    uint32_t                bufferValue;
×
324
    TPM_BOOL                found = FALSE;
×
325
    unsigned char        *from;
×
326
    unsigned char        *to;
×
327
        
328
    printf("  TPM_SizedBuffer_Remove32: Current size %u uint32 %08x\n",
×
329
           tpm_sized_buffer->size, uint32);
×
330

331
    stream = tpm_sized_buffer->buffer;
×
332
    stream_size =  tpm_sized_buffer->size;
×
333
    
334
    /* search for the uint32 */
335
    while ((rc == 0) && (stream_size != 0) && !found) {
×
336
        /* get the next value */
337
        if (rc == 0) {
×
338
            rc = TPM_Load32(&bufferValue, &stream, &stream_size);
×
339
        }
340
        /* if the value is the one to be removed */
341
        if (rc == 0) {
×
342
            if (bufferValue == uint32) {
×
343
                found = TRUE;
×
344
                /* shift the reset of the buffer down by a uint32_t */
345
                for (from = stream, to = (stream - sizeof(uint32_t)) ;
×
346
                     /* go to the end of the buffer */
347
                     from < (tpm_sized_buffer->buffer + tpm_sized_buffer->size) ;
×
348
                     from++, to++) {
×
349
                    *to = *from;
×
350
                }
351
                /* adjust the size */
352
                tpm_sized_buffer->size -= sizeof(uint32_t);
×
353
            }
354
        }
355
    }
356
    if (!found) {
×
357
        printf("TPM_SizedBuffer_Remove32: Error, value not found\n");
×
358
        rc = TPM_BAD_HANDLE;
×
359
    }
360
    return rc;
×
361
}
362

363
/* TPM_SizedBuffer_Zero() overwrites all data in the buffer with zeros
364

365
 */
366

367
void TPM_SizedBuffer_Zero(TPM_SIZED_BUFFER *tpm_sized_buffer)
3,209✔
368
{
369
    printf("  TPM_SizedBuffer_Zero:\n");
3,209✔
370
    if (tpm_sized_buffer->buffer != NULL) {
3,209✔
371
        memset(tpm_sized_buffer->buffer, 0, tpm_sized_buffer->size);
3,077✔
372
    }
373
    return;
3,209✔
374
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc