src/internal_format.c
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Thu, 04 Jun 2020 11:18:45 +0200
changeset 4729 3df9a2a67597
parent 4639 09116d12e937
child 4792 7056435ab9e7
permissions -rw-r--r--
forgot test files
vb@4625
     1
// This file is under GNU General Public License 3.0
vb@4625
     2
// see LICENSE.txt
vb@4625
     3
vb@4625
     4
#include "platform.h"
vb@4625
     5
vb@4625
     6
#include "pEp_internal.h"
vb@4625
     7
#include "internal_format.h"
vb@4625
     8
vb@4625
     9
static struct _internal_message_type {
vb@4625
    10
    char type;
vb@4625
    11
    char subtype;
vb@4625
    12
    const char *mime_type;
vb@4625
    13
} message_type[] = {
vb@4625
    14
    // Keys
vb@4625
    15
    { 'K',  0, "application/keys" },
vb@4625
    16
vb@4625
    17
    // OpenPGP
vb@4625
    18
    { 'K',  2, "application/pgp-keys" },
vb@4625
    19
vb@4625
    20
    // x.509
vb@4625
    21
    { 'K',  3, "application/pkcs10" },
vb@4625
    22
    { 'K',  4, "application/pkix-cert" },
vb@4625
    23
    { 'K',  5, "application/pkix-crl" },
vb@4625
    24
    { 'K',  6, "application/pkcs7-mime" },
vb@4625
    25
    { 'K',  7, "application/x-x509-ca-cert" },
vb@4625
    26
    { 'K',  8, "application/x-x509-user-cert" },
vb@4625
    27
    { 'K',  9, "application/x-pkcs7-crl" },
vb@4625
    28
    { 'K', 10, "application/x-pem-file" },
vb@4625
    29
    { 'K', 11, "application/x-pkcs12" },
vb@4625
    30
    { 'K', 12, "application/x-pkcs7-certificates" },
vb@4625
    31
    { 'K', 13, "application/x-pkcs7-certreqresp" },
vb@4625
    32
vb@4625
    33
    // Sync
vb@4625
    34
    { 'S', 0, "application/pEp.sync" },
vb@4625
    35
vb@4625
    36
    // Distribution
vb@4625
    37
    { 'D', 0, "application/pEp.distribution" },
vb@4625
    38
    { 'D', 0, "application/pEp.keyreset" },
vb@4625
    39
vb@4625
    40
    // Authentication
vb@4625
    41
    { 'A', 0, "application/auth" },
vb@4625
    42
    { 'A', 1, "application/signature" },
vb@4625
    43
vb@4625
    44
    // OpenPGP
vb@4625
    45
    { 'A', 2, "application/pgp-signature" },
vb@4625
    46
vb@4625
    47
    // x.509
vb@4625
    48
    { 'A', 3, "application/pkcs7-signature" },
vb@4625
    49
    { 'A', 3, "application/x-pkcs7-signature" },
vb@4625
    50
    
vb@4625
    51
    // end marker
vb@4625
    52
    { 0, 0, NULL }
vb@4625
    53
};
vb@4625
    54
vb@4625
    55
DYNAMIC_API PEP_STATUS encode_internal(
vb@4625
    56
        const char *value,
vb@4625
    57
        size_t size,
vb@4625
    58
        const char *mime_type,
vb@4625
    59
        char **code,
vb@4625
    60
        size_t *code_size
vb@4625
    61
    )
