/src/openssl30/crypto/pkcs12/p12_add.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1999-2021 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/core.h> | 
| 13 |  | #include <openssl/core_names.h> | 
| 14 |  | #include <openssl/pkcs12.h> | 
| 15 |  | #include "p12_local.h" | 
| 16 |  | #include "crypto/pkcs7/pk7_local.h" | 
| 17 |  |  | 
| 18 |  | /* Pack an object into an OCTET STRING and turn into a safebag */ | 
| 19 |  |  | 
| 20 |  | PKCS12_SAFEBAG *PKCS12_item_pack_safebag(void *obj, const ASN1_ITEM *it, | 
| 21 |  |                                          int nid1, int nid2) | 
| 22 | 0 | { | 
| 23 | 0 |     PKCS12_BAGS *bag; | 
| 24 | 0 |     PKCS12_SAFEBAG *safebag; | 
| 25 |  | 
 | 
| 26 | 0 |     if ((bag = PKCS12_BAGS_new()) == NULL) { | 
| 27 | 0 |         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); | 
| 28 | 0 |         return NULL; | 
| 29 | 0 |     } | 
| 30 | 0 |     bag->type = OBJ_nid2obj(nid1); | 
| 31 | 0 |     if (!ASN1_item_pack(obj, it, &bag->value.octet)) { | 
| 32 | 0 |         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); | 
| 33 | 0 |         goto err; | 
| 34 | 0 |     } | 
| 35 | 0 |     if ((safebag = PKCS12_SAFEBAG_new()) == NULL) { | 
| 36 | 0 |         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); | 
| 37 | 0 |         goto err; | 
| 38 | 0 |     } | 
| 39 | 0 |     safebag->value.bag = bag; | 
| 40 | 0 |     safebag->type = OBJ_nid2obj(nid2); | 
| 41 | 0 |     return safebag; | 
| 42 |  |  | 
| 43 | 0 |  err: | 
| 44 | 0 |     PKCS12_BAGS_free(bag); | 
| 45 | 0 |     return NULL; | 
| 46 | 0 | } | 
| 47 |  |  | 
| 48 |  | /* Turn a stack of SAFEBAGS into a PKCS#7 data Contentinfo */ | 
| 49 |  | PKCS7 *PKCS12_pack_p7data(STACK_OF(PKCS12_SAFEBAG) *sk) | 
| 50 | 0 | { | 
| 51 | 0 |     PKCS7 *p7; | 
| 52 |  | 
 | 
| 53 | 0 |     if ((p7 = PKCS7_new()) == NULL) { | 
| 54 | 0 |         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); | 
| 55 | 0 |         return NULL; | 
| 56 | 0 |     } | 
| 57 | 0 |     p7->type = OBJ_nid2obj(NID_pkcs7_data); | 
| 58 | 0 |     if ((p7->d.data = ASN1_OCTET_STRING_new()) == NULL) { | 
| 59 | 0 |         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); | 
| 60 | 0 |         goto err; | 
| 61 | 0 |     } | 
| 62 |  |  | 
| 63 | 0 |     if (!ASN1_item_pack(sk, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), &p7->d.data)) { | 
| 64 | 0 |         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CANT_PACK_STRUCTURE); | 
| 65 | 0 |         goto err; | 
| 66 | 0 |     } | 
| 67 | 0 |     return p7; | 
| 68 |  |  | 
| 69 | 0 |  err: | 
| 70 | 0 |     PKCS7_free(p7); | 
| 71 | 0 |     return NULL; | 
| 72 | 0 | } | 
| 73 |  |  | 
| 74 |  | /* Unpack SAFEBAGS from PKCS#7 data ContentInfo */ | 
| 75 |  | STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7data(PKCS7 *p7) | 
| 76 | 0 | { | 
| 77 | 0 |     if (!PKCS7_type_is_data(p7)) { | 
| 78 | 0 |         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA); | 
| 79 | 0 |         return NULL; | 
| 80 | 0 |     } | 
| 81 | 0 |     return ASN1_item_unpack(p7->d.data, ASN1_ITEM_rptr(PKCS12_SAFEBAGS)); | 
| 82 | 0 | } | 
| 83 |  |  | 
| 84 |  | /* Turn a stack of SAFEBAGS into a PKCS#7 encrypted data ContentInfo */ | 
| 85 |  |  | 
| 86 |  | PKCS7 *PKCS12_pack_p7encdata_ex(int pbe_nid, const char *pass, int passlen, | 
| 87 |  |                                 unsigned char *salt, int saltlen, int iter, | 
| 88 |  |                                 STACK_OF(PKCS12_SAFEBAG) *bags, | 
| 89 |  |                                 OSSL_LIB_CTX *ctx, const char *propq) | 
| 90 | 0 | { | 
| 91 | 0 |     PKCS7 *p7; | 
| 92 | 0 |     X509_ALGOR *pbe; | 
| 93 | 0 |     const EVP_CIPHER *pbe_ciph = NULL; | 
| 94 | 0 |     EVP_CIPHER *pbe_ciph_fetch = NULL; | 
| 95 |  | 
 | 
| 96 | 0 |     if ((p7 = PKCS7_new_ex(ctx, propq)) == NULL) { | 
| 97 | 0 |         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); | 
| 98 | 0 |         return NULL; | 
| 99 | 0 |     } | 
| 100 | 0 |     if (!PKCS7_set_type(p7, NID_pkcs7_encrypted)) { | 
| 101 | 0 |         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ERROR_SETTING_ENCRYPTED_DATA_TYPE); | 
| 102 | 0 |         goto err; | 
| 103 | 0 |     } | 
| 104 |  |  | 
| 105 | 0 |     ERR_set_mark(); | 
| 106 | 0 |     pbe_ciph = pbe_ciph_fetch = EVP_CIPHER_fetch(ctx, OBJ_nid2sn(pbe_nid), propq); | 
| 107 | 0 |     if (pbe_ciph == NULL) | 
| 108 | 0 |         pbe_ciph = EVP_get_cipherbynid(pbe_nid); | 
| 109 | 0 |     ERR_pop_to_mark(); | 
| 110 |  | 
 | 
