API参考
载入中...
搜索中...
未找到
hitls_pkcs12_local.h
1/*
2 * This file is part of the openHiTLS project.
3 *
4 * openHiTLS is licensed under the Mulan PSL v2.
5 * You can use this software according to the terms and conditions of the Mulan PSL v2.
6 * You may obtain a copy of Mulan PSL v2 at:
7 *
8 * http://license.coscl.org.cn/MulanPSL2
9 *
10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13 * See the Mulan PSL v2 for more details.
14 */
15
16#ifndef HITLS_PKCS12_LOCAL_H
17#define HITLS_PKCS12_LOCAL_H
18
19#include "hitls_build.h"
20#ifdef HITLS_PKI_PKCS12
21#include <stdint.h>
22#include "bsl_asn1_internal.h"
23#include "bsl_obj.h"
24#include "sal_atomic.h"
25#include "hitls_x509_local.h"
26#include "hitls_pki_cert.h"
27#include "hitls_pki_crl.h"
28#include "crypt_eal_codecs.h"
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34typedef struct {
35 BslCid contentType;
36 BSL_Buffer *contentValue;
37} HITLS_PKCS12_ContentInfo;
38
39typedef struct {
40 BslCid alg;
41 BSL_Buffer *mac;
42 BSL_Buffer *macSalt;
43 uint32_t iteration;
44} HITLS_PKCS12_MacData;
45
46/* This struct is provided for users to create related bags and add them to the p12-ctx. */
47typedef struct _HITLS_PKCS12_Bag {
48 uint32_t type;
49 uint32_t id;
50 union {
52 HITLS_X509_Cert *cert;
53 HITLS_X509_Crl *crl;
54 BSL_Buffer secret;
55 } value;
56 HITLS_X509_Attrs *attributes; // localKeyId, friendlyName, ect. Item is HITLS_PKCS12_SafeBagAttr.
57 BSL_SAL_RefCount references;
58} HITLS_PKCS12_Bag;
59
60/*
61 * The Top-Level p12-ctx, which can store certificates and pkey required by a .p12 file.
62 * Note that the entity-cert and entity-pkey are unique.
63 */
64typedef struct _HITLS_PKCS12 {
65 uint32_t version;
66 HITLS_PKCS12_Bag *key; /* for store p8ShroudedKeyBag, only one p8ShroudedKeyBag is supported. */
67 HITLS_PKCS12_Bag *entityCert; /* for store entity-cert bag. If we find a cert that matches the p8ShroudedKeyBag,
68 it will be placed here. */
69 BSL_ASN1_List *secretBags; /* for store secret-bags, we support multiple secret-bags. */
70 BSL_ASN1_List *certList; /* for store cert-bags, we support multiple cert-bags. */
71 BSL_ASN1_List *crlList; /* for store crl-bags, we support multiple crl-bags. */
72 BSL_ASN1_List *keyList; /* for store key-bags, we support multiple key-bags. */
73 HITLS_PKCS12_MacData *macData;
74 HITLS_PKI_LibCtx *libCtx;
75 const char *attrName;
76} HITLS_PKCS12;
77
78/* A common bag, could store a crl-bag, or a cert-bag, or a secret-bag... */
79typedef struct {
80 BslCid bagType;
81 BSL_Buffer bagValue; // encode data
82} HITLS_PKCS12_CommonSafeBag;
83
84/* SafeBag Attributes. */
85typedef struct {
86 BslCid attrId;
87 BSL_Buffer attrValue;
88} HITLS_PKCS12_SafeBagAttr;
89
90/* A safeBag defined in RFC 7292, which storing intermediate data in our decoding process. */
91typedef struct {
92 BslCid bagId;
93 BSL_Buffer *bag; // encode data
94 HITLS_X509_Attrs *attributes; // Currently, only support localKeyId, friendlyName. Item is HITLS_PKCS12_SafeBagAttr.
95} HITLS_PKCS12_SafeBag;
96
97void HITLS_PKCS12_SafeBagFree(HITLS_PKCS12_SafeBag *safeBag);
98
99HITLS_PKCS12_MacData *HITLS_PKCS12_MacDataNew(void);
100
101void HITLS_PKCS12_MacDataFree(HITLS_PKCS12_MacData *macData);
102
103void HITLS_PKCS12_AttributesFree(void *attribute);
104
105typedef enum {
106 HITLS_PKCS12_KDF_ENCKEY_ID = 1,
107 HITLS_PKCS12_KDF_ENCIV_ID = 2,
108 HITLS_PKCS12_KDF_MACKEY_ID = 3,
109} HITLS_PKCS12_KDF_IDX;
110
111/*
112 * A method of obtaining the mac key in key-integrity protection mode.
113 * The method implementation follows standards RFC 7292
114*/
115int32_t HITLS_PKCS12_KDF(HITLS_PKCS12 *p12, const uint8_t *pwd, uint32_t pwdLen,
116 HITLS_PKCS12_KDF_IDX type, BSL_Buffer *output);
117
118/*
119 * To cal mac data in key-integrity protection mode, we use the way of Hmac + PKCS12_KDF.
120*/
121int32_t HITLS_PKCS12_CalMac(HITLS_PKCS12 *p12, BSL_Buffer *pwd, BSL_Buffer *initData, BSL_Buffer *output);
122
123#ifdef HITLS_PKI_PKCS12_PARSE
124/*
125 * Parse the outermost layer of contentInfo, provide two functions
126 * 1. AuthSafe -> pkcs7 package format
127 * 2. contentInfo_i -> safeContents
128*/
129int32_t HITLS_PKCS12_ParseContentInfo(HITLS_PKI_LibCtx *libCtx, const char *attrName, BSL_Buffer *encode,
130 const uint8_t *password, uint32_t passLen, BSL_Buffer *data);
131
132/*
133 * Parse the 'sequences of' of p12, provide two functions
134 * 1. contentInfo -> contentInfo_i
135 * 2. safeContent -> safeBag_i
136 * Both of the above parsing only resolves to BER encoding format, and requiring further conversion.
137*/
138int32_t HITLS_PKCS12_ParseAsn1AddList(BSL_Buffer *encode, BSL_ASN1_List *list, uint32_t parseType);
139
140/*
141 * Parse each safeBag of list, and convert decode data to the cert or key.
142*/
143int32_t HITLS_PKCS12_ParseSafeBagList(BSL_ASN1_List *bagList, const uint8_t *password, uint32_t passLen,
144 HITLS_PKCS12 *p12);
145
146/*
147 * Parse attributes of a safeBag, and convert decode data to the real data.
148*/
149int32_t HITLS_PKCS12_ParseSafeBagAttr(BSL_ASN1_Buffer *attrBuff, HITLS_X509_Attrs *attrList);
150
151/*
152 * Parse AuthSafeData of a p12, and convert decode data to the real data.
153*/
154int32_t HITLS_PKCS12_ParseAuthSafeData(BSL_Buffer *encode, const uint8_t *password, uint32_t passLen,
155 HITLS_PKCS12 *p12);
156
157/*
158 * Parse MacData of a p12, and convert decode data to the real data.
159*/
160int32_t HITLS_PKCS12_ParseMacData(BSL_Buffer *encode, HITLS_PKCS12_MacData *macData);
161#endif
162
163#ifdef HITLS_PKI_PKCS12_GEN
164/*
165 * Encode MacData of a p12.
166*/
167int32_t HITLS_PKCS12_EncodeMacData(HITLS_PKCS12 *p12, BSL_Buffer *initData, const HITLS_PKCS12_MacParam *macParam,
168 BSL_Buffer *encode);
169
170/*
171 * Encode contentInfo.
172*/
173int32_t HITLS_PKCS12_EncodeContentInfo(HITLS_PKI_LibCtx *libCtx, const char *attrName, BSL_Buffer *input,
174 uint32_t encodeType, const CRYPT_EncodeParam *encryptParam, BSL_Buffer *encode);
175
176/*
177 * Encode list, including contentInfo-list, safeContent-list.
178*/
179int32_t HITLS_PKCS12_EncodeAsn1List(HITLS_PKCS12 *p12, BSL_ASN1_List *list, uint32_t encodeType,
180 const CRYPT_EncodeParam *encryptParam, BSL_Buffer *encode);
181#endif
182
187int32_t HITLS_PKCS12_BagAddAttr(HITLS_PKCS12_Bag *bag, uint32_t type, const BSL_Buffer *attrValue);
188
193int32_t HITLS_PKCS12_BagRefUp(HITLS_PKCS12_Bag *bag);
194
195#ifdef __cplusplus
196}
197#endif
198
199#endif // HITLS_PKI_PKCS12
200
201#endif // HITLS_CRL_LOCAL_H
BslCid
定义 bsl_obj.h:36
struct EAL_PkeyCtx CRYPT_EAL_PkeyCtx
定义 crypt_eal_pkey.h:108
定义 bsl_types.h:40
定义 crypt_types.h:931
定义 hitls_pki_types.h:419