Coverage Report

Created: 2026-03-01 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opensc/openpace/src/ri_lib.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2011-2012 Dominik Oepen, Frank Morgner and Paul Wilhelm
3
 *
4
 * This file is part of OpenPACE.
5
 *
6
 * OpenPACE is free software: you can redistribute it and/or modify it under
7
 * the terms of the GNU General Public License as published by the Free
8
 * Software Foundation, either version 3 of the License, or (at your option)
9
 * any later version.
10
 *
11
 * OpenPACE is distributed in the hope that it will be useful, but WITHOUT ANY
12
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14
 * details.
15
 *
16
 * You should have received a copy of the GNU General Public License along with
17
 * OpenPACE.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * Additional permission under GNU GPL version 3 section 7
20
 *
21
 * If you modify this Program, or any covered work, by linking or combining it
22
 * with OpenSSL (or a modified version of that library), containing
23
 * parts covered by the terms of OpenSSL's license, the licensors of
24
 * this Program grant you additional permission to convey the resulting work.
25
 * Corresponding Source for a non-source form of such a combination shall include
26
 * the source code for the parts of OpenSSL used as well as that of the
27
 * covered work.
28
 *
29
 * If you modify this Program, or any covered work, by linking or combining it
30
 * with OpenSC (or a modified version of that library), containing
31
 * parts covered by the terms of OpenSC's license, the licensors of
32
 * this Program grant you additional permission to convey the resulting work. 
33
 * Corresponding Source for a non-source form of such a combination shall include
34
 * the source code for the parts of OpenSC used as well as that of the
35
 * covered work.
36
 */
37
38
/**
39
 * @file ri_lib.c
40
 * @brief Data management functions
41
 *
42
 * @author Frank Morgner <frankmorgner@gmail.com>
43
 * @author Dominik Oepen <oepen@informatik.hu-berlin.de>
44
 * @author Paul Wilhelm  <wilhelm@math.hu-berlin.de>
45
 */
46
47
#ifdef HAVE_CONFIG_H
48
#include "config.h"
49
#endif
50
51
#include "eac_dh.h"
52
#include "eac_ecdh.h"
53
#include "eac_err.h"
54
#include "eac_lib.h"
55
#include "eac_util.h"
56
#include "ssl_compat.h"
57
#include <eac/ri.h>
58
#include <openssl/buffer.h>
59
#include <openssl/evp.h>
60
#include <openssl/objects.h>
61
#include <string.h>
62
63
void
64
RI_CTX_clear_free(RI_CTX * ctx)
65
0
{
66
0
    if (ctx) {
67
0
        if (ctx->static_key)
68
0
            EVP_PKEY_free(ctx->static_key);
69
0
        OPENSSL_free(ctx);
70
0
    }
71
0
}
72
73
RI_CTX *
74
RI_CTX_new(void)
75
0
{
76
0
    RI_CTX *out = NULL;
77
78
0
    out = (RI_CTX *)OPENSSL_zalloc(sizeof(RI_CTX));
79
0
    check(out, "Out of memory");
80
81
0
    out->static_key = EVP_PKEY_new();
82
0
    check(out->static_key, "Failed to create keypair for restricted identification");
83
84
0
    return out;
85
86
0
err:
87
0
    if (out) {
88
0
        if (out->static_key)
89
0
            EVP_PKEY_free(out->static_key);
90
0
        OPENSSL_free(out);
91
0
    }
92
93
0
    return NULL;
94
0
}
95
96
int
97
RI_CTX_set_protocol(RI_CTX * ctx, int protocol)
98
0
{
99
0
    if (!ctx) {
100
0
        log_err("Invalid arguments");
101
0
        return 0;
102
0
    }
103
104
0
    if (protocol == NID_id_RI_ECDH_SHA_1) {
105
0
        ctx->md = EVP_sha1();
106
0
        ctx->compute_key = ecdh_compute_key;
107
0
        ctx->generate_key = ecdh_generate_key;
108
109
0
    } else if (protocol == NID_id_RI_ECDH_SHA_224) {
110
0
        ctx->md = EVP_sha224();
111
0
        ctx->compute_key = ecdh_compute_key;
112
0
        ctx->generate_key = ecdh_generate_key;
113
114
0
    } else if (protocol == NID_id_RI_ECDH_SHA_256) {
115
0
        ctx->md = EVP_sha256();
116
0
        ctx->compute_key = ecdh_compute_key;
117
0
        ctx->generate_key = ecdh_generate_key;
118
119
0
    } else if (protocol == NID_id_RI_ECDH_SHA_384) {
120
0
        ctx->md = EVP_sha384();
121
0
        ctx->compute_key = ecdh_compute_key;
122
0
        ctx->generate_key = ecdh_generate_key;
123
124
0
    } else if (protocol == NID_id_RI_ECDH_SHA_512) {
125
0
        ctx->md = EVP_sha512();
126
0
        ctx->compute_key = ecdh_compute_key;
127
0
        ctx->generate_key = ecdh_generate_key;
128
129
0
    } else if (protocol == NID_id_RI_DH_SHA_1) {
130
0
        ctx->md = EVP_sha1();
131
0
        ctx->compute_key = dh_compute_key;
132
0
        ctx->generate_key = dh_generate_key;
133
134
0
    } else if (protocol == NID_id_RI_DH_SHA_224) {
135
0
        ctx->md = EVP_sha224();
136
0
        ctx->compute_key = dh_compute_key;
137
0
        ctx->generate_key = dh_generate_key;
138
139
0
    } else if (protocol == NID_id_RI_DH_SHA_256) {
140
0
        ctx->md = EVP_sha256();
141
0
        ctx->compute_key = dh_compute_key;
142
0
        ctx->generate_key = dh_generate_key;
143
144
0
    } else if (protocol == NID_id_RI_DH_SHA_384) {
145
0
        ctx->md = EVP_sha384();
146
0
        ctx->compute_key = dh_compute_key;
147
0
        ctx->generate_key = dh_generate_key;
148
149
0
    } else if (protocol == NID_id_RI_DH_SHA_512) {
150
0
        ctx->md = EVP_sha512();
151
0
        ctx->compute_key = dh_compute_key;
152
0
        ctx->generate_key = dh_generate_key;
153
154
0
    } else {
155
0
        log_err("Unknown object identifier");
156
0
        return 0;
157
0
    }
158
0
    ctx->protocol = protocol;
159
160
0
    return 1;
161
0
}