-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into rafal/transcode-quality-params-phase3
- Loading branch information
Showing
16 changed files
with
268 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package c2pa | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"os/exec" | ||
) | ||
|
||
const c2paManifestTemplate = ` | ||
{ | ||
"alg": "%s", | ||
"private_key": "%s", | ||
"sign_cert": "%s", | ||
"ta_url": "http://timestamp.digicert.com", | ||
"claim_generator": "LivepeerStudio", | ||
"title": "%s", | ||
"assertions": [ | ||
{ | ||
"label": "c2pa.actions", | ||
"data": { | ||
"actions": [ | ||
{ | ||
"action": "c2pa.published" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
` | ||
|
||
type C2PA struct { | ||
alg string | ||
privateKeyPath string | ||
signCertPath string | ||
} | ||
|
||
func NewC2PA(alg, privateKeyPath, signCertPath string) C2PA { | ||
return C2PA{ | ||
alg: alg, | ||
privateKeyPath: privateKeyPath, | ||
signCertPath: signCertPath, | ||
} | ||
} | ||
|
||
func (c C2PA) c2paManifest(title string) string { | ||
return fmt.Sprintf(c2paManifestTemplate, c.alg, c.privateKeyPath, c.signCertPath, title) | ||
} | ||
|
||
func (c C2PA) SignFile(inFile, outFile, title, parent string) error { | ||
args := []string{ | ||
inFile, | ||
"--config", | ||
c.c2paManifest(title), | ||
"--force", | ||
"--output", | ||
outFile, | ||
} | ||
if parent != "" { | ||
args = append(args, "--parent", parent) | ||
} | ||
_, err := runCmd(exec.CommandContext(context.TODO(), "c2patool", args...)) | ||
return err | ||
} | ||
|
||
func runCmd(cmd *exec.Cmd) (string, error) { | ||
var stdOut bytes.Buffer | ||
var stdErr bytes.Buffer | ||
cmd.Stdout = &stdOut | ||
cmd.Stderr = &stdErr | ||
err := cmd.Run() | ||
|
||
if len(stdErr.Bytes()) > 0 { | ||
return "", fmt.Errorf("failed creating C2PA Manifest: %s", stdErr.String()) | ||
} | ||
|
||
return stdOut.String(), err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package c2pa | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
) | ||
|
||
func TestSign(t *testing.T) { | ||
_, err := exec.LookPath("c2patool") | ||
if err != nil { | ||
fmt.Println("No c2patool installed, test skipped") | ||
return | ||
} | ||
|
||
outFile := "test/tiny_output_signed.mp4" | ||
c := NewC2PA("es256", "test/es256_private.key", "test/es256_certs.pem") | ||
defer os.Remove(outFile) | ||
|
||
err = c.SignFile("test/tiny.mp4", outFile, "Tiny", "") | ||
|
||
require.Nil(t, err) | ||
out, err := runCmd(exec.CommandContext(context.TODO(), "c2patool", outFile)) | ||
require.Nil(t, err) | ||
assert.Contains(t, out, "\"action\": \"c2pa.published\"") | ||
} | ||
|
||
func TestSignWithParent(t *testing.T) { | ||
_, err := exec.LookPath("c2patool") | ||
if err != nil { | ||
fmt.Println("No c2patool installed, test skipped") | ||
return | ||
} | ||
|
||
outFile := "test/tiny_cut_output_signed.mp4" | ||
c := NewC2PA("es256", "test/es256_private.key", "test/es256_certs.pem") | ||
defer os.Remove(outFile) | ||
|
||
err = c.SignFile("test/tiny_cut.mp4", outFile, "Tiny", "test/tiny_signed.mp4") | ||
|
||
require.Nil(t, err) | ||
out, err := runCmd(exec.CommandContext(context.TODO(), "c2patool", outFile)) | ||
require.Nil(t, err) | ||
assert.Contains(t, out, "\"action\": \"c2pa.published\"") | ||
assert.Contains(t, out, "\"relationship\": \"parentOf\"") | ||
} | ||
|
||
func TestSign_NotExistingPrivateKey(t *testing.T) { | ||
_, err := exec.LookPath("c2patool") | ||
if err != nil { | ||
fmt.Println("No c2patool installed, test skipped") | ||
return | ||
} | ||
|
||
c := NewC2PA("es256", "some/path/notexisting", "test/es256_certs.pem") | ||
err = c.SignFile("test/tiny.mp4", "test/tiny_signed.mp4", "Tiny", "") | ||
require.ErrorContains(t, err, "No such file or directory") | ||
} | ||
|
||
func TestSign_NotExistingSigningCert(t *testing.T) { | ||
_, err := exec.LookPath("c2patool") | ||
if err != nil { | ||
fmt.Println("No c2patool installed, test skipped") | ||
return | ||
} | ||
|
||
c := NewC2PA("es256", "test/es256_private.key", "some/path/notexisting") | ||
err = c.SignFile("test/tiny.mp4", "test/tiny_signed.mp4", "Tiny", "") | ||
require.ErrorContains(t, err, "No such file or directory") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIChzCCAi6gAwIBAgIUcCTmJHYF8dZfG0d1UdT6/LXtkeYwCgYIKoZIzj0EAwIw | ||
gYwxCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJDQTESMBAGA1UEBwwJU29tZXdoZXJl | ||
MScwJQYDVQQKDB5DMlBBIFRlc3QgSW50ZXJtZWRpYXRlIFJvb3QgQ0ExGTAXBgNV | ||
BAsMEEZPUiBURVNUSU5HX09OTFkxGDAWBgNVBAMMD0ludGVybWVkaWF0ZSBDQTAe | ||
Fw0yMjA2MTAxODQ2NDBaFw0zMDA4MjYxODQ2NDBaMIGAMQswCQYDVQQGEwJVUzEL | ||
MAkGA1UECAwCQ0ExEjAQBgNVBAcMCVNvbWV3aGVyZTEfMB0GA1UECgwWQzJQQSBU | ||
ZXN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wEHA0IABHllI4O7a0EkpTYAWfPM | ||
D6Rnfk9iqhEmCQKMOR6J47Rvh2GGjUw4CS+aLT89ySukPTnzGsMQ4jK9d3V4Aq4Q | ||
LsOjYzBhMA8GA1UdEwEB/wQFMAMBAf8wDgYDVR0PAQH/BAQDAgGGMB0GA1UdDgQW | ||
BBQOfI1yZr+iyeUAlO+1boDitg4vazAfBgNVHSMEGDAWgBRembiG4Xgb2VcVWnUA | ||
UrYpDsuojDAKBggqhkjOPQQDAgNJADBGAiEAtdZ3+05CzFo90fWeZ4woeJcNQC4B | ||
84Ill3YeZVvR8ZECIQDVRdha1xEDKuNTAManY0zthSosfXcvLnZui1A/y/DYeg== | ||
-----END CERTIFICATE----- | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgfNJBsaRLSeHizv0m | ||
GL+gcn78QmtfLSm+n+qG9veC2W2hRANCAAQPaL6RkAkYkKU4+IryBSYxJM3h77sF | ||
iMrbvbI8fG7w2Bbl9otNG/cch3DAw5rGAPV7NWkyl3QGuV/wt0MrAPDo | ||
-----END PRIVATE KEY----- |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.