blob: df3a03c7cecdb4e53f43064a49aa07047f51d2ba [file] [log] [blame]
Fabio Utzigba05f2a2017-12-05 11:00:41 -02001/**
2 * \file pk.h
3 *
4 * \brief Public Key abstraction layer
Fabio Utzig3ac36ea2018-12-27 12:35:39 -02005 */
6/*
Fabio Utzigba05f2a2017-12-05 11:00:41 -02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
8 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
21 *
22 * This file is part of mbed TLS (https://tls.mbed.org)
23 */
24
25#ifndef MBEDTLS_PK_H
26#define MBEDTLS_PK_H
27
28#if !defined(MBEDTLS_CONFIG_FILE)
29#include "config.h"
30#else
31#include MBEDTLS_CONFIG_FILE
32#endif
33
34#include "md.h"
35
36#if defined(MBEDTLS_RSA_C)
37#include "rsa.h"
38#endif
39
40#if defined(MBEDTLS_ECP_C)
41#include "ecp.h"
42#endif
43
44#if defined(MBEDTLS_ECDSA_C)
45#include "ecdsa.h"
46#endif
47
48#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
49 !defined(inline) && !defined(__cplusplus)
50#define inline __inline
51#endif
52
53#define MBEDTLS_ERR_PK_ALLOC_FAILED -0x3F80 /**< Memory allocation failed. */
54#define MBEDTLS_ERR_PK_TYPE_MISMATCH -0x3F00 /**< Type mismatch, eg attempt to encrypt with an ECDSA key */
55#define MBEDTLS_ERR_PK_BAD_INPUT_DATA -0x3E80 /**< Bad input parameters to function. */
56#define MBEDTLS_ERR_PK_FILE_IO_ERROR -0x3E00 /**< Read/write of file failed. */
57#define MBEDTLS_ERR_PK_KEY_INVALID_VERSION -0x3D80 /**< Unsupported key version */
58#define MBEDTLS_ERR_PK_KEY_INVALID_FORMAT -0x3D00 /**< Invalid key tag or value. */
59#define MBEDTLS_ERR_PK_UNKNOWN_PK_ALG -0x3C80 /**< Key algorithm is unsupported (only RSA and EC are supported). */
60#define MBEDTLS_ERR_PK_PASSWORD_REQUIRED -0x3C00 /**< Private key password can't be empty. */
61#define MBEDTLS_ERR_PK_PASSWORD_MISMATCH -0x3B80 /**< Given private key password does not allow for correct decryption. */
62#define MBEDTLS_ERR_PK_INVALID_PUBKEY -0x3B00 /**< The pubkey tag or value is invalid (only RSA and EC are supported). */
63#define MBEDTLS_ERR_PK_INVALID_ALG -0x3A80 /**< The algorithm tag or value is invalid. */
64#define MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE -0x3A00 /**< Elliptic curve is unsupported (only NIST curves are supported). */
65#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE -0x3980 /**< Unavailable feature, e.g. RSA disabled for RSA key. */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020066#define MBEDTLS_ERR_PK_SIG_LEN_MISMATCH -0x3900 /**< The buffer contains a valid signature followed by more data. */
67
68/* MBEDTLS_ERR_PK_HW_ACCEL_FAILED is deprecated and should not be used. */
69#define MBEDTLS_ERR_PK_HW_ACCEL_FAILED -0x3880 /**< PK hardware accelerator failed. */
Fabio Utzigba05f2a2017-12-05 11:00:41 -020070
71#ifdef __cplusplus
72extern "C" {
73#endif
74
75/**
76 * \brief Public key types
77 */
78typedef enum {
79 MBEDTLS_PK_NONE=0,
80 MBEDTLS_PK_RSA,
81 MBEDTLS_PK_ECKEY,
82 MBEDTLS_PK_ECKEY_DH,
83 MBEDTLS_PK_ECDSA,
84 MBEDTLS_PK_RSA_ALT,
85 MBEDTLS_PK_RSASSA_PSS,
86} mbedtls_pk_type_t;
87
88/**
89 * \brief Options for RSASSA-PSS signature verification.
90 * See \c mbedtls_rsa_rsassa_pss_verify_ext()
91 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -020092typedef struct mbedtls_pk_rsassa_pss_options
Fabio Utzigba05f2a2017-12-05 11:00:41 -020093{
94 mbedtls_md_type_t mgf1_hash_id;
95 int expected_salt_len;
96
97} mbedtls_pk_rsassa_pss_options;
98
99/**
100 * \brief Types for interfacing with the debug module
101 */
102typedef enum
103{
104 MBEDTLS_PK_DEBUG_NONE = 0,
105 MBEDTLS_PK_DEBUG_MPI,
106 MBEDTLS_PK_DEBUG_ECP,
107} mbedtls_pk_debug_type;
108
109/**
110 * \brief Item to send to the debug module
111 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200112typedef struct mbedtls_pk_debug_item
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200113{
114 mbedtls_pk_debug_type type;
115 const char *name;
116 void *value;
117} mbedtls_pk_debug_item;
118
119/** Maximum number of item send for debugging, plus 1 */
120#define MBEDTLS_PK_DEBUG_MAX_ITEMS 3
121
122/**
123 * \brief Public key information and operations
124 */
125typedef struct mbedtls_pk_info_t mbedtls_pk_info_t;
126
127/**
128 * \brief Public key container
129 */
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200130typedef struct mbedtls_pk_context
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200131{
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200132 const mbedtls_pk_info_t * pk_info; /**< Public key information */
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200133 void * pk_ctx; /**< Underlying public key context */
134} mbedtls_pk_context;
135
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200136#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
137/**
138 * \brief Context for resuming operations
139 */
140typedef struct
141{
142 const mbedtls_pk_info_t * pk_info; /**< Public key information */
143 void * rs_ctx; /**< Underlying restart context */
144} mbedtls_pk_restart_ctx;
145#else /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
146/* Now we can declare functions that take a pointer to that */
147typedef void mbedtls_pk_restart_ctx;
148#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
149
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200150#if defined(MBEDTLS_RSA_C)
151/**
152 * Quick access to an RSA context inside a PK context.
153 *
154 * \warning You must make sure the PK context actually holds an RSA context
155 * before using this function!
156 */
157static inline mbedtls_rsa_context *mbedtls_pk_rsa( const mbedtls_pk_context pk )
158{
159 return( (mbedtls_rsa_context *) (pk).pk_ctx );
160}
161#endif /* MBEDTLS_RSA_C */
162
163#if defined(MBEDTLS_ECP_C)
164/**
165 * Quick access to an EC context inside a PK context.
166 *
167 * \warning You must make sure the PK context actually holds an EC context
168 * before using this function!
169 */
170static inline mbedtls_ecp_keypair *mbedtls_pk_ec( const mbedtls_pk_context pk )
171{
172 return( (mbedtls_ecp_keypair *) (pk).pk_ctx );
173}
174#endif /* MBEDTLS_ECP_C */
175
176#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
177/**
178 * \brief Types for RSA-alt abstraction
179 */
180typedef int (*mbedtls_pk_rsa_alt_decrypt_func)( void *ctx, int mode, size_t *olen,
181 const unsigned char *input, unsigned char *output,
182 size_t output_max_len );
183typedef int (*mbedtls_pk_rsa_alt_sign_func)( void *ctx,
184 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
185 int mode, mbedtls_md_type_t md_alg, unsigned int hashlen,
186 const unsigned char *hash, unsigned char *sig );
187typedef size_t (*mbedtls_pk_rsa_alt_key_len_func)( void *ctx );
188#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
189
190/**
191 * \brief Return information associated with the given PK type
192 *
193 * \param pk_type PK type to search for.
194 *
195 * \return The PK info associated with the type or NULL if not found.
196 */
197const mbedtls_pk_info_t *mbedtls_pk_info_from_type( mbedtls_pk_type_t pk_type );
198
199/**
200 * \brief Initialize a mbedtls_pk_context (as NONE)
201 */
202void mbedtls_pk_init( mbedtls_pk_context *ctx );
203
204/**
205 * \brief Free a mbedtls_pk_context
206 */
207void mbedtls_pk_free( mbedtls_pk_context *ctx );
208
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200209#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_ECP_RESTARTABLE)
210/**
211 * \brief Initialize a restart context
212 */
213void mbedtls_pk_restart_init( mbedtls_pk_restart_ctx *ctx );
214
215/**
216 * \brief Free the components of a restart context
217 */
218void mbedtls_pk_restart_free( mbedtls_pk_restart_ctx *ctx );
219#endif /* MBEDTLS_ECDSA_C && MBEDTLS_ECP_RESTARTABLE */
220
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200221/**
222 * \brief Initialize a PK context with the information given
223 * and allocates the type-specific PK subcontext.
224 *
225 * \param ctx Context to initialize. Must be empty (type NONE).
226 * \param info Information to use
227 *
228 * \return 0 on success,
229 * MBEDTLS_ERR_PK_BAD_INPUT_DATA on invalid input,
230 * MBEDTLS_ERR_PK_ALLOC_FAILED on allocation failure.
231 *
232 * \note For contexts holding an RSA-alt key, use
233 * \c mbedtls_pk_setup_rsa_alt() instead.
234 */
235int mbedtls_pk_setup( mbedtls_pk_context *ctx, const mbedtls_pk_info_t *info );
236
237#if defined(MBEDTLS_PK_RSA_ALT_SUPPORT)
238/**
239 * \brief Initialize an RSA-alt context
240 *
241 * \param ctx Context to initialize. Must be empty (type NONE).
242 * \param key RSA key pointer
243 * \param decrypt_func Decryption function
244 * \param sign_func Signing function
245 * \param key_len_func Function returning key length in bytes
246 *
247 * \return 0 on success, or MBEDTLS_ERR_PK_BAD_INPUT_DATA if the
248 * context wasn't already initialized as RSA_ALT.
249 *
250 * \note This function replaces \c mbedtls_pk_setup() for RSA-alt.
251 */
252int mbedtls_pk_setup_rsa_alt( mbedtls_pk_context *ctx, void * key,
253 mbedtls_pk_rsa_alt_decrypt_func decrypt_func,
254 mbedtls_pk_rsa_alt_sign_func sign_func,
255 mbedtls_pk_rsa_alt_key_len_func key_len_func );
256#endif /* MBEDTLS_PK_RSA_ALT_SUPPORT */
257
258/**
259 * \brief Get the size in bits of the underlying key
260 *
261 * \param ctx Context to use
262 *
263 * \return Key size in bits, or 0 on error
264 */
265size_t mbedtls_pk_get_bitlen( const mbedtls_pk_context *ctx );
266
267/**
268 * \brief Get the length in bytes of the underlying key
269 * \param ctx Context to use
270 *
271 * \return Key length in bytes, or 0 on error
272 */
273static inline size_t mbedtls_pk_get_len( const mbedtls_pk_context *ctx )
274{
275 return( ( mbedtls_pk_get_bitlen( ctx ) + 7 ) / 8 );
276}
277
278/**
279 * \brief Tell if a context can do the operation given by type
280 *
281 * \param ctx Context to test
282 * \param type Target type
283 *
284 * \return 0 if context can't do the operations,
285 * 1 otherwise.
286 */
287int mbedtls_pk_can_do( const mbedtls_pk_context *ctx, mbedtls_pk_type_t type );
288
289/**
290 * \brief Verify signature (including padding if relevant).
291 *
292 * \param ctx PK context to use
293 * \param md_alg Hash algorithm used (see notes)
294 * \param hash Hash of the message to sign
295 * \param hash_len Hash length or 0 (see notes)
296 * \param sig Signature to verify
297 * \param sig_len Signature length
298 *
299 * \return 0 on success (signature is valid),
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200300 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
301 * signature in sig but its length is less than \p siglen,
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200302 * or a specific error code.
303 *
304 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
305 * Use \c mbedtls_pk_verify_ext( MBEDTLS_PK_RSASSA_PSS, ... )
306 * to verify RSASSA_PSS signatures.
307 *
308 * \note If hash_len is 0, then the length associated with md_alg
309 * is used instead, or an error returned if it is invalid.
310 *
311 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
312 */
313int mbedtls_pk_verify( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
314 const unsigned char *hash, size_t hash_len,
315 const unsigned char *sig, size_t sig_len );
316
317/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200318 * \brief Restartable version of \c mbedtls_pk_verify()
319 *
320 * \note Performs the same job as \c mbedtls_pk_verify(), but can
321 * return early and restart according to the limit set with
322 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
323 * operations. For RSA, same as \c mbedtls_pk_verify().
324 *
325 * \param ctx PK context to use
326 * \param md_alg Hash algorithm used (see notes)
327 * \param hash Hash of the message to sign
328 * \param hash_len Hash length or 0 (see notes)
329 * \param sig Signature to verify
330 * \param sig_len Signature length
331 * \param rs_ctx Restart context (NULL to disable restart)
332 *
333 * \return See \c mbedtls_pk_verify(), or
334 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
335 * operations was reached: see \c mbedtls_ecp_set_max_ops().
336 */
337int mbedtls_pk_verify_restartable( mbedtls_pk_context *ctx,
338 mbedtls_md_type_t md_alg,
339 const unsigned char *hash, size_t hash_len,
340 const unsigned char *sig, size_t sig_len,
341 mbedtls_pk_restart_ctx *rs_ctx );
342
343/**
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200344 * \brief Verify signature, with options.
345 * (Includes verification of the padding depending on type.)
346 *
347 * \param type Signature type (inc. possible padding type) to verify
348 * \param options Pointer to type-specific options, or NULL
349 * \param ctx PK context to use
350 * \param md_alg Hash algorithm used (see notes)
351 * \param hash Hash of the message to sign
352 * \param hash_len Hash length or 0 (see notes)
353 * \param sig Signature to verify
354 * \param sig_len Signature length
355 *
356 * \return 0 on success (signature is valid),
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200357 * #MBEDTLS_ERR_PK_TYPE_MISMATCH if the PK context can't be
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200358 * used for this type of signatures,
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200359 * #MBEDTLS_ERR_PK_SIG_LEN_MISMATCH if there is a valid
360 * signature in sig but its length is less than \p siglen,
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200361 * or a specific error code.
362 *
363 * \note If hash_len is 0, then the length associated with md_alg
364 * is used instead, or an error returned if it is invalid.
365 *
366 * \note md_alg may be MBEDTLS_MD_NONE, only if hash_len != 0
367 *
368 * \note If type is MBEDTLS_PK_RSASSA_PSS, then options must point
369 * to a mbedtls_pk_rsassa_pss_options structure,
370 * otherwise it must be NULL.
371 */
372int mbedtls_pk_verify_ext( mbedtls_pk_type_t type, const void *options,
373 mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
374 const unsigned char *hash, size_t hash_len,
375 const unsigned char *sig, size_t sig_len );
376
377/**
378 * \brief Make signature, including padding if relevant.
379 *
380 * \param ctx PK context to use - must hold a private key
381 * \param md_alg Hash algorithm used (see notes)
382 * \param hash Hash of the message to sign
383 * \param hash_len Hash length or 0 (see notes)
384 * \param sig Place to write the signature
385 * \param sig_len Number of bytes written
386 * \param f_rng RNG function
387 * \param p_rng RNG parameter
388 *
389 * \return 0 on success, or a specific error code.
390 *
391 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
392 * There is no interface in the PK module to make RSASSA-PSS
393 * signatures yet.
394 *
395 * \note If hash_len is 0, then the length associated with md_alg
396 * is used instead, or an error returned if it is invalid.
397 *
398 * \note For RSA, md_alg may be MBEDTLS_MD_NONE if hash_len != 0.
399 * For ECDSA, md_alg may never be MBEDTLS_MD_NONE.
400 */
401int mbedtls_pk_sign( mbedtls_pk_context *ctx, mbedtls_md_type_t md_alg,
402 const unsigned char *hash, size_t hash_len,
403 unsigned char *sig, size_t *sig_len,
404 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
405
406/**
Fabio Utzig3ac36ea2018-12-27 12:35:39 -0200407 * \brief Restartable version of \c mbedtls_pk_sign()
408 *
409 * \note Performs the same job as \c mbedtls_pk_sign(), but can
410 * return early and restart according to the limit set with
411 * \c mbedtls_ecp_set_max_ops() to reduce blocking for ECC
412 * operations. For RSA, same as \c mbedtls_pk_sign().
413 *
414 * \param ctx PK context to use - must hold a private key
415 * \param md_alg Hash algorithm used (see notes)
416 * \param hash Hash of the message to sign
417 * \param hash_len Hash length or 0 (see notes)
418 * \param sig Place to write the signature
419 * \param sig_len Number of bytes written
420 * \param f_rng RNG function
421 * \param p_rng RNG parameter
422 * \param rs_ctx Restart context (NULL to disable restart)
423 *
424 * \return See \c mbedtls_pk_sign(), or
425 * \return #MBEDTLS_ERR_ECP_IN_PROGRESS if maximum number of
426 * operations was reached: see \c mbedtls_ecp_set_max_ops().
427 */
428int mbedtls_pk_sign_restartable( mbedtls_pk_context *ctx,
429 mbedtls_md_type_t md_alg,
430 const unsigned char *hash, size_t hash_len,
431 unsigned char *sig, size_t *sig_len,
432 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
433 mbedtls_pk_restart_ctx *rs_ctx );
434
435/**
Fabio Utzigba05f2a2017-12-05 11:00:41 -0200436 * \brief Decrypt message (including padding if relevant).
437 *
438 * \param ctx PK context to use - must hold a private key
439 * \param input Input to decrypt
440 * \param ilen Input size
441 * \param output Decrypted output
442 * \param olen Decrypted message length
443 * \param osize Size of the output buffer
444 * \param f_rng RNG function
445 * \param p_rng RNG parameter
446 *
447 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
448 *
449 * \return 0 on success, or a specific error code.
450 */
451int mbedtls_pk_decrypt( mbedtls_pk_context *ctx,
452 const unsigned char *input, size_t ilen,
453 unsigned char *output, size_t *olen, size_t osize,
454 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
455
456/**
457 * \brief Encrypt message (including padding if relevant).
458 *
459 * \param ctx PK context to use
460 * \param input Message to encrypt
461 * \param ilen Message size
462 * \param output Encrypted output
463 * \param olen Encrypted output length
464 * \param osize Size of the output buffer
465 * \param f_rng RNG function
466 * \param p_rng RNG parameter
467 *
468 * \note For RSA keys, the default padding type is PKCS#1 v1.5.
469 *
470 * \return 0 on success, or a specific error code.
471 */
472int mbedtls_pk_encrypt( mbedtls_pk_context *ctx,
473 const unsigned char *input, size_t ilen,
474 unsigned char *output, size_t *olen, size_t osize,
475 int (*f_rng)(void *, unsigned char *, size_t), void *p_rng );
476
477/**
478 * \brief Check if a public-private pair of keys matches.
479 *
480 * \param pub Context holding a public key.
481 * \param prv Context holding a private (and public) key.
482 *
483 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
484 */
485int mbedtls_pk_check_pair( const mbedtls_pk_context *pub, const mbedtls_pk_context *prv );
486
487/**
488 * \brief Export debug information
489 *
490 * \param ctx Context to use
491 * \param items Place to write debug items
492 *
493 * \return 0 on success or MBEDTLS_ERR_PK_BAD_INPUT_DATA
494 */
495int mbedtls_pk_debug( const mbedtls_pk_context *ctx, mbedtls_pk_debug_item *items );
496
497/**
498 * \brief Access the type name
499 *
500 * \param ctx Context to use
501 *
502 * \return Type name on success, or "invalid PK"
503 */
504const char * mbedtls_pk_get_name( const mbedtls_pk_context *ctx );
505
506/**
507 * \brief Get the key type
508 *
509 * \param ctx Context to use
510 *
511 * \return Type on success, or MBEDTLS_PK_NONE
512 */
513mbedtls_pk_type_t mbedtls_pk_get_type( const mbedtls_pk_context *ctx );
514
515#if defined(MBEDTLS_PK_PARSE_C)
516/** \ingroup pk_module */
517/**
518 * \brief Parse a private key in PEM or DER format
519 *
520 * \param ctx key to be initialized
521 * \param key input buffer
522 * \param keylen size of the buffer
523 * (including the terminating null byte for PEM data)
524 * \param pwd password for decryption (optional)
525 * \param pwdlen size of the password
526 *
527 * \note On entry, ctx must be empty, either freshly initialised
528 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
529 * specific key type, check the result with mbedtls_pk_can_do().
530 *
531 * \note The key is also checked for correctness.
532 *
533 * \return 0 if successful, or a specific PK or PEM error code
534 */
535int mbedtls_pk_parse_key( mbedtls_pk_context *ctx,
536 const unsigned char *key, size_t keylen,
537 const unsigned char *pwd, size_t pwdlen );
538
539/** \ingroup pk_module */
540/**
541 * \brief Parse a public key in PEM or DER format
542 *
543 * \param ctx key to be initialized
544 * \param key input buffer
545 * \param keylen size of the buffer
546 * (including the terminating null byte for PEM data)
547 *
548 * \note On entry, ctx must be empty, either freshly initialised
549 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
550 * specific key type, check the result with mbedtls_pk_can_do().
551 *
552 * \note The key is also checked for correctness.
553 *
554 * \return 0 if successful, or a specific PK or PEM error code
555 */
556int mbedtls_pk_parse_public_key( mbedtls_pk_context *ctx,
557 const unsigned char *key, size_t keylen );
558
559#if defined(MBEDTLS_FS_IO)
560/** \ingroup pk_module */
561/**
562 * \brief Load and parse a private key
563 *
564 * \param ctx key to be initialized
565 * \param path filename to read the private key from
566 * \param password password to decrypt the file (can be NULL)
567 *
568 * \note On entry, ctx must be empty, either freshly initialised
569 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If you need a
570 * specific key type, check the result with mbedtls_pk_can_do().
571 *
572 * \note The key is also checked for correctness.
573 *
574 * \return 0 if successful, or a specific PK or PEM error code
575 */
576int mbedtls_pk_parse_keyfile( mbedtls_pk_context *ctx,
577 const char *path, const char *password );
578
579/** \ingroup pk_module */
580/**
581 * \brief Load and parse a public key
582 *
583 * \param ctx key to be initialized
584 * \param path filename to read the public key from
585 *
586 * \note On entry, ctx must be empty, either freshly initialised
587 * with mbedtls_pk_init() or reset with mbedtls_pk_free(). If
588 * you need a specific key type, check the result with
589 * mbedtls_pk_can_do().
590 *
591 * \note The key is also checked for correctness.
592 *
593 * \return 0 if successful, or a specific PK or PEM error code
594 */
595int mbedtls_pk_parse_public_keyfile( mbedtls_pk_context *ctx, const char *path );
596#endif /* MBEDTLS_FS_IO */
597#endif /* MBEDTLS_PK_PARSE_C */
598
599#if defined(MBEDTLS_PK_WRITE_C)
600/**
601 * \brief Write a private key to a PKCS#1 or SEC1 DER structure
602 * Note: data is written at the end of the buffer! Use the
603 * return value to determine where you should start
604 * using the buffer
605 *
606 * \param ctx private to write away
607 * \param buf buffer to write to
608 * \param size size of the buffer
609 *
610 * \return length of data written if successful, or a specific
611 * error code
612 */
613int mbedtls_pk_write_key_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
614
615/**
616 * \brief Write a public key to a SubjectPublicKeyInfo DER structure
617 * Note: data is written at the end of the buffer! Use the
618 * return value to determine where you should start
619 * using the buffer
620 *
621 * \param ctx public key to write away
622 * \param buf buffer to write to
623 * \param size size of the buffer
624 *
625 * \return length of data written if successful, or a specific
626 * error code
627 */
628int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
629
630#if defined(MBEDTLS_PEM_WRITE_C)
631/**
632 * \brief Write a public key to a PEM string
633 *
634 * \param ctx public key to write away
635 * \param buf buffer to write to
636 * \param size size of the buffer
637 *
638 * \return 0 if successful, or a specific error code
639 */
640int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
641
642/**
643 * \brief Write a private key to a PKCS#1 or SEC1 PEM string
644 *
645 * \param ctx private to write away
646 * \param buf buffer to write to
647 * \param size size of the buffer
648 *
649 * \return 0 if successful, or a specific error code
650 */
651int mbedtls_pk_write_key_pem( mbedtls_pk_context *ctx, unsigned char *buf, size_t size );
652#endif /* MBEDTLS_PEM_WRITE_C */
653#endif /* MBEDTLS_PK_WRITE_C */
654
655/*
656 * WARNING: Low-level functions. You probably do not want to use these unless
657 * you are certain you do ;)
658 */
659
660#if defined(MBEDTLS_PK_PARSE_C)
661/**
662 * \brief Parse a SubjectPublicKeyInfo DER structure
663 *
664 * \param p the position in the ASN.1 data
665 * \param end end of the buffer
666 * \param pk the key to fill
667 *
668 * \return 0 if successful, or a specific PK error code
669 */
670int mbedtls_pk_parse_subpubkey( unsigned char **p, const unsigned char *end,
671 mbedtls_pk_context *pk );
672#endif /* MBEDTLS_PK_PARSE_C */
673
674#if defined(MBEDTLS_PK_WRITE_C)
675/**
676 * \brief Write a subjectPublicKey to ASN.1 data
677 * Note: function works backwards in data buffer
678 *
679 * \param p reference to current position pointer
680 * \param start start of the buffer (for bounds-checking)
681 * \param key public key to write away
682 *
683 * \return the length written or a negative error code
684 */
685int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
686 const mbedtls_pk_context *key );
687#endif /* MBEDTLS_PK_WRITE_C */
688
689/*
690 * Internal module functions. You probably do not want to use these unless you
691 * know you do.
692 */
693#if defined(MBEDTLS_FS_IO)
694int mbedtls_pk_load_file( const char *path, unsigned char **buf, size_t *n );
695#endif
696
697#ifdef __cplusplus
698}
699#endif
700
701#endif /* MBEDTLS_PK_H */