Browse Source

p≡p style struct and list functions

add_key_notification
Volker Birk 3 years ago
parent
commit
fcae699d4c
5 changed files with 369 additions and 2 deletions
  1. +1
    -1
      asn.1/keyreset.asn1
  2. +19
    -0
      src/key_reset.c
  3. +5
    -1
      src/key_reset.h
  4. +195
    -0
      src/keyreset_command.c
  5. +149
    -0
      src/keyreset_command.h

+ 1
- 1
asn.1/keyreset.asn1 View File

@ -6,7 +6,7 @@
-- Written by Volker Birk
KEYRESET
{ iso(1) org(3) dod(6) internet(1) private(4) enterprise(1) pEp(47878) distribution(2) keyreset(2) }
{ iso(1) org(3) dod(6) internet(1) private(4) enterprise(1) pEp(47878) distribution(2) keyreset(1) }
DEFINITIONS AUTOMATIC TAGS EXTENSIBILITY IMPLIED ::=


+ 19
- 0
src/key_reset.c View File

@ -706,3 +706,22 @@ PEP_STATUS key_reset_own_and_deliver_revocations(PEP_SESSION session,
free(revoked_fprs);
return PEP_STATUS_OK;
}
PEP_STATUS key_reset_commands_to_binary(const keyreset_command_list *kcl, char **cmds)
{
assert(kcl && cmds);
if (!(kcl && cmds))
return PEP_ILLEGAL_VALUE;
return PEP_STATUS_OK;
}
PEP_STATUS binary_to_key_reset_commands(const char **cmds, keyreset_command_list **kcl)
{
assert(kcl && cmds);
if (!(kcl && cmds))
return PEP_ILLEGAL_VALUE;
return PEP_STATUS_OK;
}

+ 5
- 1
src/key_reset.h View File

