]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
b1322259 | 2 | * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved. |
19da1300 | 3 | * |
54fffdf4 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
b1322259 RS |
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 | |
19da1300 DSH |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
19da1300 | 12 | #include <openssl/pkcs12.h> |
25f2138b | 13 | #include "crypto/x509.h" |
19da1300 DSH |
14 | |
15 | X509_SIG *PKCS8_encrypt(int pbe_nid, const EVP_CIPHER *cipher, | |
0f113f3e MC |
16 | const char *pass, int passlen, |
17 | unsigned char *salt, int saltlen, int iter, | |
18 | PKCS8_PRIV_KEY_INFO *p8inf) | |
19da1300 | 19 | { |
6355d315 | 20 | X509_SIG *p8 = NULL; |
0f113f3e | 21 | X509_ALGOR *pbe; |
19da1300 | 22 | |
0f113f3e MC |
23 | if (pbe_nid == -1) |
24 | pbe = PKCS5_pbe2_set(cipher, iter, salt, saltlen); | |
25 | else if (EVP_PBE_find(EVP_PBE_TYPE_PRF, pbe_nid, NULL, NULL, 0)) | |
26 | pbe = PKCS5_pbe2_set_iv(cipher, iter, salt, saltlen, NULL, pbe_nid); | |
27 | else { | |
28 | ERR_clear_error(); | |
29 | pbe = PKCS5_pbe_set(pbe_nid, iter, salt, saltlen); | |
30 | } | |
12a765a5 | 31 | if (pbe == NULL) { |
0f113f3e | 32 | PKCS12err(PKCS12_F_PKCS8_ENCRYPT, ERR_R_ASN1_LIB); |
6355d315 | 33 | return NULL; |
0f113f3e | 34 | } |
6355d315 DSH |
35 | p8 = PKCS8_set0_pbe(pass, passlen, p8inf, pbe); |
36 | if (p8 == NULL) { | |
37 | X509_ALGOR_free(pbe); | |
38 | return NULL; | |
39 | } | |
40 | ||
41 | return p8; | |
42 | } | |
43 | ||
44 | X509_SIG *PKCS8_set0_pbe(const char *pass, int passlen, | |
45 | PKCS8_PRIV_KEY_INFO *p8inf, X509_ALGOR *pbe) | |
46 | { | |
47 | X509_SIG *p8; | |
48 | ASN1_OCTET_STRING *enckey; | |
49 | ||
50 | enckey = | |
0f113f3e MC |
51 | PKCS12_item_i2d_encrypt(pbe, ASN1_ITEM_rptr(PKCS8_PRIV_KEY_INFO), |
52 | pass, passlen, p8inf, 1); | |
6355d315 DSH |
53 | if (!enckey) { |
54 | PKCS12err(PKCS12_F_PKCS8_SET0_PBE, PKCS12_R_ENCRYPT_ERROR); | |
55 | return NULL; | |
0f113f3e | 56 | } |
19da1300 | 57 | |
a6eb1ce6 DSH |
58 | p8 = OPENSSL_zalloc(sizeof(*p8)); |
59 | ||
60 | if (p8 == NULL) { | |
6355d315 DSH |
61 | PKCS12err(PKCS12_F_PKCS8_SET0_PBE, ERR_R_MALLOC_FAILURE); |
62 | ASN1_OCTET_STRING_free(enckey); | |
63 | return NULL; | |
64 | } | |
6355d315 DSH |
65 | p8->algor = pbe; |
66 | p8->digest = enckey; | |
19da1300 | 67 | |
6355d315 | 68 | return p8; |
19da1300 | 69 | } |