]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
f5afac4b | 2 | * Copyright 1999-2021 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> |
cfcefcbe DSH |
15 | |
16 | /* PKCS#5 password based encryption structure */ | |
17 | ||
9d6b1ce6 | 18 | ASN1_SEQUENCE(PBEPARAM) = { |
0f113f3e MC |
19 | ASN1_SIMPLE(PBEPARAM, salt, ASN1_OCTET_STRING), |
20 | ASN1_SIMPLE(PBEPARAM, iter, ASN1_INTEGER) | |
d339187b | 21 | } ASN1_SEQUENCE_END(PBEPARAM) |
cfcefcbe | 22 | |
9d6b1ce6 | 23 | IMPLEMENT_ASN1_FUNCTIONS(PBEPARAM) |
8d8c7266 | 24 | |
8931b30d DSH |
25 | /* Set an algorithm identifier for a PKCS#5 PBE algorithm */ |
26 | ||
27 | int PKCS5_pbe_set0_algor(X509_ALGOR *algor, int alg, int iter, | |
0f113f3e MC |
28 | const unsigned char *salt, int saltlen) |
29 | { | |
30 | PBEPARAM *pbe = NULL; | |
31 | ASN1_STRING *pbe_str = NULL; | |
17ebf85a | 32 | unsigned char *sstr = NULL; |
0f113f3e MC |
33 | |
34 | pbe = PBEPARAM_new(); | |
90945fa3 | 35 | if (pbe == NULL) { |
9311d0c4 | 36 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
37 | goto err; |
38 | } | |
39 | if (iter <= 0) | |
40 | iter = PKCS5_DEFAULT_ITER; | |
41 | if (!ASN1_INTEGER_set(pbe->iter, iter)) { | |
9311d0c4 | 42 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
43 | goto err; |
44 | } | |
45 | if (!saltlen) | |
46 | saltlen = PKCS5_SALT_LEN; | |
42e7d2f1 SL |
47 | if (saltlen < 0) |
48 | goto err; | |
17ebf85a DSH |
49 | |
50 | sstr = OPENSSL_malloc(saltlen); | |
51 | if (sstr == NULL) { | |
9311d0c4 | 52 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
53 | goto err; |
54 | } | |
0f113f3e MC |
55 | if (salt) |
56 | memcpy(sstr, salt, saltlen); | |
266483d2 | 57 | else if (RAND_bytes(sstr, saltlen) <= 0) |
0f113f3e MC |
58 | goto err; |
59 | ||
17ebf85a DSH |
60 | ASN1_STRING_set0(pbe->salt, sstr, saltlen); |
61 | sstr = NULL; | |
62 | ||
0f113f3e | 63 | if (!ASN1_item_pack(pbe, ASN1_ITEM_rptr(PBEPARAM), &pbe_str)) { |
9311d0c4 | 64 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
65 | goto err; |
66 | } | |
67 | ||
68 | PBEPARAM_free(pbe); | |
69 | pbe = NULL; | |
70 | ||
71 | if (X509_ALGOR_set0(algor, OBJ_nid2obj(alg), V_ASN1_SEQUENCE, pbe_str)) | |
72 | return 1; | |
73 | ||
74 | err: | |
17ebf85a | 75 | OPENSSL_free(sstr); |
25aaa98a | 76 | PBEPARAM_free(pbe); |
0dfb9398 | 77 | ASN1_STRING_free(pbe_str); |
0f113f3e MC |
78 | return 0; |
79 | } | |
8931b30d DSH |
80 | |
81 | /* Return an algorithm identifier for a PKCS#5 PBE algorithm */ | |
82 | ||
83 | X509_ALGOR *PKCS5_pbe_set(int alg, int iter, | |
0f113f3e MC |
84 | const unsigned char *salt, int saltlen) |
85 | { | |
86 | X509_ALGOR *ret; | |
87 | ret = X509_ALGOR_new(); | |
90945fa3 | 88 | if (ret == NULL) { |
9311d0c4 | 89 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
90 | return NULL; |
91 | } | |
92 | ||
93 | if (PKCS5_pbe_set0_algor(ret, alg, iter, salt, saltlen)) | |
94 | return ret; | |
95 | ||
96 | X509_ALGOR_free(ret); | |
97 | return NULL; | |
98 | } |