pkwrite.c 17.3 KB
Newer Older
1 2 3
/*
 *  Public Key layer for writing key files and structures
 *
4
 *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5
 *  SPDX-License-Identifier: Apache-2.0
6
 *
7 8 9
 *  Licensed under the Apache License, Version 2.0 (the "License"); you may
 *  not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
10
 *
11
 *  http://www.apache.org/licenses/LICENSE-2.0
12
 *
13 14 15 16 17
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
18
 *
19
 *  This file is part of mbed TLS (https://tls.mbed.org)
20 21
 */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
22
#if !defined(MBEDTLS_CONFIG_FILE)
23
#include "mbedtls/config.h"
24
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
25
#include MBEDTLS_CONFIG_FILE
26
#endif
27

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
28
#if defined(MBEDTLS_PK_WRITE_C)
29

30 31 32
#include "mbedtls/pk.h"
#include "mbedtls/asn1write.h"
#include "mbedtls/oid.h"
33
#include "mbedtls/platform_util.h"
34

35 36
#include <string.h>

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
37
#if defined(MBEDTLS_RSA_C)
38
#include "mbedtls/rsa.h"
39
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
40
#if defined(MBEDTLS_ECP_C)
41
#include "mbedtls/ecp.h"
42
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
43
#if defined(MBEDTLS_ECDSA_C)
44
#include "mbedtls/ecdsa.h"
45
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
46
#if defined(MBEDTLS_PEM_WRITE_C)
47
#include "mbedtls/pem.h"
48 49
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
50
#if defined(MBEDTLS_PLATFORM_C)
51
#include "mbedtls/platform.h"
52 53
#else
#include <stdlib.h>
54
#define mbedtls_calloc    calloc
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
55
#define mbedtls_free       free
56 57
#endif

58 59 60 61 62 63
/* Parameter validation macros based on platform_util.h */
#define PK_VALIDATE_RET( cond )    \
    MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
#define PK_VALIDATE( cond )        \
    MBEDTLS_INTERNAL_VALIDATE( cond )

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
64
#if defined(MBEDTLS_RSA_C)
65 66 67 68 69 70 71
/*
 *  RSAPublicKey ::= SEQUENCE {
 *      modulus           INTEGER,  -- n
 *      publicExponent    INTEGER   -- e
 *  }
 */
static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker's avatar
Hanno Becker committed
72
                                mbedtls_rsa_context *rsa )
73 74 75
{
    int ret;
    size_t len = 0;
76
    mbedtls_mpi T;
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    mbedtls_mpi_init( &T );

    /* Export E */
    if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
         ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
        goto end_of_export;
    len += ret;

    /* Export N */
    if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
         ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
        goto end_of_export;
    len += ret;

end_of_export:

    mbedtls_mpi_free( &T );
    if( ret < 0 )
        return( ret );
97

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
98 99 100
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
                                                 MBEDTLS_ASN1_SEQUENCE ) );
101

102
    return( (int) len );
103
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
104
#endif /* MBEDTLS_RSA_C */
105

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
106
#if defined(MBEDTLS_ECP_C)
107 108 109 110
/*
 * EC public key is an EC point
 */
static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker's avatar
Hanno Becker committed
111
                               mbedtls_ecp_keypair *ec )
112 113 114
{
    int ret;
    size_t len = 0;
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
115
    unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
116

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
117 118
    if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
                                        MBEDTLS_ECP_PF_UNCOMPRESSED,
119 120 121 122 123
                                        &len, buf, sizeof( buf ) ) ) != 0 )
    {
        return( ret );
    }

124
    if( *p < start || (size_t)( *p - start ) < len )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
125
        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
126 127 128 129

    *p -= len;
    memcpy( *p, buf, len );

130
    return( (int) len );
131 132 133 134 135 136 137 138
}

/*
 * ECParameters ::= CHOICE {
 *   namedCurve         OBJECT IDENTIFIER
 * }
 */
static int pk_write_ec_param( unsigned char **p, unsigned char *start,
Hanno Becker's avatar
Hanno Becker committed
139
                              mbedtls_ecp_keypair *ec )
140 141 142 143 144 145
{
    int ret;
    size_t len = 0;
    const char *oid;
    size_t oid_len;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
146
    if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
147 148
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
149
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
150

151
    return( (int) len );
152
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
153
#endif /* MBEDTLS_ECP_C */
154

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
155
int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
Hanno Becker's avatar
Hanno Becker committed
156
                             const mbedtls_pk_context *key )
