]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
da1c088f | 2 | * Copyright 1999-2023 The OpenSSL Project Authors. All Rights Reserved. |
cfcefcbe | 3 | * |
365a2d99 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
2039c421 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 | |
cfcefcbe DSH |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
b39fc560 | 11 | #include "internal/cryptlib.h" |
9d6b1ce6 | 12 | #include <openssl/asn1t.h> |
f0e8ae72 | 13 | #include <openssl/x509.h> |
ec577822 | 14 | #include <openssl/rand.h> |
3859a027 | 15 | #include "crypto/evp.h" |
cfcefcbe DSH |
16 | |
17 | /* PKCS#5 password based encryption structure */ | |
18 | ||
9d6b1ce6 | 19 | ASN1_SEQUENCE(PBEPARAM) = { |
0f113f3e MC |
20 | ASN1_SIMPLE(PBEPARAM, salt, ASN1_OCTET_STRING), |
21 | ASN1_SIMPLE(PBEPARAM, iter, ASN1_INTEGER) | |
d339187b | 22 | } ASN1_SEQUENCE_END(PBEPARAM) |
cfcefcbe | 23 | |
9d6b1ce6 | 24 | IMPLEMENT_ASN1_FUNCTIONS(PBEPARAM) |
8d8c7266 | 25 | |
8931b30d DSH |
26 | /* Set an algorithm identifier for a PKCS#5 PBE algorithm */ |
27 | ||
b536880c JS |
28 | int PKCS5_pbe_set0_algor_ex(X509_ALGOR *algor, int alg, int iter, |
29 | const unsigned char *salt, int saltlen, | |
30 | OSSL_LIB_CTX *ctx) | |
0f113f3e MC |
31 | { |
32 | PBEPARAM *pbe = NULL; | |
33 | ASN1_STRING *pbe_str = NULL; | |
17ebf85a | 34 | unsigned char *sstr = NULL; |
0f113f3e MC |
35 | |
36 | pbe = PBEPARAM_new(); | |
90945fa3 | 37 | if (pbe == NULL) { |
e077455e RL |
38 | /* ERR_R_ASN1_LIB, because PBEPARAM_new() is defined in crypto/asn1 */ |
39 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); | |
0f113f3e MC |
40 | goto err; |
41 | } | |
42 | if (iter <= 0) | |
43 | iter = PKCS5_DEFAULT_ITER; | |
44 | if (!ASN1_INTEGER_set(pbe->iter, iter)) { | |
e077455e | 45 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
0f113f3e MC |
46 | goto err; |
47 | } | |
48 | if (!saltlen) | |
3859a027 | 49 | saltlen = PKCS5_DEFAULT_PBE1_SALT_LEN; |
42e7d2f1 SL |
50 | if (saltlen < 0) |
51 | goto err; | |
17ebf85a DSH |
52 | |
53 | sstr = OPENSSL_malloc(saltlen); | |
e077455e | 54 | if (sstr == NULL) |
0f113f3e | 55 | goto err; |
0f113f3e MC |
56 | if (salt) |
57 | memcpy(sstr, salt, saltlen); | |
5cbd2ea3 | 58 | else if (RAND_bytes_ex(ctx, sstr, saltlen, 0) <= 0) |
0f113f3e MC |
59 | goto err; |
60 | ||
17ebf85a DSH |
61 | ASN1_STRING_set0(pbe->salt, sstr, saltlen); |
62 | sstr = NULL; | |
63 | ||
0f113f3e | 64 | if (!ASN1_item_pack(pbe, ASN1_ITEM_rptr(PBEPARAM), &pbe_str)) { |
e077455e | 65 | ERR_raise(ERR_LIB_ASN1, ERR_R_ASN1_LIB); |
0f113f3e MC |
66 | goto err; |
67 | } | |
68 | ||
69 | PBEPARAM_free(pbe); | |
70 | pbe = NULL; | |
71 | ||
72 | if (X509_ALGOR_set0(algor, OBJ_nid2obj(alg), V_ASN1_SEQUENCE, pbe_str)) | |
73 | return 1; | |
74 | ||
75 | err: | |
17ebf85a | 76 | OPENSSL_free(sstr); |
25aaa98a | 77 | PBEPARAM_free(pbe); |
0dfb9398 | 78 | ASN1_STRING_free(pbe_str); |
0f113f3e MC |
79 | return 0; |
80 | } | |
8931b30d | 81 | |
b536880c JS |
82 | int PKCS5_pbe_set0_algor(X509_ALGOR *algor, int alg, int iter, |
83 | const unsigned char *salt, int saltlen) | |
84 | { | |
85 | return PKCS5_pbe_set0_algor_ex(algor, alg, iter, salt, saltlen, NULL); | |
86 | } | |
87 | ||
8931b30d DSH |
88 | /* Return an algorithm identifier for a PKCS#5 PBE algorithm */ |
89 | ||
b536880c JS |
90 | X509_ALGOR *PKCS5_pbe_set_ex(int alg, int iter, |
91 | const unsigned char *salt, int saltlen, | |
92 | OSSL_LIB_CTX *ctx) | |
0f113f3e MC |
93 | { |
94 | X509_ALGOR *ret; | |
95 | ret = X509_ALGOR_new(); | |
90945fa3 | 96 | if (ret == NULL) { |
e077455e | 97 | ERR_raise(ERR_LIB_ASN1, ERR_R_X509_LIB); |
0f113f3e MC |
98 | return NULL; |
99 | } | |
100 | ||
b536880c | 101 | if (PKCS5_pbe_set0_algor_ex(ret, alg, iter, salt, saltlen, ctx)) |
0f113f3e MC |
102 | return ret; |
103 | ||
104 | X509_ALGOR_free(ret); | |
105 | return NULL; | |
106 | } | |
b536880c JS |
107 | |
108 | X509_ALGOR *PKCS5_pbe_set(int alg, int iter, | |
109 | const unsigned char *salt, int saltlen) | |
110 | { | |
111 | return PKCS5_pbe_set_ex(alg, iter, salt, saltlen, NULL); | |
112 | } | |
113 |