Skip to content

Commit

Permalink
Add encrypted file relationship where required (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinweismann authored Jan 11, 2021
1 parent 3524729 commit 8a1cf5b
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Include/Common/OPC/NMR_IOpcPackageWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <memory>
#include <string>
#include <list>
#include "Common/NMR_Types.h"
#include "Common/NMR_Local.h"

Expand All @@ -19,6 +20,7 @@ namespace NMR {
virtual void addContentType(_In_ POpcPackagePart pOpcPackagePart, _In_ std::string sContentType) = 0;
virtual POpcPackageRelationship addRootRelationship(_In_ std::string sType, _In_ COpcPackagePart * pTargetPart) = 0;
virtual POpcPackageRelationship addPartRelationship(_In_ POpcPackagePart pOpcPackagePart, _In_ std::string sType, _In_ COpcPackagePart * pTargetPart) = 0;
virtual std::list<POpcPackageRelationship> addWriterSpecificRelationships(_In_ POpcPackagePart pOpcPackagePart, _In_ COpcPackagePart* pTargetPart) = 0;
virtual void close() {}
};

Expand Down
1 change: 1 addition & 0 deletions Include/Common/OPC/NMR_OpcPackageWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ namespace NMR {
void addContentType(_In_ POpcPackagePart pOpcPackagePart, _In_ std::string sContentType) override;
POpcPackageRelationship addRootRelationship(_In_ std::string sType, _In_ COpcPackagePart * pTargetPart) override;
POpcPackageRelationship addPartRelationship(_In_ POpcPackagePart pOpcPackagePart, _In_ std::string sType, _In_ COpcPackagePart * pTargetPart) override;
std::list<POpcPackageRelationship> addWriterSpecificRelationships(_In_ POpcPackagePart pOpcPackagePart, _In_ COpcPackagePart* pTargetPart) override;
};

typedef std::shared_ptr<COpcPackageWriter> POpcPackageWriter;
Expand Down
1 change: 1 addition & 0 deletions Include/Model/Classes/NMR_ModelConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ These are given by the 3MF Standard
#define PACKAGE_THUMBNAIL_RELATIONSHIP_TYPE "http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail"
#define PACKAGE_MUST_PRESERVE_RELATIONSHIP_TYPE "http://schemas.openxmlformats.org/package/2006/relationships/mustpreserve"
#define PACKAGE_KEYSTORE_RELATIONSHIP_TYPE "http://schemas.microsoft.com/3dmanufacturing/2019/04/keystore"
#define PACKAGE_ENCRYPTED_FILE_RELATIONSHIP "http://schemas.openxmlformats.org/package/2006/relationships/encryptedfile"

#define XML_3MF_NAMESPACE_XML "http://www.w3.org/XML/1998/namespace"
#define XML_3MF_NAMESPACE_XMLNS "http://www.w3.org/2000/xmlns/"
Expand Down
3 changes: 3 additions & 0 deletions Include/Model/Writer/NMR_KeyStoreOpcPackageWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ namespace NMR {
using PKeyStoreAccessRight = std::shared_ptr<CKeyStoreAccessRight>;

class CKeyStoreOpcPackageWriter : public IOpcPackageWriter {
private:
bool pathIsEncrypted(_In_ std::string sPath);
protected:
CModelContext const & m_pContext;
PIOpcPackageWriter m_pPackageWriter;
Expand All @@ -69,6 +71,7 @@ namespace NMR {
void addContentType(_In_ POpcPackagePart pOpcPackagePart, _In_ std::string sContentType) override;
POpcPackageRelationship addRootRelationship(std::string sType, COpcPackagePart * pTargetPart) override;
POpcPackageRelationship addPartRelationship(_In_ POpcPackagePart pOpcPackagePart, _In_ std::string sType, _In_ COpcPackagePart * pTargetPart) override;
std::list<POpcPackageRelationship> addWriterSpecificRelationships(_In_ POpcPackagePart pOpcPackagePart, _In_ COpcPackagePart* pTargetPart) override;
};

using PKeyStoreOpcPackageWriter = std::shared_ptr<CKeyStoreOpcPackageWriter>;
Expand Down
5 changes: 5 additions & 0 deletions Source/Common/OPC/NMR_OpcPackageWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ namespace NMR {
return pOpcPackagePart->addRelationship(generateRelationShipID(), sType, pTargetPart->getURI());
}

std::list<POpcPackageRelationship> COpcPackageWriter::addWriterSpecificRelationships(_In_ POpcPackagePart pOpcPackagePart, _In_ COpcPackagePart* pTargetPart)
{
return std::list<POpcPackageRelationship>();
}

void COpcPackageWriter::finishPackage()
{
writeContentTypes();
Expand Down
30 changes: 30 additions & 0 deletions Source/Model/Writer/NMR_KeyStoreOpcPackageWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,23 @@ namespace NMR {
}
}

bool CKeyStoreOpcPackageWriter::pathIsEncrypted(_In_ std::string sPath)
{
PSecureContext const& secureContext = m_pContext.secureContext();
PKeyStore const& keyStore = m_pContext.keyStore();

NMR::PKeyStoreResourceData rd = keyStore->findResourceData(sPath);
if (nullptr != rd) {
if (secureContext->hasDekCtx()) {
return true;
}
else {
m_pContext.warnings()->addWarning(NMR_ERROR_DEKDESCRIPTORNOTFOUND, eModelWarningLevel::mrwMissingMandatoryValue);
}
}
return false;
}

POpcPackagePart CKeyStoreOpcPackageWriter::wrapPartStream(PKeyStoreResourceData rd, POpcPackagePart part) {
PSecureContext const & secureContext = m_pContext.secureContext();
ContentEncryptionDescriptor p = secureContext->getDekCtx();
Expand Down Expand Up @@ -190,6 +207,19 @@ namespace NMR {
return m_pPackageWriter->addPartRelationship(pOpcPackagePart, sType, pTargetPart);
}

std::list<POpcPackageRelationship> CKeyStoreOpcPackageWriter::addWriterSpecificRelationships(_In_ POpcPackagePart pOpcPackagePart, _In_ COpcPackagePart* pTargetPart)
{
std::list<POpcPackageRelationship> list;
std::string sPath = fnIncludeLeadingPathDelimiter(pTargetPart->getURI());
if (pathIsEncrypted(sPath))
{
list.push_back(addPartRelationship(pOpcPackagePart, PACKAGE_ENCRYPTED_FILE_RELATIONSHIP, pTargetPart));
}

list.merge(m_pPackageWriter->addWriterSpecificRelationships(pOpcPackagePart, pTargetPart));
return list;
}

void CKeyStoreOpcPackageWriter::writeKeyStoreStream(_In_ CXmlWriter * pXMLWriter) {
pXMLWriter->WriteStartDocument();

Expand Down
4 changes: 3 additions & 1 deletion Source/Model/Writer/NMR_ModelWriter_3MF_Native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ namespace NMR {
if (pStream.get() == nullptr)
throw CNMRException(NMR_ERROR_INVALIDPARAM);

// create Texture Part
// create Attachment Part
POpcPackagePart pAttachmentPart = m_pPackageWriter->addPart(sPath);
PExportStream pExportStream = pAttachmentPart->getExportStream();

Expand All @@ -211,6 +211,8 @@ namespace NMR {
// add relationships
m_pPackageWriter->addPartRelationship(pModelPart, sRelationShipType.c_str(), pAttachmentPart.get());

m_pPackageWriter->addWriterSpecificRelationships(pModelPart, pAttachmentPart.get());

monitor()->IncrementProgress(1);
}
}
Expand Down

0 comments on commit 8a1cf5b

Please sign in to comment.