/src/openssl/crypto/asn1/p8_pkey.c
Line  | Count  | Source  | 
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  | 0  | { | 
20  | 0  |     PKCS8_PRIV_KEY_INFO *key;  | 
21  | 0  |     int version;  | 
22  |  | 
  | 
23  | 0  |     switch (operation) { | 
24  | 0  |     case ASN1_OP_FREE_PRE:  | 
25  |  |         /* The structure is still valid during ASN1_OP_FREE_PRE */  | 
26  | 0  |         key = (PKCS8_PRIV_KEY_INFO *)*pval;  | 
27  | 0  |         if (key->pkey)  | 
28  | 0  |             OPENSSL_cleanse(key->pkey->data, key->pkey->length);  | 
29  | 0  |         break;  | 
30  | 0  |     case ASN1_OP_D2I_POST:  | 
31  |  |         /* Insist on a valid version now that the structure is decoded */  | 
32  | 0  |         key = (PKCS8_PRIV_KEY_INFO *)*pval;  | 
33  | 0  |         version = ASN1_INTEGER_get(key->version);  | 
34  | 0  |         if (version < 0 || version > 1)  | 
35  | 0  |             return 0;  | 
36  | 0  |         if (version == 0 && key->kpub != NULL)  | 
37  | 0  |             return 0;  | 
38  | 0  |         break;  | 
39  | 0  |     }  | 
40  | 0  |     return 1;  | 
41  | 0  | }  | 
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  | 0  | { | 
57  | 0  |     if (version >= 0) { | 
58  |  |         /* We only support PKCS#8 v1 (0) and v2 (1). */  | 
59  | 0  |         if (version > 1)  | 
60  | 0  |             return 0;  | 
61  | 0  |         if (!ASN1_INTEGER_set(priv->version, version))  | 
62  | 0  |             return 0;  | 
63  | 0  |     }  | 
64  | 0  |     if (!X509_ALGOR_set0(priv->pkeyalg, aobj, ptype, pval))  | 
65  | 0  |         return 0;  | 
66  | 0  |     if (penc)  | 
67  | 0  |         ASN1_STRING_set0(priv->pkey, penc, penclen);  | 
68  | 0  |     return 1;  | 
69  | 0  | }  | 
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  | 0  | { | 
75  | 0  |     if (ppkalg)  | 
76  | 0  |         *ppkalg = p8->pkeyalg->algorithm;  | 
77  | 0  |     if (pk) { | 
78  | 0  |         *pk = ASN1_STRING_get0_data(p8->pkey);  | 
79  | 0  |         *ppklen = ASN1_STRING_length(p8->pkey);  | 
80  | 0  |     }  | 
81  | 0  |     if (pa)  | 
82  | 0  |         *pa = p8->pkeyalg;  | 
83  | 0  |     return 1;  | 
84  | 0  | }  | 
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  |  |     return (X509at_add1_attr(&p8->attributes, attr) != NULL);  | 
109  | 0  | }  |