pkwrite.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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: GPL-2.0
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #if !defined(MBEDTLS_CONFIG_FILE)
  24. #include "mbedtls/config.h"
  25. #else
  26. #include MBEDTLS_CONFIG_FILE
  27. #endif
  28. #if defined(MBEDTLS_PK_WRITE_C)
  29. #include "mbedtls/pk.h"
  30. #include "mbedtls/asn1write.h"
  31. #include "mbedtls/oid.h"
  32. #include <string.h>
  33. #if defined(MBEDTLS_RSA_C)
  34. #include "mbedtls/rsa.h"
  35. #endif
  36. #if defined(MBEDTLS_ECP_C)
  37. #include "mbedtls/ecp.h"
  38. #endif
  39. #if defined(MBEDTLS_ECDSA_C)
  40. #include "mbedtls/ecdsa.h"
  41. #endif
  42. #if defined(MBEDTLS_PEM_WRITE_C)
  43. #include "mbedtls/pem.h"
  44. #endif
  45. #if defined(MBEDTLS_PLATFORM_C)
  46. #include "mbedtls/platform.h"
  47. #else
  48. #include <stdlib.h>
  49. #define mbedtls_calloc calloc
  50. #define mbedtls_free free
  51. #endif
  52. #if defined(MBEDTLS_RSA_C)
  53. /*
  54. * RSAPublicKey ::= SEQUENCE {
  55. * modulus INTEGER, -- n
  56. * publicExponent INTEGER -- e
  57. * }
  58. */
  59. static int pk_write_rsa_pubkey( unsigned char **p, unsigned char *start,
  60. mbedtls_rsa_context *rsa )
  61. {
  62. int ret;
  63. size_t len = 0;
  64. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( p, start, &rsa->E ) );
  65. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( p, start, &rsa->N ) );
  66. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
  67. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, MBEDTLS_ASN1_CONSTRUCTED |
  68. MBEDTLS_ASN1_SEQUENCE ) );
  69. return( (int) len );
  70. }
  71. #endif /* MBEDTLS_RSA_C */
  72. #if defined(MBEDTLS_ECP_C)
  73. /*
  74. * EC public key is an EC point
  75. */
  76. static int pk_write_ec_pubkey( unsigned char **p, unsigned char *start,
  77. mbedtls_ecp_keypair *ec )
  78. {
  79. int ret;
  80. size_t len = 0;
  81. unsigned char buf[MBEDTLS_ECP_MAX_PT_LEN];
  82. if( ( ret = mbedtls_ecp_point_write_binary( &ec->grp, &ec->Q,
  83. MBEDTLS_ECP_PF_UNCOMPRESSED,
  84. &len, buf, sizeof( buf ) ) ) != 0 )
  85. {
  86. return( ret );
  87. }
  88. if( *p < start || (size_t)( *p - start ) < len )
  89. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  90. *p -= len;
  91. memcpy( *p, buf, len );
  92. return( (int) len );
  93. }
  94. /*
  95. * ECParameters ::= CHOICE {
  96. * namedCurve OBJECT IDENTIFIER
  97. * }
  98. */
  99. static int pk_write_ec_param( unsigned char **p, unsigned char *start,
  100. mbedtls_ecp_keypair *ec )
  101. {
  102. int ret;
  103. size_t len = 0;
  104. const char *oid;
  105. size_t oid_len;
  106. if( ( ret = mbedtls_oid_get_oid_by_ec_grp( ec->grp.id, &oid, &oid_len ) ) != 0 )
  107. return( ret );
  108. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_oid( p, start, oid, oid_len ) );
  109. return( (int) len );
  110. }
  111. #endif /* MBEDTLS_ECP_C */
  112. int mbedtls_pk_write_pubkey( unsigned char **p, unsigned char *start,
  113. const mbedtls_pk_context *key )
  114. {
  115. int ret;
  116. size_t len = 0;
  117. #if defined(MBEDTLS_RSA_C)
  118. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
  119. MBEDTLS_ASN1_CHK_ADD( len, pk_write_rsa_pubkey( p, start, mbedtls_pk_rsa( *key ) ) );
  120. else
  121. #endif
  122. #if defined(MBEDTLS_ECP_C)
  123. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
  124. MBEDTLS_ASN1_CHK_ADD( len, pk_write_ec_pubkey( p, start, mbedtls_pk_ec( *key ) ) );
  125. else
  126. #endif
  127. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  128. return( (int) len );
  129. }
  130. int mbedtls_pk_write_pubkey_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
  131. {
  132. int ret;
  133. unsigned char *c;
  134. size_t len = 0, par_len = 0, oid_len;
  135. const char *oid;
  136. c = buf + size;
  137. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
  138. if( c - buf < 1 )
  139. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  140. /*
  141. * SubjectPublicKeyInfo ::= SEQUENCE {
  142. * algorithm AlgorithmIdentifier,
  143. * subjectPublicKey BIT STRING }
  144. */
  145. *--c = 0;
  146. len += 1;
  147. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  148. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
  149. if( ( ret = mbedtls_oid_get_oid_by_pk_alg( mbedtls_pk_get_type( key ),
  150. &oid, &oid_len ) ) != 0 )
  151. {
  152. return( ret );
  153. }
  154. #if defined(MBEDTLS_ECP_C)
  155. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
  156. {
  157. MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, mbedtls_pk_ec( *key ) ) );
  158. }
  159. #endif
  160. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_algorithm_identifier( &c, buf, oid, oid_len,
  161. par_len ) );
  162. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  163. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  164. MBEDTLS_ASN1_SEQUENCE ) );
  165. return( (int) len );
  166. }
  167. int mbedtls_pk_write_key_der( mbedtls_pk_context *key, unsigned char *buf, size_t size )
  168. {
  169. int ret;
  170. unsigned char *c = buf + size;
  171. size_t len = 0;
  172. #if defined(MBEDTLS_RSA_C)
  173. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
  174. {
  175. mbedtls_rsa_context *rsa = mbedtls_pk_rsa( *key );
  176. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->QP ) );
  177. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->DQ ) );
  178. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->DP ) );
  179. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->Q ) );
  180. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->P ) );
  181. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->D ) );
  182. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->E ) );
  183. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &rsa->N ) );
  184. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 0 ) );
  185. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  186. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  187. MBEDTLS_ASN1_SEQUENCE ) );
  188. }
  189. else
  190. #endif /* MBEDTLS_RSA_C */
  191. #if defined(MBEDTLS_ECP_C)
  192. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
  193. {
  194. mbedtls_ecp_keypair *ec = mbedtls_pk_ec( *key );
  195. size_t pub_len = 0, par_len = 0;
  196. /*
  197. * RFC 5915, or SEC1 Appendix C.4
  198. *
  199. * ECPrivateKey ::= SEQUENCE {
  200. * version INTEGER { ecPrivkeyVer1(1) } (ecPrivkeyVer1),
  201. * privateKey OCTET STRING,
  202. * parameters [0] ECParameters {{ NamedCurve }} OPTIONAL,
  203. * publicKey [1] BIT STRING OPTIONAL
  204. * }
  205. */
  206. /* publicKey */
  207. MBEDTLS_ASN1_CHK_ADD( pub_len, pk_write_ec_pubkey( &c, buf, ec ) );
  208. if( c - buf < 1 )
  209. return( MBEDTLS_ERR_ASN1_BUF_TOO_SMALL );
  210. *--c = 0;
  211. pub_len += 1;
  212. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
  213. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_BIT_STRING ) );
  214. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_len( &c, buf, pub_len ) );
  215. MBEDTLS_ASN1_CHK_ADD( pub_len, mbedtls_asn1_write_tag( &c, buf,
  216. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 1 ) );
  217. len += pub_len;
  218. /* parameters */
  219. MBEDTLS_ASN1_CHK_ADD( par_len, pk_write_ec_param( &c, buf, ec ) );
  220. MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_len( &c, buf, par_len ) );
  221. MBEDTLS_ASN1_CHK_ADD( par_len, mbedtls_asn1_write_tag( &c, buf,
  222. MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
  223. len += par_len;
  224. /* privateKey: write as MPI then fix tag */
  225. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, &ec->d ) );
  226. *c = MBEDTLS_ASN1_OCTET_STRING;
  227. /* version */
  228. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, 1 ) );
  229. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
  230. MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
  231. MBEDTLS_ASN1_SEQUENCE ) );
  232. }
  233. else
  234. #endif /* MBEDTLS_ECP_C */
  235. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  236. return( (int) len );
  237. }
  238. #if defined(MBEDTLS_PEM_WRITE_C)
  239. #define PEM_BEGIN_PUBLIC_KEY "-----BEGIN PUBLIC KEY-----\n"
  240. #define PEM_END_PUBLIC_KEY "-----END PUBLIC KEY-----\n"
  241. #define PEM_BEGIN_PRIVATE_KEY_RSA "-----BEGIN RSA PRIVATE KEY-----\n"
  242. #define PEM_END_PRIVATE_KEY_RSA "-----END RSA PRIVATE KEY-----\n"
  243. #define PEM_BEGIN_PRIVATE_KEY_EC "-----BEGIN EC PRIVATE KEY-----\n"
  244. #define PEM_END_PRIVATE_KEY_EC "-----END EC PRIVATE KEY-----\n"
  245. /*
  246. * Max sizes of key per types. Shown as tag + len (+ content).
  247. */
  248. #if defined(MBEDTLS_RSA_C)
  249. /*
  250. * RSA public keys:
  251. * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 3
  252. * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
  253. * + 1 + 1 + 9 (rsa oid)
  254. * + 1 + 1 (params null)
  255. * subjectPublicKey BIT STRING } 1 + 3 + (1 + below)
  256. * RSAPublicKey ::= SEQUENCE { 1 + 3
  257. * modulus INTEGER, -- n 1 + 3 + MPI_MAX + 1
  258. * publicExponent INTEGER -- e 1 + 3 + MPI_MAX + 1
  259. * }
  260. */
  261. #define RSA_PUB_DER_MAX_BYTES 38 + 2 * MBEDTLS_MPI_MAX_SIZE
  262. /*
  263. * RSA private keys:
  264. * RSAPrivateKey ::= SEQUENCE { 1 + 3
  265. * version Version, 1 + 1 + 1
  266. * modulus INTEGER, 1 + 3 + MPI_MAX + 1
  267. * publicExponent INTEGER, 1 + 3 + MPI_MAX + 1
  268. * privateExponent INTEGER, 1 + 3 + MPI_MAX + 1
  269. * prime1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
  270. * prime2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
  271. * exponent1 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
  272. * exponent2 INTEGER, 1 + 3 + MPI_MAX / 2 + 1
  273. * coefficient INTEGER, 1 + 3 + MPI_MAX / 2 + 1
  274. * otherPrimeInfos OtherPrimeInfos OPTIONAL 0 (not supported)
  275. * }
  276. */
  277. #define MPI_MAX_SIZE_2 MBEDTLS_MPI_MAX_SIZE / 2 + \
  278. MBEDTLS_MPI_MAX_SIZE % 2
  279. #define RSA_PRV_DER_MAX_BYTES 47 + 3 * MBEDTLS_MPI_MAX_SIZE \
  280. + 5 * MPI_MAX_SIZE_2
  281. #else /* MBEDTLS_RSA_C */
  282. #define RSA_PUB_DER_MAX_BYTES 0
  283. #define RSA_PRV_DER_MAX_BYTES 0
  284. #endif /* MBEDTLS_RSA_C */
  285. #if defined(MBEDTLS_ECP_C)
  286. /*
  287. * EC public keys:
  288. * SubjectPublicKeyInfo ::= SEQUENCE { 1 + 2
  289. * algorithm AlgorithmIdentifier, 1 + 1 (sequence)
  290. * + 1 + 1 + 7 (ec oid)
  291. * + 1 + 1 + 9 (namedCurve oid)
  292. * subjectPublicKey BIT STRING 1 + 2 + 1 [1]
  293. * + 1 (point format) [1]
  294. * + 2 * ECP_MAX (coords) [1]
  295. * }
  296. */
  297. #define ECP_PUB_DER_MAX_BYTES 30 + 2 * MBEDTLS_ECP_MAX_BYTES
  298. /*
  299. * EC private keys:
  300. * ECPrivateKey ::= SEQUENCE { 1 + 2
  301. * version INTEGER , 1 + 1 + 1
  302. * privateKey OCTET STRING, 1 + 1 + ECP_MAX
  303. * parameters [0] ECParameters OPTIONAL, 1 + 1 + (1 + 1 + 9)
  304. * publicKey [1] BIT STRING OPTIONAL 1 + 2 + [1] above
  305. * }
  306. */
  307. #define ECP_PRV_DER_MAX_BYTES 29 + 3 * MBEDTLS_ECP_MAX_BYTES
  308. #else /* MBEDTLS_ECP_C */
  309. #define ECP_PUB_DER_MAX_BYTES 0
  310. #define ECP_PRV_DER_MAX_BYTES 0
  311. #endif /* MBEDTLS_ECP_C */
  312. #define PUB_DER_MAX_BYTES RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
  313. RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES
  314. #define PRV_DER_MAX_BYTES RSA_PRV_DER_MAX_BYTES > ECP_PRV_DER_MAX_BYTES ? \
  315. RSA_PRV_DER_MAX_BYTES : ECP_PRV_DER_MAX_BYTES
  316. int mbedtls_pk_write_pubkey_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
  317. {
  318. int ret;
  319. unsigned char output_buf[PUB_DER_MAX_BYTES];
  320. size_t olen = 0;
  321. if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf,
  322. sizeof(output_buf) ) ) < 0 )
  323. {
  324. return( ret );
  325. }
  326. if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_PUBLIC_KEY, PEM_END_PUBLIC_KEY,
  327. output_buf + sizeof(output_buf) - ret,
  328. ret, buf, size, &olen ) ) != 0 )
  329. {
  330. return( ret );
  331. }
  332. return( 0 );
  333. }
  334. int mbedtls_pk_write_key_pem( mbedtls_pk_context *key, unsigned char *buf, size_t size )
  335. {
  336. int ret;
  337. unsigned char output_buf[PRV_DER_MAX_BYTES];
  338. const char *begin, *end;
  339. size_t olen = 0;
  340. if( ( ret = mbedtls_pk_write_key_der( key, output_buf, sizeof(output_buf) ) ) < 0 )
  341. return( ret );
  342. #if defined(MBEDTLS_RSA_C)
  343. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_RSA )
  344. {
  345. begin = PEM_BEGIN_PRIVATE_KEY_RSA;
  346. end = PEM_END_PRIVATE_KEY_RSA;
  347. }
  348. else
  349. #endif
  350. #if defined(MBEDTLS_ECP_C)
  351. if( mbedtls_pk_get_type( key ) == MBEDTLS_PK_ECKEY )
  352. {
  353. begin = PEM_BEGIN_PRIVATE_KEY_EC;
  354. end = PEM_END_PRIVATE_KEY_EC;
  355. }
  356. else
  357. #endif
  358. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  359. if( ( ret = mbedtls_pem_write_buffer( begin, end,
  360. output_buf + sizeof(output_buf) - ret,
  361. ret, buf, size, &olen ) ) != 0 )
  362. {
  363. return( ret );
  364. }
  365. return( 0 );
  366. }
  367. #endif /* MBEDTLS_PEM_WRITE_C */
  368. #endif /* MBEDTLS_PK_WRITE_C */