Skip to content

Commit

Permalink
(WIP) FFI: Add bunch of rnp_key_signature_set_* functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ni4 committed Sep 9, 2024
1 parent 58fb05e commit 10f7330
Show file tree
Hide file tree
Showing 3 changed files with 605 additions and 0 deletions.
123 changes: 123 additions & 0 deletions include/rnp/rnp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,126 @@ RNP_API rnp_result_t rnp_key_revocation_signature_create(rnp_key_handle_t
*/
RNP_API rnp_result_t rnp_key_signature_set_hash(rnp_signature_handle_t sig, const char *hash);

/**
* @brief Set the signature creation time. While it is set by default to the current time,
* caller may override it in case of need.
*
* @param sig editable key signature handle, i.e. created with rnp_key_*_signature_create().
* @param creation timestamp with the creation time.
* @return RNP_SUCCESS or error code if failed.
*/
RNP_API rnp_result_t rnp_key_signature_set_creation(rnp_signature_handle_t sig,
uint32_t ctime);

/**
* @brief Set the key usage flags, i.e. whether it is usable for signing, encryption, whatever
* else.
*
* @param sig editable key signature handle, i.e. created with rnp_key_*_signature_create().
* @param flags key flags, which directly maps to the ones described in the OpenPGP
* specification. See the RNP_KEY_USAGE_* constants.
* Note: RNP will not check whether flags are applicable to the key itself (i.e.
* signing flag for encryption-only key), so it's up to the caller to check this.
* @return RNP_SUCCESS or error code if failed.
*/
RNP_API rnp_result_t rnp_key_signature_set_key_flags(rnp_signature_handle_t sig,
uint32_t flags);

/**
* @brief Set the key expiration time. Makes sense only for self-certification or direct-key
* signatures.
*
* @param sig editable key signature handle, i.e. created with rnp_key_*_signature_create().
* @param expiry number of seconds since key creation when it is considered as valid. Zero
* value means that key never expires.
* I.e. if you want key to last for 1 year from now (given that signature
* creation time is set to now), you should calculate the following:
* expiry = now() - rnp_key_get_creation() + 365*24*60*60
* @return RNP_SUCCESS or error code if failed.
*/
RNP_API rnp_result_t rnp_key_signature_set_key_expiration(rnp_signature_handle_t sig,
uint32_t expiry);

/**
* @brief Set the key features. Makes sense only for self-signature.
*
* @param sig editable key signature handle, i.e. created with rnp_key_*_signature_create().
* @param features or'ed together feature flags (RNP_FEATURE_*). For the list of currently
* supported flags please see the description of rnp_signature_get_features().
* @return RNP_SUCCESS or error code if failed.
*/
RNP_API rnp_result_t rnp_key_signature_set_features(rnp_signature_handle_t sig,
uint32_t features);

/**
* @brief Add preferred symmetric algorithm to the signature. Should be subsequently called for
* each algorithm, making first ones of higher priority.
*
* @param sig editable key signature handle, i.e. created with rnp_key_*_signature_create().
* @param alg symmetric algorithm name, cannot be NULL. See
* rnp_op_generate_set_protection_cipher() for the list of possible values.
* @return RNP_SUCCESS or error code if failed.
*/
RNP_API rnp_result_t rnp_key_signature_add_preferred_alg(rnp_signature_handle_t sig,
const char * alg);

/**
* @brief Add preferred hash algorithm to the signature. Should be subsequently called for each
* algorithm, making first ones of higher priority.
*
* @param sig editable key signature handle, i.e. created with rnp_key_*_signature_create().
* @param alg hash algorithm name, cannot be NULL. See rnp_op_generate_set_hash() for the list
* of possible values.
* @return RNP_SUCCESS or error code if failed.
*/
RNP_API rnp_result_t rnp_key_signature_add_preferred_hash(rnp_signature_handle_t sig,
const char * hash);

/**
* @brief Add preferred compression algorithm to the signature. Should be subsequently called
* for each algorithm, making first ones of higher priority.
*
* @param sig editable key signature handle, i.e. created with rnp_key_*_signature_create().
* @param alg compression algorithm name, cannot be NULL. See
* rnp_op_generate_add_pref_compression() for the list of possible values.
* @return RNP_SUCCESS or error code if failed.
*/
RNP_API rnp_result_t rnp_key_signature_add_preferred_zalg(rnp_signature_handle_t sig,
const char * zalg);

/**
* @brief Set whether corresponding user id should be considered as primary. Makes sense only
* for self-certification.
*
* @param sig editable key signature handle, i.e. created with rnp_key_*_signature_create().
* @param primary true for primary or false for not.
* @return RNP_SUCCESS or error code if failed.
*/
RNP_API rnp_result_t rnp_key_signature_set_primary_uid(rnp_signature_handle_t sig,
bool primary);

/**
* @brief Set the key server url which is applicable for this key.
*
* @param sig editable key signature handle, i.e. created with rnp_key_*_signature_create().
* @param keyserver key server url. If NULL or empty string then key server field in the
* signature will be removed.
* @return RNP_SUCCESS or error code if failed.
*/
RNP_API rnp_result_t rnp_key_signature_set_key_server(rnp_signature_handle_t sig,
const char * keyserver);

/**
* @brief Set the key server preferences flags.
*
* @param sig editable key signature handle, i.e. created with rnp_key_*_signature_create().
* @param flags or'ed together preferences flags. Currently only single flag is supported -
* RNP_KEY_SERVER_NO_MODIFY.
* @return RNP_SUCCESS or error code if failed.
*/
RNP_API rnp_result_t rnp_key_signature_set_key_server_prefs(rnp_signature_handle_t sig,
uint32_t flags);

/**
* @brief Set revocation reason and code for the revocation signature.
* See `rnp_key_revoke()` for the details.
Expand Down Expand Up @@ -3839,6 +3959,9 @@ RNP_API const char *rnp_backend_version();
#define RNP_ALGNAME_SHA3_512 "SHA3-512"
#define RNP_ALGNAME_RIPEMD160 "RIPEMD160"
#define RNP_ALGNAME_CRC24 "CRC24"
#define RNP_ALGNAME_ZLIB "ZLib"
#define RNP_ALGNAME_BZIP2 "BZip2"
#define RNP_ALGNAME_ZIP "ZIP"

/* SHA1 is not considered secured anymore and SHOULD NOT be used to create messages (as per
* Appendix C of RFC 4880-bis-02). SHA2 MUST be implemented.
Expand Down
184 changes: 184 additions & 0 deletions src/lib/rnp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6191,6 +6191,190 @@ try {
}
FFI_GUARD

rnp_result_t
rnp_key_signature_set_creation(rnp_signature_handle_t sig, uint32_t ctime)
try {
if (!sig) {
return RNP_ERROR_NULL_POINTER;
}
if (!sig->new_sig) {
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6201 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6201

Added line #L6201 was not covered by tests
}
sig->sig->sig.set_creation(ctime);
return RNP_SUCCESS;
}
FFI_GUARD

Check warning on line 6206 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6206

Added line #L6206 was not covered by tests

rnp_result_t
rnp_key_signature_set_key_flags(rnp_signature_handle_t sig, uint32_t flags)
try {
if (!sig) {
return RNP_ERROR_NULL_POINTER;
}
if (!sig->new_sig) {
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6215 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6215

Added line #L6215 was not covered by tests
}
uint32_t check = flags;
extract_flag(check,
PGP_KF_SIGN | PGP_KF_CERTIFY | PGP_KF_ENCRYPT_COMMS | PGP_KF_ENCRYPT_STORAGE |
PGP_KF_SPLIT | PGP_KF_AUTH | PGP_KF_SHARED);
if (check) {
FFI_LOG(sig->ffi, "Unknown key flags: %#" PRIx32, check);
return RNP_ERROR_BAD_PARAMETERS;
}
sig->sig->sig.set_key_flags(flags & 0xff);
return RNP_SUCCESS;
}
FFI_GUARD

Check warning on line 6228 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6228

Added line #L6228 was not covered by tests

rnp_result_t
rnp_key_signature_set_key_expiration(rnp_signature_handle_t sig, uint32_t expiry)
try {
if (!sig) {
return RNP_ERROR_NULL_POINTER;
}
if (!sig->new_sig) {
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6237 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6237

Added line #L6237 was not covered by tests
}
sig->sig->sig.set_key_expiration(expiry);
return RNP_SUCCESS;
}
FFI_GUARD

Check warning on line 6242 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6242

Added line #L6242 was not covered by tests

rnp_result_t
rnp_key_signature_set_features(rnp_signature_handle_t sig, uint32_t features)
try {
if (!sig) {
return RNP_ERROR_NULL_POINTER;
}
if (!sig->new_sig) {
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6251 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6251

Added line #L6251 was not covered by tests
}
uint32_t flags = features;
extract_flag(flags, RNP_KEY_FEATURE_MDC | RNP_KEY_FEATURE_AEAD | RNP_KEY_FEATURE_V5);
if (flags) {
FFI_LOG(sig->ffi, "Unknown key features: %#" PRIx32, flags);
return RNP_ERROR_BAD_PARAMETERS;
}
sig->sig->sig.set_key_features(features & 0xff);
return RNP_SUCCESS;
}
FFI_GUARD

Check warning on line 6262 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6262

Added line #L6262 was not covered by tests

rnp_result_t
rnp_key_signature_add_preferred_alg(rnp_signature_handle_t sig, const char *alg)
try {
if (!sig || !alg) {
return RNP_ERROR_NULL_POINTER;
}
if (!sig->new_sig) {
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6271 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6271

Added line #L6271 was not covered by tests
}
auto symm_alg = id_str_pair::lookup(symm_alg_map, alg, PGP_SA_UNKNOWN);
if (symm_alg == PGP_SA_UNKNOWN) {
FFI_LOG(sig->ffi, "Unknown symmetric algorithm: %s", alg);
return RNP_ERROR_BAD_PARAMETERS;
}
auto algs = sig->sig->sig.preferred_symm_algs();
algs.push_back(symm_alg);
sig->sig->sig.set_preferred_symm_algs(algs);
return RNP_SUCCESS;
}
FFI_GUARD

Check warning on line 6283 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6283

Added line #L6283 was not covered by tests

rnp_result_t
rnp_key_signature_add_preferred_hash(rnp_signature_handle_t sig, const char *hash)
try {
if (!sig || !hash) {
return RNP_ERROR_NULL_POINTER;
}
if (!sig->new_sig) {
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6292 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6292

Added line #L6292 was not covered by tests
}
auto hash_alg = id_str_pair::lookup(hash_alg_map, hash, PGP_HASH_UNKNOWN);
if (hash_alg == PGP_HASH_UNKNOWN) {
FFI_LOG(sig->ffi, "Unknown hash algorithm: %s", hash);
return RNP_ERROR_BAD_PARAMETERS;
}
auto algs = sig->sig->sig.preferred_hash_algs();
algs.push_back(hash_alg);
sig->sig->sig.set_preferred_hash_algs(algs);
return RNP_SUCCESS;
}
FFI_GUARD

Check warning on line 6304 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6304

Added line #L6304 was not covered by tests

rnp_result_t
rnp_key_signature_add_preferred_zalg(rnp_signature_handle_t sig, const char *zalg)
try {
if (!sig || !zalg) {
return RNP_ERROR_NULL_POINTER;
}
if (!sig->new_sig) {
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6313 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6313

Added line #L6313 was not covered by tests
}
auto z_alg = id_str_pair::lookup(compress_alg_map, zalg, PGP_C_UNKNOWN);
if (z_alg == PGP_C_UNKNOWN) {
FFI_LOG(sig->ffi, "Unknown compression algorithm: %s", zalg);
return RNP_ERROR_BAD_PARAMETERS;
}
auto algs = sig->sig->sig.preferred_z_algs();
algs.push_back(z_alg);
sig->sig->sig.set_preferred_z_algs(algs);
return RNP_SUCCESS;
}
FFI_GUARD

Check warning on line 6325 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6325

Added line #L6325 was not covered by tests

rnp_result_t
rnp_key_signature_set_primary_uid(rnp_signature_handle_t sig, bool primary)
try {
if (!sig) {
return RNP_ERROR_NULL_POINTER;
}
if (!sig->new_sig) {
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6334 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6334

Added line #L6334 was not covered by tests
}
sig->sig->sig.set_primary_uid(primary);
return RNP_SUCCESS;
}
FFI_GUARD

Check warning on line 6339 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6339

Added line #L6339 was not covered by tests

rnp_result_t
rnp_key_signature_set_key_server(rnp_signature_handle_t sig, const char *keyserver)
try {
if (!sig) {
return RNP_ERROR_NULL_POINTER;
}
if (!sig->new_sig) {
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6348 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6348

Added line #L6348 was not covered by tests
}
if (!keyserver) {
keyserver = "";
}
sig->sig->sig.set_key_server(keyserver);
return RNP_SUCCESS;
}
FFI_GUARD

Check warning on line 6356 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6356

Added line #L6356 was not covered by tests

rnp_result_t
rnp_key_signature_set_key_server_prefs(rnp_signature_handle_t sig, uint32_t flags)
try {
if (!sig) {
return RNP_ERROR_NULL_POINTER;
}
if (!sig->new_sig) {
return RNP_ERROR_BAD_PARAMETERS;

Check warning on line 6365 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6365

Added line #L6365 was not covered by tests
}
uint32_t check = flags;
extract_flag(check, RNP_KEY_SERVER_NO_MODIFY);
if (check) {
FFI_LOG(sig->ffi, "Unknown key server prefs: %#" PRIx32, check);
return RNP_ERROR_BAD_PARAMETERS;
}
sig->sig->sig.set_key_server_prefs(flags & 0xff);
return RNP_SUCCESS;
}
FFI_GUARD

Check warning on line 6376 in src/lib/rnp.cpp

View check run for this annotation

Codecov / codecov/patch

src/lib/rnp.cpp#L6376

Added line #L6376 was not covered by tests

rnp_result_t
rnp_key_signature_set_revocation_reason(rnp_signature_handle_t sig,
const char * code,
Expand Down
Loading

0 comments on commit 10f7330

Please sign in to comment.