| 111 | 0 |     if (pbe_ciph != NULL) { | 
| 112 | 0 |         pbe = PKCS5_pbe2_set_iv_ex(pbe_ciph, iter, salt, saltlen, NULL, -1, ctx); | 
| 113 | 0 |     } else { | 
| 114 | 0 |         pbe = PKCS5_pbe_set_ex(pbe_nid, iter, salt, saltlen, ctx); | 
| 115 | 0 |     } | 
| 116 |  | 
 | 
| 117 | 0 |     if (pbe == NULL) { | 
| 118 | 0 |         ERR_raise(ERR_LIB_PKCS12, ERR_R_MALLOC_FAILURE); | 
| 119 | 0 |         goto err; | 
| 120 | 0 |     } | 
| 121 | 0 |     X509_ALGOR_free(p7->d.encrypted->enc_data->algorithm); | 
| 122 | 0 |     p7->d.encrypted->enc_data->algorithm = pbe; | 
| 123 | 0 |     ASN1_OCTET_STRING_free(p7->d.encrypted->enc_data->enc_data); | 
| 124 | 0 |     if (!(p7->d.encrypted->enc_data->enc_data = | 
| 125 | 0 |           PKCS12_item_i2d_encrypt_ex(pbe, ASN1_ITEM_rptr(PKCS12_SAFEBAGS), pass, | 
| 126 | 0 |                                      passlen, bags, 1, ctx, propq))) { | 
| 127 | 0 |         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_ENCRYPT_ERROR); | 
| 128 | 0 |         goto err; | 
| 129 | 0 |     } | 
| 130 |  |  | 
| 131 | 0 |     EVP_CIPHER_free(pbe_ciph_fetch); | 
| 132 | 0 |     return p7; | 
| 133 |  |  | 
| 134 | 0 |  err: | 
| 135 | 0 |     PKCS7_free(p7); | 
| 136 | 0 |     EVP_CIPHER_free(pbe_ciph_fetch); | 
| 137 | 0 |     return NULL; | 
| 138 | 0 | } | 
| 139 |  |  | 
| 140 |  | PKCS7 *PKCS12_pack_p7encdata(int pbe_nid, const char *pass, int passlen, | 
| 141 |  |                              unsigned char *salt, int saltlen, int iter, | 
| 142 |  |                              STACK_OF(PKCS12_SAFEBAG) *bags) | 
| 143 | 0 | { | 
| 144 | 0 |     return PKCS12_pack_p7encdata_ex(pbe_nid, pass, passlen, salt, saltlen, | 
| 145 | 0 |                                     iter, bags, NULL, NULL); | 
| 146 | 0 | } | 
| 147 |  |  | 
| 148 |  | STACK_OF(PKCS12_SAFEBAG) *PKCS12_unpack_p7encdata(PKCS7 *p7, const char *pass, | 
| 149 |  |                                                   int passlen) | 
| 150 | 0 | { | 
| 151 | 0 |     if (!PKCS7_type_is_encrypted(p7)) | 
| 152 | 0 |         return NULL; | 
| 153 | 0 |     return PKCS12_item_decrypt_d2i_ex(p7->d.encrypted->enc_data->algorithm, | 
| 154 | 0 |                                    ASN1_ITEM_rptr(PKCS12_SAFEBAGS), | 
| 155 | 0 |                                    pass, passlen, | 
| 156 | 0 |                                    p7->d.encrypted->enc_data->enc_data, 1, | 
| 157 | 0 |                                    p7->ctx.libctx, p7->ctx.propq); | 
| 158 | 0 | } | 
| 159 |  |  | 
| 160 |  | PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey_ex(const PKCS12_SAFEBAG *bag, | 
| 161 |  |                                             const char *pass, int passlen, | 
| 162 |  |                                             OSSL_LIB_CTX *ctx, const char *propq) | 
| 163 | 0 | { | 
| 164 | 0 |     return PKCS8_decrypt_ex(bag->value.shkeybag, pass, passlen, ctx, propq); | 
| 165 | 0 | } | 
| 166 |  |  | 
| 167 |  | PKCS8_PRIV_KEY_INFO *PKCS12_decrypt_skey(const PKCS12_SAFEBAG *bag, | 
| 168 |  |                                          const char *pass, int passlen) | 
| 169 | 0 | { | 
| 170 | 0 |     return PKCS12_decrypt_skey_ex(bag, pass, passlen, NULL, NULL); | 
| 171 | 0 | } | 
| 172 |  |  | 
| 173 |  | int PKCS12_pack_authsafes(PKCS12 *p12, STACK_OF(PKCS7) *safes) | 
| 174 | 0 | { | 
| 175 | 0 |     if (ASN1_item_pack(safes, ASN1_ITEM_rptr(PKCS12_AUTHSAFES), | 
| 176 | 0 |                        &p12->authsafes->d.data)) | 
| 177 | 0 |         return 1; | 
| 178 | 0 |     return 0; | 
| 179 | 0 | } | 
| 180 |  |  | 
| 181 |  | STACK_OF(PKCS7) *PKCS12_unpack_authsafes(const PKCS12 *p12) | 
| 182 | 0 | { | 
| 183 | 0 |     STACK_OF(PKCS7) *p7s; | 
| 184 | 0 |     PKCS7 *p7; | 
| 185 | 0 |     int i; | 
| 186 |  | 
 | 
| 187 | 0 |     if (!PKCS7_type_is_data(p12->authsafes)) { | 
| 188 | 0 |         ERR_raise(ERR_LIB_PKCS12, PKCS12_R_CONTENT_TYPE_NOT_DATA); | 
| 189 | 0 |         return NULL; | 
| 190 | 0 |     } | 
| 191 | 0 |     p7s = ASN1_item_unpack(p12->authsafes->d.data, | 
| 192 | 0 |                            ASN1_ITEM_rptr(PKCS12_AUTHSAFES)); | 
| 193 | 0 |     if (p7s != NULL) { | 
| 194 | 0 |         for (i = 0; i < sk_PKCS7_num(p7s); i++) { | 
| 195 | 0 |             p7 = sk_PKCS7_value(p7s, i); | 
| 196 | 0 |             if (!ossl_pkcs7_ctx_propagate(p12->authsafes, p7)) | 
| 197 | 0 |                 goto err; | 
| 198 | 0 |         } | 
| 199 | 0 |     } | 
| 200 | 0 |     return p7s; | 
| 201 | 0 | err: | 
| 202 | 0 |     sk_PKCS7_free(p7s); | 
| 203 | 0 |     return NULL; | 
| 204 | 0 | } |