Coverage Report

Created: 2025-07-11 06:29

/src/S2OPC/src/Common/crypto/sopc_secret_buffer.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Licensed to Systerel under one or more contributor license
3
 * agreements. See the NOTICE file distributed with this work
4
 * for additional information regarding copyright ownership.
5
 * Systerel licenses this file to you under the Apache
6
 * License, Version 2.0 (the "License"); you may not use this
7
 * file except in compliance with the License. You may obtain
8
 * a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied.  See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
20
#include <stddef.h>
21
#include <string.h>
22
23
#include "sopc_buffer.h"
24
#include "sopc_macros.h"
25
#include "sopc_mem_alloc.h"
26
#include "sopc_secret_buffer.h"
27
28
struct SOPC_SecretBuffer
29
{
30
    uint32_t len; // Mandatory
31
    uint8_t* buf;
32
};
33
34
SOPC_SecretBuffer* SOPC_SecretBuffer_NewFromExposedBuffer(const SOPC_ExposedBuffer* buf, uint32_t len)
35
0
{
36
0
    SOPC_SecretBuffer* sec = NULL;
37
38
0
    if (NULL != buf)
39
0
    {
40
0
        sec = SOPC_SecretBuffer_New(len);
41
0
        if (NULL != sec && NULL != sec->buf)
42
0
            memcpy(sec->buf, buf, len);
43
0
    }
44
45
0
    return sec;
46
0
}
47
48
SOPC_SecretBuffer* SOPC_SecretBuffer_NewFromFile(const char* path)
49
0
{
50
0
    SOPC_Buffer* contents = NULL;
51
52
0
    if (SOPC_Buffer_ReadFile(path, &contents) != SOPC_STATUS_OK)
53
0
    {
54
0
        return NULL;
55
0
    }
56
57
0
    SOPC_SecretBuffer* sec = SOPC_Calloc(1, sizeof(SOPC_SecretBuffer));
58
59
0
    if (sec == NULL)
60
0
    {
61
0
        SOPC_Buffer_Delete(contents);
62
0
        return NULL;
63
0
    }
64
65
    // "Steal" the data from the buffer. This is a bit on the hacky side, but
66
    // it allows reusing existing code.
67
0
    sec->len = contents->length;
68
0
    sec->buf = contents->data;
69
0
    contents->data = NULL;
70
0
    SOPC_Buffer_Delete(contents);
71
72
0
    return sec;
73
0
}
74
75
SOPC_SecretBuffer* SOPC_SecretBuffer_New(uint32_t len)
76
0
{
77
0
    SOPC_SecretBuffer* sec = NULL;
78
79
0
    if (0 != len)
80
0
    {
81
0
        sec = SOPC_Malloc(sizeof(SOPC_SecretBuffer));
82
0
        if (NULL != sec)
83
0
        {
84
0
            sec->len = len;
85
0
            sec->buf = SOPC_Malloc((size_t) len);
86
0
            if (NULL == sec->buf)
87
0
            {
88
0
                SOPC_Free(sec);
89
0
                sec = NULL;
90
0
            }
91
0
        }
92
0
    }
93
94
0
    return sec;
95
0
}
96
97
void SOPC_SecretBuffer_DeleteClear(SOPC_SecretBuffer* sec)
98
0
{
99
0
    if (NULL != sec)
100
0
    {
101
0
        if (sec->buf)
102
0
        {
103
0
            memset(sec->buf, 0, sec->len);
104
0
            SOPC_Free(sec->buf);
105
0
        }
106
0
        SOPC_Free(sec);
107
0
    }
108
0
}
109
110
uint32_t SOPC_SecretBuffer_GetLength(const SOPC_SecretBuffer* sec)
111
0
{
112
0
    if (NULL == sec)
113
0
        return 0;
114
0
    return sec->len;
115
0
}
116
117
const SOPC_ExposedBuffer* SOPC_SecretBuffer_Expose(const SOPC_SecretBuffer* sec)
118
0
{
119
0
    if (NULL != sec)
120
0
        return sec->buf;
121
0
    return NULL;
122
0
}
123
124
void SOPC_SecretBuffer_Unexpose(const SOPC_ExposedBuffer* buf, const SOPC_SecretBuffer* sec)
125
0
{
126
0
    SOPC_UNUSED_ARG(buf);
127
0
    SOPC_UNUSED_ARG(sec);
128
0
}
129
130
SOPC_ExposedBuffer* SOPC_SecretBuffer_ExposeModify(SOPC_SecretBuffer* sec)
131
0
{
132
0
    if (NULL != sec)
133
0
        return sec->buf;
134
0
    return NULL;
135
0
}
136
137
void SOPC_SecretBuffer_UnexposeModify(SOPC_ExposedBuffer* buf, SOPC_SecretBuffer* sec)
138
0
{
139
0
    SOPC_UNUSED_ARG(buf);
140
0
    SOPC_UNUSED_ARG(sec);
141
0
}