summaryrefslogtreecommitdiffstats
path: root/src/lib/certmap/sss_cert_digest_crypto.c
blob: e2118f5ef49352db02a4db7435270ac136fc44c6 (plain)
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
/*
   SSSD - digest functions - OpenSSL version
   The calls defined here should be usable outside of SSSD as well, e.g. in
   libsss_certmap.

   Copyright (C) Sumit Bose <sbose@redhat.com> 2022

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"

#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <stdbool.h>
#include <talloc.h>
#include <openssl/opensslv.h>
#include <openssl/objects.h>
#include <openssl/evp.h>

#include "lib/certmap/sss_certmap_int.h"

#if OPENSSL_VERSION_NUMBER < 0x30000000L
#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
#define EVP_MD_free(ctx)
#define EVP_MD_fetch(ctx, algorithm, properties) discard_const(EVP_get_digestbyname(algorithm))
#endif

#define DATA_STEP_SIZE 30

struct get_digest_data {
    const char **list;
    size_t size;
    size_t idx;
    int error;
};

static void get_digest_helper(const OBJ_NAME *name, void *arg)
{
    struct get_digest_data *data = (struct get_digest_data *) arg;
    EVP_MD *md = NULL;

    if (data->error != 0) {
        return;
    }

    /* All digest names are expected to start with a lower case letter. For
     * compatibility(?) with older version the digest list might contain some
     * RSA based signature algorithms, e.g. RSA-SHA1, which should be ignored
     * here. */
    if (name == NULL || name->name == NULL || islower(*name->name) == 0
        || strstr(name->name, "RSA") != NULL
        || strstr(name->name, "rsa") != NULL) {
        return;
    }

    /* check if digest can be fetched. */
    md = EVP_MD_fetch(NULL, name->name, NULL);
    if (md == NULL) {
        return;
    }
    EVP_MD_free(md);

    data->list[data->idx] = talloc_strdup(data->list, name->name);
    if (data->list[data->idx] == NULL) {
        data->error = ENOMEM;
        return;
    }

    data->idx++;
    if (data->idx == data->size - 1) {
        data->size += DATA_STEP_SIZE;
        data->list = talloc_realloc(data->list, data->list,
                                    const char *, data->size);
        if (data->list == NULL) {
            data->error = ENOMEM;
            return;
        }
    }
    data->list[data->idx] = NULL;
}


int get_digest_list(TALLOC_CTX *mem_ctx, const char ***digest_list)
{
    struct get_digest_data data = { 0 };

    data.size = DATA_STEP_SIZE;
    data.list = talloc_array(mem_ctx, const char *, data.size);
    if (data.list == NULL) {
        return ENOMEM;
    }

    OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS \
                        | OPENSSL_INIT_ADD_ALL_DIGESTS \
                        | OPENSSL_INIT_LOAD_CONFIG, NULL);

    OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, get_digest_helper, &data);
    if (data.error != 0) {
        talloc_free(data.list);
        return data.error;
    }

    *digest_list = data.list;

    return 0;
}

int get_hash(TALLOC_CTX *mem_ctx, const uint8_t *blob, size_t blob_size,
             const char *digest, bool upper, bool colon, bool reverse,
             char **out)
{
    int ret;
    EVP_MD *md = NULL;
    unsigned char md_value[EVP_MAX_MD_SIZE];
    unsigned int md_len;
    char *tmp_str = NULL;

    md = EVP_MD_fetch(NULL, digest, NULL);
    if (md == NULL) {
        return EINVAL;
    }

    ret = EVP_Digest(blob, blob_size, md_value, &md_len, md, NULL);
    if (ret != 1) {
        ret = EIO;
        goto done;
    }

    ret = bin_to_hex(mem_ctx, upper, colon, reverse, md_value, md_len,
                     &tmp_str);
    if (ret != 0) {
        goto done;
    }

    *out = tmp_str;
    ret = 0;

done:
    if (ret != 0) {
        talloc_free(tmp_str);
    }
    EVP_MD_free(md);

    return ret;
}