2015-09-27 20:28:26 +08:00
|
|
|
/*
|
2018-06-20 22:25:43 +08:00
|
|
|
* Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved.
|
2015-09-27 20:28:26 +08:00
|
|
|
*
|
2018-12-06 20:49:51 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:52:22 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2015-09-27 20:28:26 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "internal/cryptlib.h"
|
|
|
|
#include <openssl/pkcs12.h>
|
|
|
|
#include "p12_lcl.h"
|
|
|
|
|
2018-11-30 07:05:03 +08:00
|
|
|
#if !OPENSSL_API_1_1_0
|
2016-08-13 19:07:42 +08:00
|
|
|
ASN1_TYPE *PKCS12_get_attr(const PKCS12_SAFEBAG *bag, int attr_nid)
|
|
|
|
{
|
|
|
|
return PKCS12_get_attr_gen(bag->attrib, attr_nid);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
const ASN1_TYPE *PKCS12_SAFEBAG_get0_attr(const PKCS12_SAFEBAG *bag,
|
|
|
|
int attr_nid)
|
2015-09-27 20:28:26 +08:00
|
|
|
{
|
|
|
|
return PKCS12_get_attr_gen(bag->attrib, attr_nid);
|
|
|
|
}
|
|
|
|
|
|
|
|
ASN1_TYPE *PKCS8_get_attr(PKCS8_PRIV_KEY_INFO *p8, int attr_nid)
|
|
|
|
{
|
2016-03-04 11:48:39 +08:00
|
|
|
return PKCS12_get_attr_gen(PKCS8_pkey_get0_attrs(p8), attr_nid);
|
2015-09-27 20:28:26 +08:00
|
|
|
}
|
|
|
|
|
2016-08-13 19:07:42 +08:00
|
|
|
const PKCS8_PRIV_KEY_INFO *PKCS12_SAFEBAG_get0_p8inf(const PKCS12_SAFEBAG *bag)
|
2015-09-27 20:28:26 +08:00
|
|
|
{
|
|
|
|
if (PKCS12_SAFEBAG_get_nid(bag) != NID_keyBag)
|
|
|
|
return NULL;
|
|
|
|
return bag->value.keybag;
|
|
|
|
}
|
|
|
|
|
2016-08-13 19:07:42 +08:00
|
|
|
const X509_SIG *PKCS12_SAFEBAG_get0_pkcs8(const PKCS12_SAFEBAG *bag)
|
2015-09-27 20:28:26 +08:00
|
|
|
{
|
|
|
|
if (OBJ_obj2nid(bag->type) != NID_pkcs8ShroudedKeyBag)
|
|
|
|
return NULL;
|
|
|
|
return bag->value.shkeybag;
|
|
|
|
}
|
|
|
|
|
2016-08-13 19:07:42 +08:00
|
|
|
const STACK_OF(PKCS12_SAFEBAG) *
|
|
|
|
PKCS12_SAFEBAG_get0_safes(const PKCS12_SAFEBAG *bag)
|
2015-09-27 20:28:26 +08:00
|
|
|
{
|
|
|
|
if (OBJ_obj2nid(bag->type) != NID_safeContentsBag)
|
|
|
|
return NULL;
|
|
|
|
return bag->value.safes;
|
|
|
|
}
|
|
|
|
|
2016-08-13 19:07:42 +08:00
|
|
|
const ASN1_OBJECT *PKCS12_SAFEBAG_get0_type(const PKCS12_SAFEBAG *bag)
|
2015-09-27 20:28:26 +08:00
|
|
|
{
|
|
|
|
return bag->type;
|
|
|
|
}
|
|
|
|
|
2016-08-13 19:07:42 +08:00
|
|
|
int PKCS12_SAFEBAG_get_nid(const PKCS12_SAFEBAG *bag)
|
2015-09-27 20:28:26 +08:00
|
|
|
{
|
|
|
|
return OBJ_obj2nid(bag->type);
|
|
|
|
}
|
|
|
|
|
2016-08-13 19:07:42 +08:00
|
|
|
int PKCS12_SAFEBAG_get_bag_nid(const PKCS12_SAFEBAG *bag)
|
2015-09-27 20:28:26 +08:00
|
|
|
{
|
|
|
|
int btype = PKCS12_SAFEBAG_get_nid(bag);
|
2016-02-05 23:43:53 +08:00
|
|
|
|
2016-02-12 06:46:01 +08:00
|
|
|
if (btype != NID_certBag && btype != NID_crlBag && btype != NID_secretBag)
|
2015-09-27 20:28:26 +08:00
|
|
|
return -1;
|
|
|
|
return OBJ_obj2nid(bag->value.bag->type);
|
|
|
|
}
|
|
|
|
|
2016-08-13 19:07:42 +08:00
|
|
|
X509 *PKCS12_SAFEBAG_get1_cert(const PKCS12_SAFEBAG *bag)
|
2015-09-27 20:28:26 +08:00
|
|
|
{
|
|
|
|
if (PKCS12_SAFEBAG_get_nid(bag) != NID_certBag)
|
|
|
|
return NULL;
|
|
|
|
if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Certificate)
|
|
|
|
return NULL;
|
|
|
|
return ASN1_item_unpack(bag->value.bag->value.octet,
|
|
|
|
ASN1_ITEM_rptr(X509));
|
|
|
|
}
|
|
|
|
|
2016-08-13 19:07:42 +08:00
|
|
|
X509_CRL *PKCS12_SAFEBAG_get1_crl(const PKCS12_SAFEBAG *bag)
|
2015-09-27 20:28:26 +08:00
|
|
|
{
|
|
|
|
if (PKCS12_SAFEBAG_get_nid(bag) != NID_crlBag)
|
|
|
|
return NULL;
|
|
|
|
if (OBJ_obj2nid(bag->value.bag->type) != NID_x509Crl)
|
|
|
|
return NULL;
|
|
|
|
return ASN1_item_unpack(bag->value.bag->value.octet,
|
|
|
|
ASN1_ITEM_rptr(X509_CRL));
|
|
|
|
}
|
|
|
|
|
|
|
|
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_cert(X509 *x509)
|
|
|
|
{
|
|
|
|
return PKCS12_item_pack_safebag(x509, ASN1_ITEM_rptr(X509),
|
|
|
|
NID_x509Certificate, NID_certBag);
|
|
|
|
}
|
|
|
|
|
|
|
|
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_crl(X509_CRL *crl)
|
|
|
|
{
|
|
|
|
return PKCS12_item_pack_safebag(crl, ASN1_ITEM_rptr(X509_CRL),
|
|
|
|
NID_x509Crl, NID_crlBag);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Turn PKCS8 object into a keybag */
|
|
|
|
|
2015-09-27 20:42:04 +08:00
|
|
|
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_p8inf(PKCS8_PRIV_KEY_INFO *p8)
|
2015-09-27 20:28:26 +08:00
|
|
|
{
|
2015-09-27 20:42:04 +08:00
|
|
|
PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
|
2015-09-27 20:28:26 +08:00
|
|
|
|
2015-09-27 20:42:04 +08:00
|
|
|
if (bag == NULL) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE0_P8INF, ERR_R_MALLOC_FAILURE);
|
2015-09-27 20:28:26 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bag->type = OBJ_nid2obj(NID_keyBag);
|
|
|
|
bag->value.keybag = p8;
|
|
|
|
return bag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Turn PKCS8 object into a shrouded keybag */
|
|
|
|
|
2015-09-27 20:42:04 +08:00
|
|
|
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create0_pkcs8(X509_SIG *p8)
|
2015-09-27 20:28:26 +08:00
|
|
|
{
|
2015-09-27 20:42:04 +08:00
|
|
|
PKCS12_SAFEBAG *bag = PKCS12_SAFEBAG_new();
|
2016-02-05 23:43:53 +08:00
|
|
|
|
2015-09-27 20:28:26 +08:00
|
|
|
/* Set up the safe bag */
|
2015-09-27 20:42:04 +08:00
|
|
|
if (bag == NULL) {
|
|
|
|
PKCS12err(PKCS12_F_PKCS12_SAFEBAG_CREATE0_PKCS8, ERR_R_MALLOC_FAILURE);
|
2015-09-27 20:28:26 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
bag->type = OBJ_nid2obj(NID_pkcs8ShroudedKeyBag);
|
2015-09-27 20:42:04 +08:00
|
|
|
bag->value.shkeybag = p8;
|
|
|
|
return bag;
|
|
|
|
}
|
|
|
|
|
|
|
|
PKCS12_SAFEBAG *PKCS12_SAFEBAG_create_pkcs8_encrypt(int pbe_nid,
|
|
|
|
const char *pass,
|
|
|
|
int passlen,
|
|
|
|
unsigned char *salt,
|
|
|
|
int saltlen, int iter,
|
|
|
|
PKCS8_PRIV_KEY_INFO *p8inf)
|
|
|
|
{
|
|
|
|
PKCS12_SAFEBAG *bag;
|
|
|
|
const EVP_CIPHER *pbe_ciph;
|
|
|
|
X509_SIG *p8;
|
2015-09-27 20:28:26 +08:00
|
|
|
|
|
|
|
pbe_ciph = EVP_get_cipherbynid(pbe_nid);
|
|
|
|
if (pbe_ciph)
|
|
|
|
pbe_nid = -1;
|
|
|
|
|
2015-09-27 20:42:04 +08:00
|
|
|
p8 = PKCS8_encrypt(pbe_nid, pbe_ciph, pass, passlen, salt, saltlen, iter,
|
|
|
|
p8inf);
|
2018-05-11 03:10:03 +08:00
|
|
|
if (p8 == NULL)
|
2015-09-27 20:42:04 +08:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
bag = PKCS12_SAFEBAG_create0_pkcs8(p8);
|
2018-05-11 03:10:03 +08:00
|
|
|
if (bag == NULL)
|
2015-09-27 20:42:04 +08:00
|
|
|
X509_SIG_free(p8);
|
2015-09-27 20:28:26 +08:00
|
|
|
|
|
|
|
return bag;
|
|
|
|
}
|