blob: 18db95adeec61e35985ae3b5cfca1b17087a7a73 [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
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020063#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020064 if( ctx->type == POLARSSL_PK_RSA )
65 rsa_free( ctx->data );
66 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020067#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020068#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020069 if( ctx->type == POLARSSL_PK_ECKEY || ctx->type == POLARSSL_PK_ECKEY_DH )
70 ecp_keypair_free( ctx->data );
71 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +020072#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020073#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020074 if( ctx->type == POLARSSL_PK_ECDSA )
75 ecdsa_free( ctx->data );
76 else
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +020077#endif
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020078 {
79 ; /* guard for the else's above */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020080 }
81
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +020082 if( ! ctx->dont_free )
83 free( ctx->data );
Manuel Pégourié-Gonnard1f73a652013-07-09 10:26:41 +020084
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020085 ctx->type = POLARSSL_PK_NONE;
86 ctx->data = NULL;
87}
88
89/*
90 * Set a pk_context to a given type
91 */
92int pk_set_type( pk_context *ctx, pk_type_t type )
93{
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +020094 size_t size;
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +020095
Manuel Pégourié-Gonnard374e4b82013-07-09 10:21:34 +020096 if( ctx->type == type )
97 return( 0 );
98
99 if( ctx->type != POLARSSL_PK_NONE )
100 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
101
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200102#if defined(POLARSSL_RSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200103 if( type == POLARSSL_PK_RSA )
104 size = sizeof( rsa_context );
105 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200106#endif
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200107#if defined(POLARSSL_ECP_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200108 if( type == POLARSSL_PK_ECKEY || type == POLARSSL_PK_ECKEY_DH )
109 size = sizeof( ecp_keypair );
110 else
Manuel Pégourié-Gonnard81c313c2013-07-09 10:35:54 +0200111#endif
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200112#if defined(POLARSSL_ECDSA_C)
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200113 if( type == POLARSSL_PK_ECDSA )
114 size = sizeof( ecdsa_context );
115 else
Manuel Pégourié-Gonnard7c5819e2013-07-10 12:29:57 +0200116#endif
Manuel Pégourié-Gonnardfd5164e2013-07-11 16:39:05 +0200117 size = 0; /* should never be executed */
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200118
119 if( ( ctx->data = malloc( size ) ) == NULL )
Manuel Pégourié-Gonnard7a6c9462013-07-09 10:04:07 +0200120 return( POLARSSL_ERR_PK_MALLOC_FAILED );
Manuel Pégourié-Gonnard12e0ed92013-07-04 13:31:32 +0200121
122 memset( ctx->data, 0, size );
123 ctx->type = type;
124
125 return( 0 );
126}
Manuel Pégourié-Gonnard244569f2013-07-10 09:46:30 +0200127
128#if defined(POLARSSL_RSA_C)
129/*
130 * Wrap an RSA context in a PK context
131 */
132int pk_wrap_rsa( pk_context *ctx, const rsa_context *rsa)
133{
134 if( ctx->type != POLARSSL_PK_NONE )
135 return( POLARSSL_ERR_PK_TYPE_MISMATCH );
136
137 ctx->type = POLARSSL_PK_RSA;
138 ctx->data = (rsa_context *) rsa;
139 ctx->dont_free = 1;
140
141 return( 0 );
142}
143#endif