/* * determine the encryption type for KFile f with pathname name */ rc_t EncryptionTypeCheck (const KFile * f, const char * name, EncScheme * scheme) { size_t num_read; rc_t rc; char head [128]; assert (f != NULL); assert (name != NULL); rc = KFileReadAll (f, 0, head, sizeof head, &num_read); if (rc) { PLOGERR (klogErr, (klogErr, rc, "Unable to read head of " "'$(F)'", "F=%s", name)); *scheme = encError; return rc; } rc = KFileIsEnc (head, num_read); if (rc == 0) { *scheme = encEncFile; } else { rc = KFileIsWGAEnc (head, num_read); if (rc == 0) *scheme = encWGAEncFile; else *scheme = encNone; } return 0; }
/* * determine the encryption type for KFile f with pathname name */ rc_t EncryptionTypeCheck (const KFile * f, const char * name, EncScheme * scheme) { size_t num_read; rc_t rc; char head [128]; assert (f != NULL); assert (name != NULL); rc = KFileReadAll (f, 0, head, sizeof head, &num_read); if (rc) { PLOGERR (klogErr, (klogErr, rc, "Unable to read head of " "'$(F)'", "F=%s", name)); *scheme = encError; return rc; } /* looks for files with NCBInenc or NCBIsenc signatures */ rc = KFileIsEnc (head, num_read); if (rc == 0) { /* looks for files with just NCBIsenc signatures */ rc = KFileIsSraEnc (head, num_read); *scheme = (rc == 0) ? encSraEncFile : encEncFile; } else { rc = KFileIsWGAEnc (head, num_read); if (rc == 0) *scheme = encWGAEncFile; else *scheme = encNone; } return 0; }
/* not KDB specific - just uses vfs/krypto/kfs objects */ static rc_t KDBOpenFileAsDirectory (const KDirectory * dir, const char * path, const KDirectory ** pdir, uint32_t rcobj) { const KFile * file; const KFile * f; const KDirectory * ldir; bool encrypted = false; rc_t rc; *pdir = NULL; rc = KDirectoryOpenFileRead (dir, &file, path); if (rc == 0) { rc = KFileRandomAccess(file); if (rc) rc = RC (rcDB, rcMgr, rcOpening, rcobj, rcUnsupported); else { size_t tz; char tbuff [4096]; char pbuff [4096 + 1]; rc = KFileReadAll (file, 0, tbuff, sizeof tbuff, &tz); if (rc == 0) { if (KFileIsEnc (tbuff, tz) == 0) { encrypted = true; rc = KDBOpenFileGetPassword (pbuff, sizeof (pbuff) - 1); if (rc == 0) { KKey key; rc = KKeyInitRead (&key, kkeyAES128, pbuff, string_size (pbuff)); if (rc == 0) { rc = KEncFileMakeRead (&f, file, &key); if (rc == 0) { /* KEncFileMakeRead adds a reference */ KFileRelease (file); file = f; rc = KFileReadAll (file, 0, tbuff, sizeof tbuff, &tz); } } } } else if (KFileIsWGAEnc (tbuff, tz) == 0) { encrypted = true; rc = KDBOpenFileGetPassword (pbuff, sizeof (pbuff) - 1); if (rc == 0) { rc = KFileMakeWGAEncRead (&f, file, pbuff, string_size (pbuff)); if (rc == 0) { /* KFileMakeWGAEncRead adds a reference */ KFileRelease (file); file = f; rc = KFileReadAll (file, 0, tbuff, sizeof tbuff, &tz); } } } /* else not a handled encryption or unencrypted: we can't distinguish too much */ if (rc == 0) { if (KFileIsSRA (tbuff, tz) == 0) { rc = KDirectoryOpenSraArchiveReadUnbounded_silent_preopened (dir, &ldir, false, file, path); } else { rc = KDirectoryOpenTarArchiveRead_silent_preopened (dir, &ldir, false, file, path); } /* not an archive type we handle or a bad archive */ if (rc) { if (encrypted) rc = RC ( rcDB, rcMgr, rcOpening, rcEncryptionKey, rcIncorrect ); else rc = RC ( rcDB, rcMgr, rcOpening, rcPath, rcIncorrect ); } else { /* * release our ownership of the KFile that but archive will * keep theirs */ KFileRelease (file); *pdir = ldir; return 0; } } } } KFileRelease (file); } return rc; }