pkwrite.c 14 KB

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