summaryrefslogtreecommitdiffstats
path: root/providers/implementations/storemgmt/file_store_der2obj.c
blob: 4f9053584231e8c7cb2a5e018ea48792aff38d33 (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
/*
 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

/*
 * This is a decoder that's completely internal to the 'file:' store
 * implementation.  Only code in file_store.c know about this one.  Because
 * of this close relationship, we can cut certain corners, such as making
 * assumptions about the "provider context", which is currently simply the
 * provider context that the file_store.c code operates within.
 *
 * All this does is to read DER from the input if it can, and passes it on
 * to the data callback as an object abstraction, leaving it to the callback
 * to figure out what it actually is.
 *
 * This MUST be made the last decoder in a chain, leaving it to other more
 * specialized decoders to recognise and process their stuff first.
 */

#include <openssl/core_dispatch.h>
#include <openssl/core_names.h>
#include <openssl/core_object.h>
#include <openssl/bio.h>
#include <openssl/buffer.h>
#include <openssl/err.h>
#include <openssl/asn1err.h>
#include <openssl/params.h>
#include "internal/asn1.h"
#include "prov/bio.h"
#include "file_store_local.h"

/*
 * newctx and freectx are not strictly necessary.  However, the method creator,
 * ossl_decoder_from_algorithm(), demands that they exist, so we make sure to
 * oblige.
 */

static OSSL_FUNC_decoder_newctx_fn der2obj_newctx;
static OSSL_FUNC_decoder_freectx_fn der2obj_freectx;

static void *der2obj_newctx(void *provctx)
{
    return provctx;
}

static void der2obj_freectx(void *vctx)
{
}

static OSSL_FUNC_decoder_gettable_params_fn der2obj_gettable_params;
static OSSL_FUNC_decoder_get_params_fn der2obj_get_params;
static OSSL_FUNC_decoder_decode_fn der2obj_decode;

static const OSSL_PARAM *der2obj_gettable_params(void *provctx)
{
    static const OSSL_PARAM gettables[] = {
        { OSSL_DECODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
        OSSL_PARAM_END,
    };

    return gettables;
}

static int der2obj_get_params(OSSL_PARAM params[])
{
    OSSL_PARAM *p;

    p = OSSL_PARAM_locate(params, OSSL_DECODER_PARAM_INPUT_TYPE);
    if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, "DER"))
        return 0;

    return 1;
}

static int der2obj_decode(void *provctx, OSSL_CORE_BIO *cin, int selection,
                          OSSL_CALLBACK *data_cb, void *data_cbarg,
                          OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
{
    /*
     * We're called from file_store.c, so we know that OSSL_CORE_BIO is a
     * BIO in this case.
     */
    BIO *in = ossl_bio_new_from_core_bio(provctx, cin);
    BUF_MEM *mem = NULL;
    int ok;

    if (in == NULL)
        return 0;

    ERR_set_mark();
    ok = (asn1_d2i_read_bio(in, &mem) >= 0);
    ERR_pop_to_mark();
    if (!ok && mem != NULL) {
        OPENSSL_free(mem->data);
        OPENSSL_free(mem);
        mem = NULL;
    }

    ok = 1;
    if (mem != NULL) {
        OSSL_PARAM params[3];
        int object_type = OSSL_OBJECT_UNKNOWN;

        params[0] =
            OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
        params[1] =
            OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_DATA,
                                              mem->data, mem->length);
        params[2] = OSSL_PARAM_construct_end();

        ok = data_cb(params, data_cbarg);
        OPENSSL_free(mem->data);
        OPENSSL_free(mem);
    }
    BIO_free(in);
    return ok;
}

static const OSSL_DISPATCH der_to_obj_decoder_functions[] = {
    { OSSL_FUNC_DECODER_NEWCTX, (void (*)(void))der2obj_newctx },
    { OSSL_FUNC_DECODER_FREECTX, (void (*)(void))der2obj_freectx },
    { OSSL_FUNC_DECODER_GETTABLE_PARAMS,
      (void (*)(void))der2obj_gettable_params },
    { OSSL_FUNC_DECODER_GET_PARAMS, (void (*)(void))der2obj_get_params },
    { OSSL_FUNC_DECODER_DECODE, (void (*)(void))der2obj_decode },
    { 0, NULL }
};

const OSSL_ALGORITHM ossl_der_to_obj_algorithm =
    { "obj", NULL, der_to_obj_decoder_functions };