pk.c 3.74 KB
Newer Older
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
/*
 *  Public Key abstraction layer
 *
 *  Copyright (C) 2006-2013, Brainspark B.V.
 *
 *  This file is part of PolarSSL (http://www.polarssl.org)
 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
 *
 *  All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "polarssl/config.h"

28
#include "polarssl/pk.h"
29
#include "polarssl/pk_wrap.h"
30 31

#if defined(POLARSSL_RSA_C)
32
#include "polarssl/rsa.h"
33 34
#endif
#if defined(POLARSSL_ECP_C)
35
#include "polarssl/ecp.h"
36
#endif
37 38 39
#if defined(POLARSSL_ECDSA_C)
#include "polarssl/ecdsa.h"
#endif
40

41 42 43 44 45 46 47
#if defined(POLARSSL_MEMORY_C)
#include "polarssl/memory.h"
#else
#define polarssl_malloc     malloc
#define polarssl_free       free
#endif

48 49 50 51 52 53 54 55 56 57
#include <stdlib.h>

/*
 * Initialise a pk_context
 */
void pk_init( pk_context *ctx )
{
    if( ctx == NULL )
        return;

58
    ctx->info = NULL;
59 60
    ctx->type = POLARSSL_PK_NONE;
    ctx->data = NULL;
61
    ctx->dont_free = 0;
62 63 64 65 66 67 68 69 70 71
}

/*
 * Free (the components of) a pk_context
 */
void pk_free( pk_context *ctx )
{
    if( ctx == NULL )
        return;

72
#if defined(POLARSSL_RSA_C)
73 74 75
    if( ctx->type == POLARSSL_PK_RSA )
        rsa_free( ctx->data );
    else
76 77
#endif
#if defined(POLARSSL_ECP_C)
78 79 80
    if( ctx->type == POLARSSL_PK_ECKEY || ctx->type == POLARSSL_PK_ECKEY_DH )
        ecp_keypair_free( ctx->data );
    else
81
#endif
82
#if defined(POLARSSL_ECDSA_C)
83 84 85
    if( ctx->type == POLARSSL_PK_ECDSA )
        ecdsa_free( ctx->data );
    else
86
#endif
87 88
    {
        ; /* guard for the else's above */
89 90
    }

91
    if( ! ctx->dont_free )
92
        polarssl_free( ctx->data );
93

94
    ctx->info = NULL;
95 96 97 98 99 100 101 102 103
    ctx->type = POLARSSL_PK_NONE;
    ctx->data = NULL;
}

/*
 * Set a pk_context to a given type
 */
int pk_set_type( pk_context *ctx, pk_type_t type )
{
104
    size_t size;
105
    const pk_info_t *info;
106

107 108 109 110 111 112
    if( ctx->type == type )
        return( 0 );

    if( ctx->type != POLARSSL_PK_NONE )
        return( POLARSSL_ERR_PK_TYPE_MISMATCH );

113
#if defined(POLARSSL_RSA_C)
114
    if( type == POLARSSL_PK_RSA )
115
    {
116
        size = sizeof( rsa_context );
117 118
        info = &rsa_info;
    }
119
    else
120 121
#endif
#if defined(POLARSSL_ECP_C)
122
    if( type == POLARSSL_PK_ECKEY )
123
    {
124
        size = sizeof( ecp_keypair );
125 126
        info = &eckey_info;
    }
127 128 129 130 131
    else if( type == POLARSSL_PK_ECKEY_DH )
    {
        size = sizeof( ecp_keypair );
        info = &eckeydh_info;
    }
132
    else
133
#endif
134
#if defined(POLARSSL_ECDSA_C)
135
    if( type == POLARSSL_PK_ECDSA )
136
    {
137
        size = sizeof( ecdsa_context );
138 139
        info = &ecdsa_info;
    }
140
    else
141
#endif
142
        return( POLARSSL_ERR_PK_TYPE_MISMATCH );
143

144
    if( ( ctx->data = polarssl_malloc( size ) ) == NULL )
145
        return( POLARSSL_ERR_PK_MALLOC_FAILED );
146 147 148

    memset( ctx->data, 0, size );
    ctx->type = type;
149
    ctx->info = info;
150 151 152

    return( 0 );
}
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

#if defined(POLARSSL_RSA_C)
/*
 * Wrap an RSA context in a PK context
 */
int pk_wrap_rsa( pk_context *ctx, const rsa_context *rsa)
{
    if( ctx->type != POLARSSL_PK_NONE )
        return( POLARSSL_ERR_PK_TYPE_MISMATCH );

    ctx->type = POLARSSL_PK_RSA;
    ctx->data = (rsa_context *) rsa;
    ctx->dont_free = 1;

    return( 0 );
}
#endif