Coverage Report

Created: 2025-06-13 06:58

/src/openssl30/crypto/asn1/p8_pkey.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 1999-2020 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
2.34M
{
20
    /* Since the structure must still be valid use ASN1_OP_FREE_PRE */
21
2.34M
    if (operation == ASN1_OP_FREE_PRE) {
22
465k
        PKCS8_PRIV_KEY_INFO *key = (PKCS8_PRIV_KEY_INFO *)*pval;
23
465k
        if (key->pkey)
24
465k
            OPENSSL_cleanse(key->pkey->data, key->pkey->length);
25
465k
    }
26
2.34M
    return 1;
27
2.34M
}
28
29
ASN1_SEQUENCE_cb(PKCS8_PRIV_KEY_INFO, pkey_cb) = {
30
        ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, version, ASN1_INTEGER),
31
        ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkeyalg, X509_ALGOR),
32
        ASN1_SIMPLE(PKCS8_PRIV_KEY_INFO, pkey, ASN1_OCTET_STRING),
33
        ASN1_IMP_SET_OF_OPT(PKCS8_PRIV_KEY_INFO, attributes, X509_ATTRIBUTE, 0)
34
} ASN1_SEQUENCE_END_cb(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO)
35
36
IMPLEMENT_ASN1_FUNCTIONS(PKCS8_PRIV_KEY_INFO)
37
38
int PKCS8_pkey_set0(PKCS8_PRIV_KEY_INFO *priv, ASN1_OBJECT *aobj,
39
                    int version,
40
                    int ptype, void *pval, unsigned char *penc, int penclen)
41
94
{
42
94
    if (version >= 0) {
43
94
        if (!ASN1_INTEGER_set(priv->version, version))
44
0
            return 0;
45
94
    }
46
94
    if (!X509_ALGOR_set0(priv->pkeyalg, aobj, ptype, pval))
47
0
        return 0;
48
94
    if (penc)
49
94
        ASN1_STRING_set0(priv->pkey, penc, penclen);
50
94
    return 1;
51
94
}
52
53
int PKCS8_pkey_get0(const ASN1_OBJECT **ppkalg,
54
                    const unsigned char **pk, int *ppklen,
55
                    const X509_ALGOR **pa, const PKCS8_PRIV_KEY_INFO *p8)
56
31.5k
{
57
31.5k
    if (ppkalg)
58
2.70k
        *ppkalg = p8->pkeyalg->algorithm;
59
31.5k
    if (pk) {
60
8.45k
        *pk = ASN1_STRING_get0_data(p8->pkey);
61
8.45k
        *ppklen = ASN1_STRING_length(p8->pkey);
62
8.45k
    }
63
31.5k
    if (pa)
64
28.8k
        *pa = p8->pkeyalg;
65
31.5k
    return 1;
66
31.5k
}
67
68
const STACK_OF(X509_ATTRIBUTE) *
69
PKCS8_pkey_get0_attrs(const PKCS8_PRIV_KEY_INFO *p8)
70
0
{
71
0
    return p8->attributes;
72
0
}
73
74
int PKCS8_pkey_add1_attr_by_NID(PKCS8_PRIV_KEY_INFO *p8, int nid, int type,
75
                                const unsigned char *bytes, int len)
76
0
{
77
0
    if (X509at_add1_attr_by_NID(&p8->attributes, nid, type, bytes, len) != NULL)
78
0
        return 1;
79
0
    return 0;
80
0
}
81
82
int PKCS8_pkey_add1_attr_by_OBJ(PKCS8_PRIV_KEY_INFO *p8, const ASN1_OBJECT *obj, int type,
83
                                const unsigned char *bytes, int len)
84
0
{
85
0
    return (X509at_add1_attr_by_OBJ(&p8->attributes, obj, type, bytes, len) != NULL);
86
0
}
87
88
int PKCS8_pkey_add1_attr(PKCS8_PRIV_KEY_INFO *p8, X509_ATTRIBUTE *attr)
89
0
{
90
0
    return (X509at_add1_attr(&p8->attributes, attr) != NULL);
91
0
}