blob: 3ea600ac99c58eef1997216988a8616ba18dd204 [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
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020036#if defined(POLARSSL_ECDSA_C)
37#include "polarssl/ecdsa.h"
38#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020039
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020040#include <stdlib.h>
41
42/*
43 * Initialise a pk_context
44 */
45void pk_init( pk_context *ctx )
46{
47 if( ctx == NULL )
48 return;
49
50 ctx->type = POLARSSL_PK_NONE;
51 ctx->data = NULL;
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020052 ctx->dont_free = 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020053}
54
55/*
56 * Free (the components of) a pk_context
57 */
58void pk_free( pk_context *ctx )
59{
60 if( ctx == NULL )
61 return;
62
63 switch( ctx->type )
64 {
65 case POLARSSL_PK_NONE:
66 break;
67
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020068#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020069 case POLARSSL_PK_RSA:
70 rsa_free( ctx->data );
71 break;
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020072#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020073
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020074#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020075 case POLARSSL_PK_ECKEY:
76 case POLARSSL_PK_ECKEY_DH:
77 ecp_keypair_free( ctx->data );
78 break;
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020079#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020080
81#if defined(POLARSSL_ECDSA_C)
82 case POLARSSL_PK_ECDSA:
83 ecdsa_free( ctx->data );
84 break;
85#endif
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020086 }
87
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020088 if( ! ctx->dont_free )
89 free( ctx->data );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020090
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020091 ctx->type = POLARSSL_PK_NONE;
92 ctx->data = NULL;
93}
94
95/*
96 * Set a pk_context to a given type
97 */
98int pk_set_type( pk_context *ctx, pk_type_t type )
99{
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200100 size_t size = 0;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200101
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +0200102 if( ctx->type == type )
103 return( 0 );
104
105 if( ctx->type != POLARSSL_PK_NONE )
106 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
107
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200108 switch( type )
109 {
110#if defined(POLARSSL_RSA_C)
111 case POLARSSL_PK_RSA:
112 size = sizeof( rsa_context );
113 break;
114#endif
115
116#if defined(POLARSSL_ECP_C)
117 case POLARSSL_PK_ECKEY:
118 case POLARSSL_PK_ECKEY_DH:
119 size = sizeof( ecp_keypair );
120 break;
121#endif
122
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200123#if defined(POLARSSL_ECDSA_C)
124 case POLARSSL_PK_ECDSA:
125 size = sizeof( ecdsa_context );
126 break;
127#endif
128
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200129 case POLARSSL_PK_NONE:
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200130 ; /* Cannot happen, but the compiler doesn't know */
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200131 }
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200132
133 if( ( ctx->data = malloc( size ) ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200134 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200135
136 memset( ctx->data, 0, size );
137 ctx->type = type;
138
139 return( 0 );
140}
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200141
142#if defined(POLARSSL_RSA_C)
143/*
144 * Wrap an RSA context in a PK context
145 */
146int pk_wrap_rsa( pk_context *ctx, const rsa_context *rsa)
147{
148 if( ctx->type != POLARSSL_PK_NONE )
149 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
150
151 ctx->type = POLARSSL_PK_RSA;
152 ctx->data = (rsa_context *) rsa;
153 ctx->dont_free = 1;
154
155 return( 0 );
156}
157#endif