From d3451faa2190e9f4c12bb00adf00149b7af1d18d Mon Sep 17 00:00:00 2001
From: Jakub Jelen <jjelen@redhat.com>
Date: Mon, 16 Nov 2020 11:57:10 +0100
Subject: [PATCH] tcos: Reformat insert_key

---
 src/libopensc/pkcs15-tcos.c | 65 ++++++++++++++++++++++---------------
 1 file changed, 38 insertions(+), 27 deletions(-)

diff --git a/src/libopensc/pkcs15-tcos.c b/src/libopensc/pkcs15-tcos.c
index c62a3765f9..06cb1a9010 100644
--- a/src/libopensc/pkcs15-tcos.c
+++ b/src/libopensc/pkcs15-tcos.c
@@ -99,9 +99,10 @@ static int insert_key(
 	int               key_length,
 	unsigned char     auth_id,
 	const char       *label
-){
-	sc_card_t *card=p15card->card;
-	sc_context_t *ctx=p15card->card->ctx;
+)
+{
+	sc_card_t *card = p15card->card;
+	sc_context_t *ctx = p15card->card->ctx;
 	sc_file_t *f;
 	struct sc_pkcs15_prkey_info prkey_info;
 	struct sc_pkcs15_object prkey_obj;
@@ -121,13 +122,14 @@ static int insert_key(
 	prkey_obj.auth_id.len      = 1;
 	prkey_obj.auth_id.value[0] = auth_id;
 
-	can_sign=can_crypt=0;
-	if(card->type==SC_CARD_TYPE_TCOS_V3){
+	can_sign = can_crypt = 0;
+	if (card->type == SC_CARD_TYPE_TCOS_V3) {
 		unsigned char buf[256];
-		int i, rec_no=0;
-		if(prkey_info.path.len>=2) prkey_info.path.len-=2;
+		int i, rec_no = 0;
+		if (prkey_info.path.len >= 2)
+			prkey_info.path.len -= 2;
 		sc_append_file_id(&prkey_info.path, 0x5349);
-		if(sc_select_file(card, &prkey_info.path, NULL)!=SC_SUCCESS){
+		if (sc_select_file(card, &prkey_info.path, NULL) != SC_SUCCESS) {
 			sc_log(ctx, 
 				"Select(%s) failed\n",
 				sc_print_path(&prkey_info.path));
@@ -135,24 +137,29 @@ static int insert_key(
 		}
 		sc_log(ctx, 
 			"Searching for Key-Ref %02X\n", key_reference);
-		while((r=sc_read_record(card, ++rec_no, buf, sizeof(buf), SC_RECORD_BY_REC_NR))>0){
-			int found=0;
-			if(buf[0]!=0xA0) continue;
-			for(i=2;i<buf[1]+2;i+=2+buf[i+1]){
-				if(buf[i]==0x83 && buf[i+1]==1 && buf[i+2]==key_reference) ++found;
+		while ((r = sc_read_record(card, ++rec_no, buf, sizeof(buf), SC_RECORD_BY_REC_NR)) > 0) {
+			int found = 0;
+			if (buf[0] != 0xA0)
+				continue;
+			for (i = 2; i < buf[1] + 2; i += 2 + buf[i + 1]) {
+				if (buf[i] == 0x83 && buf[i + 1] == 1 && buf[i + 2] == key_reference)
+					++found;
 			}
-			if(found) break;
+			if (found)
+				break;
 		}
-		if(r<=0){
+		if (r <= 0) {
 			sc_log(ctx, "No EF_KEYD-Record found\n");
 			return 1;
 		}
-		for(i=0;i<r;i+=2+buf[i+1]){
-			if(buf[i]==0xB6) can_sign++;
-			if(buf[i]==0xB8) can_crypt++;
+		for (i = 0; i < r; i += 2 + buf[i + 1]) {
+			if (buf[i] == 0xB6)
+				can_sign++;
+			if (buf[i] == 0xB8)
+				can_crypt++;
 		}
 	} else {
-		if(sc_select_file(card, &prkey_info.path, &f)!=SC_SUCCESS
+		if (sc_select_file(card, &prkey_info.path, &f) != SC_SUCCESS
 			   	|| !f->prop_attr || f->prop_attr_len < 2){
 			sc_log(ctx, 
 				"Select(%s) failed\n",
@@ -160,15 +167,19 @@ static int insert_key(
 			return 1;
 		}
-		if (f->prop_attr[1] & 0x04) can_crypt=1;
-		if (f->prop_attr[1] & 0x08) can_sign=1;
+		if (f->prop_attr[1] & 0x04)
+			can_crypt = 1;
+		if (f->prop_attr[1] & 0x08)
+			can_sign = 1;
 		sc_file_free(f);
 	}
-	prkey_info.usage= SC_PKCS15_PRKEY_USAGE_SIGN;
-	if(can_crypt) prkey_info.usage |= SC_PKCS15_PRKEY_USAGE_ENCRYPT|SC_PKCS15_PRKEY_USAGE_DECRYPT;
-	if(can_sign) prkey_info.usage |= SC_PKCS15_PRKEY_USAGE_NONREPUDIATION;
-
-	r=sc_pkcs15emu_add_rsa_prkey(p15card, &prkey_obj, &prkey_info);
-	if(r!=SC_SUCCESS){
+	prkey_info.usage = SC_PKCS15_PRKEY_USAGE_SIGN;
+	if (can_crypt)
+		prkey_info.usage |= SC_PKCS15_PRKEY_USAGE_ENCRYPT | SC_PKCS15_PRKEY_USAGE_DECRYPT;
+	if (can_sign)
+		prkey_info.usage |= SC_PKCS15_PRKEY_USAGE_NONREPUDIATION;
+
+	r = sc_pkcs15emu_add_rsa_prkey(p15card, &prkey_obj, &prkey_info);
+	if(r != SC_SUCCESS) {
 		sc_log(ctx,  "sc_pkcs15emu_add_rsa_prkey(%s) failed\n", path);
 		return 4;
 	}