157 158 159 160
{
    int ret;
    size_t len = 0;

161 162 163 164 165
    PK_VALIDATE_RET( p != NULL );
    PK_VALIDATE_RET( *p != NULL );
    PK_VALIDATE_RET( start != NULL );
    PK_VALIDATE_RET( key != NULL );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
166 167 168
#if defined(MBEDTLS_RSA_C)
    if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
        MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
169 170
    else
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
171 172 173
#if defined(MBEDTLS_ECP_C)
    if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
        MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
174 175
    else
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
176
        return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
177

178
    return( (int) len );
179 180
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
181
int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
182 183 184 185 186 187
{
    int ret;
    unsigned char *c;
    size_t len = 0, par_len = 0, oid_len;
    const char *oid;

188
    PK_VALIDATE_RET( key != NULL );
189 190 191
    if( size == 0 )
        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
    PK_VALIDATE_RET( buf != NULL );
192

193 194
    c = buf + size;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
195
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
196 197

    if( c - buf < 1 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
198
        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
199 200 201 202 203 204 205 206 207

    /*
     *  SubjectPublicKeyInfo  ::=  SEQUENCE  {
     *       algorithm            AlgorithmIdentifier,
     *       subjectPublicKey     BIT STRING }
     */
    *--c = 0;
    len += 1;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
208 209
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
210

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
211
    if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
212 213 214 215 216
                                       &oid, &oid_len ) ) != 0 )
    {
        return( ret );
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
217 218
#if defined(MBEDTLS_ECP_C)
    if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
219
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
220
        MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
221 222 223
    }
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
224
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
225 226
                                                        par_len ) );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
227 228 229
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
    MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
                                                MBEDTLS_ASN1_SEQUENCE ) );
230

231
    return( (int) len );
232 233
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
234
int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
235 236
{
    int ret;
237
    unsigned char *c;
238 239
    size_t len = 0;

240
    PK_VALIDATE_RET( key != NULL );
241 242 243
    if( size == 0 )
        return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
    PK_VALIDATE_RET( buf != NULL );
244 245 246

    c = buf + size;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
247 248
#if defined(MBEDTLS_RSA_C)
    if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
249
    {
250
        mbedtls_mpi T; /* Temporary holding the exported parameters */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
251 252
        mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );

253 254 255
        /*
         * Export the parameters one after another to avoid simultaneous copies.
         */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
256

257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
        mbedtls_mpi_init( &T );

        /* Export QP */
        if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
            ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
            goto end_of_export;
        len += ret;

        /* Export DQ */
        if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
            ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
            goto end_of_export;
        len += ret;

        /* Export DP */
        if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
            ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
            goto end_of_export;
        len += ret;

        /* Export Q */
278 279
        if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
                                         &T, NULL, NULL ) ) != 0 ||
280 281 282 283 284
             ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
            goto end_of_export;
        len += ret;

        /* Export P */
285 286
        if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
                                         NULL, NULL, NULL ) ) != 0 ||
287 288 289 290 291
             ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
            goto end_of_export;
        len += ret;

        /* Export D */
292 293
        if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
                                         NULL, &T, NULL ) ) != 0 ||
294 295 296 297 298
             ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
            goto end_of_export;
        len += ret;

        /* Export E */
299 300
        if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
                                         NULL, NULL, &T ) ) != 0 ||
301 302 303 304 305
             ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
            goto end_of_export;
        len += ret;

        /* Export N */
306 307
        if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
                                         NULL, NULL, NULL ) ) != 0 ||
308 309 310 311 312 313 314 315 316 317 318
             ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
            goto end_of_export;
        len += ret;

    end_of_export:

        mbedtls_mpi_free( &T );
        if( ret < 0 )
            return( ret );

        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
319
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
320 321 322
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
                                               buf, MBEDTLS_ASN1_CONSTRUCTED |
                                               MBEDTLS_ASN1_SEQUENCE ) );
323 324
    }
    else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
325 326 327
#endif /* MBEDTLS_RSA_C */
#if defined(MBEDTLS_ECP_C)
    if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
