1 /*
2  *  Public Key layer for writing key files and structures
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0
6  *
7  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
8  *  not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *
11  *  http://www.apache.org/licenses/LICENSE-2.0
12  *
13  *  Unless required by applicable law or agreed to in writing, software
14  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  *  See the License for the specific language governing permissions and
17  *  limitations under the License.
18  *
19  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27 
28 #if defined(MBEDTLS_PK_WRITE_C)
29 
30 #include "mbedtls/pk.h"
31 #include "mbedtls/asn1write.h"
32 #include "mbedtls/oid.h"
33 #include "mbedtls/platform_util.h"
34 
35 #include <string.h>
36 
37 #if defined(MBEDTLS_RSA_C)
38 #include "mbedtls/rsa.h"
39 #endif
40 #if defined(MBEDTLS_ECP_C)
41 #include "mbedtls/ecp.h"
42 #endif
43 #if defined(MBEDTLS_ECDSA_C)
44 #include "mbedtls/ecdsa.h"
45 #endif
46 #if defined(MBEDTLS_PEM_WRITE_C)
47 #include "mbedtls/pem.h"
48 #endif
49 
50 #if defined(MBEDTLS_PLATFORM_C)
51 #include "mbedtls/platform.h"
52 #else
53 #include <stdlib.h>
54 #define mbedtls_calloc    calloc
55 #define mbedtls_free       free
56 #endif
57 
58 /* Parameter validation macros based on platform_util.h */
59 #define PK_VALIDATE_RET( cond )    \
60     MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_PK_BAD_INPUT_DATA )
61 #define PK_VALIDATE( cond )        \
62     MBEDTLS_INTERNAL_VALIDATE( cond )
63 
64 #if defined(MBEDTLS_RSA_C)
65 /*
66  *  RSAPublicKey ::= SEQUENCE {
67  *      modulus           INTEGER,  -- n
68  *      publicExponent    INTEGER   -- e
69  *  }
70  */
pk_write_rsa_pubkey(unsigned char ** p,unsigned char * start,mbedtls_rsa_context * rsa)71 static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
72                                 mbedtls_rsa_context *rsa )
73 {
74     int ret;
75     size_t len = 0;
76     mbedtls_mpi T;
77 
78     mbedtls_mpi_init( &T );
79 
80     /* Export E */
81     if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 ||
82          ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
83         goto end_of_export;
84     len += ret;
85 
86     /* Export N */
87     if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 ||
88          ( ret = mbedtls_asn1_write_mpi( p, start, &T ) ) < 0 )
89         goto end_of_export;
90     len += ret;
91 
92 end_of_export:
93 
94     mbedtls_mpi_free( &T );
95     if( ret < 0 )
96         return( ret );
97 
98     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
99     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
100                                                  MBEDTLS_ASN1_SEQUENCE ) );
101 
102     return( (int) len );
103 }
104 #endif /* MBEDTLS_RSA_C */
105 
106 #if defined(MBEDTLS_ECP_C)
107 /*
108  * EC public key is an EC point
109  */
pk_write_ec_pubkey(unsigned char ** p,unsigned char * start,mbedtls_ecp_keypair * ec)110 static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
111                                mbedtls_ecp_keypair *ec )
112 {
113     int ret;
114     size_t len = 0;
115     unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
116 
117     if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
118                                         MBEDTLS_ECP_PF_UNCOMPRESSED,
119                                         &len, buf, sizeof( buf ) ) ) != 0 )
120     {
121         return( ret );
122     }
123 
124     if( *p < start || (size_t)( *p - start ) < len )
125         return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
126 
127     *p -= len;
128     memcpy( *p, buf, len );
129 
130     return( (int) len );
131 }
132 
133 /*
134  * ECParameters ::= CHOICE {
135  *   namedCurve         OBJECT IDENTIFIER
136  * }
137  */
pk_write_ec_param(unsigned char ** p,unsigned char * start,mbedtls_ecp_keypair * ec)138 static int pk_write_ec_param( unsigned char **p, unsigned char *start,
139                               mbedtls_ecp_keypair *ec )
140 {
141     int ret;
142     size_t len = 0;
143     const char *oid;
144     size_t oid_len;
145 
146     if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
147         return( ret );
148 
149     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
150 
151     return( (int) len );
152 }
153 #endif /* MBEDTLS_ECP_C */
154 
mbedtls_pk_write_pubkey(unsigned char ** p,unsigned char * start,const mbedtls_pk_context * key)155 int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
156                              const mbedtls_pk_context *key )
157 {
158     int ret;
159     size_t len = 0;
160 
161     PK_VALIDATE_RET( p != NULL );
162     PK_VALIDATE_RET( *p != NULL );
163     PK_VALIDATE_RET( start != NULL );
164     PK_VALIDATE_RET( key != NULL );
165 
166 #if defined(MBEDTLS_RSA_C)
167     if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
168         MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
169     else
170 #endif
171 #if defined(MBEDTLS_ECP_C)
172     if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
173         MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
174     else
175 #endif
176         return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
177 
178     return( (int) len );
179 }
180 
mbedtls_pk_write_pubkey_der(mbedtls_pk_context * key,unsigned char * buf,size_t size)181 int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
182 {
183     int ret;
184     unsigned char *c;
185     size_t len = 0, par_len = 0, oid_len;
186     const char *oid;
187 
188     PK_VALIDATE_RET( key != NULL );
189     if( size == 0 )
190         return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
191     PK_VALIDATE_RET( buf != NULL );
192 
193     c = buf + size;
194 
195     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
196 
197     if( c - buf < 1 )
198         return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
199 
200     /*
201      *  SubjectPublicKeyInfo  ::=  SEQUENCE  {
202      *       algorithm            AlgorithmIdentifier,
203      *       subjectPublicKey     BIT STRING }
204      */
205     *--c = 0;
206     len += 1;
207 
208     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
209     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
210 
211     if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
212                                        &oid, &oid_len ) ) != 0 )
213     {
214         return( ret );
215     }
216 
217 #if defined(MBEDTLS_ECP_C)
218     if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
219     {
220         MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
221     }
222 #endif
223 
224     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
225                                                         par_len ) );
226 
227     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
228     MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
229                                                 MBEDTLS_ASN1_SEQUENCE ) );
230 
231     return( (int) len );
232 }
233 
mbedtls_pk_write_key_der(mbedtls_pk_context * key,unsigned char * buf,size_t size)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     size_t len = 0;
239 
240     PK_VALIDATE_RET( key != NULL );
241     if( size == 0 )
242         return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
243     PK_VALIDATE_RET( buf != NULL );
244 
245     c = buf + size;
246 
247 #if defined(MBEDTLS_RSA_C)
248     if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
249     {
250         mbedtls_mpi T; /* Temporary holding the exported parameters */
251         mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
252 
253         /*
254          * Export the parameters one after another to avoid simultaneous copies.
255          */
256 
257         mbedtls_mpi_init( &T );
258 
259         /* Export QP */
260         if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 ||
261             ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
262             goto end_of_export;
263         len += ret;
264 
265         /* Export DQ */
266         if( ( ret = mbedtls_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 ||
267             ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
268             goto end_of_export;
269         len += ret;
270 
271         /* Export DP */
272         if( ( ret = mbedtls_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 ||
273             ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
274             goto end_of_export;
275         len += ret;
276 
277         /* Export Q */
278         if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
279                                          &T, NULL, NULL ) ) != 0 ||
280              ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
281             goto end_of_export;
282         len += ret;
283 
284         /* Export P */
285         if ( ( ret = mbedtls_rsa_export( rsa, NULL, &T,
286                                          NULL, NULL, NULL ) ) != 0 ||
287              ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
288             goto end_of_export;
289         len += ret;
290 
291         /* Export D */
292         if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
293                                          NULL, &T, NULL ) ) != 0 ||
294              ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
295             goto end_of_export;
296         len += ret;
297 
298         /* Export E */
299         if ( ( ret = mbedtls_rsa_export( rsa, NULL, NULL,
300                                          NULL, NULL, &T ) ) != 0 ||
301              ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
302             goto end_of_export;
303         len += ret;
304 
305         /* Export N */
306         if ( ( ret = mbedtls_rsa_export( rsa, &T, NULL,
307                                          NULL, NULL, NULL ) ) != 0 ||
308              ( ret = mbedtls_asn1_write_mpi( &c, buf, &T ) ) < 0 )
309             goto end_of_export;
310         len += ret;
311 
312     end_of_export:
313 
314         mbedtls_mpi_free( &T );
315         if( ret < 0 )
316             return( ret );
317 
318         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
319         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
320         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c,
321                                                buf, MBEDTLS_ASN1_CONSTRUCTED |
322                                                MBEDTLS_ASN1_SEQUENCE ) );
323     }
324     else
325 #endif /* MBEDTLS_RSA_C */
326 #if defined(MBEDTLS_ECP_C)
327     if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
328     {
329         mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
330         size_t pub_len = 0, par_len = 0;
331 
332         /*
333          * RFC 5915, or SEC1 Appendix C.4
334          *
335          * ECPrivateKey ::= SEQUENCE {
336          *      version        INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
337          *      privateKey     OCTET STRING,
338          *      parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
339          *      publicKey  [1] BIT STRING OPTIONAL
340          *    }
341          */
342 
343         /* publicKey */
344         MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
345 
346         if( c - buf < 1 )
347             return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
348         *--c = 0;
349         pub_len += 1;
350 
351         MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
352         MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
353 
354         MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
355         MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
356                             MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
357         len += pub_len;
358 
359         /* parameters */
360         MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
361 
362         MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
363         MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
364                             MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
365         len += par_len;
366 
367         /* privateKey: write as MPI then fix tag */
368         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &ec->d ) );
369         *c = MBEDTLS_ASN1_OCTET_STRING;
370 
371         /* version */
372         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
373 
374         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
375         MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
376                                                     MBEDTLS_ASN1_SEQUENCE ) );
377     }
378     else
379 #endif /* MBEDTLS_ECP_C */
380         return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
381 
382     return( (int) len );
383 }
384 
385 #if defined(MBEDTLS_PEM_WRITE_C)
386 
387 #define PEM_BEGIN_PUBLIC_KEY    "-----BEGIN PUBLIC KEY-----\n"
388 #define PEM_END_PUBLIC_KEY      "-----END PUBLIC KEY-----\n"
389 
390 #define PEM_BEGIN_PRIVATE_KEY_RSA   "-----BEGIN RSA PRIVATE KEY-----\n"
391 #define PEM_END_PRIVATE_KEY_RSA     "-----END RSA PRIVATE KEY-----\n"
392 #define PEM_BEGIN_PRIVATE_KEY_EC    "-----BEGIN EC PRIVATE KEY-----\n"
393 #define PEM_END_PRIVATE_KEY_EC      "-----END EC PRIVATE KEY-----\n"
394 
395 /*
396  * Max sizes of key per types. Shown as tag + len (+ content).
397  */
398 
399 #if defined(MBEDTLS_RSA_C)
400 /*
401  * RSA public keys:
402  *  SubjectPublicKeyInfo  ::=  SEQUENCE  {          1 + 3
403  *       algorithm            AlgorithmIdentifier,  1 + 1 (sequence)
404  *                                                + 1 + 1 + 9 (rsa oid)
405  *                                                + 1 + 1 (params null)
406  *       subjectPublicKey     BIT STRING }          1 + 3 + (1 + below)
407  *  RSAPublicKey ::= SEQUENCE {                     1 + 3
408  *      modulus           INTEGER,  -- n            1 + 3 + MPI_MAX + 1
409  *      publicExponent    INTEGER   -- e            1 + 3 + MPI_MAX + 1
410  *  }
411  */
412 #define RSA_PUB_DER_MAX_BYTES   38 + 2 * MBEDTLS_MPI_MAX_SIZE
413 
414 /*
415  * RSA private keys:
416  *  RSAPrivateKey ::= SEQUENCE {                    1 + 3
417  *      version           Version,                  1 + 1 + 1
418  *      modulus           INTEGER,                  1 + 3 + MPI_MAX + 1
419  *      publicExponent    INTEGER,                  1 + 3 + MPI_MAX + 1
420  *      privateExponent   INTEGER,                  1 + 3 + MPI_MAX + 1
421  *      prime1            INTEGER,                  1 + 3 + MPI_MAX / 2 + 1
422  *      prime2            INTEGER,                  1 + 3 + MPI_MAX / 2 + 1
423  *      exponent1         INTEGER,                  1 + 3 + MPI_MAX / 2 + 1
424  *      exponent2         INTEGER,                  1 + 3 + MPI_MAX / 2 + 1
425  *      coefficient       INTEGER,                  1 + 3 + MPI_MAX / 2 + 1
426  *      otherPrimeInfos   OtherPrimeInfos OPTIONAL  0 (not supported)
427  *  }
428  */
429 #define MPI_MAX_SIZE_2          MBEDTLS_MPI_MAX_SIZE / 2 + \
430                                 MBEDTLS_MPI_MAX_SIZE % 2
431 #define RSA_PRV_DER_MAX_BYTES   47 + 3 * MBEDTLS_MPI_MAX_SIZE \
432                                    + 5 * MPI_MAX_SIZE_2
433 
434 #else /* MBEDTLS_RSA_C */
435 
436 #define RSA_PUB_DER_MAX_BYTES   0
437 #define RSA_PRV_DER_MAX_BYTES   0
438 
439 #endif /* MBEDTLS_RSA_C */
440 
441 #if defined(MBEDTLS_ECP_C)
442 /*
443  * EC public keys:
444  *  SubjectPublicKeyInfo  ::=  SEQUENCE  {      1 + 2
445  *    algorithm         AlgorithmIdentifier,    1 + 1 (sequence)
446  *                                            + 1 + 1 + 7 (ec oid)
447  *                                            + 1 + 1 + 9 (namedCurve oid)
448  *    subjectPublicKey  BIT STRING              1 + 2 + 1               [1]
449  *                                            + 1 (point format)        [1]
450  *                                            + 2 * ECP_MAX (coords)    [1]
451  *  }
452  */
453 #define ECP_PUB_DER_MAX_BYTES   30 + 2 * MBEDTLS_ECP_MAX_BYTES
454 
455 /*
456  * EC private keys:
457  * ECPrivateKey ::= SEQUENCE {                  1 + 2
458  *      version        INTEGER ,                1 + 1 + 1
459  *      privateKey     OCTET STRING,            1 + 1 + ECP_MAX
460  *      parameters [0] ECParameters OPTIONAL,   1 + 1 + (1 + 1 + 9)
461  *      publicKey  [1] BIT STRING OPTIONAL      1 + 2 + [1] above
462  *    }
463  */
464 #define ECP_PRV_DER_MAX_BYTES   29 + 3 * MBEDTLS_ECP_MAX_BYTES
465 
466 #else /* MBEDTLS_ECP_C */
467 
468 #define ECP_PUB_DER_MAX_BYTES   0
469 #define ECP_PRV_DER_MAX_BYTES   0
470 
471 #endif /* MBEDTLS_ECP_C */
472 
473 #define PUB_DER_MAX_BYTES   RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
474                             RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
475 #define PRV_DER_MAX_BYTES   RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
476                             RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
477 
mbedtls_pk_write_pubkey_pem(mbedtls_pk_context * key,unsigned char * buf,size_t size)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     PK_VALIDATE_RET( key != NULL );
485     PK_VALIDATE_RET( buf != NULL || size == 0 );
486 
487     if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
488                                      sizeof(output_buf) ) ) < 0 )
489     {
490         return( ret );
491     }
492 
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         return( ret );
498     }
499 
500     return( 0 );
501 }
502 
mbedtls_pk_write_key_pem(mbedtls_pk_context * key,unsigned char * buf,size_t size)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];
507     const char *begin, *end;
508     size_t olen = 0;
509 
510     PK_VALIDATE_RET( key != NULL );
511     PK_VALIDATE_RET( buf != NULL || size == 0 );
512 
513     if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
514         return( ret );
515 
516 #if defined(MBEDTLS_RSA_C)
517     if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
518     {
519         begin = PEM_BEGIN_PRIVATE_KEY_RSA;
520         end = PEM_END_PRIVATE_KEY_RSA;
521     }
522     else
523 #endif
524 #if defined(MBEDTLS_ECP_C)
525     if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
526     {
527         begin = PEM_BEGIN_PRIVATE_KEY_EC;
528         end = PEM_END_PRIVATE_KEY_EC;
529     }
530     else
531 #endif
532         return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
533 
534     if( ( ret = mbedtls_pem_write_buffer( begin, end,
535                                   output_buf + sizeof(output_buf) - ret,
536                                   ret, buf, size, &olen ) ) != 0 )
537     {
538         return( ret );
539     }
540 
541     return( 0 );
542 }
543 #endif /* MBEDTLS_PEM_WRITE_C */
544 
545 #endif /* MBEDTLS_PK_WRITE_C */
546