File status check before fopen
authorDmitriy Zhuravlev <d.zhuravlev@samsung.com>
Fri, 1 Apr 2016 06:50:58 +0000 (09:50 +0300)
committerRandeep Singh <randeep.s@samsung.com>
Tue, 5 Apr 2016 06:17:20 +0000 (06:17 +0000)
Related issue: https://jira.iotivity.org/browse/IOT-1047

Change-Id: I83b82980b122e1336b18e4c864cf8bc3826abb6d
Signed-off-by: Dmitriy Zhuravlev <d.zhuravlev@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/7491
Reviewed-by: Oleksandr Dmytrenko <o.dmytrenko@samsung.com>
Reviewed-by: Ivan Pazderskyy <i.pazderskyy@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Jongsung Lee <js126.lee@samsung.com>
Reviewed-by: Kyungsun Cho <goodsun.cho@samsung.com>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
resource/csdk/security/provisioning/ck_manager/include/ck_manager.h
resource/csdk/security/provisioning/ck_manager/src/ck_manager.c
resource/csdk/security/provisioning/ck_manager/src/ckm_info.c
resource/csdk/stack/include/octypes.h

index 02ac6a3..19b5081 100644 (file)
@@ -135,7 +135,7 @@ PKIError CKMIssueDeviceCertificate (const uint8_t *uint8SubjectName,
  * @param[in] certFileName pointer to null-terminated string with file name
  * @return PKI_SUCCESS if success, error code otherwise
  */
-PKIError GenerateDERCertificateFile (const ByteArray *certificate, const char *certFileName);
+PKIError GenerateDERCertificateFile (const ByteArray *certificate, const char * const certFileName);
 
 /**
  * Issues certificate signing request with specified parameters.
index 1b30469..ade8815 100644 (file)
 #include "crlresource.h"
 #include "oic_malloc.h"
 
+#ifdef __unix__
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#endif // __unix__
+
 /* The first octet of the OCTET STRING indicates whether the key is
 compressed or uncompressed.  The uncompressed form is indicated by 0x04
 and the compressed form is indicated by either 0x02 or 0x03 (RFC 5480)*/
@@ -310,14 +316,37 @@ PKIError CKMIssueDeviceCertificate (const uint8_t *uint8SubjectName,
     );
 }
 