vb@4625
    62
{
vb@4625
    63
    assert(value && size && mime_type && code && code_size);
vb@4625
    64
    if (!(value && size && mime_type && code && code_size))
vb@4625
    65
        return PEP_ILLEGAL_VALUE;
vb@4625
    66
vb@4625
    67
    *code = NULL;
vb@4625
    68
    *code_size = 0;
vb@4625
    69
vb@4625
    70
    char type = 0;
vb@4625
    71
    char subtype;
vb@4625
    72
vb@4625
    73
    struct _internal_message_type *mt;
vb@4625
    74
    for (mt = message_type; mt->type; ++mt) {
vb@4625
    75
        if (strcasecmp(mime_type, mt->mime_type) == 0) {
vb@4625
    76
            type = mt->type;
vb@4625
    77
            subtype = mt->subtype;
vb@4625
    78
            break;
vb@4625
    79
        }
vb@4625
    80
    }
vb@4625
    81
vb@4625
    82
    // unsupported MIME type
vb@4625
    83
    if (!type)
vb@4625
    84
        return PEP_STATUS_OK;
vb@4625
    85
vb@4625
    86
    // those are more BSOBs than BLOBS, so we copy
vb@4625
    87
    char *result = malloc(size + 4);
vb@4625
    88
    assert(result);
vb@4625
    89
    if (!result)
vb@4625
    90
        return PEP_OUT_OF_MEMORY;
vb@4625
    91
vb@4625
    92
    result[0] = 0;
vb@4625
    93
    result[1] = type;
vb@4625
    94
    result[2] = subtype;
vb@4625
    95
    result[3] = 0;
vb@4625
    96
vb@4625
    97
    memcpy(result + 4, value, size);
vb@4625
    98
    
vb@4625
    99
    *code = result;
vb@4625
   100
    *code_size = size + 4;
vb@4625
   101
vb@4625
   102
    return PEP_STATUS_OK;
vb@4625
   103
}
vb@4625
   104
vb@4625
   105
DYNAMIC_API PEP_STATUS decode_internal(
vb@4625
   106
        const char *code,
vb@4625
   107
        size_t code_size,
vb@4625
   108
        char **value,
vb@4625
   109
        size_t *size,
vb@4625
   110
        char **mime_type
vb@4625
   111
    )
vb@4625
   112
{
vb@4639
   113
    assert(value && size && mime_type && code && !code[0] && code_size);
vb@4639
   114
    if (!(value && size && mime_type && code && !code[0] && code_size))
vb@4625
   115
        return PEP_ILLEGAL_VALUE;
vb@4625
   116
vb@4625
   117
    *value = NULL;
vb@4625
   118
    *size = 0;
vb@4626
   119
    *mime_type = NULL;
vb@4625
   120
vb@4625
   121
    // elevated attachments have at least 5 bytes
vb@4625
   122
    assert(code_size > 4);
vb@4625
   123
    if (code_size < 5)
vb@4625
   124
        return PEP_ILLEGAL_VALUE;
vb@4625
   125
vb@4625
   126
    assert(!code[0]);
vb@4625
   127
    char type = code[1];
vb@4625
   128
    char subtype = code[2];
vb@4625
   129
    // char reserved = code[3];
vb@4625
   130
vb@4625
   131
    char *_mime_type = NULL;
vb@4625
   132
vb@4625
   133
    struct _internal_message_type *mt;
vb@4625
   134
    for (mt = message_type; mt->type; ++mt) {
vb@4625
   135
        if (type == mt->type && subtype == mt->subtype) {
vb@4625
   136
            assert(mt->mime_type);
vb@4625
   137
            _mime_type = strdup(mt->mime_type);
vb@4625
   138
            assert(_mime_type);
vb@4625
   139
            if (!_mime_type)
vb@4625
   140
                return PEP_OUT_OF_MEMORY;
vb@4625
   141
vb@4625
   142
            break;
vb@4625
   143
        }
vb@4625
   144
    }
vb@4625
   145
vb@4625
   146
    if (!_mime_type)
vb@4625
   147
        return PEP_ILLEGAL_VALUE;
vb@4625
   148
vb@4625
   149
    char *result = malloc(code_size - 4);
vb@4625
   150
    assert(result);
vb@4625
   151
    if (!result) {
vb@4625
   152
        free(_mime_type);
vb@4625
   153
        return PEP_OUT_OF_MEMORY;
vb@4625
   154
    }
vb@4625
   155
vb@4625
   156
    memcpy(result, code + 4, code_size - 4);
vb@4625
   157
vb@4625
   158
    *value = result;
vb@4625
   159
    *size = code_size - 4;
vb@4626
   160
    *mime_type = _mime_type;
vb@4625
   161
vb@4625
   162
    return PEP_STATUS_OK;
vb@4625
   163
}
vb@4625
   164