/src/openssl/crypto/asn1/p5_pbe.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
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 |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/asn1t.h> |
13 | | #include <openssl/x509.h> |
14 | | #include <openssl/rand.h> |
15 | | |
16 | | /* PKCS#5 password based encryption structure */ |
17 | | |
18 | | ASN1_SEQUENCE(PBEPARAM) = { |
19 | | ASN1_SIMPLE(PBEPARAM, salt, ASN1_OCTET_STRING), |
20 | | ASN1_SIMPLE(PBEPARAM, iter, ASN1_INTEGER) |
21 | | } ASN1_SEQUENCE_END(PBEPARAM) |
22 | | |
23 | | IMPLEMENT_ASN1_FUNCTIONS(PBEPARAM) |
24 | | |
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, |
28 | | const unsigned char *salt, int saltlen) |
29 | 0 | { |
30 | 0 | PBEPARAM *pbe = NULL; |
31 | 0 | ASN1_STRING *pbe_str = NULL; |
32 | 0 | unsigned char *sstr = NULL; |
33 | 0 |
|
34 | 0 | pbe = PBEPARAM_new(); |
35 | 0 | if (pbe == NULL) { |
36 | 0 | ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE); |
37 | 0 | goto err; |
38 | 0 | } |
39 | 0 | if (iter <= 0) |
40 | 0 | iter = PKCS5_DEFAULT_ITER; |
41 | 0 | if (!ASN1_INTEGER_set(pbe->iter, iter)) { |
42 | 0 | ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE); |
43 | 0 | goto err; |
44 | 0 | } |
45 | 0 | if (!saltlen) |
46 | 0 | saltlen = PKCS5_SALT_LEN; |
47 | 0 |
|
48 | 0 | sstr = OPENSSL_malloc(saltlen); |
49 | 0 | if (sstr == NULL) { |
50 | 0 | ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE); |
51 | 0 | goto err; |
52 | 0 | } |
53 | 0 | if (salt) |
54 | 0 | memcpy(sstr, salt, saltlen); |
55 | 0 | else if (RAND_bytes(sstr, saltlen) <= 0) |
56 | 0 | goto err; |
57 | 0 | |
58 | 0 | ASN1_STRING_set0(pbe->salt, sstr, saltlen); |
59 | 0 | sstr = NULL; |
60 | 0 |
|
61 | 0 | if (!ASN1_item_pack(pbe, ASN1_ITEM_rptr(PBEPARAM), &pbe_str)) { |
62 | 0 | ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE); |
63 | 0 | goto err; |
64 | 0 | } |
65 | 0 |
|
66 | 0 | PBEPARAM_free(pbe); |
67 | 0 | pbe = NULL; |
68 | 0 |
|
69 | 0 | if (X509_ALGOR_set0(algor, OBJ_nid2obj(alg), V_ASN1_SEQUENCE, pbe_str)) |
70 | 0 | return 1; |
71 | 0 | |
72 | 0 | err: |
73 | 0 | OPENSSL_free(sstr); |
74 | 0 | PBEPARAM_free(pbe); |
75 | 0 | ASN1_STRING_free(pbe_str); |
76 | 0 | return 0; |
77 | 0 | } |
78 | | |
79 | | /* Return an algorithm identifier for a PKCS#5 PBE algorithm */ |
80 | | |
81 | | X509_ALGOR *PKCS5_pbe_set(int alg, int iter, |
82 | | const unsigned char *salt, int saltlen) |
83 | 0 | { |
84 | 0 | X509_ALGOR *ret; |
85 | 0 | ret = X509_ALGOR_new(); |
86 | 0 | if (ret == NULL) { |
87 | 0 | ASN1err(ASN1_F_PKCS5_PBE_SET, ERR_R_MALLOC_FAILURE); |
88 | 0 | return NULL; |
89 | 0 | } |
90 | 0 |
|
91 | 0 | if (PKCS5_pbe_set0_algor(ret, alg, iter, salt, saltlen)) |
92 | 0 | return ret; |
93 | 0 | |
94 | 0 | X509_ALGOR_free(ret); |
95 | 0 | return NULL; |
96 | 0 | } |