blob: 6762c75bba818217ecadc750ec76c0f2232e1749 [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;
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020049 ctx->dont_free = 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020050}
51
52/*
53 * Free (the components of) a pk_context
54 */
55void pk_free( pk_context *ctx )
56{
57 if( ctx == NULL )
58 return;
59
60 switch( ctx->type )
61 {
62 case POLARSSL_PK_NONE:
63 break;
64
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020065#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020066 case POLARSSL_PK_RSA:
67 rsa_free( ctx->data );
68 break;
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020069#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020070
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020071#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020072 case POLARSSL_PK_ECKEY:
73 case POLARSSL_PK_ECKEY_DH:
74 ecp_keypair_free( ctx->data );
75 break;
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020076#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020077 }
78
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020079 if( ! ctx->dont_free )
80 free( ctx->data );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020081
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020082 ctx->type = POLARSSL_PK_NONE;
83 ctx->data = NULL;
84}
85
86/*
87 * Set a pk_context to a given type
88 */
89int pk_set_type( pk_context *ctx, pk_type_t type )
90{
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020091 size_t size = 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020092
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +020093 if( ctx->type == type )
94 return( 0 );
95
96 if( ctx->type != POLARSSL_PK_NONE )
97 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
98
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020099 switch( type )
100 {
101#if defined(POLARSSL_RSA_C)
102 case POLARSSL_PK_RSA:
103 size = sizeof( rsa_context );
104 break;
105#endif
106
107#if defined(POLARSSL_ECP_C)
108 case POLARSSL_PK_ECKEY:
109 case POLARSSL_PK_ECKEY_DH:
110 size = sizeof( ecp_keypair );
111 break;
112#endif
113
114 case POLARSSL_PK_NONE:
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +0200115 ; /* Cannot happen, but the cmpiler doesn't know */
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200116 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200117
118 if( ( ctx->data = malloc( size ) ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200119 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200120
121 memset( ctx->data, 0, size );
122 ctx->type = type;
123
124 return( 0 );
125}
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200126
127#if defined(POLARSSL_RSA_C)
128/*
129 * Wrap an RSA context in a PK context
130 */
131int pk_wrap_rsa( pk_context *ctx, const rsa_context *rsa)
132{
133 if( ctx->type != POLARSSL_PK_NONE )
134 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
135
136 ctx->type = POLARSSL_PK_RSA;
137 ctx->data = (rsa_context *) rsa;
138 ctx->dont_free = 1;
139
140 return( 0 );
141}
142#endif