Jaeden Amero | e54e693 | 2018-08-06 16:19:58 +0100 | [diff] [blame] | 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 Crypto (https://tls.mbed.org) |
| 20 | */ |
| 21 | |
| 22 | #if !defined(MBEDCRYPTO_CONFIG_FILE) |
| 23 | #include "mbedcrypto/config.h" |
| 24 | #else |
| 25 | #include MBEDCRYPTO_CONFIG_FILE |
| 26 | #endif |
| 27 | |
| 28 | #if defined(MBEDCRYPTO_PK_WRITE_C) |
| 29 | |
| 30 | #include "mbedcrypto/pk.h" |
| 31 | #include "mbedcrypto/asn1write.h" |
| 32 | #include "mbedcrypto/oid.h" |
| 33 | |
| 34 | #include <string.h> |
| 35 | |
| 36 | #if defined(MBEDCRYPTO_RSA_C) |
| 37 | #include "mbedcrypto/rsa.h" |
| 38 | #endif |
| 39 | #if defined(MBEDCRYPTO_ECP_C) |
| 40 | #include "mbedcrypto/ecp.h" |
| 41 | #endif |
| 42 | #if defined(MBEDCRYPTO_ECDSA_C) |
| 43 | #include "mbedcrypto/ecdsa.h" |
| 44 | #endif |
| 45 | #if defined(MBEDCRYPTO_PEM_WRITE_C) |
| 46 | #include "mbedcrypto/pem.h" |
| 47 | #endif |
| 48 | |
| 49 | #if defined(MBEDCRYPTO_PLATFORM_C) |
| 50 | #include "mbedcrypto/platform.h" |
| 51 | #else |
| 52 | #include <stdlib.h> |
| 53 | #define mbedcrypto_calloc calloc |
| 54 | #define mbedcrypto_free free |
| 55 | #endif |
| 56 | |
| 57 | #if defined(MBEDCRYPTO_RSA_C) |
| 58 | /* |
| 59 | * RSAPublicKey ::= SEQUENCE { |
| 60 | * modulus INTEGER, -- n |
| 61 | * publicExponent INTEGER -- e |
| 62 | * } |
| 63 | */ |
| 64 | static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start, |
| 65 | mbedcrypto_rsa_context *rsa ) |
| 66 | { |
| 67 | int ret; |
| 68 | size_t len = 0; |
| 69 | mbedcrypto_mpi T; |
| 70 | |
| 71 | mbedcrypto_mpi_init( &T ); |
| 72 | |
| 73 | /* Export E */ |
| 74 | if ( ( ret = mbedcrypto_rsa_export( rsa, NULL, NULL, NULL, NULL, &T ) ) != 0 || |
| 75 | ( ret = mbedcrypto_asn1_write_mpi( p, start, &T ) ) < 0 ) |
| 76 | goto end_of_export; |
| 77 | len += ret; |
| 78 | |
| 79 | /* Export N */ |
| 80 | if ( ( ret = mbedcrypto_rsa_export( rsa, &T, NULL, NULL, NULL, NULL ) ) != 0 || |
| 81 | ( ret = mbedcrypto_asn1_write_mpi( p, start, &T ) ) < 0 ) |
| 82 | goto end_of_export; |
| 83 | len += ret; |
| 84 | |
| 85 | end_of_export: |
| 86 | |
| 87 | mbedcrypto_mpi_free( &T ); |
| 88 | if( ret < 0 ) |
| 89 | return( ret ); |
| 90 | |
| 91 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_len( p, start, len ) ); |
| 92 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_tag( p, start, MBEDCRYPTO_ASN1_CONSTRUCTED | |
| 93 | MBEDCRYPTO_ASN1_SEQUENCE ) ); |
| 94 | |
| 95 | return( (int) len ); |
| 96 | } |
| 97 | #endif /* MBEDCRYPTO_RSA_C */ |
| 98 | |
| 99 | #if defined(MBEDCRYPTO_ECP_C) |
| 100 | /* |
| 101 | * EC public key is an EC point |
| 102 | */ |
| 103 | static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start, |
| 104 | mbedcrypto_ecp_keypair *ec ) |
| 105 | { |
| 106 | int ret; |
| 107 | size_t len = 0; |
| 108 | unsigned char buf[MBEDCRYPTO_ECP_MAX_PT_LEN]; |
| 109 | |
| 110 | if( ( ret = mbedcrypto_ecp_point_write_binary( &ec->grp, &ec->Q, |
| 111 | MBEDCRYPTO_ECP_PF_UNCOMPRESSED, |
| 112 | &len, buf, sizeof( buf ) ) ) != 0 ) |
| 113 | { |
| 114 | return( ret ); |
| 115 | } |
| 116 | |
| 117 | if( *p < start || (size_t)( *p - start ) < len ) |
| 118 | return( MBEDCRYPTO_ERR_ASN1_BUF_TOO_SMALL ); |
| 119 | |
| 120 | *p -= len; |
| 121 | memcpy( *p, buf, len ); |
| 122 | |
| 123 | return( (int) len ); |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * ECParameters ::= CHOICE { |
| 128 | * namedCurve OBJECT IDENTIFIER |
| 129 | * } |
| 130 | */ |
| 131 | static int pk_write_ec_param( unsigned char **p, unsigned char *start, |
| 132 | mbedcrypto_ecp_keypair *ec ) |
| 133 | { |
| 134 | int ret; |
| 135 | size_t len = 0; |
| 136 | const char *oid; |
| 137 | size_t oid_len; |
| 138 | |
| 139 | if( ( ret = mbedcrypto_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 ) |
| 140 | return( ret ); |
| 141 | |
| 142 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_oid( p, start, oid, oid_len ) ); |
| 143 | |
| 144 | return( (int) len ); |
| 145 | } |
| 146 | #endif /* MBEDCRYPTO_ECP_C */ |
| 147 | |
| 148 | int mbedcrypto_pk_write_pubkey( unsigned char **p, unsigned char *start, |
| 149 | const mbedcrypto_pk_context *key ) |
| 150 | { |
| 151 | int ret; |
| 152 | size_t len = 0; |
| 153 | |
| 154 | #if defined(MBEDCRYPTO_RSA_C) |
| 155 | if( mbedcrypto_pk_get_type( key ) == MBEDCRYPTO_PK_RSA ) |
| 156 | MBEDCRYPTO_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedcrypto_pk_rsa( *key ) ) ); |
| 157 | else |
| 158 | #endif |
| 159 | #if defined(MBEDCRYPTO_ECP_C) |
| 160 | if( mbedcrypto_pk_get_type( key ) == MBEDCRYPTO_PK_ECKEY ) |
| 161 | MBEDCRYPTO_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedcrypto_pk_ec( *key ) ) ); |
| 162 | else |
| 163 | #endif |
| 164 | return( MBEDCRYPTO_ERR_PK_FEATURE_UNAVAILABLE ); |
| 165 | |
| 166 | return( (int) len ); |
| 167 | } |
| 168 | |
| 169 | int mbedcrypto_pk_write_pubkey_der( mbedcrypto_pk_context *key, unsigned char *buf, size_t size ) |
| 170 | { |
| 171 | int ret; |
| 172 | unsigned char *c; |
| 173 | size_t len = 0, par_len = 0, oid_len; |
| 174 | const char *oid; |
| 175 | |
| 176 | c = buf + size; |
| 177 | |
| 178 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_pk_write_pubkey( &c, buf, key ) ); |
| 179 | |
| 180 | if( c - buf < 1 ) |
| 181 | return( MBEDCRYPTO_ERR_ASN1_BUF_TOO_SMALL ); |
| 182 | |
| 183 | /* |
| 184 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 185 | * algorithm AlgorithmIdentifier, |
| 186 | * subjectPublicKey BIT STRING } |
| 187 | */ |
| 188 | *--c = 0; |
| 189 | len += 1; |
| 190 | |
| 191 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_len( &c, buf, len ) ); |
| 192 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_tag( &c, buf, MBEDCRYPTO_ASN1_BIT_STRING ) ); |
| 193 | |
| 194 | if( ( ret = mbedcrypto_oid_get_oid_by_pk_alg( mbedcrypto_pk_get_type( key ), |
| 195 | &oid, &oid_len ) ) != 0 ) |
| 196 | { |
| 197 | return( ret ); |
| 198 | } |
| 199 | |
| 200 | #if defined(MBEDCRYPTO_ECP_C) |
| 201 | if( mbedcrypto_pk_get_type( key ) == MBEDCRYPTO_PK_ECKEY ) |
| 202 | { |
| 203 | MBEDCRYPTO_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedcrypto_pk_ec( *key ) ) ); |
| 204 | } |
| 205 | #endif |
| 206 | |
| 207 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_algorithm_identifier( &c, buf, oid, oid_len, |
| 208 | par_len ) ); |
| 209 | |
| 210 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_len( &c, buf, len ) ); |
| 211 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_tag( &c, buf, MBEDCRYPTO_ASN1_CONSTRUCTED | |
| 212 | MBEDCRYPTO_ASN1_SEQUENCE ) ); |
| 213 | |
| 214 | return( (int) len ); |
| 215 | } |
| 216 | |
| 217 | int mbedcrypto_pk_write_key_der( mbedcrypto_pk_context *key, unsigned char *buf, size_t size ) |
| 218 | { |
| 219 | int ret; |
| 220 | unsigned char *c = buf + size; |
| 221 | size_t len = 0; |
| 222 | |
| 223 | #if defined(MBEDCRYPTO_RSA_C) |
| 224 | if( mbedcrypto_pk_get_type( key ) == MBEDCRYPTO_PK_RSA ) |
| 225 | { |
| 226 | mbedcrypto_mpi T; /* Temporary holding the exported parameters */ |
| 227 | mbedcrypto_rsa_context *rsa = mbedcrypto_pk_rsa( *key ); |
| 228 | |
| 229 | /* |
| 230 | * Export the parameters one after another to avoid simultaneous copies. |
| 231 | */ |
| 232 | |
| 233 | mbedcrypto_mpi_init( &T ); |
| 234 | |
| 235 | /* Export QP */ |
| 236 | if( ( ret = mbedcrypto_rsa_export_crt( rsa, NULL, NULL, &T ) ) != 0 || |
| 237 | ( ret = mbedcrypto_asn1_write_mpi( &c, buf, &T ) ) < 0 ) |
| 238 | goto end_of_export; |
| 239 | len += ret; |
| 240 | |
| 241 | /* Export DQ */ |
| 242 | if( ( ret = mbedcrypto_rsa_export_crt( rsa, NULL, &T, NULL ) ) != 0 || |
| 243 | ( ret = mbedcrypto_asn1_write_mpi( &c, buf, &T ) ) < 0 ) |
| 244 | goto end_of_export; |
| 245 | len += ret; |
| 246 | |
| 247 | /* Export DP */ |
| 248 | if( ( ret = mbedcrypto_rsa_export_crt( rsa, &T, NULL, NULL ) ) != 0 || |
| 249 | ( ret = mbedcrypto_asn1_write_mpi( &c, buf, &T ) ) < 0 ) |
| 250 | goto end_of_export; |
| 251 | len += ret; |
| 252 | |
| 253 | /* Export Q */ |
| 254 | if ( ( ret = mbedcrypto_rsa_export( rsa, NULL, NULL, |
| 255 | &T, NULL, NULL ) ) != 0 || |
| 256 | ( ret = mbedcrypto_asn1_write_mpi( &c, buf, &T ) ) < 0 ) |
| 257 | goto end_of_export; |
| 258 | len += ret; |
| 259 | |
| 260 | /* Export P */ |
| 261 | if ( ( ret = mbedcrypto_rsa_export( rsa, NULL, &T, |
| 262 | NULL, NULL, NULL ) ) != 0 || |
| 263 | ( ret = mbedcrypto_asn1_write_mpi( &c, buf, &T ) ) < 0 ) |
| 264 | goto end_of_export; |
| 265 | len += ret; |
| 266 | |
| 267 | /* Export D */ |
| 268 | if ( ( ret = mbedcrypto_rsa_export( rsa, NULL, NULL, |
| 269 | NULL, &T, NULL ) ) != 0 || |
| 270 | ( ret = mbedcrypto_asn1_write_mpi( &c, buf, &T ) ) < 0 ) |
| 271 | goto end_of_export; |
| 272 | len += ret; |
| 273 | |
| 274 | /* Export E */ |
| 275 | if ( ( ret = mbedcrypto_rsa_export( rsa, NULL, NULL, |
| 276 | NULL, NULL, &T ) ) != 0 || |
| 277 | ( ret = mbedcrypto_asn1_write_mpi( &c, buf, &T ) ) < 0 ) |
| 278 | goto end_of_export; |
| 279 | len += ret; |
| 280 | |
| 281 | /* Export N */ |
| 282 | if ( ( ret = mbedcrypto_rsa_export( rsa, &T, NULL, |
| 283 | NULL, NULL, NULL ) ) != 0 || |
| 284 | ( ret = mbedcrypto_asn1_write_mpi( &c, buf, &T ) ) < 0 ) |
| 285 | goto end_of_export; |
| 286 | len += ret; |
| 287 | |
| 288 | end_of_export: |
| 289 | |
| 290 | mbedcrypto_mpi_free( &T ); |
| 291 | if( ret < 0 ) |
| 292 | return( ret ); |
| 293 | |
| 294 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_int( &c, buf, 0 ) ); |
| 295 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_len( &c, buf, len ) ); |
| 296 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_tag( &c, |
| 297 | buf, MBEDCRYPTO_ASN1_CONSTRUCTED | |
| 298 | MBEDCRYPTO_ASN1_SEQUENCE ) ); |
| 299 | } |
| 300 | else |
| 301 | #endif /* MBEDCRYPTO_RSA_C */ |
| 302 | #if defined(MBEDCRYPTO_ECP_C) |
| 303 | if( mbedcrypto_pk_get_type( key ) == MBEDCRYPTO_PK_ECKEY ) |
| 304 | { |
| 305 | mbedcrypto_ecp_keypair *ec = mbedcrypto_pk_ec( *key ); |
| 306 | size_t pub_len = 0, par_len = 0; |
| 307 | |
| 308 | /* |
| 309 | * RFC 5915, or SEC1 Appendix C.4 |
| 310 | * |
| 311 | * ECPrivateKey ::= SEQUENCE { |
| 312 | * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), |
| 313 | * privateKey OCTET STRING, |
| 314 | * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL, |
| 315 | * publicKey [1] BIT STRING OPTIONAL |
| 316 | * } |
| 317 | */ |
| 318 | |
| 319 | /* publicKey */ |
| 320 | MBEDCRYPTO_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) ); |
| 321 | |
| 322 | if( c - buf < 1 ) |
| 323 | return( MBEDCRYPTO_ERR_ASN1_BUF_TOO_SMALL ); |
| 324 | *--c = 0; |
| 325 | pub_len += 1; |
| 326 | |
| 327 | MBEDCRYPTO_ASN1_CHK_ADD( pub_len, mbedcrypto_asn1_write_len( &c, buf, pub_len ) ); |
| 328 | MBEDCRYPTO_ASN1_CHK_ADD( pub_len, mbedcrypto_asn1_write_tag( &c, buf, MBEDCRYPTO_ASN1_BIT_STRING ) ); |
| 329 | |
| 330 | MBEDCRYPTO_ASN1_CHK_ADD( pub_len, mbedcrypto_asn1_write_len( &c, buf, pub_len ) ); |
| 331 | MBEDCRYPTO_ASN1_CHK_ADD( pub_len, mbedcrypto_asn1_write_tag( &c, buf, |
| 332 | MBEDCRYPTO_ASN1_CONTEXT_SPECIFIC | MBEDCRYPTO_ASN1_CONSTRUCTED | 1 ) ); |
| 333 | len += pub_len; |
| 334 | |
| 335 | /* parameters */ |
| 336 | MBEDCRYPTO_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) ); |
| 337 | |
| 338 | MBEDCRYPTO_ASN1_CHK_ADD( par_len, mbedcrypto_asn1_write_len( &c, buf, par_len ) ); |
| 339 | MBEDCRYPTO_ASN1_CHK_ADD( par_len, mbedcrypto_asn1_write_tag( &c, buf, |
| 340 | MBEDCRYPTO_ASN1_CONTEXT_SPECIFIC | MBEDCRYPTO_ASN1_CONSTRUCTED | 0 ) ); |
| 341 | len += par_len; |
| 342 | |
| 343 | /* privateKey: write as MPI then fix tag */ |
| 344 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_mpi( &c, buf, &ec->d ) ); |
| 345 | *c = MBEDCRYPTO_ASN1_OCTET_STRING; |
| 346 | |
| 347 | /* version */ |
| 348 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_int( &c, buf, 1 ) ); |
| 349 | |
| 350 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_len( &c, buf, len ) ); |
| 351 | MBEDCRYPTO_ASN1_CHK_ADD( len, mbedcrypto_asn1_write_tag( &c, buf, MBEDCRYPTO_ASN1_CONSTRUCTED | |
| 352 | MBEDCRYPTO_ASN1_SEQUENCE ) ); |
| 353 | } |
| 354 | else |
| 355 | #endif /* MBEDCRYPTO_ECP_C */ |
| 356 | return( MBEDCRYPTO_ERR_PK_FEATURE_UNAVAILABLE ); |
| 357 | |
| 358 | return( (int) len ); |
| 359 | } |
| 360 | |
| 361 | #if defined(MBEDCRYPTO_PEM_WRITE_C) |
| 362 | |
| 363 | #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n" |
| 364 | #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n" |
| 365 | |
| 366 | #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n" |
| 367 | #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n" |
| 368 | #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n" |
| 369 | #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n" |
| 370 | |
| 371 | /* |
| 372 | * Max sizes of key per types. Shown as tag + len (+ content). |
| 373 | */ |
| 374 | |
| 375 | #if defined(MBEDCRYPTO_RSA_C) |
| 376 | /* |
| 377 | * RSA public keys: |
| 378 | * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3 |
| 379 | * algorithm AlgorithmIdentifier, 1 + 1 (sequence) |
| 380 | * + 1 + 1 + 9 (rsa oid) |
| 381 | * + 1 + 1 (params null) |
| 382 | * subjectPublicKey BIT STRING } 1 + 3 + (1 + below) |
| 383 | * RSAPublicKey ::= SEQUENCE { 1 + 3 |
| 384 | * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1 |
| 385 | * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1 |
| 386 | * } |
| 387 | */ |
| 388 | #define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDCRYPTO_MPI_MAX_SIZE |
| 389 | |
| 390 | /* |
| 391 | * RSA private keys: |
| 392 | * RSAPrivateKey ::= SEQUENCE { 1 + 3 |
| 393 | * version Version, 1 + 1 + 1 |
| 394 | * modulus INTEGER, 1 + 3 + MPI_MAX + 1 |
| 395 | * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1 |
| 396 | * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1 |
| 397 | * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 398 | * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 399 | * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 400 | * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 401 | * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1 |
| 402 | * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported) |
| 403 | * } |
| 404 | */ |
| 405 | #define MPI_MAX_SIZE_2 MBEDCRYPTO_MPI_MAX_SIZE / 2 + \ |
| 406 | MBEDCRYPTO_MPI_MAX_SIZE % 2 |
| 407 | #define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDCRYPTO_MPI_MAX_SIZE \ |
| 408 | + 5 * MPI_MAX_SIZE_2 |
| 409 | |
| 410 | #else /* MBEDCRYPTO_RSA_C */ |
| 411 | |
| 412 | #define RSA_PUB_DER_MAX_BYTES 0 |
| 413 | #define RSA_PRV_DER_MAX_BYTES 0 |
| 414 | |
| 415 | #endif /* MBEDCRYPTO_RSA_C */ |
| 416 | |
| 417 | #if defined(MBEDCRYPTO_ECP_C) |
| 418 | /* |
| 419 | * EC public keys: |
| 420 | * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2 |
| 421 | * algorithm AlgorithmIdentifier, 1 + 1 (sequence) |
| 422 | * + 1 + 1 + 7 (ec oid) |
| 423 | * + 1 + 1 + 9 (namedCurve oid) |
| 424 | * subjectPublicKey BIT STRING 1 + 2 + 1 [1] |
| 425 | * + 1 (point format) [1] |
| 426 | * + 2 * ECP_MAX (coords) [1] |
| 427 | * } |
| 428 | */ |
| 429 | #define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDCRYPTO_ECP_MAX_BYTES |
| 430 | |
| 431 | /* |
| 432 | * EC private keys: |
| 433 | * ECPrivateKey ::= SEQUENCE { 1 + 2 |
| 434 | * version INTEGER , 1 + 1 + 1 |
| 435 | * privateKey OCTET STRING, 1 + 1 + ECP_MAX |
| 436 | * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9) |
| 437 | * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above |
| 438 | * } |
| 439 | */ |
| 440 | #define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDCRYPTO_ECP_MAX_BYTES |
| 441 | |
| 442 | #else /* MBEDCRYPTO_ECP_C */ |
| 443 | |
| 444 | #define ECP_PUB_DER_MAX_BYTES 0 |
| 445 | #define ECP_PRV_DER_MAX_BYTES 0 |
| 446 | |
| 447 | #endif /* MBEDCRYPTO_ECP_C */ |
| 448 | |
| 449 | #define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \ |
| 450 | RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES |
| 451 | #define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \ |
| 452 | RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES |
| 453 | |
| 454 | int mbedcrypto_pk_write_pubkey_pem( mbedcrypto_pk_context *key, unsigned char *buf, size_t size ) |
| 455 | { |
| 456 | int ret; |
| 457 | unsigned char output_buf[PUB_DER_MAX_BYTES]; |
| 458 | size_t olen = 0; |
| 459 | |
| 460 | if( ( ret = mbedcrypto_pk_write_pubkey_der( key, output_buf, |
| 461 | sizeof(output_buf) ) ) < 0 ) |
| 462 | { |
| 463 | return( ret ); |
| 464 | } |
| 465 | |
| 466 | if( ( ret = mbedcrypto_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY, |
| 467 | output_buf + sizeof(output_buf) - ret, |
| 468 | ret, buf, size, &olen ) ) != 0 ) |
| 469 | { |
| 470 | return( ret ); |
| 471 | } |
| 472 | |
| 473 | return( 0 ); |
| 474 | } |
| 475 | |
| 476 | int mbedcrypto_pk_write_key_pem( mbedcrypto_pk_context *key, unsigned char *buf, size_t size ) |
| 477 | { |
| 478 | int ret; |
| 479 | unsigned char output_buf[PRV_DER_MAX_BYTES]; |
| 480 | const char *begin, *end; |
| 481 | size_t olen = 0; |
| 482 | |
| 483 | if( ( ret = mbedcrypto_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 ) |
| 484 | return( ret ); |
| 485 | |
| 486 | #if defined(MBEDCRYPTO_RSA_C) |
| 487 | if( mbedcrypto_pk_get_type( key ) == MBEDCRYPTO_PK_RSA ) |
| 488 | { |
| 489 | begin = PEM_BEGIN_PRIVATE_KEY_RSA; |
| 490 | end = PEM_END_PRIVATE_KEY_RSA; |
| 491 | } |
| 492 | else |
| 493 | #endif |
| 494 | #if defined(MBEDCRYPTO_ECP_C) |
| 495 | if( mbedcrypto_pk_get_type( key ) == MBEDCRYPTO_PK_ECKEY ) |
| 496 | { |
| 497 | begin = PEM_BEGIN_PRIVATE_KEY_EC; |
| 498 | end = PEM_END_PRIVATE_KEY_EC; |
| 499 | } |
| 500 | else |
| 501 | #endif |
| 502 | return( MBEDCRYPTO_ERR_PK_FEATURE_UNAVAILABLE ); |
| 503 | |
| 504 | if( ( ret = mbedcrypto_pem_write_buffer( begin, end, |
| 505 | output_buf + sizeof(output_buf) - ret, |
| 506 | ret, buf, size, &olen ) ) != 0 ) |
| 507 | { |
| 508 | return( ret ); |
| 509 | } |
| 510 | |
| 511 | return( 0 ); |
| 512 | } |
| 513 | #endif /* MBEDCRYPTO_PEM_WRITE_C */ |
| 514 | |
| 515 | #endif /* MBEDCRYPTO_PK_WRITE_C */ |