From 358a734ae92731e558c3473309d0f146589e29e1 Mon Sep 17 00:00:00 2001
From: Nikos Mavrogiannopoulos <nmav@redhat.com>
Date: Mon, 18 Jan 2016 13:32:13 +0100
Subject: [PATCH] Fixed crash in public key access

Resolves: rhbz#1298669
---
 opensc-0.15.0-pubkey-crash.patch | 31 +++++++++++++++++++++++++++++++
 opensc.spec                      |  6 +++++-
 2 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 opensc-0.15.0-pubkey-crash.patch

diff --git a/opensc-0.15.0-pubkey-crash.patch b/opensc-0.15.0-pubkey-crash.patch
new file mode 100644
index 0000000..da6dd77
--- /dev/null
+++ b/opensc-0.15.0-pubkey-crash.patch
@@ -0,0 +1,31 @@
+From 6e5ae841eb398b6393d7349d45f2386f820c9f5f Mon Sep 17 00:00:00 2001
+From: LE TOUX Vincent <HG2025@sdmn01.sirius.infra.com>
+Date: Sat, 2 Jan 2016 09:31:36 +0100
+Subject: [PATCH] fix a pkcs11 crash when the public key reading fails
+
+---
+ src/pkcs11/framework-pkcs15.c | 8 +++++---
+ 1 file changed, 5 insertions(+), 3 deletions(-)
+
+diff --git a/src/pkcs11/framework-pkcs15.c b/src/pkcs11/framework-pkcs15.c
+index e103550..8496f43 100644
+--- a/src/pkcs11/framework-pkcs15.c
++++ b/src/pkcs11/framework-pkcs15.c
+@@ -772,9 +772,11 @@ __pkcs15_prkey_bind_related(struct pkcs15_fw_data *fw_data, struct pkcs15_prkey_
+ 			if (sc_pkcs15_compare_id(&pubkey->pub_info->id, id)) {
+ 				sc_log(context, "Associating object %d as public key", i);
+ 				pk->prv_pubkey = pubkey;
+-				sc_pkcs15_dup_pubkey(context, pubkey->pub_data, &pk->pub_data);
+-				if (pk->prv_info->modulus_length == 0)
+-					pk->prv_info->modulus_length = pubkey->pub_info->modulus_length;
++				if (pubkey->pub_data) {
++					sc_pkcs15_dup_pubkey(context, pubkey->pub_data, &pk->pub_data);
++					if (pk->prv_info->modulus_length == 0)
++						pk->prv_info->modulus_length = pubkey->pub_info->modulus_length;
++				}
+ 			}
+ 		}
+ 	}
+-- 
+2.5.0
+
diff --git a/opensc.spec b/opensc.spec
index 1a51793..f0d91a2 100644
--- a/opensc.spec
+++ b/opensc.spec
@@ -1,6 +1,6 @@
 Name:           opensc
 Version:        0.15.0
-Release:        3%{?dist}
+Release:        4%{?dist}
 Summary:        Smart card library and applications
 
 Group:          System Environment/Libraries
@@ -22,6 +22,7 @@ Obsoletes:      opensc-devel < 0.12.0
 
 Patch0:		opensc-0.15.0-fork-issue.patch
 Patch1:		opensc-export-symbols.patch
+Patch2:		opensc-0.15.0-pubkey-crash.patch
 
 %description
 OpenSC provides a set of libraries and utilities to work with smart cards. Its
@@ -139,6 +140,9 @@ rm -rf %{buildroot}%{_sysconfdir}/bash_completion.d/
 
 
 %changelog
+* Thu Nov 19 2015 Nikos Mavrogiannopoulos <nmav@redhat.com> - 0.15.0-4
+- Fix a crash in accessing public key (#1298669)
+
 * Thu Nov 19 2015 Nikos Mavrogiannopoulos <nmav@redhat.com> - 0.15.0-3
 - Export PKCS#11 symbols from spy library (#1283306)