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