328
    {
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
329
        mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
330 331 332 333 334 335 336 337 338 339 340 341 342 343
        size_t pub_len = 0, par_len = 0;

        /*
         * RFC 5915, or SEC1 Appendix C.4
         *
         * ECPrivateKey ::= SEQUENCE {
         *      version        INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
         *      privateKey     OCTET STRING,
         *      parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
         *      publicKey  [1] BIT STRING OPTIONAL
         *    }
         */

        /* publicKey */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
344
        MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
345 346

        if( c - buf < 1 )
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
347
            return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
348 349 350
        *--c = 0;
        pub_len += 1;

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
351 352
        MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
        MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
353

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
354 355 356
        MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
        MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
                            MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
357 358 359
        len += pub_len;

        /* parameters */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
360
        MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
361

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
362 363 364
        MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
        MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
                            MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
365 366 367
        len += par_len;

        /* privateKey: write as MPI then fix tag */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
368 369
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &ec->d ) );
        *c = MBEDTLS_ASN1_OCTET_STRING;
370 371

        /* version */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
372
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
373

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
374 375 376
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
        MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
                                                    MBEDTLS_ASN1_SEQUENCE ) );
377 378
    }
    else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
379 380
#endif /* MBEDTLS_ECP_C */
        return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
381

382
    return( (int) len );
383 384
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
385
#if defined(MBEDTLS_PEM_WRITE_C)
386 387 388 389 390 391 392 393 394

#define PEM_BEGIN_PUBLIC_KEY    "-----BEGIN PUBLIC KEY-----\n"
#define PEM_END_PUBLIC_KEY      "-----END PUBLIC KEY-----\n"

#define PEM_BEGIN_PRIVATE_KEY_RSA   "-----BEGIN RSA PRIVATE KEY-----\n"
#define PEM_END_PRIVATE_KEY_RSA     "-----END RSA PRIVATE KEY-----\n"
#define PEM_BEGIN_PRIVATE_KEY_EC    "-----BEGIN EC PRIVATE KEY-----\n"
#define PEM_END_PRIVATE_KEY_EC      "-----END EC PRIVATE KEY-----\n"

395 396 397 398
/*
 * Max sizes of key per types. Shown as tag + len (+ content).
 */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
399
#if defined(MBEDTLS_RSA_C)
400 401 402 403 404 405 406 407 408 409 410 411
/*
 * RSA public keys:
 *  SubjectPublicKeyInfo  ::=  SEQUENCE  {          1 + 3
 *       algorithm            AlgorithmIdentifier,  1 + 1 (sequence)
 *                                                + 1 + 1 + 9 (rsa oid)
 *                                                + 1 + 1 (params null)
 *       subjectPublicKey     BIT STRING }          1 + 3 + (1 + below)
 *  RSAPublicKey ::= SEQUENCE {                     1 + 3
 *      modulus           INTEGER,  -- n            1 + 3 + MPI_MAX + 1
 *      publicExponent    INTEGER   -- e            1 + 3 + MPI_MAX + 1
 *  }
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
412
#define RSA_PUB_DER_MAX_BYTES   38 + 2 * MBEDTLS_MPI_MAX_SIZE
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428

/*
 * RSA private keys:
 *  RSAPrivateKey ::= SEQUENCE {                    1 + 3
 *      version           Version,                  1 + 1 + 1
 *      modulus           INTEGER,                  1 + 3 + MPI_MAX + 1
 *      publicExponent    INTEGER,                  1 + 3 + MPI_MAX + 1
 *      privateExponent   INTEGER,                  1 + 3 + MPI_MAX + 1
 *      prime1            INTEGER,                  1 + 3 + MPI_MAX / 2 + 1
 *      prime2            INTEGER,                  1 + 3 + MPI_MAX / 2 + 1
 *      exponent1         INTEGER,                  1 + 3 + MPI_MAX / 2 + 1
 *      exponent2         INTEGER,                  1 + 3 + MPI_MAX / 2 + 1
 *      coefficient       INTEGER,                  1 + 3 + MPI_MAX / 2 + 1
 *      otherPrimeInfos   OtherPrimeInfos OPTIONAL  0 (not supported)
 *  }
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
429 430 431
#define MPI_MAX_SIZE_2          MBEDTLS_MPI_MAX_SIZE / 2 + \
                                MBEDTLS_MPI_MAX_SIZE % 2
#define RSA_PRV_DER_MAX_BYTES   47 + 3 * MBEDTLS_MPI_MAX_SIZE \
432 433
                                   + 5 * MPI_MAX_SIZE_2

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
434
#else /* MBEDTLS_RSA_C */
435 436 437 438

