|
1 | | -/* |
| 1 | +/* |
2 | 2 | This file is licensed to you under the Apache License, Version 2.0 |
3 | 3 | (http://www.apache.org/licenses/LICENSE-2.0) or the MIT license |
4 | 4 | (http://opensource.org/licenses/MIT), at your option. |
@@ -54,6 +54,132 @@ class Signer internal constructor(internal var ptr: Long) : Closeable { |
54 | 54 | if (handle == 0L) null else Signer(handle) |
55 | 55 | } |
56 | 56 |
|
| 57 | + /** |
| 58 | + * Creates a signer from JSON settings configuration. |
| 59 | + * |
| 60 | + * This method creates a signer from a JSON settings object that can include certificate |
| 61 | + * paths, private keys, algorithm selection, and other configuration options. This is useful |
| 62 | + * for loading signer configuration from external sources, configuration files, or for CAWG |
| 63 | + * (Creator Assertions Working Group) signers. |
| 64 | + * |
| 65 | + * @param settingsJson A JSON string containing signer configuration. |
| 66 | + * @return A new [Signer] instance configured according to the settings. |
| 67 | + * @throws C2PAError if the settings are invalid or the signer cannot be created. |
| 68 | + * |
| 69 | + * Example JSON for a CAWG signer: |
| 70 | + * ```json |
| 71 | + * { |
| 72 | + * "version": 1, |
| 73 | + * "signer": { |
| 74 | + * "local": { |
| 75 | + * "alg": "es256", |
| 76 | + * "sign_cert": "-----BEGIN CERTIFICATE-----\n...", |
| 77 | + * "private_key": "-----BEGIN PRIVATE KEY-----\n...", |
| 78 | + * "tsa_url": "http://timestamp.digicert.com" |
| 79 | + * } |
| 80 | + * }, |
| 81 | + * "cawg_x509_signer": { |
| 82 | + * "local": { |
| 83 | + * "alg": "es256", |
| 84 | + * "sign_cert": "-----BEGIN CERTIFICATE-----\n...", |
| 85 | + * "private_key": "-----BEGIN PRIVATE KEY-----\n...", |
| 86 | + * "tsa_url": "http://timestamp.digicert.com", |
| 87 | + * "referenced_assertions": ["cawg.training-mining"] |
| 88 | + * } |
| 89 | + * } |
| 90 | + * } |
| 91 | + * ``` |
| 92 | + */ |
| 93 | + @JvmStatic |
| 94 | + @Throws(C2PAError::class) |
| 95 | + fun fromSettingsJson(settingsJson: String): Signer = |
| 96 | + fromSettings(settingsJson, "json") |
| 97 | + |
| 98 | + /** |
| 99 | + * Creates a signer from TOML settings configuration. |
| 100 | + * |
| 101 | + * This method creates a signer from a TOML settings string. TOML format supports additional |
| 102 | + * features like CAWG (Creator Assertions Working Group) X.509 signers that generate identity |
| 103 | + * assertions. |
| 104 | + * |
| 105 | + * @param settingsToml A TOML string containing signer configuration. |
| 106 | + * @return A new [Signer] instance configured according to the settings. |
| 107 | + * @throws C2PAError if the settings are invalid or the signer cannot be created. |
| 108 | + * |
| 109 | + * Example TOML for a CAWG signer: |
| 110 | + * ```toml |
| 111 | + * version = 1 |
| 112 | + * |
| 113 | + * [signer.local] |
| 114 | + * alg = "es256" |
| 115 | + * sign_cert = """-----BEGIN CERTIFICATE----- |
| 116 | + * ...certificate chain... |
| 117 | + * -----END CERTIFICATE----- |
| 118 | + * """ |
| 119 | + * private_key = """-----BEGIN PRIVATE KEY----- |
| 120 | + * ...private key... |
| 121 | + * -----END PRIVATE KEY----- |
| 122 | + * """ |
| 123 | + * tsa_url = "http://timestamp.digicert.com" |
| 124 | + * |
| 125 | + * [cawg_x509_signer.local] |
| 126 | + * alg = "es256" |
| 127 | + * sign_cert = """-----BEGIN CERTIFICATE----- |
| 128 | + * ...certificate chain... |
| 129 | + * -----END CERTIFICATE----- |
| 130 | + * """ |
| 131 | + * private_key = """-----BEGIN PRIVATE KEY----- |
| 132 | + * ...private key... |
| 133 | + * -----END PRIVATE KEY----- |
| 134 | + * """ |
| 135 | + * tsa_url = "http://timestamp.digicert.com" |
| 136 | + * referenced_assertions = ["cawg.training-mining"] |
| 137 | + * ``` |
| 138 | + */ |
| 139 | + @JvmStatic |
| 140 | + @Throws(C2PAError::class) |
| 141 | + fun fromSettingsToml(settingsToml: String): Signer = |
| 142 | + fromSettings(settingsToml, "toml") |
| 143 | + |
| 144 | + /** |
| 145 | + * Creates a signer from settings configuration in the specified format. |
| 146 | + * |
| 147 | + * @param settings The settings string in the specified format. |
| 148 | + * @param format The format of the settings string ("json" or "toml"). |
| 149 | + * @return A new [Signer] instance configured according to the settings. |
| 150 | + * @throws C2PAError if the settings are invalid or the signer cannot be created. |
| 151 | + */ |
| 152 | + @JvmStatic |
| 153 | + @Throws(C2PAError::class) |
| 154 | + private fun fromSettings(settings: String, format: String): Signer = |
| 155 | + executeC2PAOperation("Failed to create signer from settings") { |
| 156 | + val loadResult = C2PA.loadSettingsResult(settings, format) |
| 157 | + if (loadResult != 0) { |
| 158 | + throw C2PAError.Api(C2PA.getError() ?: "Failed to load settings") |
| 159 | + } |
| 160 | + val handle = nativeFromSettings() |
| 161 | + if (handle == 0L) null else Signer(handle) |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Loads global C2PA settings without creating a signer. |
| 166 | + * |
| 167 | + * This method loads settings that will be used by subsequent signing operations. Use this |
| 168 | + * to load CAWG identity assertion settings separately from the main signer. |
| 169 | + * |
| 170 | + * @param settings The settings string in the specified format. |
| 171 | + * @param format The format of the settings string ("json" or "toml"). |
| 172 | + * @throws C2PAError if the settings are invalid. |
| 173 | + */ |
| 174 | + @JvmStatic |
| 175 | + @Throws(C2PAError::class) |
| 176 | + fun loadSettings(settings: String, format: String) { |
| 177 | + val result = C2PA.loadSettingsResult(settings, format) |
| 178 | + if (result != 0) { |
| 179 | + throw C2PAError.Api(C2PA.getError() ?: "Failed to load settings") |
| 180 | + } |
| 181 | + } |
| 182 | + |
57 | 183 | /** Create signer with custom signing callback */ |
58 | 184 | @JvmStatic |
59 | 185 | @Throws(C2PAError::class) |
@@ -92,6 +218,9 @@ class Signer internal constructor(internal var ptr: Long) : Closeable { |
92 | 218 | tsaURL: String?, |
93 | 219 | callback: SignCallback, |
94 | 220 | ): Long |
| 221 | + |
| 222 | + @JvmStatic |
| 223 | + private external fun nativeFromSettings(): Long |
95 | 224 | } |
96 | 225 |
|
97 | 226 | /** Get the reserve size for this signer */ |
|
0 commit comments