]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
2039c421 | 2 | * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved. |
cfcefcbe | 3 | * |
2039c421 RS |
4 | * Licensed under the OpenSSL license (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 | |
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; | |
32 | unsigned char *sstr; | |
33 | ||
34 | pbe = PBEPARAM_new(); | |
90945fa3 | 35 | if (pbe == NULL) { |
0f113f3e MC |
36 | ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE); |
37 | goto err; | |
38 | } | |
39 | if (iter <= 0) | |
40 | iter = PKCS5_DEFAULT_ITER; | |
41 | if (!ASN1_INTEGER_set(pbe->iter, iter)) { | |
42 | ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE); | |
43 | goto err; | |
44 | } | |
45 | if (!saltlen) | |
46 | saltlen = PKCS5_SALT_LEN; | |
47 | if (!ASN1_STRING_set(pbe->salt, NULL, saltlen)) { | |
48 | ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE); | |
49 | goto err; | |
50 | } | |
51 | sstr = ASN1_STRING_data(pbe->salt); | |
52 | if (salt) | |
53 | memcpy(sstr, salt, saltlen); | |
266483d2 | 54 | else if (RAND_bytes(sstr, saltlen) <= 0) |
0f113f3e MC |
55 | goto err; |
56 | ||
57 | if (!ASN1_item_pack(pbe, ASN1_ITEM_rptr(PBEPARAM), &pbe_str)) { | |
58 | ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE); | |
59 | goto err; | |
60 | } | |
61 | ||
62 | PBEPARAM_free(pbe); | |
63 | pbe = NULL; | |
64 | ||
65 | if (X509_ALGOR_set0(algor, OBJ_nid2obj(alg), V_ASN1_SEQUENCE, pbe_str)) | |
66 | return 1; | |
67 | ||
68 | err: | |
25aaa98a | 69 | PBEPARAM_free(pbe); |
0dfb9398 | 70 | ASN1_STRING_free(pbe_str); |
0f113f3e MC |
71 | return 0; |
72 | } | |
8931b30d DSH |
73 | |
74 | /* Return an algorithm identifier for a PKCS#5 PBE algorithm */ | |
75 | ||
76 | X509_ALGOR *PKCS5_pbe_set(int alg, int iter, | |
0f113f3e MC |
77 | const unsigned char *salt, int saltlen) |
78 | { | |
79 | X509_ALGOR *ret; | |
80 | ret = X509_ALGOR_new(); | |
90945fa3 | 81 | if (ret == NULL) { |
0f113f3e MC |
82 | ASN1err(ASN1_F_PKCS5_PBE_SET, ERR_R_MALLOC_FAILURE); |
83 | return NULL; | |
84 | } | |
85 | ||
86 | if (PKCS5_pbe_set0_algor(ret, alg, iter, salt, saltlen)) | |
87 | return ret; | |
88 | ||
89 | X509_ALGOR_free(ret); | |
90 | return NULL; | |
91 | } |