Coverage Report

Created: 2025-12-31 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl33/crypto/asn1/i2d_evp.c
Line
Count
Source
1
/*
2
 * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License 2.0 (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
/*
11
 * Low level APIs are deprecated for public use, but still ok for
12
 * internal use.
13
 */
14
#include "internal/deprecated.h"
15
16
#include <stdio.h>
17
#include "internal/cryptlib.h"
18
#include <openssl/evp.h>
19
#include <openssl/encoder.h>
20
#include <openssl/buffer.h>
21
#include <openssl/x509.h>
22
#include <openssl/rsa.h> /* For i2d_RSAPublicKey */
23
#include <openssl/dsa.h> /* For i2d_DSAPublicKey */
24
#include <openssl/ec.h> /* For i2o_ECPublicKey */
25
#include "crypto/asn1.h"
26
#include "crypto/evp.h"
27
28
struct type_and_structure_st {
29
    const char *output_type;
30
    const char *output_structure;
31
};
32
33
static int i2d_provided(const EVP_PKEY *a, int selection,
34
    const struct type_and_structure_st *output_info,
35
    unsigned char **pp)
36
9.59k
{
37
9.59k
    int ret;
38
39
9.59k
    for (ret = -1;
40
20.9k
        ret == -1 && output_info->output_type != NULL;
41
11.3k
        output_info++) {
42
        /*
43
         * The i2d_ calls don't take a boundary length for *pp.  However,
44
         * OSSL_ENCODER_to_data() needs one, so we make one up.  Because
45
         * OSSL_ENCODER_to_data() decrements this number by the amount of
46
         * bytes written, we need to calculate the length written further
47
         * down, when pp != NULL.
48
         */
49
11.3k
        size_t len = INT_MAX;
50
11.3k
        int pp_was_NULL = (pp == NULL || *pp == NULL);
51
11.3k
        OSSL_ENCODER_CTX *ctx;
52
53
11.3k
        ctx = OSSL_ENCODER_CTX_new_for_pkey(a, selection,
54
11.3k
            output_info->output_type,
55
11.3k
            output_info->output_structure,
56
11.3k
            NULL);
57
11.3k
        if (ctx == NULL)
58
0
            return -1;
59
11.3k
        if (OSSL_ENCODER_to_data(ctx, pp, &len)) {
60
8.15k
            if (pp_was_NULL)
61
8.15k
                ret = (int)len;
62
0
            else
63
0
                ret = INT_MAX - (int)len;
64
8.15k
        }
65
11.3k
        OSSL_ENCODER_CTX_free(ctx);
66
11.3k
    }
67
68
9.59k
    if (ret == -1)
69
9.59k
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_TYPE);
70
9.59k
    return ret;
71
9.59k
}
72
73
int i2d_KeyParams(const EVP_PKEY *a, unsigned char **pp)
74
0
{
75
0
    if (evp_pkey_is_provided(a)) {
76
0
        static const struct type_and_structure_st output_info[] = {
77
0
            { "DER", "type-specific" },
78
0
            {
79
0
                NULL,
80
0
            }
81
0
        };
82
83
0
        return i2d_provided(a, EVP_PKEY_KEY_PARAMETERS, output_info, pp);
84
0
    }
85
0
    if (a->ameth != NULL && a->ameth->param_encode != NULL)
86
0
        return a->ameth->param_encode(a, pp);
87
0
    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_TYPE);
88
0
    return -1;
89
0
}
90
91
int i2d_KeyParams_bio(BIO *bp, const EVP_PKEY *pkey)
92
0
{
93
0
    return ASN1_i2d_bio_of(EVP_PKEY, i2d_KeyParams, bp, pkey);
94
0
}
95
96
int i2d_PrivateKey(const EVP_PKEY *a, unsigned char **pp)
97
7.66k
{
98
7.66k
    if (evp_pkey_is_provided(a)) {
99
6.73k
        static const struct type_and_structure_st output_info[] = {
100
6.73k
            { "DER", "type-specific" },
101
6.73k
            { "DER", "PrivateKeyInfo" },
102
6.73k
            {
103
6.73k
                NULL,
104
6.73k
            }
105
6.73k
        };
106
107
6.73k
        return i2d_provided(a, EVP_PKEY_KEYPAIR, output_info, pp);
108
6.73k
    }
109
935
    if (a->ameth != NULL && a->ameth->old_priv_encode != NULL) {
110
917
        return a->ameth->old_priv_encode(a, pp);
111
917
    }
112
18
    if (a->ameth != NULL && a->ameth->priv_encode != NULL) {
113
18
        PKCS8_PRIV_KEY_INFO *p8 = EVP_PKEY2PKCS8(a);
114
18
        int ret = 0;
115
116
18
        if (p8 != NULL) {
117
18
            ret = i2d_PKCS8_PRIV_KEY_INFO(p8, pp);
118
18
            PKCS8_PRIV_KEY_INFO_free(p8);
119
18
        }
120
18
        return ret;
121
18
    }
122
18
    ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
123
0
    return -1;
124
18
}
125
126
int i2d_PublicKey(const EVP_PKEY *a, unsigned char **pp)
127
0
{
128
0
    if (evp_pkey_is_provided(a)) {
129
0
        static const struct type_and_structure_st output_info[] = {
130
0
            { "DER", "type-specific" },
131
0
            { "blob", NULL }, /* for EC */
132
0
            {
133
0
                NULL,
134
0
            }
135
0
        };
136
137
0
        return i2d_provided(a, EVP_PKEY_PUBLIC_KEY, output_info, pp);
138
0
    }
139
0
    switch (EVP_PKEY_get_base_id(a)) {
140
0
    case EVP_PKEY_RSA:
141
0
        return i2d_RSAPublicKey(EVP_PKEY_get0_RSA(a), pp);
142
0
#ifndef OPENSSL_NO_DSA
143
0
    case EVP_PKEY_DSA:
144
0
        return i2d_DSAPublicKey(EVP_PKEY_get0_DSA(a), pp);
145
0
#endif
146
0
#ifndef OPENSSL_NO_EC
147
0
    case EVP_PKEY_EC:
148
0
        return i2o_ECPublicKey(EVP_PKEY_get0_EC_KEY(a), pp);
149
0
#endif
150
0
    default:
151
        ERR_raise(ERR_LIB_ASN1, ASN1_R_UNSUPPORTED_PUBLIC_KEY_TYPE);
152
0
        return -1;
153
0
    }
154
0
}