-PKIError GenerateDERCertificateFile (const ByteArray *certificate, const char *certFileName)
+PKIError GenerateDERCertificateFile (const ByteArray *certificate, const char * const certFileName)
 {
     FUNCTION_INIT();
+
+#ifdef __unix__
+    struct stat st;
+    int fd = -1;
+#else
     FILE *filePointer = NULL;
+#endif
 
     CHECK_NULL(certFileName, ISSUER_NULL_PASSED);
     CHECK_NULL(certificate, ISSUER_NULL_PASSED);
     CHECK_NULL(certificate->data, ISSUER_NULL_PASSED);
+
+#ifdef __unix__
+    fd = open(certFileName, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
+    CHECK_NOT_EQUAL(fd, -1, ISSUER_NULL_PASSED);
+    CHECK_EQUAL(fstat(fd, &st), 0, ISSUER_NULL_PASSED);
+    CHECK_COND(S_ISREG(st.st_mode), ISSUER_FILE_WRITE_ERROR);
+    CHECK_COND(!S_ISLNK(st.st_mode), ISSUER_FILE_WRITE_ERROR);
+    CHECK_EQUAL(write(fd, certificate->data, certificate->len), (ssize_t) certificate->len,
+            ISSUER_FILE_WRITE_ERROR);
+
+    FUNCTION_CLEAR(
+        if(-1 != fd)
+        {
+            close(fd);
+        }
+    );
+#else
     filePointer = fopen(certFileName, "wb");
     CHECK_NULL(filePointer, ISSUER_FILE_WRITE_ERROR);
     CHECK_EQUAL(fwrite(certificate->data, 1, certificate->len, filePointer), certificate->len,
@@ -325,11 +354,13 @@ PKIError GenerateDERCertificateFile (const ByteArray *certificate, const char *c
 
     FUNCTION_CLEAR(
         if(filePointer)
-            {
-                fclose(filePointer);
-            }
+        {
+            fclose(filePointer);
+        }
         filePointer = NULL;
     );
+#endif
+
 }
 
 PKIError SetSerialNumber (const long serNum)
index ee03dfc..2aea365 100644 (file)
 #include "crlresource.h"
 #include "crl_generator.h"
 
+#ifdef __unix__
+#include <sys/stat.h>
+#endif // __unix__
+
 //constants used in ckmInfo
 #define CKM_INFO_IS_NOT_LOADED                       (0)
 #define CKM_INFO_IS_LOADED                           (1)
@@ -84,6 +88,14 @@ PKIError InitCKMInfo(void)
         }
         else ////create new storage
         {
+#ifdef __unix__
+            struct stat st;
+            if (0 == lstat(CA_STORAGE_FILE, &st))
+            {
+                CHECK_COND(S_ISREG(st.st_mode), ISSUER_FILE_WRITE_ERROR);
+                CHECK_COND(!S_ISLNK(st.st_mode), ISSUER_FILE_WRITE_ERROR);
+            }
+#endif
             filePointer = fopen(CA_STORAGE_FILE, "wb");
             CHECK_NULL(filePointer, ISSUER_CA_STORAGE_FILE_WRITE_ERROR);
             objectsWrote = fwrite(&g_ckmInfo, sizeof(CKMInfo_t), count, filePointer);
@@ -108,8 +120,18 @@ PKIError SaveCKMInfo(void)
     FILE *filePointer = NULL;
     int count = 1;
     int objectsWrote = 0;
+#ifdef __unix__
+    struct stat st;
+#endif
 
     CHECK_COND(g_ckmInfo.CKMInfoIsLoaded, CKM_INFO_IS_NOT_INIT);
+#ifdef __unix__
+    if (0 == lstat(CA_STORAGE_FILE, &st))
+    {
+        CHECK_COND(S_ISREG(st.st_mode), ISSUER_FILE_WRITE_ERROR);
+        CHECK_COND(!S_ISLNK(st.st_mode), ISSUER_FILE_WRITE_ERROR);
+    }
+#endif
     filePointer = fopen(CA_STORAGE_FILE, "wb");
     CHECK_NULL(filePointer, ISSUER_CA_STORAGE_FILE_WRITE_ERROR);
     objectsWrote = fwrite(&g_ckmInfo, sizeof(CKMInfo_t), count, filePointer);
@@ -326,7 +348,14 @@ PKIError SaveCRT(void)
     FILE *filePointer = NULL;
     uint32_t objectsWrote = 0;
     uint8_t prefix[CERT_LEN_PREFIX] = {0};
-
+#ifdef __unix__
+    struct stat st;
+    if (0 == lstat(CA_STORAGE_CRT_FILE, &st))
+    {
+        CHECK_COND(S_ISREG(st.st_mode), ISSUER_FILE_WRITE_ERROR);
+        CHECK_COND(!S_ISLNK(st.st_mode), ISSUER_FILE_WRITE_ERROR);
+    }
+#endif
     filePointer = fopen(CA_STORAGE_CRT_FILE, "wb");
     CHECK_NULL(filePointer, ISSUER_CA_STORAGE_CRT_WRITE_ERROR);
 
index 3bf3c8b..97b748c 100644 (file)
@@ -791,6 +791,7 @@ typedef enum
  * Persistent storage handlers. An APP must provide OCPersistentStorage handler pointers
  * when it calls OCRegisterPersistentStorageHandler.
  * Persistent storage open handler points to default file path.
+ * It should check file path and whether the file is symbolic link or no.
  * Application can point to appropriate SVR database path for it's IoTivity Server.
  */
 typedef struct {