Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

certstore.c 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <ipxe/init.h>
  23. #include <ipxe/dhcp.h>
  24. #include <ipxe/settings.h>
  25. #include <ipxe/malloc.h>
  26. #include <ipxe/crypto.h>
  27. #include <ipxe/asn1.h>
  28. #include <ipxe/x509.h>
  29. #include <ipxe/certstore.h>
  30. /** @file
  31. *
  32. * Certificate store
  33. *
  34. */
  35. /** Raw certificate data for all permanent stored certificates */
  36. #undef CERT
  37. #define CERT( _index, _path ) \
  38. extern char stored_cert_ ## _index ## _data[]; \
  39. extern char stored_cert_ ## _index ## _len[]; \
  40. __asm__ ( ".section \".rodata\", \"a\", @progbits\n\t" \
  41. "\nstored_cert_" #_index "_data:\n\t" \
  42. ".incbin \"" _path "\"\n\t" \
  43. "\nstored_cert_" #_index "_end:\n\t" \
  44. ".equ stored_cert_" #_index "_len, " \
  45. "( stored_cert_" #_index "_end - " \
  46. " stored_cert_" #_index "_data )\n\t" \
  47. ".previous\n\t" );
  48. CERT_ALL
  49. /** Raw certificate cursors for all permanent stored certificates */
  50. #undef CERT
  51. #define CERT( _index, _path ) { \
  52. .data = stored_cert_ ## _index ## _data, \
  53. .len = ( size_t ) stored_cert_ ## _index ## _len, \
  54. },
  55. static struct asn1_cursor certstore_raw[] = {
  56. CERT_ALL
  57. };
  58. /** X.509 certificate structures for all permanent stored certificates */
  59. static struct x509_certificate certstore_certs[ sizeof ( certstore_raw ) /
  60. sizeof ( certstore_raw[0] ) ];
  61. /** Certificate store */
  62. struct x509_chain certstore = {
  63. .refcnt = REF_INIT ( ref_no_free ),
  64. .links = LIST_HEAD_INIT ( certstore.links ),
  65. };
  66. /**
  67. * Mark stored certificate as most recently used
  68. *
  69. * @v cert X.509 certificate
  70. * @ret cert X.509 certificate
  71. */
  72. static struct x509_certificate *
  73. certstore_found ( struct x509_certificate *cert ) {
  74. /* Mark as most recently used */
  75. list_del ( &cert->store.list );
  76. list_add ( &cert->store.list, &certstore.links );
  77. DBGC2 ( &certstore, "CERTSTORE found certificate %s\n",
  78. x509_name ( cert ) );
  79. return cert;
  80. }
  81. /**
  82. * Find certificate in store
  83. *
  84. * @v raw Raw certificate data
  85. * @ret cert X.509 certificate, or NULL if not found
  86. */
  87. struct x509_certificate * certstore_find ( struct asn1_cursor *raw ) {
  88. struct x509_certificate *cert;
  89. /* Search for certificate within store */
  90. list_for_each_entry ( cert, &certstore.links, store.list ) {
  91. if ( asn1_compare ( raw, &cert->raw ) == 0 )
  92. return certstore_found ( cert );
  93. }
  94. return NULL;
  95. }
  96. /**
  97. * Find certificate in store corresponding to a private key
  98. *
  99. * @v key Private key
  100. * @ret cert X.509 certificate, or NULL if not found
  101. */
  102. struct x509_certificate * certstore_find_key ( struct asn1_cursor *key ) {
  103. struct x509_certificate *cert;
  104. /* Search for certificate within store */
  105. list_for_each_entry ( cert, &certstore.links, store.list ) {
  106. if ( pubkey_match ( cert->signature_algorithm->pubkey,
  107. key->data, key->len,
  108. cert->subject.public_key.raw.data,
  109. cert->subject.public_key.raw.len ) == 0 )
  110. return certstore_found ( cert );
  111. }
  112. return NULL;
  113. }
  114. /**
  115. * Add certificate to store
  116. *
  117. * @v cert X.509 certificate
  118. */
  119. void certstore_add ( struct x509_certificate *cert ) {
  120. /* Add certificate to store */
  121. cert->store.cert = cert;
  122. x509_get ( cert );
  123. list_add ( &cert->store.list, &certstore.links );
  124. DBGC ( &certstore, "CERTSTORE added certificate %s\n",
  125. x509_name ( cert ) );
  126. }
  127. /**
  128. * Discard a stored certificate
  129. *
  130. * @ret discarded Number of cached items discarded
  131. */
  132. static unsigned int certstore_discard ( void ) {
  133. struct x509_certificate *cert;
  134. /* Discard the least recently used certificate for which the
  135. * only reference is held by the store itself.
  136. */
  137. list_for_each_entry_reverse ( cert, &certstore.links, store.list ) {
  138. if ( cert->refcnt.count == 0 ) {
  139. DBGC ( &certstore, "CERTSTORE discarded certificate "
  140. "%s\n", x509_name ( cert ) );
  141. list_del ( &cert->store.list );
  142. x509_put ( cert );
  143. return 1;
  144. }
  145. }
  146. return 0;
  147. }
  148. /** Certificate store cache discarder */
  149. struct cache_discarder certstore_discarder __cache_discarder ( CACHE_NORMAL ) ={
  150. .discard = certstore_discard,
  151. };
  152. /**
  153. * Construct permanent certificate store
  154. *
  155. */
  156. static void certstore_init ( void ) {
  157. struct asn1_cursor *raw;
  158. struct x509_certificate *cert;
  159. int i;
  160. int rc;
  161. /* Skip if we have no permanent stored certificates */
  162. if ( ! sizeof ( certstore_raw ) )
  163. return;
  164. /* Add certificates */
  165. for ( i = 0 ; i < ( int ) ( sizeof ( certstore_raw ) /
  166. sizeof ( certstore_raw[0] ) ) ; i++ ) {
  167. /* Skip if certificate already present in store */
  168. raw = &certstore_raw[i];
  169. if ( ( cert = certstore_find ( raw ) ) != NULL ) {
  170. DBGC ( &certstore, "CERTSTORE permanent certificate %d "
  171. "is a duplicate of %s\n", i, x509_name ( cert ));
  172. continue;
  173. }
  174. /* Parse certificate */
  175. cert = &certstore_certs[i];
  176. ref_init ( &cert->refcnt, ref_no_free );
  177. if ( ( rc = x509_parse ( cert, raw ) ) != 0 ) {
  178. DBGC ( &certstore, "CERTSTORE could not parse "
  179. "permanent certificate %d: %s\n",
  180. i, strerror ( rc ) );
  181. continue;
  182. }
  183. /* Add certificate to store. Certificate will never
  184. * be discarded from the store, since we retain a
  185. * permanent reference to it.
  186. */
  187. certstore_add ( cert );
  188. DBGC ( &certstore, "CERTSTORE permanent certificate %d is %s\n",
  189. i, x509_name ( cert ) );
  190. }
  191. }
  192. /** Certificate store initialisation function */
  193. struct init_fn certstore_init_fn __init_fn ( INIT_LATE ) = {
  194. .initialise = certstore_init,
  195. };
  196. /** Additional certificate setting */
  197. static struct setting cert_setting __setting ( SETTING_CRYPTO, cert ) = {
  198. .name = "cert",
  199. .description = "Certificate",
  200. .tag = DHCP_EB_CERT,
  201. .type = &setting_type_hex,
  202. };
  203. /**
  204. * Apply certificate store configuration settings
  205. *
  206. * @ret rc Return status code
  207. */
  208. static int certstore_apply_settings ( void ) {
  209. static struct x509_certificate *cert = NULL;
  210. struct x509_certificate *old_cert;
  211. void *cert_data;
  212. int len;
  213. int rc;
  214. /* Record any existing additional certificate */
  215. old_cert = cert;
  216. cert = NULL;
  217. /* Add additional certificate, if any */
  218. if ( ( len = fetch_raw_setting_copy ( NULL, &cert_setting,
  219. &cert_data ) ) >= 0 ) {
  220. if ( ( rc = x509_certificate ( cert_data, len, &cert ) ) == 0 ){
  221. DBGC ( &certstore, "CERTSTORE added additional "
  222. "certificate %s\n", x509_name ( cert ) );
  223. } else {
  224. DBGC ( &certstore, "CERTSTORE could not parse "
  225. "additional certificate: %s\n",
  226. strerror ( rc ) );
  227. /* Do not fail; leave as an unusable certificate */
  228. }
  229. free ( cert_data );
  230. }
  231. /* Free old additional certificiate. Do this after reparsing
  232. * the additional certificate; in the common case that the
  233. * certificate has not changed, this will allow the stored
  234. * certificate to be reused.
  235. */
  236. x509_put ( old_cert );
  237. return 0;
  238. }
  239. /** Certificate store settings applicator */
  240. struct settings_applicator certstore_applicator __settings_applicator = {
  241. .apply = certstore_apply_settings,
  242. };