Skip to content

Commit

Permalink
Add callback to ask online validation confirmation
Browse files Browse the repository at this point in the history
IB-7865

Signed-off-by: Raul Metsma <[email protected]>
  • Loading branch information
metsma committed Nov 2, 2023
1 parent 6b747fd commit e0a9340
Show file tree
Hide file tree
Showing 19 changed files with 120 additions and 58 deletions.
10 changes: 5 additions & 5 deletions examples/DigiDocCSharp/DigiDocCSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
<Platform Condition=" '$(Platform)' == '' ">x64</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{DDEE2029-EA2A-49D2-80CB-F0E2E396B005}</ProjectGuid>
Expand All @@ -14,8 +14,8 @@
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x64' ">
<PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
Expand All @@ -25,8 +25,8 @@
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<PlatformTarget>x86</PlatformTarget>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x64' ">
<PlatformTarget>x64</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
Expand Down
12 changes: 6 additions & 6 deletions examples/DigiDocCSharp/DigiDocCSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DigiDocCSharp", "DigiDocCSh
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x86 = Debug|x86
Release|x86 = Release|x86
Debug|x64 = Debug|x64
Release|x64 = Release|x64
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DDEE2029-EA2A-49D2-80CB-F0E2E396B005}.Debug|x86.ActiveCfg = Debug|x86
{DDEE2029-EA2A-49D2-80CB-F0E2E396B005}.Debug|x86.Build.0 = Debug|x86
{DDEE2029-EA2A-49D2-80CB-F0E2E396B005}.Release|x86.ActiveCfg = Release|x86
{DDEE2029-EA2A-49D2-80CB-F0E2E396B005}.Release|x86.Build.0 = Release|x86
{DDEE2029-EA2A-49D2-80CB-F0E2E396B005}.Debug|x64.ActiveCfg = Debug|x64
{DDEE2029-EA2A-49D2-80CB-F0E2E396B005}.Debug|x64.Build.0 = Debug|x64
{DDEE2029-EA2A-49D2-80CB-F0E2E396B005}.Release|x64.ActiveCfg = Release|x64
{DDEE2029-EA2A-49D2-80CB-F0E2E396B005}.Release|x64.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
8 changes: 7 additions & 1 deletion examples/DigiDocCSharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,8 @@ private static void Verify(string file)
try
{
Console.WriteLine("Opening file: " + file);
Container b = Container.open(file);
var cb = new ContainerOpen();
Container b = Container.open(file, cb);

Console.WriteLine("Files:");
foreach (DataFile d in b.dataFiles())
Expand Down Expand Up @@ -226,4 +227,9 @@ private static void Version()
" libdigidocpp " + digidoc.digidoc.version());
}
}

class ContainerOpen : ContainerOpenCB
{
override public bool validateOnline() { return true; }
}
}
Binary file modified examples/java/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
3 changes: 2 additions & 1 deletion examples/java/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
15 changes: 10 additions & 5 deletions examples/java/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,8 @@ done
# This is normally unused
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -133,10 +131,13 @@ location of your Java installation."
fi
else
JAVACMD=java
which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
if ! command -v java >/dev/null 2>&1
then
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation."
fi
fi

# Increase the maximum file descriptors if we can.
Expand Down Expand Up @@ -197,6 +198,10 @@ if "$cygwin" || "$msys" ; then
done
fi


# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ static void verify(String file) {
try
{
System.out.println("Opening file: " + file);
Container b = Container.open(file);
ContainerOpen cb = new ContainerOpen();
Container b = Container.open(file, cb);
assert b != null;

System.out.println("Files:");
Expand Down Expand Up @@ -200,4 +201,10 @@ static byte[] fromHex(String s) {
}
return data;
}

static private class ContainerOpen extends ContainerOpenCB
{
@Override
public boolean validateOnline() { return true; }
}
}
10 changes: 9 additions & 1 deletion examples/python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
import sys
import os

class ContainerOpenCB(digidoc.ContainerOpenCB):

def __init__(self):
digidoc.ContainerOpenCB.__init__(self)

def validateOnline(self):
return True

class Program:
digidoc.initialize()
Expand Down Expand Up @@ -78,7 +85,8 @@ def sign(self, args):

def verify(self, file):
print("Opening file: " + file)
doc = digidoc.Container.open(file)
cb = ContainerOpenCB()
doc = digidoc.Container.open(file, cb)

