]> www.wagner.pp.ru Git - openssl-gost/engine.git/commitdiff
Refactoring unprotected attributes processing
authorDmitry Belyavskiy <beldmit@gmail.com>
Sat, 9 May 2020 10:18:32 +0000 (13:18 +0300)
committerDmitry Belyavskiy <beldmit@gmail.com>
Sat, 9 May 2020 10:18:32 +0000 (13:18 +0300)
gost_gost2015.c
gost_gost2015.h
gost_grasshopper_cipher.c

index c0a914d80da563b4d5f9280f3fdc49392cefe304..a9e3d351f3cd30907007099be9f99d0293a62dd9 100644 (file)
@@ -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;
+}
index 4feed585b3098f65de5843f286b463b577652c90..31f71e49c00bb21ce14e1ba57f19574bf63e9eb3 100644 (file)
@@ -2,6 +2,7 @@
 #define GOST_GOST2015_H
 
 #include <openssl/evp.h>
+#include <openssl/x509.h>
 
 #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
index f0419e3029c8f8aa8ebe1d6fdebece9477354d87..aee1c64f2e08cd79f8d04701e30f47d17749c0e7 100644 (file)
@@ -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;