@ -8,6 +8,7 @@
#include "message.h"
#include "message_api.h"
#include "cryptotech.h"
#include "keyreset_command.h"
#ifdef __cplusplus
extern "C" {
@ -163,7 +164,10 @@ PEP_STATUS create_standalone_key_reset_message(PEP_SESSION session,
PEP_STATUS send_key_reset_to_recents(PEP_SESSION session,
const char* old_fpr,
const char* new_fpr);
PEP_STATUS key_reset_commands_to_binary(const keyreset_command_list *kcl, char **cmds);
PEP_STATUS binary_to_key_reset_commands(const char **cmds, keyreset_command_list **kcl);
#ifdef __cplusplus
}
#endif

+ 195
- 0
src/keyreset_command.c View File

@ -0,0 +1,195 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#include "pEp_internal.h"
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "keyreset_command.h"
DYNAMIC_API keyreset_command * new_keyreset_command(const pEp_identity *ident, const char *new_key)
{
keyreset_command *command = NULL;
// key and command should not be NULL, that's bad style (while legal)
assert(ident);
assert(new_key);
command = calloc(1, sizeof(keyreset_command));
assert(command);
if (command == NULL)
goto enomem;
command->ident = ident ? identity_dup(ident) : new_identity(NULL, NULL, NULL, NULL);
if (command->ident == NULL)
goto enomem;
command->new_key = new_key ? strdup(new_key) : strdup("");
assert(command->new_key);
if (command->new_key == NULL)
goto enomem;
return command;
enomem:
free_keyreset_command(command);
return NULL;
}
DYNAMIC_API void free_keyreset_command(keyreset_command * command)
{
if (command) {
free_identity(command->ident);
free(command->new_key);
free(command);
}
}
DYNAMIC_API keyreset_command * keyreset_command_dup(const keyreset_command *src)
{
assert(src);
if (src == NULL)
return NULL;
return new_keyreset_command(src->ident, src->new_key);
}
DYNAMIC_API keyreset_command_list *new_keyreset_command_list(keyreset_command *command)
{
keyreset_command_list *result = calloc(1, sizeof(keyreset_command_list));
assert(result);
if (result && command)
result->command = command;
return result;
}
DYNAMIC_API keyreset_command_list *keyreset_command_list_dup(
const keyreset_command_list *src
)
{
assert(src);
if (src == NULL)
return NULL;
keyreset_command* copy_pair = keyreset_command_dup(src->command);
keyreset_command_list *dst = new_keyreset_command_list(copy_pair);
if (dst == NULL)
return NULL;
keyreset_command_list* src_curr = src->next;
keyreset_command_list** dst_curr_ptr = &dst->next;
while (src_curr) {
copy_pair = keyreset_command_dup(src_curr->command);
if (copy_pair == NULL) {
free_keyreset_command_list(dst);
return NULL;
}
*dst_curr_ptr = new_keyreset_command_list(copy_pair);
if (*dst_curr_ptr == NULL) {
free_keyreset_command(copy_pair);
free_keyreset_command_list(dst);
return NULL;
}
src_curr = src_curr->next;
dst_curr_ptr = &((*dst_curr_ptr)->next);
}
return dst;
}
DYNAMIC_API keyreset_command_list *keyreset_command_list_add(
keyreset_command_list *command_list,
keyreset_command *command
)
{
assert(command);
// empty list (no nodes)
if (command_list == NULL)
return new_keyreset_command_list(command);
// empty list (one node, no command)
if (command_list->command == NULL) {
if (command_list->next)
return NULL; // invalid list
command_list->command = command;
assert(command_list->command);
if (command_list->command == NULL)
return NULL;
return command_list;
}
keyreset_command_list* list_curr = command_list;
while (list_curr->next)
list_curr = list_curr->next;
list_curr->next = new_keyreset_command_list(command);
assert(list_curr->next);
if (list_curr->next == NULL)
return NULL;
return list_curr->next;
}
DYNAMIC_API keyreset_command_list *keyreset_command_list_append(
keyreset_command_list *command_list,
keyreset_command_list *second
)
{
assert(command_list);
if (command_list == NULL)
return NULL;
// second list is empty
if (second == NULL || second->command == NULL)
return command_list;
keyreset_command_list *_s = command_list;
for (keyreset_command_list *_s2 = second; _s2 != NULL; _s2 = _s2->next) {
keyreset_command *_sp = keyreset_command_dup(_s2->command);
if (_sp == NULL)
return NULL;
_s = keyreset_command_list_add(_s, _sp);
if (_s == NULL){
free_keyreset_command(_sp);
return NULL;
}
}
return _s;
}
DYNAMIC_API int keyreset_command_list_length(
const keyreset_command_list *command_list
)
{
int len = 0;
for (const keyreset_command_list *_sl = command_list; _sl && _sl->command; _sl = _sl->next)
len++;
return len;
}
DYNAMIC_API void free_keyreset_command_list(keyreset_command_list *command_list)
{
if (command_list) {
free_keyreset_command_list(command_list->next);
free_keyreset_command(command_list->command);
free(command_list);
}
}

+ 149
- 0
src/keyreset_command.h View File

@ -0,0 +1,149 @@
// This file is under GNU General Public License 3.0
// see LICENSE.txt
#pragma once
#include "dynamic_api.h"
#include "pEpEngine.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _keyreset_command {
pEp_identity *ident;
char *new_key;
} keyreset_command;
// new_keyreset_command() - allocate new keyreset_command
//
// parameters:
// ident (in) identity to reset, including fpr of existing key
// new_key (in) fpr of new key
//
// return command:
// pointer to keyreset_command or NULL on failure
//
// caveat:
// ident and new_key are copied and remain in the ownership of the caller
DYNAMIC_API keyreset_command * new_keyreset_command(const pEp_identity *ident, const char *new_key);
// free_keyreset_command() - free memory allocated by keyreset_command
//
// parameters:
// command (in) pointer to keyreset_command to free
DYNAMIC_API void free_keyreset_command(keyreset_command *command);
// keyreset_command_dup() - duplicate keyreset_command (deep copy)
//
// parameters:
// src (in) pointer to keyreset_command to duplicate
//
// return command:
// pointer to copy of src or NULL on failure
DYNAMIC_API keyreset_command * keyreset_command_dup(const keyreset_command *src);
typedef struct _keyreset_command_list {
keyreset_command *command;
struct _keyreset_command_list *next;
} keyreset_command_list;
// new_keyreset_command_list() - allocate a new keyreset_command_list
//
// parameters:
// command (in) initial command
//
// return command:
// pointer to keyreset_command_list object or NULL if out of memory
//
// caveat:
// the ownership of the command goes to the keyreset_command_list
// next pointer is NULL
DYNAMIC_API keyreset_command_list *new_keyreset_command_list(keyreset_command *command);
// keyreset_command_list_dup() - duplicate a keyreset_command_list (deep copy)
//
// parameters:
// src (in) keyreset_command_list to copy
//
// return command:
// pointer to keyreset_command_list object or NULL if out of memory
// keyreset_command command copies created by this function belong to the returned list
DYNAMIC_API keyreset_command_list *keyreset_command_list_dup(
const keyreset_command_list *src
);
// keyreset_command_list_add() - add key to keyreset_command_list
//
// parameters:
// command_list (in) keyreset_command_list struct or NULL to create a new one
// command (in) keyreset_command to add
//
// return command:
// pointer to last element in keyreset_command_list or NULL if out of memory
//
// caveat:
// the ownership of the command goes to the keyreset_command_list if add is successful
DYNAMIC_API keyreset_command_list *keyreset_command_list_add(
keyreset_command_list *command_list,
keyreset_command *command
);
// keyreset_command_list_append() - append keyreset_command_list to keyreset_command_list
//
// parameters:
// command_list (in) keyreset_command_list struct to append to
// second (in) keyreset_command_list struct to append
//
// return command:
// pointer to last element in command_list or NULL if out of memory
// or command_list is NULL
//
// caveat:
// all commands are being copied before being added to the list
// the original commands are still being owned by the caller
DYNAMIC_API keyreset_command_list *keyreset_command_list_append(
keyreset_command_list *command_list,
keyreset_command_list *second
);
// keyreset_command_list_length() - get length of keyreset_command_list
//
// parameters:
// command_list (in) keyreset_command_list struct to determine length of
//
// return command:
// length of command_list in number of elements
DYNAMIC_API int keyreset_command_list_length(
const keyreset_command_list *command_list
);
// free_keyreset_command_list() - free memory occupied by command_list
//
// parameters:
// command_list (in) keyreset_command_list to free
DYNAMIC_API void free_keyreset_command_list(keyreset_command_list *command_list);
#ifdef __cplusplus
}
#endif

Loading…
Cancel
Save