Coverage Report

Created: 2025-08-28 07:07

/src/openssl35/crypto/asn1/p8_pkey.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2025 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
#include <stdio.h>
11
#include "internal/cryptlib.h"
12
#include <openssl/asn1t.h>
13
#include <openssl/x509.h>
14
#include "crypto/x509.h"
15
16
/* Minor tweak to operation: zero private key data */
17
static int pkey_cb(int operation, ASN1_VALUE **pval, const ASN1_ITEM *it,
18
                   void *exarg)
19
3.15M
{
20
3.15M
    PKCS8_PRIV_KEY_INFO *key;
21
3.15M
    int version;
22
23
3.15M
    switch (operation) {
24
620k
    case ASN1_OP_FREE_PRE:
25
        /* The structure is still valid during ASN1_OP_FREE_PRE */
26
620k
        key = (PKCS8_PRIV_KEY_INFO *)*pval;
27
620k
        if (key->pkey)
28
620k
            OPENSSL_cleanse(key->pkey->data, key->pkey->length);
29
620k
        break;
30
39.5k
    case ASN1_OP_D2I_POST:
31
        /* Insist on a valid version now that the structure is decoded */
32
39.5k
        key = (PKCS8_PRIV_KEY_INFO *)*pval;
33
39.5k
        version = ASN1_INTEGER_get(key->version);
34
39.5k
        if (version < 0 || version > 1)
35
17.9k
            return 0;
36
21.5k
        if (version == 0 && key->kpub != NULL)
37
188
            return 0;
38
21.3k
        break;
39
3.15M
    }
40
3.13M
    return 1;
41
3.15M
}
42
43
ASN1_SEQUENCE_cb(PKCS8_PRIV_KEY_INFO, pkey_cb) = {
44
        ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, version, ASN1_INTEGER),
45
        ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkeyalg, X509_ALGOR),
46
        ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkey, ASN1_OCTET_STRING),
47
        ASN1_IMP_SET_OF_OPT(PKCS8_PRIV_KEY_INFO, attributes, X509_ATTRIBUTE, 0),
48
        ASN1_IMP_OPT(PKCS8_PRIV_KEY_INFO, kpub, ASN1_BIT_STRING, 1)
49
} ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
50
51
IMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO)
52
53
int PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj,
54
                    int version,
55
                    int ptype, void *pval, unsigned char *penc, int penclen)
56
37
{
57
37
    if (version >= 0) {
58
        /* We only support PKCS#8 v1 (0) and v2 (1). */
59
37
        if (version > 1)
60
0
            return 0;
61
37
        if (!ASN1_INTEGER_set(priv->version, version))
62
0
            return 0;
63
37
    }
64
37
    if (!X509_ALGOR_set0(priv->pkeyalg, aobj, ptype, pval))
65
0
        return 0;
66
37
    if (penc)
67
37
        ASN1_STRING_set0(priv->pkey, penc, penclen);
68
37
    return 1;
69
37
}
70
71
int PKCS8_pkey_get0(const ASN1_OBJECT **ppkalg,
72
                    const unsigned char **pk, int *ppklen,
73
                    const X509_ALGOR **pa, const PKCS8_PRIV_KEY_INFO *p8)
74
133k
{
75
133k
    if (ppkalg)
76
4.20k
        *ppkalg = p8->pkeyalg->algorithm;
77
133k
    if (pk) {
78
21.2k
        *pk = ASN1_STRING_get0_data(p8->pkey);
79
21.2k
        *ppklen = ASN1_STRING_length(p8->pkey);
80
21.2k
    }
81
133k
    if (pa)
82
129k
        *pa = p8->pkeyalg;
83
133k
    return 1;
84
133k
}
85
86
const STACK_OF(X509_ATTRIBUTE) *
87
PKCS8_pkey_get0_attrs(const PKCS8_PRIV_KEY_INFO *p8)
88
0
{
89
0
    return p8->attributes;
90
0
}
91
92
int PKCS8_pkey_add1_attr_by_NID(PKCS8_PRIV_KEY_INFO *p8, int nid, int type,
93
                                const unsigned char *bytes, int len)
94
0
{
95
0
    if (X509at_add1_attr_by_NID(&p8->attributes, nid, type, bytes, len) != NULL)
96
0
        return 1;
97
0
    return 0;
98
0
}
99
100
int PKCS8_pkey_add1_attr_by_OBJ(PKCS8_PRIV_KEY_INFO *p8, const ASN1_OBJECT *obj, int type,
101
                                const unsigned char *bytes, int len)
102
0
{
103
0
    return (X509at_add1_attr_by_OBJ(&p8->attributes, obj, type, bytes, len) != NULL);
104
0
}
105
106
int PKCS8_pkey_add1_attr(PKCS8_PRIV_KEY_INFO *p8, X509_ATTRIBUTE *attr)
107
0
{
108
0
    return (X509at_add1_attr(&p8->attributes, attr) != NULL);
109
0
}