first step of v2 final release

This commit is contained in:
Zhi Guan
2017-11-05 21:00:36 +08:00
parent 480b9e8d88
commit 27bde477a5
395 changed files with 26341 additions and 31364 deletions

View File

@@ -13,7 +13,12 @@
#include <openssl/x509v3.h>
#include <openssl/err.h>
#include <openssl/cms.h>
#include <openssl/aes.h>
#ifndef OPENSSL_NO_AES
# include <openssl/aes.h>
#endif
#ifndef OPENSSL_NO_SMS4
# include <openssl/sms4.h>
#endif
#include "cms_lcl.h"
#include "internal/asn1_int.h"
#include "internal/evp_int.h"
@@ -614,10 +619,17 @@ static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
{
CMS_EncryptedContentInfo *ec;
CMS_KEKRecipientInfo *kekri;
AES_KEY actx;
unsigned char *wkey = NULL;
int wkeylen;
int r = 0;
#if !defined(OPENSSL_NO_AES)
AES_KEY actx;
#elif !defined(OPENSSL_NO_SMS4)
sms4_key_t sctx;
#else
CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_NO_AVAILABLE_CIPHER);
return 0;
#endif
ec = cms->d.envelopedData->encryptedContentInfo;
@@ -628,11 +640,15 @@ static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
return 0;
}
#if !defined(OPENSSL_NO_AES)
if (AES_set_encrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT,
CMS_R_ERROR_SETTING_KEY);
goto err;
}
#elif !defined(OPENSSL_NO_SMS4)
sms4_set_encrypt_key(&sctx, kekri->key);
#endif
wkey = OPENSSL_malloc(ec->keylen + 8);
@@ -641,7 +657,11 @@ static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
goto err;
}
#ifndef OPENSSL_NO_AES
wkeylen = AES_wrap_key(&actx, NULL, wkey, ec->key, ec->keylen);
#elif !defined(OPENSSL_NO_SMS4)
wkeylen = sms4_wrap_key(&sctx, NULL, wkey, ec->key, ec->keylen);
#endif
if (wkeylen <= 0) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_ENCRYPT, CMS_R_WRAP_ERROR);
@@ -656,7 +676,11 @@ static int cms_RecipientInfo_kekri_encrypt(CMS_ContentInfo *cms,
if (!r)
OPENSSL_free(wkey);
#ifndef OPENSSL_NO_AES
OPENSSL_cleanse(&actx, sizeof(actx));
#elif !defined(OPENSSL_NO_SMS4)
OPENSSL_cleanse(&sctx, sizeof(sctx));
#endif
return r;
@@ -669,10 +693,17 @@ static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
{
CMS_EncryptedContentInfo *ec;
CMS_KEKRecipientInfo *kekri;
AES_KEY actx;
unsigned char *ukey = NULL;
int ukeylen;
int r = 0, wrap_nid;
#ifndef OPENSSL_NO_AES
AES_KEY actx;
#elif !defined(OPENSSL_NO_SMS4)
sms4_key_t sctx;
#else
CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_NO_AVAILABLE_CIPHER);
return 0;
#endif
ec = cms->d.envelopedData->encryptedContentInfo;
@@ -684,11 +715,19 @@ static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
}
wrap_nid = OBJ_obj2nid(kekri->keyEncryptionAlgorithm->algorithm);
#ifndef OPENSSL_NO_AES
if (aes_wrap_keylen(wrap_nid) != kekri->keylen) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
CMS_R_INVALID_KEY_LENGTH);
return 0;
}
#elif !defined(OPENSSL_NO_SMS4)
if (SMS4_KEY_LENGTH != kekri->keylen) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
CMS_R_INVALID_KEY_LENGTH);
return 0;
}
#endif
/* If encrypted key length is invalid don't bother */
@@ -698,11 +737,15 @@ static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
goto err;
}
#ifndef OPENSSL_NO_AES
if (AES_set_decrypt_key(kekri->key, kekri->keylen << 3, &actx)) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT,
CMS_R_ERROR_SETTING_KEY);
goto err;
}
#elif !defined(OPENSSL_NO_SMS4)
sms4_set_decrypt_key(&sctx, kekri->key);
#endif
ukey = OPENSSL_malloc(kekri->encryptedKey->length - 8);
@@ -711,9 +754,15 @@ static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
goto err;
}
#ifndef OPENSSL_NO_AES
ukeylen = AES_unwrap_key(&actx, NULL, ukey,
kekri->encryptedKey->data,
kekri->encryptedKey->length);
#elif !defined(OPENSSL_NO_SMS4)
ukeylen = sms4_unwrap_key(&sctx, NULL, ukey,
kekri->encryptedKey->data,
kekri->encryptedKey->length);
#endif
if (ukeylen <= 0) {
CMSerr(CMS_F_CMS_RECIPIENTINFO_KEKRI_DECRYPT, CMS_R_UNWRAP_ERROR);
@@ -729,7 +778,11 @@ static int cms_RecipientInfo_kekri_decrypt(CMS_ContentInfo *cms,
if (!r)
OPENSSL_free(ukey);
#ifndef OPENSSL_NO_AES
OPENSSL_cleanse(&actx, sizeof(actx));
#elif !defined(OPENSSL_NO_SMS4)
OPENSSL_cleanse(&sctx, sizeof(sctx));
#endif
return r;

View File

@@ -1,6 +1,6 @@
/*
* Generated by util/mkerr.pl DO NOT EDIT
* Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
* Copyright 1995-2017 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the OpenSSL license (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
@@ -189,6 +189,7 @@ static ERR_STRING_DATA CMS_str_reasons[] = {
{ERR_REASON(CMS_R_NOT_PWRI), "not pwri"},
{ERR_REASON(CMS_R_NOT_SUPPORTED_FOR_THIS_KEY_TYPE),
"not supported for this key type"},
{ERR_REASON(CMS_R_NO_AVAILABLE_CIPHER), "no available cipher"},
{ERR_REASON(CMS_R_NO_CIPHER), "no cipher"},
{ERR_REASON(CMS_R_NO_CONTENT), "no content"},
{ERR_REASON(CMS_R_NO_CONTENT_TYPE), "no content type"},

View File

@@ -13,7 +13,9 @@
#include <openssl/x509v3.h>
#include <openssl/err.h>
#include <openssl/cms.h>
#include <openssl/aes.h>
#ifndef OPENSSL_NO_AES
# include <openssl/aes.h>
#endif
#include "cms_lcl.h"
#include "internal/asn1_int.h"
@@ -331,7 +333,9 @@ static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari,
{
EVP_CIPHER_CTX *ctx = kari->ctx;
const EVP_CIPHER *kekcipher;
#ifndef OPENSSL_NO_AES
int keylen = EVP_CIPHER_key_length(cipher);
#endif
/* If a suitable wrap algorithm is already set nothing to do */
kekcipher = EVP_CIPHER_CTX_cipher(ctx);
@@ -344,17 +348,30 @@ static int cms_wrap_init(CMS_KeyAgreeRecipientInfo *kari,
* Pick a cipher based on content encryption cipher. If it is DES3 use
* DES3 wrap otherwise use AES wrap similar to key size.
*/
#ifndef OPENSSL_NO_DES
#if !defined(OPENSSL_NO_DES) && !defined(OPENSSL_NO_SHA)
/* EVP_des_ede3_wrap() depends on EVP_sha1() */
if (EVP_CIPHER_type(cipher) == NID_des_ede3_cbc)
kekcipher = EVP_des_ede3_wrap();
else
#endif
#ifndef OPENSSL_NO_SMS4
if (EVP_CIPHER_type(cipher) == NID_sms4_cbc
|| EVP_CIPHER_type(cipher) == NID_sm1_cbc
|| EVP_CIPHER_type(cipher) == NID_ssf33_cbc)
kekcipher = EVP_sms4_wrap();
else
#endif
#ifndef OPENSSL_NO_AES
if (keylen <= 16)
kekcipher = EVP_aes_128_wrap();
else if (keylen <= 24)
kekcipher = EVP_aes_192_wrap();
else
kekcipher = EVP_aes_256_wrap();
#endif
if (kekcipher == NULL)
return 0;
return EVP_EncryptInit_ex(ctx, kekcipher, NULL, NULL, NULL);
}

View File

@@ -14,7 +14,9 @@
#include <openssl/err.h>
#include <openssl/cms.h>
#include <openssl/rand.h>
#include <openssl/aes.h>
#ifndef OPENSSL_NO_AES
# include <openssl/aes.h>
#endif
#include "cms_lcl.h"
#include "internal/asn1_int.h"

View File

@@ -158,7 +158,12 @@ CMS_ContentInfo *CMS_digest_create(BIO *in, const EVP_MD *md,
{
CMS_ContentInfo *cms;
if (!md)
#ifndef OPENSSL_NO_SHA
md = EVP_sha1();
#else
md = EVP_get_default_digest();
#endif
cms = cms_DigestedData_create(md);
if (!cms)
return NULL;