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