print("Files:")
for d in doc.dataFiles():
Expand Down
2 changes: 2 additions & 0 deletions libdigidocpp.dox
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,8 @@ Signature Validation Policy
Default POLv2

http://open-eid.github.io/SiVa/siva/appendix/validation_policy/</td></tr>
<tr><td>\-\-offline </td><td>Optional</td><td>
open container offline (eg. Don't send to SiVa)</td></tr>
<tr><td>\-\-warnings=

(ignore, warning, error) </td><td>Optional</td><td>
Expand Down
15 changes: 11 additions & 4 deletions libdigidocpp.i
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

// digidocpp.i - SWIG interface for libdigidocpp library

%module digidoc
%module(directors="1") digidoc

%begin %{
#ifdef _MSC_VER
Expand Down Expand Up @@ -231,6 +231,8 @@ extern "C"
%newobject digidoc::Container::open;
%newobject digidoc::Container::create;

%feature("director") digidoc::ContainerOpenCB;

%typemap(javacode) digidoc::Conf %{
public Conf transfer() {
swigCMemOwn = false;
Expand Down Expand Up @@ -318,16 +320,21 @@ namespace std {
}
}
%extend digidoc::Container {
static digidoc::Container* open(const std::string &path, digidoc::ContainerOpenCB *cb)
{
return digidoc::Container::openPtr(path, cb).release();
}

digidoc::Signature* prepareWebSignature(const std::vector<unsigned char> &cert, const std::string &profile = {},
const std::vector<std::string> &roles = {},
const std::string &city = {}, const std::string &state = {},
const std::string &postalCode = {}, const std::string &country = {})
{
class : public digidoc::Signer
class final: public digidoc::Signer
{
public:
digidoc::X509Cert cert() const override { return _cert; }
std::vector<unsigned char> sign(const std::string &, const std::vector<unsigned char> &) const override
digidoc::X509Cert cert() const final { return _cert; }
std::vector<unsigned char> sign(const std::string &, const std::vector<unsigned char> &) const final
{
THROW("Not implemented");
}
Expand Down
2 changes: 1 addition & 1 deletion src/ASiC_S.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ void ASiC_S::addAdESSignature(istream & /*signature*/)
THROW("Not implemented.");
}

unique_ptr<Container> ASiC_S::openInternal(const string &path)
unique_ptr<Container> ASiC_S::openInternal(const string &path, ContainerOpenCB * /*cb*/)
{
if (!isContainerSimpleFormat(path))
return {};
Expand Down
2 changes: 1 addition & 1 deletion src/ASiC_S.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace digidoc
Signature* sign(Signer* signer) override;

static std::unique_ptr<Container> createInternal(const std::string &path);
static std::unique_ptr<Container> openInternal(const std::string &path);
static std::unique_ptr<Container> openInternal(const std::string &path, ContainerOpenCB *cb);

private:
ASiC_S();
Expand Down
17 changes: 9 additions & 8 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,13 +271,11 @@ if(SWIG_FOUND)
target_include_directories(digidoc_java PRIVATE ${JAVA_INCLUDE_PATH} $<$<BOOL:${JAVA_INCLUDE_PATH2}>:${JAVA_INCLUDE_PATH2}>)
target_compile_definitions(digidoc_java PRIVATE TARGET_NAME="$<TARGET_NAME:digidoc_java>")
target_link_libraries(digidoc_java digidocpp digidocpp_util digidocpp_ver)
set_property(TARGET digidoc_java PROPERTY SWIG_COMPILE_DEFINITIONS $<$<PLATFORM_ID:Windows>:SWIGWIN>)
if(APPLE)
set_target_properties(digidoc_java PROPERTIES MACOSX_RPATH YES INSTALL_RPATH /Library/Frameworks)
install(TARGETS digidoc_java DESTINATION /Library/Java/Extensions)
else()
install(TARGETS digidoc_java DESTINATION ${CMAKE_INSTALL_LIBDIR})
endif()
set_target_properties(digidoc_java PROPERTIES
INSTALL_RPATH $<$<PLATFORM_ID:Darwin>:/Library/Frameworks>
SWIG_COMPILE_DEFINITIONS $<$<PLATFORM_ID:Windows>:SWIGWIN>
)
install(TARGETS digidoc_java DESTINATION $<IF:$<PLATFORM_ID:Darwin>,/Library/Java/Extensions,${CMAKE_INSTALL_LIBDIR}>)
install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/java/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/ee/ria/libdigidocpp FILES_MATCHING PATTERN "*.java")
if(WIN32)
install(FILES $<TARGET_PDB_FILE:digidoc_java> DESTINATION ${CMAKE_INSTALL_BINDIR} OPTIONAL)
Expand All @@ -289,7 +287,6 @@ if(SWIG_FOUND)
swig_add_library(digidoc_python LANGUAGE python SOURCES ../libdigidocpp.i)
target_compile_definitions(digidoc_python PRIVATE TARGET_NAME="$<TARGET_NAME:digidoc_python>")
target_link_libraries(digidoc_python digidocpp digidocpp_util digidocpp_ver Python3::Module)
set_property(TARGET digidoc_python PROPERTY SWIG_COMPILE_DEFINITIONS $<$<PLATFORM_ID:Windows>:SWIGWIN>)
#configure_file(setup.py.cmake setup.py)
#install(CODE "execute_process(COMMAND python3 ${CMAKE_CURRENT_BINARY_DIR}/setup.py install)")
if(NOT Python3_SITELIB)
Expand All @@ -303,6 +300,10 @@ if(SWIG_FOUND)
else()
set_target_properties(digidoc_python PROPERTIES SUFFIX .so)
endif()
set_target_properties(digidoc_python PROPERTIES
INSTALL_RPATH $<$<PLATFORM_ID:Darwin>:/Library/Frameworks>
SWIG_COMPILE_DEFINITIONS $<$<PLATFORM_ID:Windows>:SWIGWIN>
)
install(TARGETS digidoc_python DESTINATION ${Python3_SITELIB})
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/digidoc.py DESTINATION ${Python3_SITELIB})
endif()
Expand Down
28 changes: 17 additions & 11 deletions src/Container.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ namespace digidoc
static string m_appName = "libdigidocpp";
static string m_userAgent = "libdigidocpp";
static vector<decltype(&Container::createPtr)> m_createList {};
static vector<decltype(&Container::openPtr)> m_openList {};
using OpenCB = std::unique_ptr<Container> (*)(const std::string &path, ContainerOpenCB *cb);
static vector<OpenCB> m_openList {};
}

/**
Expand Down Expand Up @@ -96,14 +97,7 @@ string digidoc::userAgent() { return m_userAgent; }
* Returns libdigidocpp library version
*/
string digidoc::version() {
string ver = FILE_VER_STR;
#if defined(DYNAMIC_LIBDIGIDOC) || defined(LINKED_LIBDIGIDOC)
ver += "_ddoc";
#endif
#ifdef PDF_SUPPORT
ver += "_siva";
#endif
return ver;
return FILE_VER_STR;
}

/**
Expand Down Expand Up @@ -348,10 +342,22 @@ Container* Container::open(const string &path)
* @throws Exception
*/
unique_ptr<Container> Container::openPtr(const string &path)
{
return openPtr(path, {});
}

/**
* Opens container from a file
*
* @param path
* @param cb Callback called when needed
* @throws Exception
*/
unique_ptr<Container> Container::openPtr(const string &path, ContainerOpenCB *cb)
{
for(auto open: m_openList)
{
if(unique_ptr<Container> container = open(path))
if(unique_ptr<Container> container = open(path, cb))
return container;
}
return ASiC_E::openInternal(path);
Expand Down Expand Up @@ -417,7 +423,7 @@ unique_ptr<Container> Container::openPtr(const string &path)
*
* It must contain static members:
* * static Container* createInternal(const std::string &path);
* * static Container* openInternal(const std::string &path);
* * static Container* openInternal(const std::string &path, digidoc::ContainerOpenCB *cb);
*
* @see Container::create, Container::open
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Container.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ DIGIDOCPP_EXPORT void terminate();
DIGIDOCPP_EXPORT std::string userAgent();
DIGIDOCPP_EXPORT std::string version();

struct ContainerOpenCB {
virtual ~ContainerOpenCB() = default;
virtual bool validateOnline() const { return true; }
};

class DIGIDOCPP_EXPORT Container
{
public:
Expand All @@ -66,6 +71,7 @@ class DIGIDOCPP_EXPORT Container
static std::unique_ptr<Container> createPtr(const std::string &path);
DIGIDOCPP_DEPRECATED static Container* open(const std::string &path);
static std::unique_ptr<Container> openPtr(const std::string &path);
static std::unique_ptr<Container> openPtr(const std::string &path, digidoc::ContainerOpenCB *cb);
template<class T>
static void addContainerImplementation();

Expand Down
Loading

0 comments on commit e0a9340

Please sign in to comment.