Catch Base64 exception in Certificate class 12/44812/1 accepted/tizen/mobile/20150728.101153 accepted/tizen/tv/20150728.101234 accepted/tizen/wearable/20150728.101253 submit/tizen/20150728.071759
authorKyungwook Tak <k.tak@samsung.com>
Tue, 28 Jul 2015 06:47:38 +0000 (15:47 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Tue, 28 Jul 2015 06:47:38 +0000 (15:47 +0900)
Change-Id: I2f319e4c39d3e2b4790cad1187cb0b6875732884
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
vcore/src/vcore/Certificate.cpp
vcore/src/vcore/Certificate.h

index c9c8969..f5e3cec 100644 (file)
@@ -55,30 +55,34 @@ Certificate::Certificate(cert_svc_mem_buff &buffer)
                       "Internal Openssl error in d2i_X509 function.");
 }
 
-Certificate::Certificate(const std::string &der,
+Certificate::Certificate(const std::string &data,
                          Certificate::FormType form)
 {
-    Assert(der.size());
+    Assert(data.size());
 
     int size;
     const unsigned char *ptr;
-    std::string tmp;
+    std::string tmp = data;
 
+    // transform to DER format
     if (FORM_BASE64 == form) {
-        Base64Decoder base64;
-        base64.reset();
-        base64.append(der);
-        if (!base64.finalize()) {
-            LogWarning("Error during decoding");
+        try {
+            Base64Decoder base64;
+            base64.reset();
+            base64.append(data);
+            if (!base64.finalize()) {
+                LogWarning("Error during decoding");
+            }
+            tmp = base64.get();
+        } catch (const Base64Decoder::Exception::Base &e) {
+            LogError("Exception in Certificate constructor : " << e.DumpToString());
+            VcoreThrowMsg(Certificate::Exception::Base64Error, "Failed to Base64Decoder");
         }
-        tmp = base64.get();
-        ptr = reinterpret_cast<const unsigned char*>(tmp.c_str());
-        size = static_cast<int>(tmp.size());
-    } else {
-        ptr = reinterpret_cast<const unsigned char*>(der.c_str());
-        size = static_cast<int>(der.size());
     }
 
+    ptr = reinterpret_cast<const unsigned char*>(tmp.c_str());
+    size = static_cast<int>(tmp.size());
+
     m_x509 = d2i_X509(NULL, &ptr, size);
     if (!m_x509)
         VcoreThrowMsg(Certificate::Exception::OpensslInternalError,
@@ -111,9 +115,14 @@ std::string Certificate::getDER(void) const
 std::string Certificate::getBase64(void) const
 {
     Base64Encoder base64;
-    base64.reset();
-    base64.append(getDER());
-    base64.finalize();
+    try {
+        base64.reset();
+        base64.append(getDER());
+        base64.finalize();
+    } catch (const Base64Encoder::Exception::Base &e) {
+        LogError("Exception in Certificate getBase64 : " << e.DumpToString());
+        VcoreThrowMsg(Certificate::Exception::Base64Error, "Failed to Base64Encoder");
+    }
     return base64.get();
 }
 
index e8d2364..1bde60a 100644 (file)
@@ -55,6 +55,7 @@ public:
     public:
         VCORE_DECLARE_EXCEPTION_TYPE(ValidationCore::Exception, Base);
         VCORE_DECLARE_EXCEPTION_TYPE(Base, OpensslInternalError);
+        VCORE_DECLARE_EXCEPTION_TYPE(Base, Base64Error);
     };
 
     typedef std::vector<unsigned char> Fingerprint;
@@ -84,7 +85,7 @@ public:
 
     explicit Certificate(cert_svc_mem_buff &buffer);
 
-    explicit Certificate(const std::string &der,
+    explicit Certificate(const std::string &data,
                          FormType form = FORM_DER);
 
     ~Certificate();