Skip to content

Commit

Permalink
Merge branch 'main' into mathern/monorepo-build
Browse files Browse the repository at this point in the history
  • Loading branch information
tmathern authored Nov 19, 2024
2 parents 66ba32a + 800a6e5 commit fc0d0b7
Show file tree
Hide file tree
Showing 15 changed files with 599 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: closing-ticket
name: Closing ticket
on:
issues:
types: [closed]
Expand Down
17 changes: 17 additions & 0 deletions .github/workflows/labeling_ticket_done.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Labeling ticket "Done"
on:
issues:
types: [labeled]
jobs:
label_issues:
runs-on: ubuntu-latest
if: |
contains(github.event.issue.labels.*.name, 'status: done')
permissions:
issues: write
steps:
- run: 'gh issue close "$NUMBER"'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
NUMBER: ${{ github.event.issue.number }}
17 changes: 17 additions & 0 deletions .github/workflows/labeling_ticket_todo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Labeling ticket "To Do"
on:
issues:
types: [labeled]
jobs:
label_issues:
runs-on: ubuntu-latest
if: |
!contains(github.event.issue.labels.*.name, 'status: done')
permissions:
issues: write
steps:
- run: 'gh issue reopen "$NUMBER"'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GH_REPO: ${{ github.repository }}
NUMBER: ${{ github.event.issue.number }}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: reopening-ticket
name: Reopening ticket
on:
issues:
types: [reopened]
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ serde_derive = "1.0"
serde_json = "1.0"
thiserror = "1.0.64"

[dev-dependencies]
url = "2.2.2"


