Skip to main content
  • Home
  • login
  • Browse the archive

    swh mirror partner logo
swh logo
SoftwareHeritage
Software
Heritage
Mirror
Features
  • Search

  • Downloads

  • Save code now

  • Add forge now

  • Help

  • 8cde69a
  • /
  • providers
  • /
  • implementations
  • /
  • serializers
  • /
  • serializer_ec_pub.c
Raw File
Permalinks

To reference or cite the objects present in the Software Heritage archive, permalinks based on SoftWare Hash IDentifiers (SWHIDs) must be used.
Select below a type of object currently browsed in order to display its associated SWHID and permalink.

  • content
  • directory
content badge Iframe embedding
swh:1:cnt:e9d90f1d200c027bace911f10afb251d37c75c21
directory badge Iframe embedding
swh:1:dir:582dd766c5f6d0afbeceeabeca0e4a689adc56cc
serializer_ec_pub.c
/*
 * 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/core_numbers.h>
#include <openssl/err.h>
#include <openssl/pem.h>
#include <openssl/types.h>
#include <openssl/params.h>
#include "prov/bio.h"
#include "prov/implementations.h"
#include "serializer_local.h"

static OSSL_OP_serializer_newctx_fn ec_pub_newctx;
static OSSL_OP_serializer_freectx_fn ec_pub_freectx;
static OSSL_OP_serializer_serialize_data_fn ec_pub_der_data;
static OSSL_OP_serializer_serialize_object_fn ec_pub_der;
static OSSL_OP_serializer_serialize_data_fn ec_pub_pem_data;
static OSSL_OP_serializer_serialize_object_fn ec_pub_pem;

static OSSL_OP_serializer_serialize_data_fn ec_pub_print_data;
static OSSL_OP_serializer_serialize_object_fn ec_pub_print;

/* Public key : context */

/*
 * There's no specific implementation context, so we use the provider context
 */
static void *ec_pub_newctx(void *provctx)
{
    return provctx;
}

static void ec_pub_freectx(void *ctx)
{
}

/* Public key : DER */
static int ec_pub_der_data(void *vctx, const OSSL_PARAM params[], BIO *out,
                           OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
    OSSL_OP_keymgmt_new_fn *ec_new;
    OSSL_OP_keymgmt_free_fn *ec_free;
    OSSL_OP_keymgmt_import_fn *ec_import;
    int ok = 0;

    ec_get_new_free_import(&ec_new, &ec_free, &ec_import);

    if (ec_import != NULL) {
        EC_KEY *eckey;

        /* vctx == provctx */
        if ((eckey = ec_new(vctx)) != NULL
            && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
            && ec_pub_der(vctx, eckey, out, cb, cbarg))
            ok = 1;
        ec_free(eckey);
    }
    return ok;
}

static int ec_pub_der(void *ctx, void *eckey, BIO *out,
                      OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
    return ossl_prov_write_pub_der_from_obj(out, eckey, EVP_PKEY_EC,
                                           ossl_prov_prepare_ec_params,
                                           ossl_prov_ec_pub_to_der);
}

/* Public key : PEM */
static int ec_pub_pem_data(void *vctx, const OSSL_PARAM params[], BIO *out,
                           OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
    OSSL_OP_keymgmt_new_fn *ec_new;
    OSSL_OP_keymgmt_free_fn *ec_free;
    OSSL_OP_keymgmt_import_fn *ec_import;
    int ok = 0;

    ec_get_new_free_import(&ec_new, &ec_free, &ec_import);

    if (ec_import != NULL) {
        EC_KEY *eckey;

        /* ctx == provctx */
        if ((eckey = ec_new(vctx)) != NULL
            && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
            && ec_pub_pem(vctx, eckey, out, cb, cbarg))
            ok = 1;
        ec_free(eckey);
    }
    return ok;
}

static int ec_pub_pem(void *vctx, void *eckey, BIO *out,
                      OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
    return ossl_prov_write_pub_pem_from_obj(out, eckey, EVP_PKEY_EC,
                                            ossl_prov_prepare_ec_params,
                                            ossl_prov_ec_pub_to_der);
}

static int ec_pub_print_data(void *vctx, const OSSL_PARAM params[], BIO *out,
                             OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
    OSSL_OP_keymgmt_new_fn *ec_new;
    OSSL_OP_keymgmt_free_fn *ec_free;
    OSSL_OP_keymgmt_import_fn *ec_import;
    int ok = 0;

    ec_get_new_free_import(&ec_new, &ec_free, &ec_import);

    if (ec_import != NULL) {
        EC_KEY *eckey;

        /* ctx == provctx */
        if ((eckey = ec_new(vctx)) != NULL
            && ec_import(eckey, OSSL_KEYMGMT_SELECT_KEYPAIR, params)
            && ec_pub_print(vctx, eckey, out, cb, cbarg))
            ok = 1;
        ec_free(eckey);
    }
    return ok;
}

static int ec_pub_print(void *vctx, void *eckey, BIO *out,
                        OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
{
    return ossl_prov_print_eckey(out, eckey, ec_print_pub);
}

const OSSL_DISPATCH ec_pub_der_serializer_functions[] = {
    { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_pub_newctx },
    { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_pub_freectx },
    { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_pub_der_data },
    { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pub_der },
    { 0, NULL }
};

const OSSL_DISPATCH ec_pub_pem_serializer_functions[] = {
    { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_pub_newctx },
    { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_pub_freectx },
    { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA, (void (*)(void))ec_pub_pem_data },
    { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pub_pem },
    { 0, NULL }
};

const OSSL_DISPATCH ec_pub_text_serializer_functions[] = {
    { OSSL_FUNC_SERIALIZER_NEWCTX, (void (*)(void))ec_pub_newctx },
    { OSSL_FUNC_SERIALIZER_FREECTX, (void (*)(void))ec_pub_freectx },
    { OSSL_FUNC_SERIALIZER_SERIALIZE_OBJECT, (void (*)(void))ec_pub_print },
    { OSSL_FUNC_SERIALIZER_SERIALIZE_DATA,
      (void (*)(void))ec_pub_print_data },
    { 0, NULL }
};

ENEA — Copyright (C), ENEA. License: GNU AGPLv3+.
Legal notes  ::  JavaScript license information ::  Web API

back to top