blob: 0591b3f1c4469c184ae5f81c5fc21b52dd3e1969 [file] [log] [blame]
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +02001/*
2 * Public Key abstraction layer
3 *
4 * Copyright (C) 2006-2013, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#include "polarssl/config.h"
27
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020028#include "polarssl/pk.h"
29
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020030#if defined(POLARSSL_RSA_C)
31#include "polarssl/rsa.h"
32#endif
33#if defined(POLARSSL_ECP_C)
34#include "polarssl/ecp.h"
35#endif
36
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020037#include <stdlib.h>
38
39/*
40 * Initialise a pk_context
41 */
42void pk_init( pk_context *ctx )
43{
44 if( ctx == NULL )
45 return;
46
47 ctx->type = POLARSSL_PK_NONE;
48 ctx->data = NULL;
49}
50
51/*
52 * Free (the components of) a pk_context
53 */
54void pk_free( pk_context *ctx )
55{
56 if( ctx == NULL )
57 return;
58
59 switch( ctx->type )
60 {
61 case POLARSSL_PK_NONE:
62 break;
63
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020064#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020065 case POLARSSL_PK_RSA:
66 rsa_free( ctx->data );
67 break;
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020068#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020069
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020070#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020071 case POLARSSL_PK_ECKEY:
72 case POLARSSL_PK_ECKEY_DH:
73 ecp_keypair_free( ctx->data );
74 break;
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020075#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020076 }
77
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020078 free( ctx-> data );
79
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020080 ctx->type = POLARSSL_PK_NONE;
81 ctx->data = NULL;
82}
83
84/*
85 * Set a pk_context to a given type
86 */
87int pk_set_type( pk_context *ctx, pk_type_t type )
88{
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020089 size_t size = 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020090
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +020091 if( ctx->type == type )
92 return( 0 );
93
94 if( ctx->type != POLARSSL_PK_NONE )
95 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
96
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020097 switch( type )
98 {
99#if defined(POLARSSL_RSA_C)
100 case POLARSSL_PK_RSA:
101 size = sizeof( rsa_context );
102 break;
103#endif
104
105#if defined(POLARSSL_ECP_C)
106 case POLARSSL_PK_ECKEY:
107 case POLARSSL_PK_ECKEY_DH:
108 size = sizeof( ecp_keypair );
109 break;
110#endif
111
112 case POLARSSL_PK_NONE:
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +0200113 ; /* Cannot happen, but the cmpiler doesn't know */
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200114 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200115
116 if( ( ctx->data = malloc( size ) ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200117 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200118
119 memset( ctx->data, 0, size );
120 ctx->type = type;
121
122 return( 0 );
123}