[profile.release]
strip = true # Strip symbols from the output binary.
lto = true # Enable link-time optimization.
Expand Down
106 changes: 101 additions & 5 deletions include/c2pa.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,34 @@ IMPORT extern struct C2paBuilder *c2pa_builder_from_archive(struct CStream *stre
*/
IMPORT extern void c2pa_builder_free(struct C2paBuilder *builder_ptr);

/**
* Sets the no-embed flag on the Builder.
* When set, the builder will not embed a C2PA manifest store into the asset when signing.
* This is useful when creating cloud or sidecar manifests.
* # Parameters
* * builder_ptr: pointer to a Builder.
* # Safety
* builder_ptr must be a valid pointer to a Builder.
*/
IMPORT extern void c2pa_builder_set_no_embed(struct C2paBuilder *builder_ptr);

/**
* Sets the remote URL on the Builder.
* When set, the builder will embed a remote URL into the asset when signing.
* This is useful when creating cloud based Manifests.
* # Parameters
* * builder_ptr: pointer to a Builder.
* * remote_url: pointer to a C string with the remote URL.
* # Errors
* Returns -1 if there were errors, otherwise returns 0.
* The error string can be retrieved by calling c2pa_error.
* # Safety
* Reads from NULL-terminated C strings.
*/
IMPORT extern
int c2pa_builder_set_remote_url(struct C2paBuilder *builder_ptr,
const char *remote_url);

/**
* Adds a resource to the C2paBuilder.
*
Expand Down Expand Up @@ -408,10 +436,10 @@ int c2pa_builder_add_resource(struct C2paBuilder *builder_ptr,
* Reads from NULL-terminated C strings.
*/
IMPORT extern
int c2pa_builder_add_ingredient(struct C2paBuilder *builder_ptr,
const char *ingredient_json,
const char *format,
struct CStream *source);
int c2pa_builder_add_ingredient_from_stream(struct C2paBuilder *builder_ptr,
const char *ingredient_json,
const char *format,
struct CStream *source);

/**
* Writes an Archive of the Builder to the destination stream.
Expand Down Expand Up @@ -454,7 +482,7 @@ IMPORT extern int c2pa_builder_to_archive(struct C2paBuilder *builder_ptr, struc
*
* # Safety
* Reads from NULL-terminated C strings
* If c2pa_data_ptr is not NULL, the returned value MUST be released by calling c2pa_release_string
* If manifest_bytes_ptr is not NULL, the returned value MUST be released by calling c2pa_manifest_bytes_free
* and it is no longer valid after that call.
*/
IMPORT extern
Expand All @@ -473,6 +501,59 @@ int c2pa_builder_sign(struct C2paBuilder *builder_ptr,
*/
IMPORT extern void c2pa_manifest_bytes_free(const unsigned char *manifest_bytes_ptr);

/**
* Creates a hashed placeholder from a Builder.
* The placeholder is used to reserve size in an asset for later signing.
*
* # Parameters
* * builder_ptr: pointer to a Builder.
* * reserved_size: the size required for a signature from the intended signer.
* * format: pointer to a C string with the mime type or extension.
* * manifest_bytes_ptr: pointer to a pointer to a c_uchar to return manifest_bytes.
*
* # Errors
* Returns -1 if there were errors, otherwise returns the size of the manifest_bytes.
* The error string can be retrieved by calling c2pa_error.
*
* # Safety
* Reads from NULL-terminated C strings.
* If manifest_bytes_ptr is not NULL, the returned value MUST be released by calling c2pa_manifest_bytes_free
* and it is no longer valid after that call.
*/
IMPORT extern
int c2pa_builder_data_hashed_placeholder(struct C2paBuilder *builder_ptr,
uintptr_t reserved_size,
const char *format,
const unsigned char **manifest_bytes_ptr);

/**
* Sign a Builder using the specified signer and data hash.
* The data hash is a JSON string containing DataHash information for the asset.
* This is a low-level method for advanced use cases where the caller handles embedding the manifest.
*
* # Parameters
* * builder_ptr: pointer to a Builder.
* * signer: pointer to a C2paSigner.
* * data_hash: pointer to a C string with the JSON data hash.
* * format: pointer to a C string with the mime type or extension.
* * manifest_bytes_ptr: pointer to a pointer to a c_uchar to return manifest_bytes (optional, can be NULL).
*
* # Errors
* Returns -1 if there were errors, otherwise returns the size of the manifest_bytes.
* The error string can be retrieved by calling c2pa_error.
*
* # Safety
* Reads from NULL-terminated C strings.
* If manifest_bytes_ptr is not NULL, the returned value MUST be released by calling c2pa_manifest_bytes_free
* and it is no longer valid after that call.
*/
IMPORT extern
int c2pa_builder_sign_data_hashed_embeddable(struct C2paBuilder *builder_ptr,
struct C2paSigner *signer,
const char *data_hash,
const char *format,
const unsigned char **manifest_bytes_ptr);

/**
* Creates a C2paSigner from a callback and configuration.
*
Expand Down Expand Up @@ -506,6 +587,21 @@ struct C2paSigner *c2pa_signer_create(const void *context,
const char *certs,
const char *tsa_url);

/**
* Returns the size to reserve for the signature for this signer.
*
* # Parameters
* * signer_ptr: pointer to a C2paSigner.
*
* # Errors
* Returns -1 if there were errors, otherwise returns the size to reserve.
* The error string can be retrieved by calling c2pa_error.
*
* # Safety
* The signer_ptr must be a valid pointer to a C2paSigner.
*/
IMPORT extern int64_t c2pa_signer_reserve_size(struct C2paSigner *signer_ptr);

/**
* Frees a C2paSigner allocated by Rust.
*
Expand Down
29 changes: 28 additions & 1 deletion include/c2pa.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,13 @@ namespace c2pa
Signer(SignerFunc *callback, C2paSigningAlg alg, const string &sign_cert, const string &tsa_uri);

Signer(C2paSigner *signer) : signer(signer) {}

~Signer();

/// @brief Get the size to reserve for a signature for this signer.
/// @return Reserved size for the signature.
uintptr_t reserve_size();

/// @brief Get the C2paSigner
C2paSigner *c2pa_signer();
};
Expand All @@ -198,6 +202,14 @@ namespace c2pa

~Builder();

/// @brief Set the no embed flag.
void set_no_embed();

/// @brief Set the remote URL.
/// @param remote_url The remote URL to set.
/// @throws C2pa::Exception for errors encountered by the C2PA library.
void set_remote_url(const string &remote_url);

/// @brief Add a resource to the builder.
/// @param uri The uri of the resource.
/// @param source The input stream to read the resource from.
Expand Down Expand Up @@ -260,6 +272,21 @@ namespace c2pa
/// @throws C2pa::Exception for errors encountered by the C2PA library.
void to_archive(const path &dest_path);

/// @brief Create a hashed placeholder from the builder.
/// @param reserved_size The size required for a signature from the intended signer.
/// @param format The format of the mime type or extension.
/// @return A vector containing the hashed placeholder.
/// @throws C2pa::Exception for errors encountered by the C2PA library.
std::unique_ptr<std::vector<unsigned char>> data_hashed_placeholder(uintptr_t reserved_size, const string &format);

/// @brief Sign a Builder using the specified signer and data hash.
/// @param signer The signer to use for signing.
/// @param data_hash The data hash to sign.
/// @param format The format of the data hash.
/// @return A vector containing the signed data.
/// @throws C2pa::Exception for errors encountered by the C2PA library.
std::unique_ptr<std::vector<unsigned char>> sign_data_hashed_embeddable(Signer &signer, const string &data_hash, const string &format);

private:
// Private constructor for Builder from an archive (todo: find a better way to handle this)
Builder(istream &archive);
Expand Down
63 changes: 60 additions & 3 deletions src/c2pa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,12 @@ namespace c2pa
return signer;
}

/// @brief Get the size to reserve for a signature for this signer.
uintptr_t Signer::reserve_size()
{
return c2pa_signer_reserve_size(signer);
}

/// @brief Builder class for creating a manifest implementation.
Builder::Builder(const string &manifest_json)
{
Expand Down Expand Up @@ -600,6 +606,20 @@ namespace c2pa
c2pa_builder_free(builder);
}

void Builder::set_no_embed()
{
c2pa_builder_set_no_embed(builder);
}

void Builder::set_remote_url(const string &remote_url)
{
int result = c2pa_builder_set_remote_url(builder, remote_url.c_str());
if (result < 0)
{
throw Exception();
}
}

void Builder::add_resource(const string &uri, istream &source)
{
CppIStream c_source = CppIStream(source);
Expand All @@ -623,7 +643,7 @@ namespace c2pa
void Builder::add_ingredient(const string &ingredient_json, const string &format, istream &source)
{
CppIStream c_source = CppIStream(source);
int result = c2pa_builder_add_ingredient(builder, ingredient_json.c_str(), format.c_str(), c_source.c_stream);
int result = c2pa_builder_add_ingredient_from_stream(builder, ingredient_json.c_str(), format.c_str(), c_source.c_stream);
if (result < 0)
{
throw Exception();
Expand All @@ -649,7 +669,7 @@ namespace c2pa
{
CppIStream c_source = CppIStream(source);
CppOStream c_dest = CppOStream(dest);
const unsigned char *c2pa_manifest_bytes = NULL; // TODO: Make returning manifest bytes optional.
const unsigned char *c2pa_manifest_bytes = NULL;
auto result = c2pa_builder_sign(builder, format.c_str(), c_source.c_stream, c_dest.c_stream, signer.c2pa_signer(), &c2pa_manifest_bytes);
if (result < 0)
{
Expand Down Expand Up @@ -746,4 +766,41 @@ namespace c2pa
to_archive(dest);
}

}
std::unique_ptr<std::vector<unsigned char>> Builder::data_hashed_placeholder(uintptr_t reserve_size, const string &format)
{
const unsigned char *c2pa_manifest_bytes = NULL;
auto result = c2pa_builder_data_hashed_placeholder(builder, reserve_size, format.c_str(), &c2pa_manifest_bytes);
if (result < 0)
{
throw Exception();
}
if (c2pa_manifest_bytes != NULL)
{
// Allocate a new vector on the heap and fill it with the data.
auto data = std::make_unique<std::vector<unsigned char>>(c2pa_manifest_bytes, c2pa_manifest_bytes + result);

c2pa_manifest_bytes_free(c2pa_manifest_bytes);
return data;
}
throw(c2pa::Exception("Failed to create data hashed placeholder"));
}

std::unique_ptr<std::vector<unsigned char>> Builder::sign_data_hashed_embeddable(Signer &signer, const string &data_hash, const string &format)
{
const unsigned char *c2pa_manifest_bytes = NULL;
auto result = c2pa_builder_sign_data_hashed_embeddable(builder, signer.c2pa_signer(), data_hash.c_str(), format.c_str(), &c2pa_manifest_bytes);
if (result < 0)
{
throw Exception();
}
if (c2pa_manifest_bytes != NULL)
{
// Allocate a new vector on the heap and fill it with the data.
auto data = std::make_unique<std::vector<unsigned char>>(c2pa_manifest_bytes, c2pa_manifest_bytes + result);

c2pa_manifest_bytes_free(c2pa_manifest_bytes);
return data;
}
throw(c2pa::Exception("Failed to create data hashed placeholder"));
}
} // namespace c2pa
Loading

0 comments on commit fc0d0b7

Please sign in to comment.