aboutsummaryrefslogtreecommitdiff
path: root/providers/implementations/serializers/serializer_ecx.c
blob: 589c6c25f5a6f1f79727424a2fcd8a5f412afad8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <openssl/err.h>
#include "crypto/ecx.h"
#include "prov/bio.h"             /* ossl_prov_bio_printf() */
#include "prov/implementations.h" /* ecx_keymgmt_functions */
#include "serializer_local.h"

void ecx_get_new_free_import(ECX_KEY_TYPE type,
                             OSSL_OP_keymgmt_new_fn **ecx_new,
                             OSSL_OP_keymgmt_free_fn **ecx_free,
                             OSSL_OP_keymgmt_import_fn **ecx_import)
{
    if (type == ECX_KEY_TYPE_X25519) {
        *ecx_new = ossl_prov_get_keymgmt_new(x25519_keymgmt_functions);
        *ecx_free = ossl_prov_get_keymgmt_free(x25519_keymgmt_functions);
        *ecx_import = ossl_prov_get_keymgmt_import(x25519_keymgmt_functions);
    } else if (type == ECX_KEY_TYPE_X448) {
        *ecx_new = ossl_prov_get_keymgmt_new(x448_keymgmt_functions);
        *ecx_free = ossl_prov_get_keymgmt_free(x448_keymgmt_functions);
        *ecx_import = ossl_prov_get_keymgmt_import(x448_keymgmt_functions);
    } else {
        *ecx_new = NULL;
        *ecx_free = NULL;
        *ecx_import = NULL;
    }
}


int ossl_prov_print_ecx(BIO *out, ECX_KEY *ecxkey, enum ecx_print_type type)
{
    const char *type_label = NULL;

    switch (type) {
    case ecx_print_priv:
        switch (ecxkey->keylen) {
        case X25519_KEYLEN:
            type_label = "X25519 Private-Key";
            break;
        case X448_KEYLEN:
            type_label = "X448 Private-Key";
            break;
        }
        break;
    case ecx_print_pub:
        switch (ecxkey->keylen) {
        case X25519_KEYLEN:
            type_label = "X25519 Public-Key";
            break;
        case X448_KEYLEN:
            type_label = "X448 Public-Key";
            break;
        }
        break;
    }

    if (type == ecx_print_priv && ecxkey->privkey == NULL) {
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }

    if (ossl_prov_bio_printf(out, "%s:\n", type_label) <= 0)
        return 0;
    if (type == ecx_print_priv
            && !ossl_prov_print_labeled_buf(out, "priv:", ecxkey->privkey,
                                            ecxkey->keylen))
        return 0;
    if (!ossl_prov_print_labeled_buf(out, "pub:", ecxkey->pubkey,
                                     ecxkey->keylen))
        return 0;

    return 1;
}


int ossl_prov_ecx_pub_to_der(const void *vecxkey, unsigned char **pder)
{
    const ECX_KEY *ecxkey = vecxkey;
    unsigned char *keyblob;

    if (ecxkey == NULL) {
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }

    keyblob = OPENSSL_memdup(ecxkey->pubkey, ecxkey->keylen);
    if (keyblob == NULL) {
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
        return 0;
    }

    *pder = keyblob;
    return ecxkey->keylen;
}

int ossl_prov_ecx_priv_to_der(const void *vecxkey, unsigned char **pder)
{
    const ECX_KEY *ecxkey = vecxkey;
    ASN1_OCTET_STRING oct;
    int keybloblen;

    if (ecxkey == NULL || ecxkey->privkey == NULL) {
        ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
        return 0;
    }

    oct.data = ecxkey->privkey;
    oct.length = ecxkey->keylen;
    oct.flags = 0;

    keybloblen = i2d_ASN1_OCTET_STRING(&oct, pder);
    if (keybloblen < 0) {
        ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
        return 0;
    }

    return keybloblen;
}