#define RSA_PUB_DER_MAX_BYTES   0
#define RSA_PRV_DER_MAX_BYTES   0

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
439
#endif /* MBEDTLS_RSA_C */
440

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
441
#if defined(MBEDTLS_ECP_C)
442 443 444 445 446 447 448 449 450 451 452
/*
 * EC public keys:
 *  SubjectPublicKeyInfo  ::=  SEQUENCE  {      1 + 2
 *    algorithm         AlgorithmIdentifier,    1 + 1 (sequence)
 *                                            + 1 + 1 + 7 (ec oid)
 *                                            + 1 + 1 + 9 (namedCurve oid)
 *    subjectPublicKey  BIT STRING              1 + 2 + 1               [1]
 *                                            + 1 (point format)        [1]
 *                                            + 2 * ECP_MAX (coords)    [1]
 *  }
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
453
#define ECP_PUB_DER_MAX_BYTES   30 + 2 * MBEDTLS_ECP_MAX_BYTES
454 455 456 457 458 459 460 461 462 463

/*
 * EC private keys:
 * ECPrivateKey ::= SEQUENCE {                  1 + 2
 *      version        INTEGER ,                1 + 1 + 1
 *      privateKey     OCTET STRING,            1 + 1 + ECP_MAX
 *      parameters [0] ECParameters OPTIONAL,   1 + 1 + (1 + 1 + 9)
 *      publicKey  [1] BIT STRING OPTIONAL      1 + 2 + [1] above
 *    }
 */
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
464
#define ECP_PRV_DER_MAX_BYTES   29 + 3 * MBEDTLS_ECP_MAX_BYTES
465

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
466
#else /* MBEDTLS_ECP_C */
467 468 469 470

#define ECP_PUB_DER_MAX_BYTES   0
#define ECP_PRV_DER_MAX_BYTES   0

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
471
#endif /* MBEDTLS_ECP_C */
472 473 474 475 476 477

#define PUB_DER_MAX_BYTES   RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
                            RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
#define PRV_DER_MAX_BYTES   RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
                            RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
478
int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
479 480
{
    int ret;
481
    unsigned char output_buf[PUB_DER_MAX_BYTES];
482
    size_t olen = 0;
483

484 485 486
    PK_VALIDATE_RET( key != NULL );
    PK_VALIDATE_RET( buf != NULL || size == 0 );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
487
    if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
488
                                     sizeof(output_buf) ) ) < 0 )
489 490 491 492
    {
        return( ret );
    }

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
493
    if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
494
                                  output_buf + sizeof(output_buf) - ret,
495
                                  ret, buf, size, &olen ) ) != 0 )
496 497 498 499 500 501 502
    {
        return( ret );
    }

    return( 0 );
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
503
int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
504 505
{
    int ret;
506
    unsigned char output_buf[PRV_DER_MAX_BYTES];
Paul Bakker's avatar
Paul Bakker committed
507
    const char *begin, *end;
508
    size_t olen = 0;
509

510 511 512
    PK_VALIDATE_RET( key != NULL );
    PK_VALIDATE_RET( buf != NULL || size == 0 );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
513
    if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
514 515
        return( ret );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
516 517
#if defined(MBEDTLS_RSA_C)
    if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
518 519 520 521 522 523
    {
        begin = PEM_BEGIN_PRIVATE_KEY_RSA;
        end = PEM_END_PRIVATE_KEY_RSA;
    }
    else
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
524 525
#if defined(MBEDTLS_ECP_C)
    if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
526 527 528 529 530 531
    {
        begin = PEM_BEGIN_PRIVATE_KEY_EC;
        end = PEM_END_PRIVATE_KEY_EC;
    }
    else
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
532
        return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
533

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
534
    if( ( ret = mbedtls_pem_write_buffer( begin, end,
535
                                  output_buf + sizeof(output_buf) - ret,
536
                                  ret, buf, size, &olen ) ) != 0 )
537 538 539 540 541 542
    {
        return( ret );
    }

    return( 0 );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
543
#endif /* MBEDTLS_PEM_WRITE_C */
544

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
545
#endif /* MBEDTLS_PK_WRITE_C */