From: Dmitry Belyavskiy Date: Sat, 9 May 2020 10:18:32 +0000 (+0300) Subject: Refactoring unprotected attributes processing X-Git-Tag: v3.0.0~131 X-Git-Url: http://www.wagner.pp.ru/gitweb/?a=commitdiff_plain;h=15066ba010e653efb062087f81d86e1ede1692cf;p=openssl-gost%2Fengine.git Refactoring unprotected attributes processing --- diff --git a/gost_gost2015.c b/gost_gost2015.c index c0a914d..a9e3d35 100644 --- a/gost_gost2015.c +++ b/gost_gost2015.c @@ -120,3 +120,23 @@ end: GOST2015_CIPHER_PARAMS_free(gcp); return ret; } + +int gost2015_process_unprotected_attributes(STACK_OF(X509_ATTRIBUTE) *attrs, + int encryption, size_t mac_len, unsigned char *final_tag) +{ + if (encryption == 0) /*Decrypting*/ { + ASN1_OCTET_STRING *osExpectedMac = X509at_get0_data_by_OBJ(attrs, + OBJ_txt2obj(OID_GOST_CMS_MAC, 1), -3, V_ASN1_OCTET_STRING); + + if (!osExpectedMac || osExpectedMac->length != (int)mac_len) + return -1; + + memcpy(final_tag, osExpectedMac->data, osExpectedMac->length); + } else { + if (attrs == NULL) + return -1; + return (X509at_add1_attr_by_OBJ(&attrs, OBJ_txt2obj(OID_GOST_CMS_MAC, 1), + V_ASN1_OCTET_STRING, final_tag, mac_len) == NULL) ? -1 : 1; + } + return 1; +} diff --git a/gost_gost2015.h b/gost_gost2015.h index 4feed58..31f71e4 100644 --- a/gost_gost2015.h +++ b/gost_gost2015.h @@ -2,6 +2,7 @@ #define GOST_GOST2015_H #include +#include #define MAGMA_MAC_MAX_SIZE 8 #define KUZNYECHIK_MAC_MAX_SIZE 16 @@ -20,4 +21,7 @@ int gost2015_get_asn1_params(const ASN1_TYPE *params, size_t ukm_size, int gost2015_set_asn1_params(ASN1_TYPE *params, const unsigned char *iv, size_t iv_size, const unsigned char *kdf_seed); + +int gost2015_process_unprotected_attributes(STACK_OF(X509_ATTRIBUTE) *attrs, + int encryption, size_t mac_len, unsigned char *final_tag); #endif diff --git a/gost_grasshopper_cipher.c b/gost_grasshopper_cipher.c index f0419e3..aee1c64 100644 --- a/gost_grasshopper_cipher.c +++ b/gost_grasshopper_cipher.c @@ -819,8 +819,7 @@ GRASSHOPPER_INLINE int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX return 0; } -int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, - void *ptr) +int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr) { switch (type) { case EVP_CTRL_RAND_KEY:{ @@ -922,34 +921,16 @@ int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX *ctx, int type, int arg, } #endif case EVP_CTRL_PROCESS_UNPROTECTED: - { - gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx); - ASN1_OBJECT *cmsmacobj = NULL; - if (c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC) - return -1; - cmsmacobj = OBJ_txt2obj(OID_GOST_CMS_MAC, 1); - if (cmsmacobj == NULL) { - GOSTerr(GOST_F_GOST_GRASSHOPPER_CIPHER_CTL, ERR_R_MALLOC_FAILURE); - return -1; - } - if (arg == 0) /*Decrypting*/ { - STACK_OF(X509_ATTRIBUTE) *x = ptr; - ASN1_OCTET_STRING *osExpectedMac = X509at_get0_data_by_OBJ(x, - cmsmacobj, -3, V_ASN1_OCTET_STRING); - ASN1_OBJECT_free(cmsmacobj); - - if (ptr == NULL || osExpectedMac ==NULL || osExpectedMac->length != KUZNYECHIK_MAC_MAX_SIZE) - return -1; - - memcpy(c->tag, osExpectedMac->data, osExpectedMac->length); - return 1; - } else { - STACK_OF(X509_ATTRIBUTE) *x = ptr; - return (X509at_add1_attr_by_OBJ(&x, cmsmacobj, - V_ASN1_OCTET_STRING, c->tag, KUZNYECHIK_MAC_MAX_SIZE) == NULL) ? -1 : 1; - } - } - return 1; + { + STACK_OF(X509_ATTRIBUTE) *x = ptr; + gost_grasshopper_cipher_ctx_ctr *c = EVP_CIPHER_CTX_get_cipher_data(ctx); + + if (c->c.type != GRASSHOPPER_CIPHER_CTRACPKMOMAC) + return -1; + + return gost2015_process_unprotected_attributes(x, arg, KUZNYECHIK_MAC_MAX_SIZE, c->tag); + } + return 1; case EVP_CTRL_COPY: { EVP_CIPHER_CTX *out = ptr;