| 1 |
/* |
| 2 |
* Copyright 1999-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 |
#include <stdio.h> |
| 11 |
#include "internal/cryptlib.h" |
| 12 |
#include <openssl/pkcs12.h> |
| 13 |
#include "p12_local.h" |
| 14 |
#include "crypto/x509.h" |
| 15 |
|
| 16 |
#ifndef OPENSSL_NO_DEPRECATED_1_1_0 |
| 17 |
ASN1_TYPE *PKCS12_get_attr(const PKCS12_SAFEBAG *bag, int attr_nid) |
| 18 |
{ |
| 19 |
return PKCS12_get_attr_gen(bag->attrib, attr_nid); |
| 20 |
} |
| 21 |
#endif |
| 22 |
|
| 23 |
const ASN1_TYPE *PKCS12_SAFEBAG_get0_attr(const PKCS12_SAFEBAG *bag, |
| 24 |
int attr_nid) |
| 25 |
{ |
| 26 |
return PKCS12_get_attr_gen(bag->attrib, attr_nid); |
| 27 |
} |
| 28 |
|
| 29 |
ASN1_TYPE *PKCS8_get_attr(PKCS8_PRIV_KEY_INFO *p8, int attr_nid) |
| 30 |
{ |
| 31 |
return PKCS12_get_attr_gen(PKCS8_pkey_get0_attrs(p8), attr_nid); |
| 32 |
} |
| 33 |
|
| 34 |
const PKCS8_PRIV_KEY_INFO *PKCS12_SAFEBAG_get0_p8inf(const PKCS12_SAFEBAG *bag) |
| 35 |
{ |
| 36 |
if (PKCS12_SAFEBAG_get_nid(bag) != NID_keyBag) |
| 37 |
return NULL; |
| 38 |
return bag->value.keybag; |
| 39 |
} |
| 40 |
|
| 41 |
const X509_SIG *PKCS12_SAFEBAG_get0_pkcs8(const PKCS12_SAFEBAG *bag) |
| 42 |
{ |
| 43 |
if (OBJ_obj2nid(bag->type) != NID_pkcs8ShroudedKeyBag) |
| 44 |
return NULL; |
| 45 |
return bag->value.shkeybag; |
| 46 |
} |
| 47 |
|
| 48 |
const STACK_OF(PKCS12_SAFEBAG) * |
| 49 |
PKCS12_SAFEBAG_get0_safes(const PKCS12_SAFEBAG *bag) |
| 50 |
{ |
| 51 |
if (OBJ_obj2nid(bag->type) != NID_safeContentsBag) |
| 52 |
return NULL; |
| 53 |
return bag->value.safes; |
| 54 |
} |
| 55 |
|
| 56 |
const ASN1_OBJECT *PKCS12_SAFEBAG_get0_type(const PKCS12_SAFEBAG *bag) |
| 57 |
{ |
| 58 |
return bag->type; |
| 59 |
} |
| 60 |
|
| 61 |
int PKCS12_SAFEBAG_get_nid(const PKCS12_SAFEBAG *bag) |
| 62 |
{ |
| 63 |
return OBJ_obj2nid(bag->type); |
| 64 |
} |
| 65 |
|
| 66 |
int PKCS12_SAFEBAG_get_bag_nid(const PKCS12_SAFEBAG *bag) |
| 67 |
{ |
| 68 |
int btype = PKCS12_SAFEBAG_get_nid(bag); |
| 69 |
|
| 70 |
if (btype != NID_certBag && btype != NID_crlBag && btype != NID_secretBag) |
| 71 |
return -1; |
| 72 |
return OBJ_obj2nid(bag->value.bag->type); |
| 73 |
} |
| 74 |
|
| 75 |
const ASN1_OBJECT *PKCS12_SAFEBAG_get0_bag_type(const PKCS12_SAFEBAG *bag) |
| 76 |
{ |
| 77 |
int btype = PKCS12_SAFEBAG_get_nid(bag); |
| 78 |
|
| 79 |
if (btype != NID_certBag && btype != NID_crlBag && btype != NID_secretBag) |
| 80 |
return NULL; |
| 81 |
return bag->value.bag->type; |
| 82 |
} |
| 83 |
|
| 84 |
const ASN1_TYPE *PKCS12_SAFEBAG_get0_bag_obj(const PKCS12_SAFEBAG *bag) |
| 85 |
{ |
| 86 |
int vtype = PKCS12_SAFEBAG_get_bag_nid(bag); |
| 87 |
|
| 88 |
if (vtype == -1 || vtype == NID_x509Certificate || vtype == NID_x509Crl |
| 89 |
|| vtype == NID_sdsiCertificate) |
| 90 |
return NULL; |
| 91 |
return bag->value.bag->value.other; |
| 92 |
} |
| 93 |
|
| 94 |
X509 *PKCS12_SAFEBAG_get1_cert(const PKCS12_SAFEBAG *bag) |
| 95 |
{ |
| 96 |
if (PKCS12_SAFEBAG_get_nid(bag) != NID_certBag) |
| 97 |
return NULL; |
| 98 |
if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate) |
| 99 |
return NULL; |
| 100 |
return ASN1_item_unpack(bag->value.bag->value.octet, |
| 101 |
ASN1_ITEM_rptr(X509)); |
| 102 |
} |
| 103 |
|
| 104 |
X509_CRL *PKCS12_SAFEBAG_get1_crl(const PKCS12_SAFEBAG *bag) |
| 105 |
{ |
| 106 |
if (PKCS12_SAFEBAG_get_nid(bag) != NID_crlBag) |
| 107 |
return NULL; |
| 108 |
if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Crl) |
| 109 |
return NULL; |
| 110 |
return ASN1_item_unpack(bag->value.bag->value.octet, |
| 111 |
ASN1_ITEM_rptr(X509_CRL)); |
| 112 |
} |
| 113 |
|
| 114 |
X509 *PKCS12_SAFEBAG_get1_cert_ex(const PKCS12_SAFEBAG *bag, |
| 115 |
OSSL_LIB_CTX *libctx, const char *propq) |
| 116 |
{ |
| 117 |
X509 *ret = NULL; |
| 118 |
|
| 119 |
if (PKCS12_SAFEBAG_get_nid(bag) != NID_certBag) |
| 120 |
return NULL; |
| 121 |
if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate) |
| 122 |
return NULL; |
| 123 |
ret = ASN1_item_unpack_ex(bag->value.bag->value.octet, |
| 124 |
ASN1_ITEM_rptr(X509), libctx, propq); |
| 125 |
if (!ossl_x509_set0_libctx(ret, libctx, propq)) { |
| 126 |
X509_free(ret); |
| 127 |
return NULL; |
| 128 |
} |
| 129 |
return ret; |
| 130 |
} |
| 131 |
|
| 132 |
X509_CRL *PKCS12_SAFEBAG_get1_crl_ex(const PKCS12_SAFEBAG *bag, |
| 133 |
OSSL_LIB_CTX *libctx, const char *propq) |
| 134 |
{ |
| 135 |
X509_CRL *ret = NULL; |
| 136 |
|
| 137 |
if (PKCS12_SAFEBAG_get_nid(bag) != NID_crlBag) |
| 138 |
return NULL; |
| 139 |
if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Crl) |
| 140 |
return NULL; |
| 141 |
ret = ASN1_item_unpack_ex(bag->value.bag->value.octet, |
| 142 |
ASN1_ITEM_rptr(X509_CRL), libctx, propq); |
| 143 |
if (!ossl_x509_crl_set0_libctx(ret, libctx, propq)) { |
| 144 |
X509_CRL_free(ret); |
| 145 |
return NULL; |
| 146 |
} |
| 147 |
return ret; |
| 148 |
} |
| 149 |
|
| 150 |
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_cert(X509 *x509) |
| 151 |
{ |
| 152 |
return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509), |
| 153 |
NID_x509Certificate, NID_certBag); |
| 154 |
} |
| 155 |
|
| 156 |
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_crl(X509_CRL *crl) |
| 157 |
{ |
| 158 |
return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL), |
| 159 |
NID_x509Crl, NID_crlBag); |
| 160 |
} |
| 161 |
|
| 162 |
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_secret(int type, int vtype, const unsigned char *value, int len) |
| 163 |
{ |
| 164 |
PKCS12_BAGS *bag; |
| 165 |
PKCS12_SAFEBAG *safebag; |
| 166 |
|
| 167 |
if ((bag = PKCS12_BAGS_new()) == NULL) { |
| 168 |
ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
| 169 |
return NULL; |
| 170 |
} |
| 171 |
bag->type = OBJ_nid2obj(type); |
| 172 |
|
| 173 |
switch (vtype) { |
| 174 |
case V_ASN1_OCTET_STRING: { |
| 175 |
ASN1_OCTET_STRING *strtmp = ASN1_OCTET_STRING_new(); |
| 176 |
|
| 177 |
if (strtmp == NULL) { |
| 178 |
ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
| 179 |
goto err; |
| 180 |
} |
| 181 |
/* Pack data into an octet string */ |
| 182 |
if (!ASN1_OCTET_STRING_set(strtmp, value, len)) { |
| 183 |
ASN1_OCTET_STRING_free(strtmp); |
| 184 |
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCODE_ERROR); |
| 185 |
goto err; |
| 186 |
} |
| 187 |
bag->value.other = ASN1_TYPE_new(); |
| 188 |
if (bag->value.other == NULL) { |
| 189 |
ASN1_OCTET_STRING_free(strtmp); |
| 190 |
ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
| 191 |
goto err; |
| 192 |
} |
| 193 |
ASN1_TYPE_set(bag->value.other, vtype, strtmp); |
| 194 |
} break; |
| 195 |
|
| 196 |
default: |
| 197 |
ERR_raise(ERR_LIB_PKCS12, PKCS12_R_INVALID_TYPE); |
| 198 |
goto err; |
| 199 |
} |
| 200 |
|
| 201 |
if ((safebag = PKCS12_SAFEBAG_new()) == NULL) { |
| 202 |
ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
| 203 |
goto err; |
| 204 |
} |
| 205 |
safebag->value.bag = bag; |
| 206 |
safebag->type = OBJ_nid2obj(NID_secretBag); |
| 207 |
return safebag; |
| 208 |
|
| 209 |
err: |
| 210 |
PKCS12_BAGS_free(bag); |
| 211 |
return NULL; |
| 212 |
} |
| 213 |
|
| 214 |
/* Turn PKCS8 object into a keybag */ |
| 215 |
|
| 216 |
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_p8inf(PKCS8_PRIV_KEY_INFO *p8) |
| 217 |
{ |
| 218 |
PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new(); |
| 219 |
|
| 220 |
if (bag == NULL) { |
| 221 |
ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
| 222 |
return NULL; |
| 223 |
} |
| 224 |
bag->type = OBJ_nid2obj(NID_keyBag); |
| 225 |
bag->value.keybag = p8; |
| 226 |
return bag; |
| 227 |
} |
| 228 |
|
| 229 |
/* Turn PKCS8 object into a shrouded keybag */ |
| 230 |
|
| 231 |
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_pkcs8(X509_SIG *p8) |
| 232 |
{ |
| 233 |
PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new(); |
| 234 |
|
| 235 |
/* Set up the safe bag */ |
| 236 |
if (bag == NULL) { |
| 237 |
ERR_raise(ERR_LIB_PKCS12, ERR_R_ASN1_LIB); |
| 238 |
return NULL; |
| 239 |
} |
| 240 |
bag->type = OBJ_nid2obj(NID_pkcs8ShroudedKeyBag); |
| 241 |
bag->value.shkeybag = p8; |
| 242 |
return bag; |
| 243 |
} |
| 244 |
|
| 245 |
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(int pbe_nid, |
| 246 |
const char *pass, |
| 247 |
int passlen, |
| 248 |
unsigned char *salt, |
| 249 |
int saltlen, int iter, |
| 250 |
PKCS8_PRIV_KEY_INFO *p8inf, |
| 251 |
OSSL_LIB_CTX *ctx, |
| 252 |
const char *propq) |
| 253 |
{ |
| 254 |
PKCS12_SAFEBAG *bag = NULL; |
| 255 |
const EVP_CIPHER *pbe_ciph = NULL; |
| 256 |
EVP_CIPHER *pbe_ciph_fetch = NULL; |
| 257 |
X509_SIG *p8; |
| 258 |
|
| 259 |
ERR_set_mark(); |
| 260 |
pbe_ciph = pbe_ciph_fetch = EVP_CIPHER_fetch(ctx, OBJ_nid2sn(pbe_nid), propq); |
| 261 |
if (pbe_ciph == NULL) |
| 262 |
pbe_ciph = EVP_get_cipherbynid(pbe_nid); |
| 263 |
ERR_pop_to_mark(); |
| 264 |
|
| 265 |
if (pbe_ciph != NULL) |
| 266 |
pbe_nid = -1; |
| 267 |
|
| 268 |
p8 = PKCS8_encrypt_ex(pbe_nid, pbe_ciph, pass, passlen, salt, saltlen, iter, |
| 269 |
p8inf, ctx, propq); |
| 270 |
if (p8 == NULL) |
| 271 |
goto err; |
| 272 |
|
| 273 |
bag = PKCS12_SAFEBAG_create0_pkcs8(p8); |
| 274 |
if (bag == NULL) |
| 275 |
X509_SIG_free(p8); |
| 276 |
|
| 277 |
err: |
| 278 |
EVP_CIPHER_free(pbe_ciph_fetch); |
| 279 |
return bag; |
| 280 |
} |
| 281 |
|
| 282 |
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt(int pbe_nid, |
| 283 |
const char *pass, |
| 284 |
int passlen, |
| 285 |
unsigned char *salt, |
| 286 |
int saltlen, int iter, |
| 287 |
PKCS8_PRIV_KEY_INFO *p8inf) |
| 288 |
{ |
| 289 |
return PKCS12_SAFEBAG_create_pkcs8_encrypt_ex(pbe_nid, pass, passlen, |
| 290 |
salt, saltlen, iter, p8inf, |
| 291 |
NULL, NULL); |
| 292 |
} |