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