Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Public Key layer for writing key files and structures |
| 3 | * |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, Brainspark B.V. |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 27 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else |
| 29 | #include POLARSSL_CONFIG_FILE |
| 30 | #endif |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 31 | |
Paul Bakker | 4606c73 | 2013-09-15 17:04:23 +0200 | [diff] [blame] | 32 | #if defined(POLARSSL_PK_WRITE_C) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 33 | |
| 34 | #include "polarssl/pk.h" |
| 35 | #include "polarssl/asn1write.h" |
| 36 | #include "polarssl/oid.h" |
| 37 | |
| 38 | #if defined(POLARSSL_RSA_C) |
| 39 | #include "polarssl/rsa.h" |
| 40 | #endif |
| 41 | #if defined(POLARSSL_ECP_C) |
| 42 | #include "polarssl/ecp.h" |
| 43 | #endif |
| 44 | #if defined(POLARSSL_ECDSA_C) |
| 45 | #include "polarssl/ecdsa.h" |
| 46 | #endif |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 47 | #if defined(POLARSSL_PEM_WRITE_C) |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 48 | #include "polarssl/pem.h" |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 49 | #endif |
| 50 | |
Paul Bakker | 7dc4c44 | 2014-02-01 22:50:26 +0100 | [diff] [blame] | 51 | #if defined(POLARSSL_PLATFORM_C) |
| 52 | #include "polarssl/platform.h" |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 53 | #else |
| 54 | #include <stdlib.h> |
| 55 | #define polarssl_malloc malloc |
| 56 | #define polarssl_free free |
| 57 | #endif |
| 58 | |
| 59 | #if defined(POLARSSL_RSA_C) |
| 60 | /* |
| 61 | * RSAPublicKey ::= SEQUENCE { |
| 62 | * modulus INTEGER, -- n |
| 63 | * publicExponent INTEGER -- e |
| 64 | * } |
| 65 | */ |
| 66 | static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start, |
| 67 | rsa_context *rsa ) |
| 68 | { |
| 69 | int ret; |
| 70 | size_t len = 0; |
| 71 | |
| 72 | ASN1_CHK_ADD( len, asn1_write_mpi( p, start, &rsa->E ) ); |
| 73 | ASN1_CHK_ADD( len, asn1_write_mpi( p, start, &rsa->N ) ); |
| 74 | |
| 75 | ASN1_CHK_ADD( len, asn1_write_len( p, start, len ) ); |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 76 | ASN1_CHK_ADD( len, asn1_write_tag( p, start, ASN1_CONSTRUCTED | |
| 77 | ASN1_SEQUENCE ) ); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 78 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 79 | return( (int) len ); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 80 | } |
| 81 | #endif /* POLARSSL_RSA_C */ |
| 82 | |
| 83 | #if defined(POLARSSL_ECP_C) |
| 84 | /* |
| 85 | * EC public key is an EC point |
| 86 | */ |
| 87 | static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start, |
| 88 | ecp_keypair *ec ) |
| 89 | { |
| 90 | int ret; |
| 91 | size_t len = 0; |
| 92 | unsigned char buf[POLARSSL_ECP_MAX_PT_LEN]; |
| 93 | |
| 94 | if( ( ret = ecp_point_write_binary( &ec->grp, &ec->Q, |
| 95 | POLARSSL_ECP_PF_UNCOMPRESSED, |
| 96 | &len, buf, sizeof( buf ) ) ) != 0 ) |
| 97 | { |
| 98 | return( ret ); |
| 99 | } |
| 100 | |
| 101 | if( *p - start < (int) len ) |
| 102 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 103 | |
| 104 | *p -= len; |
| 105 | memcpy( *p, buf, len ); |
| 106 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 107 | return( (int) len ); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /* |
| 111 | * ECParameters ::= CHOICE { |
| 112 | * namedCurve OBJECT IDENTIFIER |
| 113 | * } |
| 114 | */ |
| 115 | static int pk_write_ec_param( unsigned char **p, unsigned char *start, |
| 116 | ecp_keypair *ec ) |
| 117 | { |
| 118 | int ret; |
| 119 | size_t len = 0; |
| 120 | const char *oid; |
| 121 | size_t oid_len; |
| 122 | |
| 123 | if( ( ret = oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 ) |
| 124 | return( ret ); |
| 125 | |
| 126 | ASN1_CHK_ADD( len, asn1_write_oid( p, start, oid, oid_len ) ); |
| 127 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 128 | return( (int) len ); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 129 | } |
| 130 | #endif /* POLARSSL_ECP_C */ |
| 131 | |
| 132 | int pk_write_pubkey( unsigned char **p, unsigned char *start, |
| 133 | const pk_context *key ) |
| 134 | { |
| 135 | int ret; |
| 136 | size_t len = 0; |
| 137 | |
| 138 | #if defined(POLARSSL_RSA_C) |
| 139 | if( pk_get_type( key ) == POLARSSL_PK_RSA ) |
| 140 | ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, pk_rsa( *key ) ) ); |
| 141 | else |
| 142 | #endif |
| 143 | #if defined(POLARSSL_ECP_C) |
| 144 | if( pk_get_type( key ) == POLARSSL_PK_ECKEY ) |
| 145 | ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, pk_ec( *key ) ) ); |
| 146 | else |
| 147 | #endif |
| 148 | return( POLARSSL_ERR_PK_FEATURE_UNAVAILABLE ); |
| 149 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 150 | return( (int) len ); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | int pk_write_pubkey_der( pk_context *key, unsigned char *buf, size_t size ) |
| 154 | { |
| 155 | int ret; |
| 156 | unsigned char *c; |
| 157 | size_t len = 0, par_len = 0, oid_len; |
| 158 | const char *oid; |
| 159 | |
| 160 | c = buf + size; |
| 161 | |
| 162 | ASN1_CHK_ADD( len, pk_write_pubkey( &c, buf, key ) ); |
| 163 | |
| 164 | if( c - buf < 1 ) |
| 165 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 166 | |
| 167 | /* |
| 168 | * SubjectPublicKeyInfo ::= SEQUENCE { |
| 169 | * algorithm AlgorithmIdentifier, |
| 170 | * subjectPublicKey BIT STRING } |
| 171 | */ |
| 172 | *--c = 0; |
| 173 | len += 1; |
| 174 | |
| 175 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
| 176 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_BIT_STRING ) ); |
| 177 | |
| 178 | if( ( ret = oid_get_oid_by_pk_alg( pk_get_type( key ), |
| 179 | &oid, &oid_len ) ) != 0 ) |
| 180 | { |
| 181 | return( ret ); |
| 182 | } |
| 183 | |
| 184 | #if defined(POLARSSL_ECP_C) |
| 185 | if( pk_get_type( key ) == POLARSSL_PK_ECKEY ) |
| 186 | { |
| 187 | ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, pk_ec( *key ) ) ); |
| 188 | } |
| 189 | #endif |
| 190 | |
| 191 | ASN1_CHK_ADD( len, asn1_write_algorithm_identifier( &c, buf, oid, oid_len, |
| 192 | par_len ) ); |
| 193 | |
| 194 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 195 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | |
| 196 | ASN1_SEQUENCE ) ); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 197 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 198 | return( (int) len ); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | int pk_write_key_der( pk_context *key, unsigned char *buf, size_t size ) |
| 202 | { |
| 203 | int ret; |
| 204 | unsigned char *c = buf + size; |
| 205 | size_t len = 0; |
| 206 | |
| 207 | #if defined(POLARSSL_RSA_C) |
| 208 | if( pk_get_type( key ) == POLARSSL_PK_RSA ) |
| 209 | { |
| 210 | rsa_context *rsa = pk_rsa( *key ); |
| 211 | |
| 212 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->QP ) ); |
| 213 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DQ ) ); |
| 214 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->DP ) ); |
| 215 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->Q ) ); |
| 216 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->P ) ); |
| 217 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->D ) ); |
| 218 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->E ) ); |
| 219 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &rsa->N ) ); |
| 220 | ASN1_CHK_ADD( len, asn1_write_int( &c, buf, 0 ) ); |
| 221 | |
| 222 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 223 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | |
| 224 | ASN1_SEQUENCE ) ); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 225 | } |
| 226 | else |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 227 | #endif /* POLARSSL_RSA_C */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 228 | #if defined(POLARSSL_ECP_C) |
| 229 | if( pk_get_type( key ) == POLARSSL_PK_ECKEY ) |
| 230 | { |
| 231 | ecp_keypair *ec = pk_ec( *key ); |
| 232 | size_t pub_len = 0, par_len = 0; |
| 233 | |
| 234 | /* |
| 235 | * RFC 5915, or SEC1 Appendix C.4 |
| 236 | * |
| 237 | * ECPrivateKey ::= SEQUENCE { |
| 238 | * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1), |
| 239 | * privateKey OCTET STRING, |
| 240 | * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL, |
| 241 | * publicKey [1] BIT STRING OPTIONAL |
| 242 | * } |
| 243 | */ |
| 244 | |
| 245 | /* publicKey */ |
| 246 | ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) ); |
| 247 | |
| 248 | if( c - buf < 1 ) |
| 249 | return( POLARSSL_ERR_ASN1_BUF_TOO_SMALL ); |
| 250 | *--c = 0; |
| 251 | pub_len += 1; |
| 252 | |
| 253 | ASN1_CHK_ADD( pub_len, asn1_write_len( &c, buf, pub_len ) ); |
| 254 | ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, buf, ASN1_BIT_STRING ) ); |
| 255 | |
| 256 | ASN1_CHK_ADD( pub_len, asn1_write_len( &c, buf, pub_len ) ); |
| 257 | ASN1_CHK_ADD( pub_len, asn1_write_tag( &c, buf, |
| 258 | ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 1 ) ); |
| 259 | len += pub_len; |
| 260 | |
| 261 | /* parameters */ |
| 262 | ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) ); |
| 263 | |
| 264 | ASN1_CHK_ADD( par_len, asn1_write_len( &c, buf, par_len ) ); |
| 265 | ASN1_CHK_ADD( par_len, asn1_write_tag( &c, buf, |
| 266 | ASN1_CONTEXT_SPECIFIC | ASN1_CONSTRUCTED | 0 ) ); |
| 267 | len += par_len; |
| 268 | |
| 269 | /* privateKey: write as MPI then fix tag */ |
| 270 | ASN1_CHK_ADD( len, asn1_write_mpi( &c, buf, &ec->d ) ); |
| 271 | *c = ASN1_OCTET_STRING; |
| 272 | |
| 273 | /* version */ |
| 274 | ASN1_CHK_ADD( len, asn1_write_int( &c, buf, 1 ) ); |
| 275 | |
| 276 | ASN1_CHK_ADD( len, asn1_write_len( &c, buf, len ) ); |
Paul Bakker | b9e4e2c | 2014-05-01 14:18:25 +0200 | [diff] [blame] | 277 | ASN1_CHK_ADD( len, asn1_write_tag( &c, buf, ASN1_CONSTRUCTED | |
| 278 | ASN1_SEQUENCE ) ); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 279 | } |
| 280 | else |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 281 | #endif /* POLARSSL_ECP_C */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 282 | return( POLARSSL_ERR_PK_FEATURE_UNAVAILABLE ); |
| 283 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 284 | return( (int) len ); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 285 | } |
| 286 | |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 287 | #if defined(POLARSSL_PEM_WRITE_C) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 288 | |
| 289 | #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n" |
| 290 | #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n" |
| 291 | |
| 292 | #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n" |
| 293 | #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n" |
| 294 | #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n" |
| 295 | #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n" |
| 296 | |
| 297 | int pk_write_pubkey_pem( pk_context *key, unsigned char *buf, size_t size ) |
| 298 | { |
| 299 | int ret; |
| 300 | unsigned char output_buf[4096]; |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 301 | size_t olen = 0; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 302 | |
| 303 | if( ( ret = pk_write_pubkey_der( key, output_buf, |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 304 | sizeof(output_buf) ) ) < 0 ) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 305 | { |
| 306 | return( ret ); |
| 307 | } |
| 308 | |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 309 | if( ( ret = pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY, |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 310 | output_buf + sizeof(output_buf) - ret, |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 311 | ret, buf, size, &olen ) ) != 0 ) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 312 | { |
| 313 | return( ret ); |
| 314 | } |
| 315 | |
| 316 | return( 0 ); |
| 317 | } |
| 318 | |
| 319 | int pk_write_key_pem( pk_context *key, unsigned char *buf, size_t size ) |
| 320 | { |
| 321 | int ret; |
| 322 | unsigned char output_buf[4096]; |
Paul Bakker | fcc1721 | 2013-10-11 09:36:52 +0200 | [diff] [blame] | 323 | const char *begin, *end; |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 324 | size_t olen = 0; |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 325 | |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 326 | if( ( ret = pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 ) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 327 | return( ret ); |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 328 | |
| 329 | #if defined(POLARSSL_RSA_C) |
| 330 | if( pk_get_type( key ) == POLARSSL_PK_RSA ) |
| 331 | { |
| 332 | begin = PEM_BEGIN_PRIVATE_KEY_RSA; |
| 333 | end = PEM_END_PRIVATE_KEY_RSA; |
| 334 | } |
| 335 | else |
| 336 | #endif |
| 337 | #if defined(POLARSSL_ECP_C) |
| 338 | if( pk_get_type( key ) == POLARSSL_PK_ECKEY ) |
| 339 | { |
| 340 | begin = PEM_BEGIN_PRIVATE_KEY_EC; |
| 341 | end = PEM_END_PRIVATE_KEY_EC; |
| 342 | } |
| 343 | else |
| 344 | #endif |
| 345 | return( POLARSSL_ERR_PK_FEATURE_UNAVAILABLE ); |
| 346 | |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 347 | if( ( ret = pem_write_buffer( begin, end, |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 348 | output_buf + sizeof(output_buf) - ret, |
Paul Bakker | 77e23fb | 2013-09-15 20:03:26 +0200 | [diff] [blame] | 349 | ret, buf, size, &olen ) ) != 0 ) |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 350 | { |
| 351 | return( ret ); |
| 352 | } |
| 353 | |
| 354 | return( 0 ); |
| 355 | } |
Paul Bakker | cff6842 | 2013-09-15 20:43:33 +0200 | [diff] [blame] | 356 | #endif /* POLARSSL_PEM_WRITE_C */ |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 357 | |
Paul Bakker | 4606c73 | 2013-09-15 17:04:23 +0200 | [diff] [blame] | 358 | #endif /* POLARSSL_PK_WRITE_C */ |