2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2016-05-17 18:51:34 +00:00
|
|
|
* Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
|
1999-03-28 17:46:10 +00:00
|
|
|
*
|
2016-05-17 18:51:34 +00:00
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
1999-03-28 17:46:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2000-12-08 19:09:35 +00:00
|
|
|
#include <openssl/asn1t.h>
|
1999-07-21 22:10:23 +00:00
|
|
|
#include <openssl/x509.h>
|
1999-04-23 22:13:45 +00:00
|
|
|
#include <openssl/rand.h>
|
1999-03-28 17:46:10 +00:00
|
|
|
|
|
|
|
/* PKCS#5 password based encryption structure */
|
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
ASN1_SEQUENCE(PBEPARAM) = {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1_SIMPLE(PBEPARAM, salt, ASN1_OCTET_STRING),
|
|
|
|
ASN1_SIMPLE(PBEPARAM, iter, ASN1_INTEGER)
|
2001-02-23 12:47:06 +00:00
|
|
|
} ASN1_SEQUENCE_END(PBEPARAM)
|
1999-03-28 17:46:10 +00:00
|
|
|
|
2000-12-08 19:09:35 +00:00
|
|
|
IMPLEMENT_ASN1_FUNCTIONS(PBEPARAM)
|
1999-03-28 23:17:34 +00:00
|
|
|
|
2008-03-12 21:14:28 +00:00
|
|
|
/* Set an algorithm identifier for a PKCS#5 PBE algorithm */
|
|
|
|
|
|
|
|
int PKCS5_pbe_set0_algor(X509_ALGOR *algor, int alg, int iter,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *salt, int saltlen)
|
|
|
|
{
|
|
|
|
PBEPARAM *pbe = NULL;
|
|
|
|
ASN1_STRING *pbe_str = NULL;
|
|
|
|
unsigned char *sstr;
|
|
|
|
|
|
|
|
pbe = PBEPARAM_new();
|
2015-10-30 11:12:26 +00:00
|
|
|
if (pbe == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (iter <= 0)
|
|
|
|
iter = PKCS5_DEFAULT_ITER;
|
|
|
|
if (!ASN1_INTEGER_set(pbe->iter, iter)) {
|
|
|
|
ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!saltlen)
|
|
|
|
saltlen = PKCS5_SALT_LEN;
|
|
|
|
if (!ASN1_STRING_set(pbe->salt, NULL, saltlen)) {
|
|
|
|
ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
sstr = ASN1_STRING_data(pbe->salt);
|
|
|
|
if (salt)
|
|
|
|
memcpy(sstr, salt, saltlen);
|
2015-02-26 11:57:37 +00:00
|
|
|
else if (RAND_bytes(sstr, saltlen) <= 0)
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
if (!ASN1_item_pack(pbe, ASN1_ITEM_rptr(PBEPARAM), &pbe_str)) {
|
|
|
|
ASN1err(ASN1_F_PKCS5_PBE_SET0_ALGOR, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
|
|
|
|
PBEPARAM_free(pbe);
|
|
|
|
pbe = NULL;
|
|
|
|
|
|
|
|
if (X509_ALGOR_set0(algor, OBJ_nid2obj(alg), V_ASN1_SEQUENCE, pbe_str))
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
err:
|
2015-05-01 18:37:16 +00:00
|
|
|
PBEPARAM_free(pbe);
|
2015-03-24 11:52:24 +00:00
|
|
|
ASN1_STRING_free(pbe_str);
|
2015-01-22 03:40:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2008-03-12 21:14:28 +00:00
|
|
|
|
|
|
|
/* Return an algorithm identifier for a PKCS#5 PBE algorithm */
|
|
|
|
|
|
|
|
X509_ALGOR *PKCS5_pbe_set(int alg, int iter,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *salt, int saltlen)
|
|
|
|
{
|
|
|
|
X509_ALGOR *ret;
|
|
|
|
ret = X509_ALGOR_new();
|
2015-10-30 11:12:26 +00:00
|
|
|
if (ret == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
ASN1err(ASN1_F_PKCS5_PBE_SET, ERR_R_MALLOC_FAILURE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PKCS5_pbe_set0_algor(ret, alg, iter, salt, saltlen))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
X509_ALGOR_free(ret);
|
|
|
|
return NULL;
|
|
|
|
}
|