├── .gitignore ├── .gitmodules ├── LICENSE ├── MANIFEST.in ├── Makefile ├── Makefile.config ├── README.md ├── config ├── config.py ├── python3.manifest.template └── sealing ├── lib └── swig │ ├── epid_group_id.i │ ├── except.h │ ├── except.i │ ├── mac.i │ ├── nonce.i │ ├── public_key.i │ ├── report.i │ ├── signature.i │ ├── spid.i │ ├── target_info.i │ └── util.h ├── scripts ├── python3-sgx ├── sgx-quoting-manager └── sgx-ra-manager ├── setup.py ├── setup.sh ├── sgx ├── Makefile ├── __init__.py ├── exceptions.py ├── socket_util.py ├── trusted │ ├── Makefile │ ├── __init__.py │ ├── attestation │ │ ├── Makefile │ │ ├── __init__.py │ │ ├── attestation.c │ │ ├── attestation.i.template │ │ ├── attestation.py │ │ └── call_tables.c │ └── sealing │ │ ├── Makefile │ │ ├── __init__.py │ │ ├── call_tables.c │ │ ├── sealing.c │ │ ├── sealing.i.template │ │ ├── sealing.py │ │ └── wrapper.py ├── untrusted │ ├── Makefile │ ├── __init__.py │ └── attestation │ │ ├── Makefile │ │ ├── __init__.py │ │ ├── attestation.c │ │ ├── attestation.i.template │ │ └── attestation.py └── util.py ├── tests ├── test_seal.py └── test_sealing_wrapper.py ├── uninstall.sh └── utils ├── create_manifest.py └── sign_manifest.py /.gitignore: -------------------------------------------------------------------------------- 1 | tags 2 | __pycache__ 3 | .idea 4 | *.manifest.sgx 5 | *.manifest 6 | *.token 7 | *.sig 8 | *.o 9 | .nfs* 10 | cmake-build-debug 11 | *.egg-info 12 | build 13 | dist 14 | *.sealed 15 | scratch 16 | sgx/config.py 17 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "linux-sgx"] 2 | path = linux-sgx 3 | url = https://github.com/adombeck/linux-sgx 4 | [submodule "graphene"] 5 | path = graphene 6 | url = https://github.com/adombeck/graphene 7 | [submodule "linux-sgx-driver"] 8 | path = linux-sgx-driver 9 | url = https://github.com/adombeck/linux-sgx-driver 10 | branch = master 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include sgx/trusted/*/_*.so 2 | include sgx/untrusted/*/_*.so 3 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | targets = all clean install 2 | 3 | include Makefile.config 4 | 5 | .PHONY: $(targets) 6 | $(targets): 7 | $(MAKE) -C sgx $@ 8 | 9 | -------------------------------------------------------------------------------- /Makefile.config: -------------------------------------------------------------------------------- 1 | SDK ?= /opt/python-sgx/sgxsdk 2 | SDK_GIT_DIR ?= $(realpath $(PROJECT_HOME)/linux-sgx) 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python SGX 2 | 3 | A Python interface to the SGX SDK for Linux. Uses [Graphene-SGX](https://github.com/oscarlab/graphene) to execute Python in an enclave. 4 | 5 | ## Project Status 6 | 7 | **Not actively maintained as of August 2017.** 8 | 9 | **Probably doesn't even build anymore.** 10 | 11 | **You don't want to use this if you are not prepared to maintain it yourself.** 12 | 13 | 14 | ## Submodules 15 | 16 | **The submodules are outdated. Don't file bug reports to the original authors if you use these forks.** 17 | 18 | 1. [Intel SGX Linux Driver](https://github.com/adombeck/linux-sgx-driver) 19 | 20 | 1. [Patched Intel SGX Linux SDK](https://github.com/adombeck/linux-sgx) 21 | 22 | 1. [Patched Graphene Library OS](https://github.com/adombeck/graphene) 23 | 24 | 25 | ## Installation 26 | 27 | 1. Initialize the submodules: 28 | 29 | git submodule update --init 30 | 31 | 2. Run the submodules' setup scripts: 32 | 33 | cd linux-sgx-driver && ./setup.sh && cd .. 34 | cd linux-sgx && ./setup.sh && cd .. 35 | cd graphene && ./setup.sh && cd .. 36 | 37 | 3. Adjust the paths in `config/config.py` (or keep the default paths) 38 | 4. Copy the public key of the [SGX Remote Attestation Challenger](https://github.com/adombeck/sgx-ra-challenger) to `/etc/python-sgx/challenger_public.key`, or delete this line from the `config/python3.manifest.template` if you don't want to use the remote attestation: 39 | 40 | sgx.trusted_files.challenger_public_key = file:$(CONFIG_DIR)/challenger_public.key 41 | 42 | 5. Run the setup script: 43 | 44 | ./setup.sh 45 | 46 | 47 | 6. Add your user to the `sgx` group: 48 | 49 | sudo usermod -a -G sgx $USER 50 | 51 | Note that this will only take effect after a new login. 52 | 53 | 54 | ## Uninstallation 55 | 56 | 1. Run the uninstallation script: 57 | 58 | sudo ./uninstall.sh 59 | 60 | 61 | ## Test sealing 62 | 63 | python3-sgx tests/test_seal.py seal 64 | python3-sgx tests/test_seal.py unseal 65 | python3-sgx tests/test_sealing_wrapper.py 66 | 67 | Note: You have to be in the repository's base directory to be able to run the tests. 68 | 69 | ## Test remote attestation 70 | Note: You need to install the [challenger package](https://github.com/adombeck/sgx-ra-challenger) on the challenging machine and store a copy of the challenger's public key in `/etc/python-sgx/challenger_public.key`. The [SGX Remote Attestation Challenger package](https://github.com/adombeck/sgx-ra-challenger) contains a script `generate_key_pair.py` which creates a key in the required format. 71 | 72 | 1. Run the Quoting Manager (handles communication with the Quoting Enclave): 73 | 74 | quoting-manager 75 | 76 | 2. Run the Remote Attestation Manager (handles communication with the challenger): 77 | 78 | sgx-ra-manager 79 | 80 | 3. Use the [SGX Remote Attestation Challenger](https://github.com/adombeck/sgx-ra-challenger) to connect to the Remote Attestation Manager: 81 | 82 | sgx-ra-challenger -c 127.0.0.1 6789 83 | 84 | -------------------------------------------------------------------------------- /config/config.py: -------------------------------------------------------------------------------- 1 | CONFIG_DIR = "/etc/python-sgx" 2 | DATA_DIR = "/var/lib/python-sgx" 3 | SEALED_DIR = DATA_DIR + "sealed/" 4 | 5 | GROUP = "sgx" 6 | 7 | # Currently only Ubuntu 16.04 is supported 8 | DISTRIBUTION = "Ubuntu" 9 | UBUNTU_VERSION = "16.04" 10 | LIBPROTOBUF_VERSION = "9" 11 | 12 | # Currently we require Python 3.5. 13 | PYTHON_VERSION = "3.5" 14 | 15 | # Use NIST curve P-384 (required by Intel's SGX SDK) 16 | OPENSSL_ELLIPTIC_CURVE = "secp384r1" 17 | 18 | -------------------------------------------------------------------------------- /config/python3.manifest.template: -------------------------------------------------------------------------------- 1 | #!$(PAL) 2 | 3 | loader.preload = file:$(RUNTIME)/libsysdb.so 4 | loader.exec = file:/usr/bin/python3 5 | loader.execname = python3 6 | loader.env.LD_LIBRARY_PATH = /graphene:/host:/usr/lib:/usr/lib/x86_64-linux-gnu 7 | loader.env.PATH = /usr/bin:/bin 8 | loader.env.USERNAME = 9 | loader.env.HOME = 10 | loader.env.PWD = 11 | loader.debug_type = none 12 | 13 | fs.mount.lib1.type = chroot 14 | fs.mount.lib1.path = /graphene 15 | fs.mount.lib1.uri = file:$(RUNTIME) 16 | 17 | fs.mount.lib2.type = chroot 18 | fs.mount.lib2.path = /host 19 | fs.mount.lib2.uri = file:/lib/x86_64-linux-gnu 20 | 21 | fs.mount.bin.type = chroot 22 | fs.mount.bin.path = /bin 23 | fs.mount.bin.uri = file:/bin 24 | 25 | fs.mount.usr.type = chroot 26 | fs.mount.usr.path = /usr 27 | fs.mount.usr.uri = file:/usr 28 | 29 | fs.mount.config.type = chroot 30 | fs.mount.config.path = /python-sgx 31 | fs.mount.config.uri = file:$(CONFIG_DIR) 32 | 33 | fs.mount.tests.type = chroot 34 | fs.mount.tests.path = /tests 35 | fs.mount.tests.uri = file:$(TESTS_DIR) 36 | 37 | fs.mount.sealed.type = chroot 38 | fs.mount.sealed.path = /sealed 39 | fs.mount.sealed.uri = file:$(DATA_DIR)/sealed 40 | 41 | net.allow_bind.1 = 127.0.0.1:6789 42 | 43 | sys.stack.size = 4M 44 | sys.brk.size = 32M 45 | glibc.heap_size = 16M 46 | 47 | sgx.trusted_files.ld = file:$(RUNTIME)/ld-linux-x86-64.so.2 48 | sgx.trusted_files.libc = file:$(RUNTIME)/libc.so.6 49 | sgx.trusted_files.libdl = file:$(RUNTIME)/libdl.so.2 50 | sgx.trusted_files.libm = file:$(RUNTIME)/libm.so.6 51 | sgx.trusted_files.libpthread = file:$(RUNTIME)/libpthread.so.0 52 | sgx.trusted_files.liburil = file:$(RUNTIME)/libutil.so.1 53 | sgx.trusted_files.libresolv = file:$(RUNTIME)/libresolv.so.2 54 | sgx.trusted_files.libz = file:/lib/x86_64-linux-gnu/libz.so.1 55 | sgx.trusted_files.libexpat = file:/lib/x86_64-linux-gnu/libexpat.so.1 56 | 57 | # For attestation 58 | sgx.trusted_files.sgx_ra_manager = file:/usr/local/bin/sgx-ra-manager 59 | sgx.trusted_files.libsgx_uae_service = file:/usr/lib/libsgx_uae_service.so 60 | sgx.trusted_files.libprotopuf = file:/usr/lib/x86_64-linux-gnu/libprotobuf.so.$(LIBPROTOBUF_VERSION) 61 | sgx.trusted_files.libstdcpp = file:/usr/lib/x86_64-linux-gnu/libstdc++.so.6 62 | sgx.trusted_files.libgcc_s = file:/lib/x86_64-linux-gnu/libgcc_s.so.1 63 | sgx.trusted_files.challenger_public_key = file:$(CONFIG_DIR)/challenger_public.key 64 | 65 | # For sealing 66 | sgx.trusted_files.sealing_manifest = file:$(CONFIG_DIR)/sealing 67 | sgx.allowed_files.sealed = file:$(DATA_DIR)/sealed 68 | 69 | # For the attested executable 70 | #sgx.trusted_files.ncat = file:/usr/bin/ncat 71 | #sgx.trusted_files.libpcap = file:/usr/lib/x86_64-linux-gnu/libpcap.so.0.8 72 | #sgx.trusted_files.liblua = file:/usr/lib/x86_64-linux-gnu/liblua5.2.so.0 73 | #sgx.trusted_files.date = file:/bin/date 74 | 75 | # Allow tests 76 | sgx.allowed_files.tests = file:$(TESTS_DIR) 77 | 78 | # For Python 3.5 79 | # You can uncomment the following lines to allow all python modules for testing 80 | #sgx.allowed_files.python_home = file:/usr/lib/python3.5 81 | #sgx.allowed_files.python_home2 = file:/usr/local/lib/python3.5 82 | 83 | sgx.trusted_files.0 = file:/usr/lib/python3.5/encodings/__init__.py 84 | sgx.trusted_files.1 = file:/usr/lib/python3.5/codecs.py 85 | sgx.trusted_files.2 = file:/usr/lib/python3.5/encodings/aliases.py 86 | sgx.trusted_files.3 = file:/usr/lib/python3.5/encodings/ascii.py 87 | sgx.trusted_files.4 = file:/usr/lib/python3.5/encodings/utf_8.py 88 | sgx.trusted_files.5 = file:/usr/lib/python3.5/encodings/latin_1.py 89 | sgx.trusted_files.6 = file:/usr/lib/python3.5/io.py 90 | sgx.trusted_files.7 = file:/usr/lib/python3.5/abc.py 91 | sgx.trusted_files.8 = file:/usr/lib/python3.5/_weakrefset.py 92 | sgx.trusted_files.9 = file:/usr/lib/python3.5/_bootlocale.py 93 | sgx.trusted_files.10 = file:/usr/lib/python3.5/site.py 94 | sgx.trusted_files.11 = file:/usr/lib/python3.5/os.py 95 | sgx.trusted_files.12 = file:/usr/lib/python3.5/stat.py 96 | sgx.trusted_files.13 = file:/usr/lib/python3.5/posixpath.py 97 | sgx.trusted_files.14 = file:/usr/lib/python3.5/genericpath.py 98 | sgx.trusted_files.15 = file:/usr/lib/python3.5/_collections_abc.py 99 | sgx.trusted_files.16 = file:/usr/lib/python3.5/_sitebuiltins.py 100 | sgx.trusted_files.17 = file:/usr/lib/python3.5/sysconfig.py 101 | sgx.trusted_files.18 = file:/usr/lib/python3.5/_sysconfigdata.py 102 | sgx.trusted_files.19 = file:/usr/lib/python3.5/plat-x86_64-linux-gnu/_sysconfigdata_m.py 103 | sgx.trusted_files.20 = file:/usr/lib/python3.5/sitecustomize.py 104 | sgx.trusted_files.21 = file:/usr/lib/python3.5/encodings/cp437.py 105 | sgx.trusted_files.22 = file:/usr/lib/python3.5/socketserver.py 106 | sgx.trusted_files.23 = file:/usr/lib/python3.5/socket.py 107 | sgx.trusted_files.24 = file:/usr/lib/python3.5/selectors.py 108 | sgx.trusted_files.25 = file:/usr/lib/python3.5/collections/__init__.py 109 | sgx.trusted_files.26 = file:/usr/lib/python3.5/operator.py 110 | sgx.trusted_files.27 = file:/usr/lib/python3.5/keyword.py 111 | sgx.trusted_files.28 = file:/usr/lib/python3.5/heapq.py 112 | sgx.trusted_files.29 = file:/usr/lib/python3.5/reprlib.py 113 | sgx.trusted_files.30 = file:/usr/lib/python3.5/enum.py 114 | sgx.trusted_files.31 = file:/usr/lib/python3.5/types.py 115 | sgx.trusted_files.32 = file:/usr/lib/python3.5/functools.py 116 | sgx.trusted_files.33 = file:/usr/lib/python3.5/weakref.py 117 | sgx.trusted_files.34 = file:/usr/lib/python3.5/collections/abc.py 118 | sgx.trusted_files.35 = file:/usr/lib/python3.5/threading.py 119 | sgx.trusted_files.36 = file:/usr/lib/python3.5/traceback.py 120 | sgx.trusted_files.37 = file:/usr/lib/python3.5/linecache.py 121 | sgx.trusted_files.38 = file:/usr/lib/python3.5/tokenize.py 122 | sgx.trusted_files.39 = file:/usr/lib/python3.5/re.py 123 | sgx.trusted_files.40 = file:/usr/lib/python3.5/sre_compile.py 124 | sgx.trusted_files.41 = file:/usr/lib/python3.5/sre_parse.py 125 | sgx.trusted_files.42 = file:/usr/lib/python3.5/sre_constants.py 126 | sgx.trusted_files.43 = file:/usr/lib/python3.5/copyreg.py 127 | sgx.trusted_files.44 = file:/usr/lib/python3.5/token.py 128 | sgx.trusted_files.45 = file:/usr/lib/python3.5/logging/__init__.py 129 | sgx.trusted_files.46 = file:/usr/lib/python3.5/warnings.py 130 | sgx.trusted_files.47 = file:/usr/lib/python3.5/string.py 131 | sgx.trusted_files.48 = file:/usr/lib/python3.5/argparse.py 132 | sgx.trusted_files.49 = file:/usr/lib/python3.5/copy.py 133 | sgx.trusted_files.50 = file:/usr/lib/python3.5/textwrap.py 134 | sgx.trusted_files.51 = file:/usr/lib/python3.5/gettext.py 135 | sgx.trusted_files.52 = file:/usr/lib/python3.5/locale.py 136 | sgx.trusted_files.53 = file:/usr/lib/python3.5/struct.py 137 | sgx.trusted_files.54 = file:/usr/lib/python3.5/json/__init__.py 138 | sgx.trusted_files.55 = file:/usr/lib/python3.5/json/decoder.py 139 | sgx.trusted_files.56 = file:/usr/lib/python3.5/json/scanner.py 140 | sgx.trusted_files.57 = file:/usr/lib/python3.5/json/encoder.py 141 | sgx.trusted_files.58 = file:/usr/lib/python3.5/contextlib.py 142 | sgx.trusted_files.59 = file:/usr/lib/python3.5/importlib/__init__.py 143 | sgx.trusted_files.60 = file:/usr/local/lib/python3.5/dist-packages/easy-install.pth 144 | 145 | # For python-sgx 146 | sgx.trusted_files.pysgx0 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/__init__.py 147 | sgx.trusted_files.pysgx1 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/util.py 148 | sgx.trusted_files.pysgx2 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/exceptions.py 149 | sgx.trusted_files.pysgx3 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/config.py 150 | sgx.trusted_files.pysgx4 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/socket_util.py 151 | sgx.trusted_files.pysgx5 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/trusted/__init__.py 152 | sgx.trusted_files.pysgx6 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/trusted/attestation/__init__.py 153 | sgx.trusted_files.pysgx7 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/trusted/attestation/attestation.py 154 | sgx.trusted_files.pysgx8 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/trusted/attestation/attestation_swig.py 155 | sgx.trusted_files.pysgx9 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/trusted/attestation/_attestation_swig.so 156 | sgx.trusted_files.pysgx10 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/trusted/sealing/__init__.py 157 | sgx.trusted_files.pysgx11 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/trusted/sealing/sealing.py 158 | sgx.trusted_files.pysgx12 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/trusted/sealing/sealing_swig.py 159 | sgx.trusted_files.pysgx13 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/trusted/sealing/_sealing_swig.so 160 | sgx.trusted_files.pysgx14 = file:/usr/local/lib/python3.5/dist-packages/sgx-0.1a0-py3.5.egg/sgx/trusted/sealing/wrapper.py 161 | -------------------------------------------------------------------------------- /config/sealing: -------------------------------------------------------------------------------- 1 | # Type ([f]ile / [d]irectory) Sealed Path Unsealed Path 2 | f /sealed/test /tests/unsealed 3 | -------------------------------------------------------------------------------- /lib/swig/epid_group_id.i: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // map sgx_epid_group_id_t to Python bytes 3 | // ---------------------------------------------------------------------------- 4 | 5 | %typemap(in) sgx_epid_group_id_t epid_group_id { 6 | assert(sizeof(sgx_epid_group_id_t) == 4); 7 | 8 | if (!PyBytes_Check($input)) { 9 | PyErr_SetString(PyExc_TypeError, "Expected a bytes parameter"); 10 | SWIG_fail; 11 | } 12 | 13 | if (PyObject_Length($input) != sizeof(sgx_epid_group_id_t)) { 14 | PyErr_SetString(PyExc_ValueError, "Expected a bytes parameter with 4 elements"); 15 | SWIG_fail; 16 | } 17 | 18 | memcpy(&$1, PyBytes_AsString($input), sizeof(sgx_epid_group_id_t)); 19 | 20 | // Reverse byte order to little-endian 21 | reverse_byte_array((uint8_t*) &$1, 4); 22 | } 23 | 24 | // Define typemaps for p_epid_group_id 25 | 26 | // This typemap suppresses requiring the parameter as an input. 27 | %typemap(in,numinputs=0) sgx_epid_group_id_t* p_epid_group_id (sgx_epid_group_id_t temp) { 28 | memset(&temp, 0, sizeof(sgx_epid_group_id_t)); 29 | $1 = &temp; 30 | } 31 | 32 | %typemap(argout) sgx_epid_group_id_t* p_epid_group_id { 33 | uint8_t tmp[4]; 34 | memcpy(tmp, $1, 4); 35 | 36 | // Reverse byte order to big-endian 37 | reverse_byte_array(tmp, 4); 38 | 39 | PyObject* pybytes = PyBytes_FromStringAndSize((char*) tmp, sizeof(sgx_epid_group_id_t)); 40 | $result = SWIG_Python_AppendOutput($result, pybytes); 41 | } 42 | -------------------------------------------------------------------------------- /lib/swig/except.h: -------------------------------------------------------------------------------- 1 | extern void clear_exception(); 2 | extern char* check_exception(); 3 | -------------------------------------------------------------------------------- /lib/swig/except.i: -------------------------------------------------------------------------------- 1 | %exception { 2 | char *err; 3 | clear_exception(); 4 | $action 5 | if ((err = check_exception())) { 6 | PyErr_SetString(PyExc_Exception, err); 7 | SWIG_fail; 8 | } 9 | } -------------------------------------------------------------------------------- /lib/swig/mac.i: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // map sgx_mac_t to Python bytes 3 | // ---------------------------------------------------------------------------- 4 | 5 | %typemap(in) sgx_mac_t mac { 6 | assert(sizeof(sgx_mac_t) == 16); 7 | 8 | if (!PyBytes_Check($input)) { 9 | PyErr_SetString(PyExc_TypeError, "Expected a bytes parameter"); 10 | SWIG_fail; 11 | } 12 | 13 | if (PyObject_Length($input) != sizeof(sgx_mac_t)) { 14 | PyErr_SetString(PyExc_ValueError, "MAC must be 16 bytes"); 15 | SWIG_fail; 16 | } 17 | 18 | memcpy($1, PyBytes_AsString($input), 16); 19 | } 20 | 21 | 22 | // Define typemaps for p_mac 23 | 24 | // This typemap suppresses requiring the parameter as an input. 25 | %typemap(in,numinputs=0) sgx_mac_t* p_mac (sgx_mac_t temp) { 26 | memset(&temp, 0, sizeof(sgx_mac_t)); 27 | $1 = &temp; 28 | } 29 | 30 | %typemap(argout) sgx_mac_t* p_mac { 31 | PyObject* pybytes = PyBytes_FromStringAndSize((char*) $1, sizeof(sgx_mac_t)); 32 | $result = SWIG_Python_AppendOutput($result, pybytes); 33 | } 34 | -------------------------------------------------------------------------------- /lib/swig/nonce.i: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // map sgx_quote_nonce_t to Python bytes 3 | // ---------------------------------------------------------------------------- 4 | 5 | %typemap(in) sgx_quote_nonce_t nonce { 6 | assert(sizeof(sgx_quote_nonce_t) == 16); 7 | 8 | if (!PyBytes_Check($input)) { 9 | PyErr_SetString(PyExc_TypeError, "Expected a bytes parameter"); 10 | SWIG_fail; 11 | } 12 | 13 | if (PyObject_Length($input) != sizeof(sgx_quote_nonce_t)) { 14 | PyErr_SetString(PyExc_ValueError, "Expected a bytes parameter with 16 elements"); 15 | SWIG_fail; 16 | } 17 | 18 | memcpy(&$1, PyBytes_AsString($input), sizeof(sgx_quote_nonce_t)); 19 | } 20 | 21 | 22 | // Define typemaps for p_nonce 23 | 24 | // This typemap suppresses requiring the parameter as an input. 25 | %typemap(in,numinputs=0) sgx_quote_nonce_t* p_nonce (sgx_quote_nonce_t temp) { 26 | memset(&temp, 0, sizeof(sgx_quote_nonce_t)); 27 | $1 = &temp; 28 | } 29 | 30 | %typemap(argout) sgx_quote_nonce_t* p_nonce { 31 | PyObject* pybytes = PyBytes_FromStringAndSize((char*) $1, sizeof(sgx_quote_nonce_t)); 32 | $result = SWIG_Python_AppendOutput($result, pybytes); 33 | } 34 | -------------------------------------------------------------------------------- /lib/swig/public_key.i: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // map sgx_ec256_public_t to Python bytes 3 | // ---------------------------------------------------------------------------- 4 | 5 | // Define typemaps for sgx_ec256_public_t 6 | 7 | %typemap(in) sgx_ec256_public_t public_key { 8 | assert(sizeof(sgx_ec256_public_t) == 64); 9 | 10 | if (!PyBytes_Check($input)) { 11 | PyErr_SetString(PyExc_TypeError, "Expected a bytes parameter"); 12 | SWIG_fail; 13 | } 14 | 15 | if (PyObject_Length($input) != sizeof(sgx_ec256_public_t)) { 16 | PyErr_SetString(PyExc_ValueError, "Expected a bytes parameter with 64 elements"); 17 | SWIG_fail; 18 | } 19 | 20 | uint8_t* bytes = (uint8_t*) PyBytes_AsString($input); 21 | 22 | memcpy(&$1, bytes, sizeof(sgx_ec256_public_t)); 23 | 24 | // Reverse byte order to little-endian 25 | reverse_byte_array((uint8_t*) &$1, 32); 26 | reverse_byte_array((uint8_t*) &$1 + 32, 32); 27 | } 28 | 29 | 30 | // Define typemaps for p_public_key 31 | 32 | // This typemap suppresses requiring the parameter as an input. 33 | %typemap(in,numinputs=0) sgx_ec256_public_t* p_public_key (sgx_ec256_public_t temp) { 34 | memset(&temp, 0, sizeof(sgx_ec256_public_t)); 35 | $1 = &temp; 36 | } 37 | 38 | %typemap(argout) sgx_ec256_public_t* p_public_key { 39 | uint8_t tmp[64]; 40 | memcpy(&tmp[0], $1->gx, 32); 41 | memcpy(&tmp[32], $1->gy, 32); 42 | 43 | // Reverse byte order to big-endian 44 | reverse_byte_array(&tmp[0], 32); 45 | reverse_byte_array(&tmp[32], 32); 46 | 47 | PyObject* pybytes = PyBytes_FromStringAndSize((char*) tmp, 64); 48 | $result = SWIG_Python_AppendOutput($result, pybytes); 49 | } 50 | -------------------------------------------------------------------------------- /lib/swig/report.i: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // map sgx_report_t to Python bytes 3 | // ---------------------------------------------------------------------------- 4 | 5 | %typemap(in) sgx_report_t report { 6 | assert(sizeof(sgx_report_t) == 432); 7 | 8 | if (!PyBytes_Check($input)) { 9 | PyErr_SetString(PyExc_TypeError, "Expected a bytes parameter"); 10 | SWIG_fail; 11 | } 12 | 13 | if (PyObject_Length($input) != sizeof(sgx_report_t)) { 14 | PyErr_SetString(PyExc_ValueError, "Expected a bytes parameter with 432 elements"); 15 | SWIG_fail; 16 | } 17 | 18 | memcpy(&$1, PyBytes_AsString($input), sizeof(sgx_report_t)); 19 | } 20 | 21 | 22 | // Define typemaps for p_report 23 | 24 | // This typemap suppresses requiring the parameter as an input. 25 | %typemap(in,numinputs=0) sgx_report_t* p_report (sgx_report_t temp) { 26 | memset(&temp, 0, sizeof(sgx_report_t)); 27 | $1 = &temp; 28 | } 29 | 30 | %typemap(argout) sgx_report_t* p_report { 31 | PyObject* pybytes = PyBytes_FromStringAndSize((char*) $1, sizeof(sgx_report_t)); 32 | $result = SWIG_Python_AppendOutput($result, pybytes); 33 | } 34 | -------------------------------------------------------------------------------- /lib/swig/signature.i: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // map sgx_ec256_signature_t to Python bytes 3 | // ---------------------------------------------------------------------------- 4 | 5 | %typemap(in) sgx_ec256_signature_t signature { 6 | assert(sizeof(sgx_ec256_signature_t) == 64); 7 | 8 | if (!PyBytes_Check($input)) { 9 | PyErr_SetString(PyExc_TypeError, "Expected a bytes parameter"); 10 | SWIG_fail; 11 | } 12 | 13 | if (PyObject_Length($input) != sizeof(sgx_ec256_signature_t)) { 14 | PyErr_SetString(PyExc_ValueError, "Signature must be 64 bytes"); 15 | SWIG_fail; 16 | } 17 | 18 | uint8_t* bytes = (uint8_t*) PyBytes_AsString($input); 19 | 20 | memcpy(&$1, bytes, sizeof(sgx_ec256_signature_t)); 21 | 22 | // Reverse byte order to little-endian 23 | reverse_byte_array((uint8_t*) &$1, 32); 24 | reverse_byte_array((uint8_t*) &$1 + 32, 32); 25 | } 26 | -------------------------------------------------------------------------------- /lib/swig/spid.i: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // map spid_t to Python bytes 3 | // ---------------------------------------------------------------------------- 4 | 5 | %typemap(in) sgx_spid_t spid { 6 | assert(sizeof(sgx_spid_t) == 16); 7 | 8 | if (!PyBytes_Check($input)) { 9 | PyErr_SetString(PyExc_TypeError, "Expected a bytes parameter"); 10 | SWIG_fail; 11 | } 12 | 13 | if (PyObject_Length($input) != sizeof(sgx_spid_t)) { 14 | PyErr_SetString(PyExc_ValueError, "Expected a bytes parameter with 16 elements"); 15 | SWIG_fail; 16 | } 17 | 18 | memcpy(&$1, PyBytes_AsString($input), sizeof(sgx_spid_t)); 19 | } 20 | -------------------------------------------------------------------------------- /lib/swig/target_info.i: -------------------------------------------------------------------------------- 1 | // ---------------------------------------------------------------------------- 2 | // map sgx_target_info_t to Python bytes 3 | // ---------------------------------------------------------------------------- 4 | 5 | %typemap(in) sgx_target_info_t target_info { 6 | assert(sizeof(sgx_target_info_t) == 512); 7 | 8 | if (!PyBytes_Check($input)) { 9 | PyErr_SetString(PyExc_TypeError, "Expected a bytes parameter"); 10 | SWIG_fail; 11 | } 12 | 13 | if (PyObject_Length($input) != sizeof(sgx_target_info_t)) { 14 | PyErr_SetString(PyExc_ValueError, "Expected a bytes parameter with 512 elements"); 15 | SWIG_fail; 16 | } 17 | 18 | memcpy(&$1, PyBytes_AsString($input), sizeof(sgx_target_info_t)); 19 | } 20 | 21 | 22 | // Define typemaps for p_target_info 23 | 24 | // This typemap suppresses requiring the parameter as an input. 25 | %typemap(in,numinputs=0) sgx_target_info_t* p_target_info (sgx_target_info_t temp) { 26 | memset(&temp, 0, sizeof(sgx_target_info_t)); 27 | $1 = &temp; 28 | } 29 | 30 | %typemap(argout) sgx_target_info_t* p_target_info { 31 | PyObject* pybytes = PyBytes_FromStringAndSize((char*) $1, sizeof(sgx_target_info_t)); 32 | $result = SWIG_Python_AppendOutput($result, pybytes); 33 | } 34 | -------------------------------------------------------------------------------- /lib/swig/util.h: -------------------------------------------------------------------------------- 1 | static void reverse_byte_array(uint8_t *array, size_t size) 2 | { 3 | size_t i = 0; 4 | for(i = 0; i < size / 2; i++) 5 | { 6 | uint8_t temp = array[i]; 7 | array[i] = array[size - i - 1]; 8 | array[size - i - 1] = temp; 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /scripts/python3-sgx: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | cd /var/lib/python-sgx/ 4 | /usr/bin/env graphene-pal python3.manifest.sgx -B "$@" 5 | -------------------------------------------------------------------------------- /scripts/sgx-quoting-manager: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3.5 2 | 3 | import socketserver 4 | import socket 5 | import logging 6 | import argparse 7 | 8 | from sgx.util import check_msg 9 | from sgx.socket_util import send_msg, receive_msg 10 | from sgx.untrusted.attestation import get_extended_epid_group_id 11 | from sgx.untrusted.attestation import initialize_quoting_enclave 12 | from sgx.untrusted.attestation import get_quote_size 13 | from sgx.untrusted.attestation import get_quote 14 | 15 | 16 | manager = None 17 | 18 | 19 | class RequestHandler(socketserver.StreamRequestHandler): 20 | 21 | _request_num = 0 22 | 23 | def __init__(self, *args, **kwargs): 24 | self._request_num += 1 25 | self.id = self._request_num 26 | self.logger = logging.getLogger("Request#%s" % self.id) 27 | super(RequestHandler, self).__init__(*args, **kwargs) 28 | 29 | def handle(self): 30 | client_socket = self.request 31 | 32 | msg = receive_msg(client_socket) 33 | check_msg(msg, dict) 34 | 35 | request = msg["request"] 36 | self.logger.info("Got request %r", request) 37 | 38 | if request == "GET extended_epid_gid": 39 | extended_epid_gid = get_extended_epid_group_id() 40 | self.send_extended_epid_gid(client_socket, extended_epid_gid) 41 | 42 | if request == "INIT QE": 43 | qe_target_info, epid_gid = initialize_quoting_enclave() 44 | self.send_qe_init_response(client_socket, qe_target_info, epid_gid) 45 | 46 | if request == "GET QUOTE": 47 | report = bytes.fromhex(msg["report"]) 48 | quote_type = msg["quote_type"] 49 | spid = bytes.fromhex(msg["spid"]) 50 | nonce = bytes.fromhex(msg["nonce"]) 51 | revocation_list = bytes.fromhex(msg["revocation_list"]) 52 | 53 | self.logger.warning("revocation list: %r", revocation_list) 54 | 55 | quote_size = get_quote_size(revocation_list) 56 | quote, qe_report = get_quote( 57 | report, 58 | quote_type, 59 | spid, 60 | nonce, 61 | revocation_list, 62 | quote_size 63 | ) 64 | 65 | self.send_quote(client_socket, quote, qe_report) 66 | 67 | def send_extended_epid_gid(self, client_socket: socket.socket, extended_epid_gid): 68 | msg = {"extended_epid_gid": extended_epid_gid} 69 | self.logger.info("Sending extended EPID group ID (message size: %r)", len(msg)) 70 | send_msg(client_socket, msg) 71 | self.logger.info("Extended EPID group ID sent") 72 | 73 | def send_qe_init_response(self, client_socket: socket.socket, qe_target_info, epid_gid): 74 | msg = {"qe_target_info": qe_target_info.hex(), "epid_gid": epid_gid.hex()} 75 | self.logger.info("Sending quoting enclave init response (message size: %r)", len(msg)) 76 | send_msg(client_socket, msg) 77 | self.logger.info("Quoting enclave init response sent") 78 | 79 | def receive_report(self, client_socket: socket.socket): 80 | self.logger.info("Waiting for report") 81 | msg = receive_msg(client_socket) 82 | check_msg(msg, dict, 1) 83 | return bytes.fromhex(msg["report"]) 84 | 85 | def send_quote(self, client_socket: socket.socket, quote, qe_report): 86 | msg = {"quote": quote.hex(), "qe_report": qe_report.hex()} 87 | self.logger.info("Sending quote") 88 | send_msg(client_socket, msg) 89 | self.logger.info("Quote sent") 90 | 91 | 92 | class Manager(object): 93 | def __init__(self, interface, port): 94 | self.interface = interface 95 | self.port = port 96 | self.server = socketserver.TCPServer((interface, port), RequestHandler) 97 | self.logger = logging.getLogger("Manager") 98 | 99 | def __enter__(self): 100 | return self 101 | 102 | def __exit__(self, exc_type, exc_val, exc_tb): 103 | self.logger.info("Stopping server") 104 | self.server.server_close() 105 | 106 | def run(self): 107 | self.logger.info("Listening for requests on %s:%s", self.interface, self.port) 108 | self.server.serve_forever() 109 | 110 | 111 | def parse_args(): 112 | parser = argparse.ArgumentParser() 113 | parser.add_argument("--interface", "-i", default="127.0.0.1") 114 | parser.add_argument("--port", "-p", type=int, default=9876) 115 | parser.add_argument("--verbose", "-v", action="store_true") 116 | return parser.parse_args() 117 | 118 | 119 | def init(args): 120 | if args.verbose: 121 | logging.basicConfig(level=logging.DEBUG) 122 | else: 123 | logging.basicConfig(level=logging.INFO) 124 | 125 | 126 | def main(): 127 | args = parse_args() 128 | init(args) 129 | 130 | global manager 131 | with Manager(args.interface, args.port) as manager: 132 | manager.run() 133 | 134 | 135 | if __name__ == "__main__": 136 | main() 137 | -------------------------------------------------------------------------------- /scripts/sgx-ra-manager: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3-sgx 2 | 3 | import socketserver 4 | import socket 5 | import logging 6 | import argparse 7 | import struct 8 | import os 9 | 10 | from sgx.util import get_challenger_public_key, check_msg 11 | from sgx.socket_util import receive_msg, send_msg 12 | 13 | # noinspection PyBroadException 14 | try: 15 | from sgx.trusted.attestation import remote_attestation_context 16 | from sgx.trusted.attestation import get_new_public_key 17 | from sgx.trusted.attestation import process_msg2 18 | from sgx.trusted.attestation import get_msg3 19 | except: 20 | logging.exception("") 21 | exit() 22 | 23 | 24 | CHALLENGER_LT_PUBLIC_KEY = get_challenger_public_key() 25 | 26 | logging_handlers = list() 27 | 28 | manager = None 29 | qm_port = None 30 | 31 | application = None 32 | arguments = None 33 | run_in_parallel = False 34 | 35 | 36 | class RequestHandler(socketserver.StreamRequestHandler): 37 | 38 | _request_num = 0 39 | 40 | def __init__(self, *args, **kwargs): 41 | self._request_num += 1 42 | self.id = self._request_num 43 | self.logger = logging.getLogger("Request#%s" % self.id) 44 | 45 | self.public_key = None 46 | self.challenger_public_key = None 47 | self.spid = None 48 | self.quote_type = None 49 | self.kdf_id = None 50 | self.key_signature = None 51 | self.msg2_mac = None 52 | self.revocation_list = None 53 | self.qe_target_info = None 54 | self.report = None 55 | self.nonce = None 56 | self.quote = None 57 | self.qe_report = None 58 | self.msg3_mac = None 59 | self.ps_sec_prop = None 60 | self.verification_result = None 61 | 62 | super(RequestHandler, self).__init__(*args, **kwargs) 63 | 64 | def handle(self): 65 | self.logger.info("Got request") 66 | 67 | client_socket = self.request 68 | 69 | request = client_socket.recv(1024) 70 | 71 | if request == b"attest": 72 | self.perform_attestation(client_socket) 73 | 74 | def perform_attestation(self, client_socket: socket.socket): 75 | self.send_msg0(client_socket) 76 | 77 | with remote_attestation_context(challenger_public_key=CHALLENGER_LT_PUBLIC_KEY, 78 | use_pse=0) as ra_context: 79 | 80 | self.create_msg1(ra_context) 81 | self.send_msg1(client_socket) 82 | 83 | self.receive_msg2(client_socket) 84 | self.process_msg2(ra_context) 85 | 86 | self.create_msg3(ra_context) 87 | self.send_msg3(client_socket) 88 | 89 | self.receive_msg4(client_socket) 90 | 91 | if self.verification_result and application and not run_in_parallel: 92 | run_application(application, arguments) 93 | 94 | # ------------------------------------------------------------------------- 95 | # MSG0 96 | # ------------------------------------------------------------------------- 97 | 98 | def send_msg0(self, client_socket: socket.socket): 99 | self.logger.info("Preparing to send msg0") 100 | extended_epid_gid = self.get_extended_epid_gid() 101 | msg0 = {"extended_epid_gid": extended_epid_gid} 102 | self.logger.info("Sending msg0") 103 | send_msg(client_socket, msg0) 104 | 105 | def get_extended_epid_gid(self): 106 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as ura_socket: 107 | ura_socket.connect(("127.0.0.1", qm_port)) 108 | self.send_extended_epid_gid_request(ura_socket) 109 | return self.receive_extended_epid_gid(ura_socket) 110 | 111 | def send_extended_epid_gid_request(self, ura_socket: socket.socket): 112 | self.logger.info("Sending extended EPID group ID request") 113 | msg = {"request": "GET extended_epid_gid"} 114 | send_msg(ura_socket, msg) 115 | self.logger.info("Extended EPID group ID request sent") 116 | 117 | def receive_extended_epid_gid(self, ura_socket: socket.socket()): 118 | self.logger.info("Waiting for extended EPID group ID") 119 | msg = receive_msg(ura_socket) 120 | check_msg(msg, dict, 1) 121 | return msg["extended_epid_gid"] 122 | 123 | # ------------------------------------------------------------------------- 124 | # MSG1 125 | # ------------------------------------------------------------------------- 126 | 127 | def initialize_quoting_enclave(self): 128 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as ura_socket: 129 | ura_socket.connect(("127.0.0.1", qm_port)) 130 | self.send_qe_init_request(ura_socket) 131 | return self.receive_qe_init_response(ura_socket) 132 | 133 | def send_qe_init_request(self, ura_socket: socket.socket): 134 | self.logger.info("Sending quoting enclave initialization request") 135 | msg = {"request": "INIT QE"} 136 | send_msg(ura_socket, msg) 137 | self.logger.info("Quoting enclave initialization request sent") 138 | 139 | def receive_qe_init_response(self, ura_socket: socket.socket()): 140 | self.logger.info("Waiting for quoting enclave target info") 141 | msg = receive_msg(ura_socket) 142 | check_msg(msg, dict, 2) 143 | return bytes.fromhex(msg["qe_target_info"]), bytes.fromhex(msg["epid_gid"]) 144 | 145 | def create_msg1(self, ra_context): 146 | self.public_key = get_new_public_key(ra_context) 147 | self.qe_target_info, self.epid_gid = self.initialize_quoting_enclave() 148 | 149 | def send_msg1(self, client_socket: socket.socket): 150 | self.logger.info("Preparing to send msg1") 151 | msg1 = {"public_key": self.public_key.hex(), 152 | "epid_gid": self.epid_gid.hex()} 153 | self.logger.info("Sending msg1") 154 | send_msg(client_socket, msg1) 155 | 156 | # ------------------------------------------------------------------------- 157 | # MSG2 158 | # ------------------------------------------------------------------------- 159 | 160 | def receive_msg2(self, client_socket: socket.socket): 161 | self.logger.info("Waiting for msg2") 162 | 163 | msg2 = receive_msg(client_socket) 164 | check_msg(msg2, dict, 7) 165 | 166 | self.challenger_public_key = bytes.fromhex(msg2["public_key"]) 167 | self.spid = bytes.fromhex(msg2["spid"]) 168 | self.quote_type = struct.unpack("H", bytes.fromhex(msg2["quote_type"]))[0] 169 | self.kdf_id = struct.unpack("H", bytes.fromhex(msg2["kdf_id"]))[0] 170 | self.key_signature = bytes.fromhex(msg2["key_signature"]) 171 | self.msg2_mac = bytes.fromhex(msg2["mac"]) 172 | self.revocation_list = bytes.fromhex(msg2["revocation_list"]) 173 | 174 | self.logger.info("Msg2 received") 175 | 176 | def process_msg2(self, ra_context): 177 | self.logger.info("Processing msg2") 178 | 179 | self.report, self.nonce = process_msg2( 180 | ra_context, 181 | self.qe_target_info, 182 | self.challenger_public_key, 183 | self.spid, 184 | self.quote_type, 185 | self.kdf_id, 186 | self.key_signature, 187 | self.msg2_mac, 188 | self.revocation_list 189 | ) 190 | 191 | self.logger.info("Msg2 processed") 192 | 193 | # ------------------------------------------------------------------------- 194 | # MSG3 195 | # ------------------------------------------------------------------------- 196 | 197 | def create_msg3(self, ra_context): 198 | self.logger.info("Creating msg3") 199 | 200 | self.quote, self.qe_report = self.get_quote() 201 | 202 | self.msg3_mac, self.ps_sec_prop = get_msg3( 203 | ra_context, 204 | self.quote, 205 | self.qe_report 206 | ) 207 | 208 | self.logger.info("Msg3 created") 209 | 210 | def get_quote(self): 211 | with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as ura_socket: 212 | ura_socket.connect(("127.0.0.1", qm_port)) 213 | self.send_quote_request(ura_socket) 214 | return self.receive_quote(ura_socket) 215 | 216 | def send_quote_request(self, ura_socket: socket.socket): 217 | msg = { 218 | "request": "GET QUOTE", 219 | "report": self.report.hex(), 220 | "quote_type": self.quote_type, 221 | "spid": self.spid.hex(), 222 | "nonce": self.nonce.hex(), 223 | "revocation_list": self.revocation_list.hex() 224 | } 225 | 226 | self.logger.info("Sending quote request (message size: %s)", len(msg)) 227 | send_msg(ura_socket, msg) 228 | self.logger.info("Quote request sent") 229 | 230 | def receive_quote(self, ura_socket: socket.socket()): 231 | self.logger.info("Waiting for quote") 232 | msg = receive_msg(ura_socket) 233 | check_msg(msg, dict, 2) 234 | self.logger.info("Quote received") 235 | return bytes.fromhex(msg["quote"]), bytes.fromhex(msg["qe_report"]) 236 | 237 | def send_msg3(self, client_socket: socket.socket): 238 | self.logger.info("Preparing to send msg3") 239 | 240 | msg3 = { 241 | "quote": self.quote.hex(), 242 | "mac": self.msg3_mac.hex(), 243 | "ps_sec_prop": self.ps_sec_prop.hex() 244 | } 245 | 246 | self.logger.debug("Sending msg3 %r", msg3) 247 | send_msg(client_socket, msg3) 248 | self.logger.info("Msg3 sent") 249 | 250 | # ------------------------------------------------------------------------- 251 | # MSG4 252 | # ------------------------------------------------------------------------- 253 | 254 | def receive_msg4(self, client_socket: socket.socket): 255 | self.logger.info("Waiting for msg4") 256 | msg4 = receive_msg(client_socket) 257 | check_msg(msg4, dict, 1) 258 | self.logger.info("Msg4 received") 259 | self.verification_result = msg4["verification_result"] 260 | 261 | 262 | class Manager(object): 263 | def __init__(self, interface, port): 264 | self.interface = interface 265 | self.port = port 266 | self.server = socketserver.TCPServer((interface, port), RequestHandler) 267 | self.server.handle_error = self.handle_error 268 | self.logger = logging.getLogger("Manager") 269 | 270 | def __enter__(self): 271 | return self 272 | 273 | def __exit__(self, exc_type, exc_val, exc_tb): 274 | self.logger.info("Stopping server") 275 | self.server.server_close() 276 | 277 | # noinspection PyUnusedLocal 278 | def handle_error(self, request, client_address): 279 | self.logger.exception("Exception while handling request %r" % request) 280 | 281 | def run(self): 282 | self.logger.info("Listening for requests on %s:%s", self.interface, self.port) 283 | self.server.serve_forever() 284 | 285 | 286 | def run_application_in_new_process(application, argv): 287 | # XXX: Fails with "bug() bookkeep/shim_vma.c:246" 288 | argv = [application] + argv 289 | logging.info("Executing %r", " ".join(argv)) 290 | 291 | import subprocess 292 | subprocess.Popen(argv) 293 | 294 | 295 | def run_application(application, argv): 296 | os.execvp(application, argv) 297 | 298 | 299 | def parse_args(): 300 | parser = argparse.ArgumentParser() 301 | parser.add_argument("--interface", "-i", default="127.0.0.1") 302 | parser.add_argument("--port", "-p", type=int, default=6789) 303 | parser.add_argument("--qm-port", type=int, default=9876, help="Port of the quoting manager") 304 | parser.add_argument("--parallel", action="store_true", help="Execute the target executable in parallel with the remote attestation manager") 305 | parser.add_argument("--verbose", "-v", action="store_true") 306 | parser.add_argument("EXECUTABLE", nargs="?", help="Path to the remote attestation's target executable") 307 | parser.add_argument("ARGUMENTS", nargs=argparse.REMAINDER, help="Arguments passed to the target executable") 308 | return parser.parse_args() 309 | 310 | 311 | def init(args): 312 | if args.verbose: 313 | logging.basicConfig(level=logging.DEBUG) 314 | else: 315 | logging.basicConfig(level=logging.INFO) 316 | 317 | global qm_port 318 | qm_port = args.qm_port 319 | 320 | 321 | def main(): 322 | args = parse_args() 323 | init(args) 324 | 325 | global application, arguments 326 | application = args.EXECUTABLE 327 | arguments = args.ARGUMENTS 328 | run_in_parallel = args.parallel 329 | 330 | if run_in_parallel and args.EXECUTABLE: 331 | run_application_in_new_process(args.EXECUTABLE, args.ARGUMENTS) 332 | 333 | global manager 334 | with Manager(args.interface, args.port) as manager: 335 | manager.run() 336 | 337 | os.wait() 338 | 339 | 340 | if __name__ == "__main__": 341 | # noinspection PyBroadException 342 | try: 343 | main() 344 | except: 345 | # Without this graphene's pal does not return on exceptions 346 | logging.exception("") 347 | exit() 348 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3.5 2 | """A setuptools based setup module. 3 | See: 4 | https://packaging.python.org/en/latest/distributing.html 5 | https://github.com/pypa/sampleproject 6 | """ 7 | 8 | # Always prefer setuptools over distutils 9 | import os 10 | import subprocess 11 | # To use a consistent encoding 12 | from codecs import open 13 | from distutils.version import LooseVersion 14 | 15 | from setuptools import setup, find_packages 16 | 17 | import os, sys 18 | sys.path.append(os.path.join(os.path.dirname(__file__), "config")) 19 | 20 | import importlib 21 | config = importlib.import_module("config") 22 | 23 | try: 24 | import sh 25 | except ImportError: 26 | import pip 27 | pip.main(["install", "sh>=1.12"]) 28 | import sh 29 | 30 | GRAPHENE_DIR = os.path.abspath("./graphene/") 31 | 32 | # Copy config file 33 | sh.cp("config/config.py", "sgx/config.py") 34 | 35 | # Check if swig >= 3.0.10 is installed 36 | try: 37 | out = sh.grep(sh.dpkg("-s", "swig", "swig3.0"), '^Version:') 38 | version = out.split(" ")[-1].split("-")[0] 39 | except sh.ErrorReturnCode_1: 40 | version = None 41 | if not version: 42 | exit("Error: Couldn't find swig") 43 | if LooseVersion(version) < LooseVersion("3.0.10"): 44 | exit("Error: swig version is lower than 3.0.10. Install swig 3.0.10 or higher.") 45 | 46 | 47 | # Check if apport is installed (it produces error messages when executing Python with Graphene) 48 | try: 49 | sh.dpkg("-s", "python3-apport") 50 | exit("Error: python3-apport is installed. Please uninstall it.") 51 | except sh.ErrorReturnCode_1: 52 | pass 53 | 54 | here = os.path.abspath(os.path.dirname(__file__)) 55 | 56 | # Link graphene's pal launcher to /usr/bin/graphene-pal 57 | pal = "/usr/bin/graphene-pal" 58 | if not os.path.islink(pal): 59 | subprocess.check_call(["ln", "-s", os.path.join(GRAPHENE_DIR, "Runtime/pal-Linux-SGX"), pal]) 60 | 61 | # Create sgx group (if it doesn't exist) 62 | try: 63 | sh.groupadd(config.GROUP) 64 | except sh.ErrorReturnCode_9: 65 | pass 66 | 67 | # Create the config directory 68 | sh.install("-m", "750", "-g", config.GROUP, "-d", config.CONFIG_DIR) 69 | 70 | # Create the data directory 71 | sh.install("-m", "770", "-g", config.GROUP, "-d", config.DATA_DIR) 72 | 73 | # Create the sealed directory 74 | sh.install("-m", "770", "-g", config.GROUP, "-d", config.SEALED_DIR) 75 | 76 | # Copy sgx-ra-manager to `/usr/local/bin/`. 77 | # We don't include sgx-ra-manager in the `scripts` argument to setup(), because setup() creates a script 78 | # which uses run_script() to execute the actual script, thereby ignoring our custom shebang to run python3-sgx. 79 | sh.cp("scripts/sgx-ra-manager", "/usr/local/bin/") 80 | 81 | # Copy sealing manifest to config directory 82 | sh.cp("config/sealing", config.CONFIG_DIR) 83 | 84 | # Get the long description from the README file 85 | with open(os.path.join(here, 'README.md'), encoding='utf-8') as f: 86 | long_description = f.read() 87 | 88 | dist = setup( 89 | name='sgx', 90 | 91 | # Versions should comply with PEP440. For a discussion on single-sourcing 92 | # the version across setup.py and the project code, see 93 | # https://packaging.python.org/en/latest/single_source_version.html 94 | version='0.1a0', 95 | 96 | description='Intel SGX Python wrapper', 97 | long_description=long_description, 98 | 99 | # The project's main homepage. 100 | # url='https://github.com/pypa/sampleproject', 101 | 102 | # Author details 103 | author='Adrian Dombeck', 104 | author_email='adrian.dombeck@rub.de', 105 | # Choose your license 106 | # license='MIT', 107 | 108 | # See https://pypi.python.org/pypi?%3Aaction=list_classifiers 109 | classifiers=[ 110 | # How mature is this project? Common values are 111 | # 3 - Alpha 112 | # 4 - Beta 113 | # 5 - Production/Stable 114 | 'Development Status :: 3 - Alpha', 115 | 116 | # Indicate who your project is intended for 117 | 'Intended Audience :: Developers', 118 | 'Topic :: Software Development', 119 | 120 | # Pick your license as you wish (should match "license" above) 121 | # 'License :: OSI Approved :: MIT License', 122 | 123 | # Specify the Python versions you support here. In particular, ensure 124 | # that you indicate whether you support Python 2, Python 3 or both. 125 | 'Programming Language :: Python :: 3', 126 | 'Programming Language :: Python :: 3.5', 127 | ], 128 | 129 | # What does your project relate to? 130 | keywords='sgx security development', 131 | 132 | include_package_data=True, 133 | 134 | # You can just specify the packages manually here if your project is 135 | # simple. Or you can use find_packages(). 136 | packages=find_packages(exclude=['contrib', 'docs', 'tests']), 137 | 138 | scripts=["scripts/python3-sgx", 139 | "scripts/sgx-quoting-manager"], 140 | 141 | # Alternatively, if you want to distribute just a my_module.py, uncomment 142 | # this: 143 | # py_modules=["my_module"], 144 | 145 | # List run-time dependencies here. These will be installed by pip when 146 | # your project is installed. For an analysis of "install_requires" vs pip's 147 | # requirements files see: 148 | # https://packaging.python.org/en/latest/requirements.html 149 | #install_requires=[], 150 | 151 | # List additional groups of dependencies here (e.g. development 152 | # dependencies). You can install these using the following syntax, 153 | # for example: 154 | # $ pip install -e .[dev,test] 155 | # extras_require={ 156 | # 'dev': ['check-manifest'], 157 | # 'test': ['coverage'], 158 | # }, 159 | 160 | # If there are data files included in your packages that need to be 161 | # installed, specify them here. If using Python 2.6 or less, then these 162 | # have to be included in MANIFEST.in as well. 163 | # package_data={ 164 | # 'sample': ['package_data.dat'], 165 | # }, 166 | 167 | # Although 'package_data' is the preferred approach, in some case you may 168 | # need to place data files outside of your packages. See: 169 | # http://docs.python.org/3.4/distutils/setupscript.html#installing-additional-files # noqa 170 | # In this case, 'data_file' will be installed into '/my_data' 171 | # data_files=[('my_data', ['data/data_file'])], 172 | 173 | # To provide executable scripts, use entry points in preference to the 174 | # "scripts" keyword. Entry points provide cross-platform support and allow 175 | # pip to create the appropriate form of executable for the target platform. 176 | # entry_points={ 177 | # 'console_scripts': [ 178 | # 'sample=sample:main', 179 | # ], 180 | # }, 181 | ) 182 | 183 | # Create python3 sgx launcher in the data directory 184 | sh.cp("config/python3.manifest.template", config.DATA_DIR) 185 | create_manifest = sh.Command(os.path.abspath("utils/create_manifest.py")) 186 | sign_manifest = sh.Command(os.path.abspath("utils/sign_manifest.py")) 187 | create_manifest(config.DATA_DIR, _fg=True) 188 | sign_manifest(config.DATA_DIR, _fg=True) 189 | -------------------------------------------------------------------------------- /setup.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | # Install required packages 6 | dpkg -s python3-dev > /dev/null || sudo apt install python3-dev 7 | dpkg -s python3-pip > /dev/null || sudo apt install python3-pip 8 | dpkg -s python3-setuptools > /dev/null || sudo apt install python3-setuptools 9 | 10 | # We need swig version 3.0.10 or higher. Install from zesty or newer. 11 | dpkg -s swig > /dev/null || sudo apt install swig/zesty 12 | 13 | # Install sh module via pip (version in xenial repositories is not recent enough) 14 | pip3 -q show sh || sudo pip3 install sh 15 | 16 | echo "Building python-sgx" 17 | make clean && make 18 | 19 | echo "Installing python-sgx" 20 | sudo ./setup.py install 21 | -------------------------------------------------------------------------------- /sgx/Makefile: -------------------------------------------------------------------------------- 1 | targets = all clean install 2 | 3 | .PHONY: $(targets) 4 | $(targets): 5 | $(MAKE) -C trusted $@ 6 | $(MAKE) -C untrusted $@ 7 | 8 | -------------------------------------------------------------------------------- /sgx/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adombeck/python-sgx/26395f8e6305f0c013f9689d768050d1aad5a94b/sgx/__init__.py -------------------------------------------------------------------------------- /sgx/exceptions.py: -------------------------------------------------------------------------------- 1 | class ConnectionClosedError(Exception): 2 | pass 3 | 4 | 5 | class UnexpectedLengthError(Exception): 6 | pass 7 | -------------------------------------------------------------------------------- /sgx/socket_util.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import logging 3 | import json 4 | 5 | from sgx.exceptions import ConnectionClosedError 6 | 7 | logger = logging.getLogger(__name__) 8 | 9 | MSG_TERMINATOR = b"\0" 10 | 11 | 12 | def receive_msg(sock: socket.socket): 13 | json_bytes = bytes() 14 | while True: 15 | data = sock.recv(4096).strip() 16 | if data.endswith(MSG_TERMINATOR): 17 | data = data[:-1] 18 | json_bytes += data 19 | break 20 | else: 21 | json_bytes += data 22 | 23 | if not json_bytes: 24 | raise ConnectionClosedError() 25 | 26 | logger.debug("Received (size: %s): %s", len(json_bytes), json_bytes) 27 | 28 | json_string = json_bytes.decode('utf8') 29 | msg = json.loads(json_string) 30 | return msg 31 | 32 | 33 | def send_msg(sock: socket.socket, msg): 34 | json_string = json.dumps(msg) 35 | json_bytes = json_string.encode() 36 | sock.sendall(json_bytes + MSG_TERMINATOR) 37 | -------------------------------------------------------------------------------- /sgx/trusted/Makefile: -------------------------------------------------------------------------------- 1 | targets = all clean install 2 | 3 | .PHONY: $(targets) 4 | $(targets): 5 | $(MAKE) -C sealing $@ 6 | $(MAKE) -C attestation $@ 7 | 8 | -------------------------------------------------------------------------------- /sgx/trusted/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adombeck/python-sgx/26395f8e6305f0c013f9689d768050d1aad5a94b/sgx/trusted/__init__.py -------------------------------------------------------------------------------- /sgx/trusted/attestation/Makefile: -------------------------------------------------------------------------------- 1 | NAME=attestation 2 | SO_NAME=_$(NAME)_swig.so 3 | 4 | all: $(SO_NAME) 5 | 6 | PROJECT_HOME = ../../.. 7 | 8 | UTILS = $(PROJECT_HOME)/utils 9 | 10 | include $(PROJECT_HOME)/Makefile.config 11 | 12 | SWIG_LIB = $(PROJECT_HOME)/lib/swig 13 | 14 | $(NAME).i: 15 | echo "// Generated from $(NAME).i.template\n" > $(NAME).i 16 | sed -e "s:\$$(NAME):$(NAME):g" $(NAME).i.template >> $(NAME).i 17 | chmod 444 $(NAME).i 18 | 19 | $(NAME)_wrap.c: $(NAME).i 20 | swig -Wall -Werror -python -I$(SDK)/include -I$(SWIG_LIB) $< 21 | 22 | $(NAME)_wrap.o: $(NAME)_wrap.c 23 | gcc -Wall -Werror -fPIC -c $^ -I$(SDK)/include -I$(SWIG_LIB) -I/usr/include/python3.5 -ldl 24 | 25 | $(NAME).o: $(NAME).c 26 | gcc -Wall -Werror -fPIC -c $^ -I$(SDK)/include -I/usr/include/python3.5 27 | 28 | $(SO_NAME): $(NAME).o $(NAME)_wrap.o call_tables.o 29 | gcc -g -Wall -Werror -shared $^ -o $@ \ 30 | -I$(SDK)/include \ 31 | -I/usr/include/python3.5 \ 32 | -shared-libgcc \ 33 | -L$(SDK)/lib64 \ 34 | -Wl,-Bstatic \ 35 | -Wl,--whole-archive -lsgx_trts -Wl,--no-whole-archive \ 36 | -Wl,--start-group \ 37 | -lsgx_tstdc \ 38 | -lsgx_tkey_exchange \ 39 | -lsgx_tcrypto \ 40 | -lsgx_tservice \ 41 | -Wl,--end-group \ 42 | -Wl,-Bsymbolic \ 43 | -Wl,--defsym,__ImageBase=0 \ 44 | -Wl,-Bdynamic -lsgx_uae_service -lsgx_ukey_exchange 45 | 46 | call_tables.o: call_tables.c 47 | gcc -g -Wall -Werror -fPIC -c $^ -I$(SDK)/include 48 | 49 | exec: $(NAME).o call_tables.o 50 | gcc -g -Wall -Werror $^ -o $@ \ 51 | -I$(SDK)/include \ 52 | -I/usr/include/python3.5 \ 53 | -shared-libgcc \ 54 | -L$(SDK)/lib64 \ 55 | -Wl,-Bstatic \ 56 | -Wl,--whole-archive -lsgx_trts -Wl,--no-whole-archive \ 57 | -Wl,--start-group \ 58 | -lsgx_tstdc \ 59 | -lsgx_tstdcxx \ 60 | -lsgx_tkey_exchange \ 61 | -lsgx_tcrypto \ 62 | -lsgx_tservice \ 63 | -Wl,--end-group \ 64 | -Wl,-Bsymbolic \ 65 | -Wl,--defsym,__ImageBase=0 \ 66 | -Wl,-Bdynamic -lsgx_uae_service -lsgx_ukey_exchange 67 | 68 | $(UTILS)/create_manifest.py 69 | $(UTILS)/sign_manifest.py 70 | 71 | 72 | .PHONY: clean 73 | clean: 74 | rm -rf $(NAME).i $(NAME)_wrap.c $(SO_NAME) $(NAME)_swig.py *.o exec *.sig *.token *.manifest.sgx *.manifest 75 | -------------------------------------------------------------------------------- /sgx/trusted/attestation/__init__.py: -------------------------------------------------------------------------------- 1 | from sgx.trusted.attestation.attestation import * 2 | -------------------------------------------------------------------------------- /sgx/trusted/attestation/attestation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | #include "sgx_tkey_exchange.h" 9 | #include "sgx_uae_service.h" 10 | #include "sgx_utils.h" 11 | 12 | sgx_status_t sgx_ra_get_ga(sgx_ra_context_t context, sgx_ec256_public_t* g_a); 13 | sgx_status_t sgx_ra_proc_msg2_trusted(sgx_ra_context_t context, const sgx_ra_msg2_t* p_msg2, const sgx_target_info_t* p_qe_target, sgx_report_t* p_report, sgx_quote_nonce_t* p_nonce); 14 | sgx_status_t sgx_ra_get_msg3_trusted(sgx_ra_context_t context, uint32_t quote_size, sgx_report_t* qe_report, sgx_ra_msg3_t* p_msg3, uint32_t msg3_size); 15 | 16 | static char error_message[256]; 17 | static int error_status = 0; 18 | 19 | 20 | void clear_exception() { 21 | error_status = 0; 22 | } 23 | 24 | 25 | char* check_exception() { 26 | if (error_status) 27 | return error_message; 28 | else 29 | return NULL; 30 | } 31 | 32 | 33 | void initialize_remote_attestation(sgx_ec256_public_t public_key, int use_pse, sgx_ra_context_t* p_context) 34 | { 35 | sgx_status_t ret = sgx_ra_init(&public_key, use_pse, p_context); 36 | if(ret != SGX_SUCCESS) 37 | { 38 | snprintf(error_message, 256, "Failed to call sgx_ra_init. Error code: 0x%x\n", ret); 39 | error_status = 1; 40 | } 41 | } 42 | 43 | 44 | void close_remote_attestation(sgx_ra_context_t context) 45 | { 46 | sgx_status_t ret = sgx_ra_close(context); 47 | if(ret != SGX_SUCCESS) 48 | { 49 | snprintf(error_message, 256, "Failed to call sgx_ra_close. Error code: 0x%x\n", ret); 50 | error_status = 1; 51 | } 52 | } 53 | 54 | 55 | void get_new_public_key(sgx_ra_context_t context, sgx_ec256_public_t* p_enclave_public_key) 56 | { 57 | sgx_status_t ret = sgx_ra_get_ga(context, p_enclave_public_key); 58 | if(ret != SGX_SUCCESS) 59 | { 60 | snprintf(error_message, 256, "Failed to call sgx_ra_get_ga. Error code: 0x%x\n", ret); 61 | error_status = 1; 62 | } 63 | } 64 | 65 | 66 | void process_msg2(sgx_ra_context_t context, 67 | sgx_target_info_t qe_target_info, 68 | sgx_ec256_public_t public_key, 69 | sgx_spid_t spid, 70 | uint16_t quote_type, 71 | uint16_t kdf_id, 72 | sgx_ec256_signature_t key_signature, 73 | sgx_mac_t mac, 74 | uint8_t* revocation_list, 75 | uint32_t revocation_list_size, 76 | sgx_report_t* p_report, 77 | sgx_quote_nonce_t* p_nonce 78 | ) 79 | { 80 | sgx_ra_msg2_t msg2; 81 | msg2.g_b = public_key; 82 | msg2.spid = spid; 83 | msg2.quote_type = quote_type; 84 | msg2.kdf_id = kdf_id; 85 | msg2.sign_gb_ga = key_signature; 86 | memcpy(msg2.mac, mac, sizeof(msg2.mac)); 87 | msg2.sig_rl_size = revocation_list_size; 88 | memcpy(msg2.sig_rl, revocation_list, msg2.sig_rl_size); 89 | 90 | sgx_status_t ret = sgx_ra_proc_msg2_trusted(context, &msg2, &qe_target_info, p_report, p_nonce); 91 | if(ret != SGX_SUCCESS) 92 | { 93 | snprintf(error_message, 256, "Failed to call sgx_ra_proc_msg2_trusted. Error code: 0x%x\n", ret); 94 | error_status = 1; 95 | } 96 | } 97 | 98 | 99 | void get_msg3(sgx_ra_context_t context, 100 | uint8_t* quote, 101 | uint32_t quote_size, 102 | sgx_report_t qe_report, 103 | sgx_mac_t* p_mac, 104 | char* platform_service_security_properties 105 | ) 106 | { 107 | uint32_t msg3_size = sizeof(sgx_ra_msg3_t) + quote_size; 108 | sgx_ra_msg3_t* p_msg3 = malloc(msg3_size); 109 | if(!p_msg3) 110 | { 111 | snprintf(error_message, 256, "Failed to allocate memory for msg3\n"); 112 | error_status = 1; 113 | return; 114 | } 115 | memset(p_msg3, 0, msg3_size); 116 | 117 | memcpy(&p_msg3->quote, quote, quote_size); 118 | 119 | sgx_status_t ret = sgx_ra_get_msg3_trusted(context, quote_size, &qe_report, p_msg3, msg3_size); 120 | if(ret != SGX_SUCCESS) 121 | { 122 | snprintf(error_message, 256, "Failed to call sgx_ra_get_msg3_trusted. Error code: 0x%x\n", ret); 123 | error_status = 1; 124 | goto cleanup; 125 | } 126 | 127 | memcpy(p_mac, &p_msg3->mac, sizeof(sgx_mac_t)); 128 | memcpy(platform_service_security_properties, &p_msg3->ps_sec_prop, sizeof(sgx_ps_sec_prop_desc_t)); 129 | 130 | cleanup: 131 | // Clear msg3 and free it 132 | memset(p_msg3, 0, msg3_size); 133 | free(p_msg3); 134 | } 135 | -------------------------------------------------------------------------------- /sgx/trusted/attestation/attestation.i.template: -------------------------------------------------------------------------------- 1 | %module $(NAME)_swig 2 | 3 | %{ 4 | #define SWIG_PYTHON_STRICT_BYTE_CHAR 5 | #include "sgx_key_exchange.h" 6 | 7 | #include "util.h" 8 | #include "except.h" 9 | 10 | typedef uint32_t sgx_ra_context_t; 11 | 12 | extern void main(); 13 | 14 | extern void get_new_public_key(sgx_ra_context_t context, sgx_ec256_public_t* p_enclave_public_key); 15 | extern void initialize_remote_attestation(sgx_ec256_public_t enclave_public_key, int use_pse, sgx_ra_context_t* p_context); 16 | extern void close_remote_attestation(sgx_ra_context_t context); 17 | 18 | extern void process_msg2( 19 | sgx_ra_context_t context, 20 | sgx_target_info_t qe_target_info, 21 | sgx_ec256_public_t public_key, 22 | sgx_spid_t spid, 23 | uint16_t quote_type, 24 | uint16_t kdf_id, 25 | sgx_ec256_signature_t key_signature, 26 | sgx_mac_t mac, 27 | uint8_t* revocation_list, 28 | uint32_t revocation_list_size, 29 | sgx_report_t* p_report, 30 | sgx_quote_nonce_t* p_nonce 31 | ); 32 | 33 | extern void get_msg3( 34 | sgx_ra_context_t context, 35 | uint8_t* quote, 36 | uint32_t quote_size, 37 | sgx_report_t qe_report, 38 | sgx_mac_t* p_mac, 39 | char* platform_service_security_properties 40 | ); 41 | 42 | %} 43 | 44 | 45 | %include "cdata.i" 46 | %include "stdint.i" 47 | %include "typemaps.i" 48 | %include "cstring.i" 49 | 50 | %include "sgx_defs.h" 51 | 52 | %include "public_key.i" 53 | %include "target_info.i" 54 | %include "spid.i" 55 | %include "signature.i" 56 | %include "mac.i" 57 | %include "report.i" 58 | %include "nonce.i" 59 | 60 | %include "except.i" 61 | 62 | extern void main(); 63 | 64 | 65 | %apply uint32_t { sgx_ra_context_t context }; 66 | %apply sgx_ec256_public_t* p_public_key { sgx_ec256_public_t* p_enclave_public_key }; 67 | extern void get_new_public_key(sgx_ra_context_t context, sgx_ec256_public_t* p_enclave_public_key); 68 | 69 | %apply uint32_t* OUTPUT { sgx_ra_context_t* p_context }; 70 | %apply sgx_ec256_public_t public_key { sgx_ec256_public_t enclave_public_key }; 71 | extern void initialize_remote_attestation(sgx_ec256_public_t enclave_public_key, int use_pse, sgx_ra_context_t* p_context); 72 | 73 | %apply uint32_t { sgx_ra_context_t context }; 74 | extern void close_remote_attestation(sgx_ra_context_t context); 75 | 76 | 77 | %apply sgx_target_info_t target_info { sgx_target_info_t qe_target_info }; 78 | %apply sgx_ec256_public_t public_key { sgx_ec256_public_t public_key }; 79 | %apply sgx_spid_t spid { sgx_spid_t spid }; 80 | %apply sgx_ec256_signature_t signature { sgx_ec256_signature_t key_signature }; 81 | %apply sgx_mac_t mac { sgx_mac_t mac }; 82 | %apply (char* STRING, size_t LENGTH) { (uint8_t* revocation_list, uint32_t revocation_list_size) }; 83 | %apply sgx_report_t* p_report { sgx_report_t* p_report }; 84 | %apply sgx_quote_nonce_t* p_nonce { sgx_quote_nonce_t* p_nonce }; 85 | 86 | extern void process_msg2( 87 | sgx_ra_context_t context, 88 | sgx_target_info_t qe_target_info, 89 | sgx_ec256_public_t public_key, 90 | sgx_spid_t spid, 91 | uint16_t quote_type, 92 | uint16_t kdf_id, 93 | sgx_ec256_signature_t key_signature, 94 | sgx_mac_t mac, 95 | uint8_t* revocation_list, 96 | uint32_t revocation_list_size, 97 | sgx_report_t* p_report, 98 | sgx_quote_nonce_t* p_nonce 99 | ); 100 | 101 | 102 | %apply (char* STRING, size_t LENGTH) { (uint8_t* quote, uint32_t quote_size) }; 103 | %apply sgx_mac_t* p_mac { sgx_mac_t* p_mac }; 104 | %apply sgx_report_t report { sgx_report_t qe_report }; 105 | %cstring_chunk_output(char* platform_service_security_properties, sizeof(sgx_ps_sec_prop_desc_t)); 106 | 107 | extern void get_msg3( 108 | sgx_ra_context_t context, 109 | uint8_t* quote, 110 | uint32_t quote_size, 111 | sgx_report_t qe_report, 112 | sgx_mac_t* p_mac, 113 | char* platform_service_security_properties 114 | ); 115 | 116 | 117 | %include "sgx_tcrypto.h" 118 | -------------------------------------------------------------------------------- /sgx/trusted/attestation/attestation.py: -------------------------------------------------------------------------------- 1 | from contextlib import contextmanager 2 | 3 | from sgx.trusted.attestation import attestation_swig 4 | from sgx.trusted.attestation.attestation_swig import * 5 | 6 | 7 | def initialize_remote_attestation(challenger_public_key, use_pse=False): 8 | return attestation_swig.initialize_remote_attestation(challenger_public_key, 1 if use_pse else 0) 9 | 10 | 11 | @contextmanager 12 | def remote_attestation_context(challenger_public_key, use_pse=False): 13 | ra_context = initialize_remote_attestation(challenger_public_key, use_pse) 14 | yield ra_context 15 | close_remote_attestation(ra_context) 16 | -------------------------------------------------------------------------------- /sgx/trusted/attestation/call_tables.c: -------------------------------------------------------------------------------- 1 | /* The SDK expects that these are defined, so we define them. But we don't use them. */ 2 | 3 | #include "sgx_edger8r.h" 4 | 5 | SGX_EXTERNC const struct { 6 | size_t nr_ecall; 7 | struct {void* ecall_addr; uint8_t is_priv;} ecall_table[0]; 8 | } g_ecall_table = { 9 | 0, 10 | { 11 | } 12 | }; 13 | 14 | SGX_EXTERNC const struct { 15 | size_t nr_ocall; 16 | uint8_t entry_table[9][7]; 17 | } g_dyn_entry_table = { 18 | 0, 19 | { 20 | } 21 | }; 22 | 23 | sgx_status_t SGX_CDECL sgx_oc_cpuidex(int cpuinfo[4], int leaf, int subleaf) 24 | { 25 | return 0; 26 | } 27 | 28 | sgx_status_t SGX_CDECL sgx_thread_wait_untrusted_event_ocall(int* retval, const void* self) 29 | { 30 | return 0; 31 | } 32 | 33 | sgx_status_t SGX_CDECL sgx_thread_set_untrusted_event_ocall(int* retval, const void* waiter) 34 | { 35 | return 0; 36 | } 37 | 38 | sgx_status_t SGX_CDECL sgx_thread_setwait_untrusted_events_ocall(int* retval, const void* waiter, const void* self) 39 | { 40 | return 0; 41 | } 42 | 43 | sgx_status_t SGX_CDECL sgx_thread_set_multiple_untrusted_events_ocall(int* retval, const void** waiters, size_t total) 44 | { 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /sgx/trusted/sealing/Makefile: -------------------------------------------------------------------------------- 1 | NAME=sealing 2 | SO_NAME=_$(NAME)_swig.so 3 | 4 | all: $(SO_NAME) 5 | 6 | PROJECT_HOME = ../../.. 7 | 8 | include $(PROJECT_HOME)/Makefile.config 9 | 10 | SWIG_LIB = $(PROJECT_HOME)/lib/swig 11 | 12 | $(NAME).i: 13 | sed -e "s:\$$(NAME):$(NAME):g" $(NAME).i.template > $(NAME).i 14 | 15 | $(NAME)_wrap.c: $(NAME).i 16 | swig -Wall -Werror -python -I$(SDK)/include -I$(SWIG_LIB) $< 17 | 18 | $(NAME)_wrap.o: $(NAME)_wrap.c 19 | gcc -Wall -Werror -fPIC -c $^ -I$(SDK)/include -I$(SWIG_LIB) -I/usr/include/python3.5 -ldl 20 | 21 | $(NAME).o: $(NAME).c 22 | gcc -Wall -Werror -fPIC -c $^ -I$(SDK)/include -I/usr/include/python3.5 23 | 24 | $(SO_NAME): $(NAME).o $(NAME)_wrap.o call_tables.o 25 | gcc -g -Wall -Werror -shared $^ -o $@ \ 26 | -I$(SDK)/include \ 27 | -I/usr/include/python3.5 \ 28 | -shared-libgcc \ 29 | -L$(SDK)/lib64 \ 30 | -Wl,-Bstatic \ 31 | -lprotobuf \ 32 | -Wl,--whole-archive -lsgx_trts -Wl,--no-whole-archive \ 33 | -Wl,--start-group \ 34 | -lsgx_tstdc \ 35 | -lsgx_tkey_exchange \ 36 | -lsgx_tcrypto \ 37 | -lsgx_tservice \ 38 | -Wl,--end-group \ 39 | -Wl,-Bsymbolic \ 40 | -Wl,--defsym,__ImageBase=0 \ 41 | -Wl,-Bdynamic 42 | 43 | call_tables.o: call_tables.c 44 | gcc -g -Wall -Werror -fPIC -c $^ -I$(SDK)/include 45 | 46 | .PHONY: clean 47 | clean: 48 | rm -rf $(NAME).i $(NAME)_wrap.c $(SO_NAME) $(NAME)_swig.py *.o 49 | -------------------------------------------------------------------------------- /sgx/trusted/sealing/__init__.py: -------------------------------------------------------------------------------- 1 | from sgx.trusted.sealing.sealing import * 2 | -------------------------------------------------------------------------------- /sgx/trusted/sealing/call_tables.c: -------------------------------------------------------------------------------- 1 | /* The SDK expects that these are defined, so we define them. But we don't use them. */ 2 | 3 | #include "sgx_edger8r.h" 4 | 5 | SGX_EXTERNC const struct { 6 | size_t nr_ecall; 7 | struct {void* ecall_addr; uint8_t is_priv;} ecall_table[0]; 8 | } g_ecall_table = { 9 | 0, 10 | { 11 | } 12 | }; 13 | 14 | SGX_EXTERNC const struct { 15 | size_t nr_ocall; 16 | uint8_t entry_table[9][7]; 17 | } g_dyn_entry_table = { 18 | 0, 19 | { 20 | } 21 | }; 22 | 23 | sgx_status_t SGX_CDECL sgx_oc_cpuidex(int cpuinfo[4], int leaf, int subleaf) 24 | { 25 | return 0; 26 | } 27 | 28 | sgx_status_t SGX_CDECL sgx_thread_wait_untrusted_event_ocall(int* retval, const void* self) 29 | { 30 | return 0; 31 | } 32 | 33 | sgx_status_t SGX_CDECL sgx_thread_set_untrusted_event_ocall(int* retval, const void* waiter) 34 | { 35 | return 0; 36 | } 37 | 38 | sgx_status_t SGX_CDECL sgx_thread_setwait_untrusted_events_ocall(int* retval, const void* waiter, const void* self) 39 | { 40 | return 0; 41 | } 42 | 43 | sgx_status_t SGX_CDECL sgx_thread_set_multiple_untrusted_events_ocall(int* retval, const void** waiters, size_t total) 44 | { 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /sgx/trusted/sealing/sealing.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include "sgx_tseal.h" 6 | 7 | #include 8 | 9 | #define SECRET_LEN 256 10 | #define PLAIN_TEXT_LEN 256 11 | 12 | // Global data 13 | sgx_sealed_data_t* g_sealed_buf; 14 | uint8_t* g_secret; 15 | uint8_t* g_plain_text; 16 | uint8_t* g_unsealed_secret; 17 | uint8_t* g_unsealed_plain_text; 18 | 19 | 20 | static char error_message[256]; 21 | static int error_status = 0; 22 | 23 | 24 | void clear_exception() { 25 | error_status = 0; 26 | } 27 | 28 | 29 | char* check_exception() { 30 | if (error_status) 31 | return error_message; 32 | else 33 | return NULL; 34 | } 35 | 36 | 37 | 38 | // XXX: Use sgx_seal_data_ex and add seal_to_signer option (default to False, i.e. seal to enclave by default, because 39 | // else the signer can access the secret with other enclaves) 40 | 41 | 42 | void seal_data(uint8_t* secret, uint32_t secret_len, uint8_t* plain_text, uint32_t plain_text_len, sgx_sealed_data_t* sealed_buf) 43 | { 44 | // fprintf(stderr, "Sealing data\n"); 45 | 46 | uint32_t sealed_len = sizeof(sgx_sealed_data_t) + secret_len + plain_text_len; 47 | 48 | // Seal the secret data 49 | sgx_status_t ret = sgx_seal_data(plain_text_len, plain_text, secret_len, secret, sealed_len, sealed_buf); 50 | if(ret != SGX_SUCCESS) 51 | { 52 | snprintf(error_message, 256, "Failed to call sgx_seal_data. Error code: 0x%x\n", ret); 53 | error_status = 1; 54 | } 55 | } 56 | 57 | 58 | void seal(char* secret, uint32_t secret_len, char* plain_text, uint32_t plain_text_len, char** p_sealed_buf, uint32_t* p_sealed_len) 59 | { 60 | uint32_t sealed_len = sizeof(sgx_sealed_data_t) + secret_len + plain_text_len; 61 | 62 | sgx_sealed_data_t* sealed_buf = malloc(sealed_len); 63 | if(sealed_buf == NULL) 64 | { 65 | snprintf(error_message, 256, "Failed to allocate memory for sealed_buf.\n"); 66 | error_status = 1; 67 | return; 68 | } 69 | memset(sealed_buf, 0, sealed_len); 70 | 71 | seal_data((uint8_t*) secret, secret_len, (uint8_t*) plain_text, plain_text_len, sealed_buf); 72 | 73 | *p_sealed_buf = (char*) sealed_buf; 74 | *p_sealed_len = sealed_len; 75 | } 76 | 77 | 78 | void unseal_data(uint8_t* secret, uint8_t* plain_text, sgx_sealed_data_t* sealed_buf) 79 | { 80 | // fprintf(stderr, "Unsealing data\n"); 81 | 82 | uint32_t unsealed_data_len = sgx_get_encrypt_txt_len(sealed_buf); 83 | // fprintf(stderr, "unsealed_data_len: %u\n", unsealed_data_len); 84 | 85 | uint32_t plain_text_len = sgx_get_add_mac_txt_len(sealed_buf); 86 | // fprintf(stderr, "plain_text_len: %u\n", plain_text_len); 87 | 88 | // Unseal current sealed buf 89 | sgx_status_t ret = sgx_unseal_data((sgx_sealed_data_t*) sealed_buf, plain_text, &plain_text_len, secret, &unsealed_data_len); 90 | if(ret != SGX_SUCCESS) 91 | { 92 | snprintf(error_message, 256, "Failed to call sgx_unseal_data. Error code: 0x%x\n", ret); 93 | error_status = 1; 94 | } 95 | } 96 | 97 | 98 | void unseal(char** p_secret, uint32_t* p_secret_len, char** p_plain_text, uint32_t* p_plain_text_len, char* sealed_buf) 99 | { 100 | uint32_t secret_len = sgx_get_encrypt_txt_len((sgx_sealed_data_t*) sealed_buf); 101 | uint32_t plain_text_len = sgx_get_add_mac_txt_len((sgx_sealed_data_t*) sealed_buf); 102 | 103 | uint8_t* secret = (uint8_t*) calloc(secret_len, sizeof(uint8_t)); 104 | if(secret == NULL) 105 | { 106 | snprintf(error_message, 256, "Failed to allocate memory for secret.\n"); 107 | error_status = 1; 108 | return; 109 | } 110 | 111 | uint8_t* plain_text = (uint8_t*) calloc(plain_text_len, sizeof(uint8_t)); 112 | if(plain_text == NULL) 113 | { 114 | snprintf(error_message, 256, "Failed to allocate memory for plain_text.\n"); 115 | error_status = 1; 116 | free(secret); 117 | return; 118 | } 119 | 120 | unseal_data(secret, plain_text, (sgx_sealed_data_t*) sealed_buf); 121 | 122 | *p_secret = (char*) secret; 123 | *p_secret_len = secret_len; 124 | *p_plain_text = (char*) plain_text; 125 | *p_plain_text_len = plain_text_len; 126 | } 127 | 128 | -------------------------------------------------------------------------------- /sgx/trusted/sealing/sealing.i.template: -------------------------------------------------------------------------------- 1 | %module $(NAME)_swig 2 | 3 | %{ 4 | #define SWIG_PYTHON_STRICT_BYTE_CHAR 5 | #include "except.h" 6 | 7 | //extern void seal(char* secret, uint32_t secret_len, char* plain_text, uint32_t plain_text_len, char** p_sealed_buf, uint32_t* p_sealed_len); 8 | extern void seal(char* secret, uint32_t secret_len, char* plain_text, uint32_t plain_text_len, char** p_sealed_buf, uint32_t* p_sealed_len); 9 | extern void unseal(char** p_secret, uint32_t* p_secret_len, char** p_plain_text, uint32_t* p_plain_text_len, char* sealed_buf); 10 | %} 11 | 12 | %include "stdint.i" 13 | %include "cstring.i" 14 | 15 | %include "except.i" 16 | 17 | %cstring_output_allocate_size(char** p_sealed_buf, uint32_t* p_sealed_len, free(*$1)); 18 | extern void seal(char* secret, uint32_t secret_len, char* plain_text, uint32_t plain_text_len, char** p_sealed_buf, uint32_t* p_sealed_len); 19 | 20 | %cstring_output_allocate_size(char** p_secret, uint32_t* p_secret_len, free(*$1)); 21 | %cstring_output_allocate_size(char** p_plain_text, uint32_t* p_plain_text_len, free(*$1)); 22 | extern void unseal(char** p_secret, uint32_t* p_secret_len, char** p_plain_text, uint32_t* p_plain_text_len, char* sealed_buf); 23 | 24 | -------------------------------------------------------------------------------- /sgx/trusted/sealing/sealing.py: -------------------------------------------------------------------------------- 1 | from sgx.trusted.sealing import sealing_swig 2 | from sgx.trusted.sealing.sealing_swig import * 3 | 4 | 5 | def seal(secret, plain_text=bytes()): 6 | return sealing_swig.seal(secret, len(secret), plain_text, len(plain_text)) 7 | -------------------------------------------------------------------------------- /sgx/trusted/sealing/wrapper.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import os 3 | from contextlib import contextmanager 4 | 5 | from sgx.trusted import sealing 6 | 7 | 8 | class PersistentFileNotFoundError(Exception): 9 | pass 10 | 11 | 12 | # XXX: Implement replay protection 13 | 14 | @contextmanager 15 | def handle_sealing(manifest_path): 16 | """Unseals the files specified in the sealing manifest. 17 | Requires an ephemeral filesystem in the enclave, to protect against the host OS 18 | (which Graphene does currently not provide).""" 19 | _unseal(manifest_path) 20 | yield 21 | _seal(manifest_path) 22 | 23 | 24 | def _get_manifest_entries(manifest_path): 25 | with open(manifest_path) as manifest: 26 | lines = manifest.readlines() 27 | for line in lines: 28 | if line.startswith("#"): 29 | # It's a comment 30 | continue 31 | yield line.split() 32 | 33 | 34 | def _unseal(manifest_path): 35 | for type_, persistent_path, ephemeral_path in _get_manifest_entries(manifest_path): 36 | if type_ == "d" and not os.path.isdir(persistent_path): 37 | os.mkdir(persistent_path) 38 | 39 | if type_ == "d": 40 | _unseal_directory(ephemeral_path, persistent_path) 41 | else: 42 | try: 43 | _unseal_file(ephemeral_path, persistent_path) 44 | except PersistentFileNotFoundError: 45 | _create_empty(persistent_path) 46 | _create_empty(ephemeral_path) 47 | 48 | 49 | def _create_empty(path): 50 | open(path, 'bw').close() 51 | 52 | 53 | def _unseal_directory(ephemeral_path, persistent_path): 54 | try: 55 | os.mkdir(ephemeral_path) 56 | except FileExistsError: 57 | pass 58 | for entry in os.listdir(persistent_path): 59 | new_persistent_path = os.path.join(persistent_path, entry) 60 | new_ephemeral_path = os.path.join(ephemeral_path, entry) 61 | _unseal_path(new_ephemeral_path, new_persistent_path) 62 | 63 | 64 | def _unseal_path(ephemeral_path, persistent_path): 65 | if os.path.isdir(persistent_path): 66 | _unseal_directory(ephemeral_path, persistent_path) 67 | else: 68 | _unseal_file(ephemeral_path, persistent_path) 69 | 70 | 71 | def _unseal_file(ephemeral_path, persistent_path): 72 | logging.debug("Unsealing file %r to %r", persistent_path, ephemeral_path) 73 | try: 74 | with open(persistent_path, 'br') as source: 75 | data = source.read() 76 | except FileNotFoundError: 77 | raise PersistentFileNotFoundError 78 | 79 | if data: 80 | secret, plain_text = sealing.unseal(data) 81 | else: 82 | secret = bytes() 83 | 84 | with open(ephemeral_path, 'bw') as dest: 85 | dest.write(secret) 86 | 87 | 88 | def _seal(manifest_path): 89 | for type_, persistent_path, ephemeral_path in _get_manifest_entries(manifest_path): 90 | _seal_mapping(persistent_path, ephemeral_path) 91 | _check_if_deleted(persistent_path, ephemeral_path) 92 | 93 | 94 | def _seal_mapping(persistent_path, ephemeral_path): 95 | if os.path.isdir(ephemeral_path): 96 | _seal_directory(ephemeral_path, persistent_path) 97 | else: 98 | _seal_file(ephemeral_path, persistent_path) 99 | 100 | 101 | def _seal_directory(ephemeral_path, persistent_path): 102 | try: 103 | os.mkdir(persistent_path) 104 | except FileExistsError: 105 | pass 106 | for entry in os.listdir(ephemeral_path): 107 | new_persistent_path = os.path.join(persistent_path, entry) 108 | new_ephemeral_path = os.path.join(ephemeral_path, entry) 109 | 110 | # In Graphene chroots, for some reason files that were deleted are still being listed by os.listdir 111 | if not os.path.exists(new_ephemeral_path): 112 | continue 113 | 114 | _seal_mapping(new_persistent_path, new_ephemeral_path) 115 | 116 | for entry in os.listdir(persistent_path): 117 | new_persistent_path = os.path.join(persistent_path, entry) 118 | new_ephemeral_path = os.path.join(ephemeral_path, entry) 119 | _check_if_deleted(new_persistent_path, new_ephemeral_path) 120 | 121 | 122 | def _seal_file(ephemeral_path, persistent_path): 123 | logging.debug("Sealing file %r to %r", ephemeral_path, persistent_path) 124 | with open(ephemeral_path, 'br') as source: 125 | secret = source.read() 126 | 127 | logging.debug("secret: %r", secret) 128 | 129 | if secret: 130 | data = sealing.seal(secret) 131 | else: 132 | data = bytes() 133 | 134 | with open(persistent_path, 'bw') as dest: 135 | dest.write(data) 136 | 137 | 138 | def _check_if_deleted(persistent_path, ephemeral_path): 139 | if not os.path.exists(ephemeral_path): 140 | os.remove(persistent_path) 141 | -------------------------------------------------------------------------------- /sgx/untrusted/Makefile: -------------------------------------------------------------------------------- 1 | targets = all clean install 2 | 3 | .PHONY: $(targets) 4 | $(targets): 5 | $(MAKE) -C attestation $@ 6 | 7 | -------------------------------------------------------------------------------- /sgx/untrusted/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adombeck/python-sgx/26395f8e6305f0c013f9689d768050d1aad5a94b/sgx/untrusted/__init__.py -------------------------------------------------------------------------------- /sgx/untrusted/attestation/Makefile: -------------------------------------------------------------------------------- 1 | NAME=attestation 2 | SO_NAME=_$(NAME)_swig.so 3 | 4 | all: $(SO_NAME) 5 | 6 | PROJECT_HOME = ../../.. 7 | 8 | include $(PROJECT_HOME)/Makefile.config 9 | 10 | UTILS = $(PROJECT_HOME)/utils 11 | SWIG_LIB = $(PROJECT_HOME)/lib/swig 12 | 13 | INCLUDES = -I$(SDK)/include -I$(SDK_GIT_DIR)/common/inc -I$(SDK_GIT_DIR)/common/inc/internal -I$(SDK_GIT_DIR)/external/crypto_px/include -I$(SDK_GIT_DIR)/sdk/trts/linux -I$(SDK_GIT_DIR)/external/rdrand -I$(SDK_GIT_DIR)/sdk/tseal 14 | 15 | $(NAME).i: 16 | echo "// Generated from $(NAME).i.template\n" > $(NAME).i 17 | sed -e "s:\$$(NAME):$(NAME):g" $(NAME).i.template >> $(NAME).i 18 | chmod 444 $(NAME).i 19 | 20 | $(NAME)_wrap.c: $(NAME).i 21 | swig -Wall -python -I$(SDK)/include -I$(SWIG_LIB) $< 22 | 23 | $(NAME)_wrap.o: $(NAME)_wrap.c 24 | gcc -Wall -Werror -fPIC -c $^ -I$(SDK)/include -I$(SWIG_LIB) -I/usr/include/python3.5 -ldl 25 | 26 | $(NAME).o: $(NAME).c 27 | gcc -Wall -Werror -fPIC -c $^ $(INCLUDES) -I/usr/include/python3.5 28 | 29 | $(SO_NAME): $(NAME).o $(NAME)_wrap.o 30 | gcc -Wall -Werror -shared *.o -lsgx_uae_service -o $@ -Wl,--trace-symbol,sgx_rijndael128GCM_encrypt 31 | 32 | exec: $(NAME).o 33 | gcc -g -Wall -Werror $^ \ 34 | -lsgx_uae_service \ 35 | -lpython3.5m 36 | 37 | 38 | 39 | .PHONY: clean 40 | clean: 41 | rm -rf $(NAME).i $(NAME)_wrap.c $(SO_NAME) $(NAME)_swig.py *.o 42 | -------------------------------------------------------------------------------- /sgx/untrusted/attestation/__init__.py: -------------------------------------------------------------------------------- 1 | from sgx.untrusted.attestation.attestation import * 2 | -------------------------------------------------------------------------------- /sgx/untrusted/attestation/attestation.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | #include "sgx_uae_service.h" 8 | 9 | 10 | static char error_message[256]; 11 | static int error_status = 0; 12 | 13 | 14 | void clear_exception() { 15 | error_status = 0; 16 | } 17 | 18 | 19 | char* check_exception() { 20 | if (error_status) 21 | return error_message; 22 | else 23 | return NULL; 24 | } 25 | 26 | 27 | void get_extended_epid_group_id(uint32_t* p_group_id) 28 | { 29 | sgx_status_t ret = sgx_get_extended_epid_group_id(p_group_id); 30 | if(ret != SGX_SUCCESS) 31 | { 32 | snprintf(error_message, 256, "Failed to get EPID group id. Error code: 0x%x\n", ret); 33 | error_status = 1; 34 | } 35 | } 36 | 37 | 38 | void initialize_quoting_enclave(sgx_target_info_t* p_qe_target_info, sgx_epid_group_id_t* p_gid) 39 | { 40 | sgx_status_t ret = sgx_init_quote(p_qe_target_info, p_gid); 41 | if(ret != SGX_SUCCESS) 42 | { 43 | snprintf(error_message, 256, "Failed to call sgx_init_quote. Error code: 0x%x\n", ret); 44 | error_status = 1; 45 | } 46 | } 47 | 48 | 49 | void get_quote_size(uint8_t* revocation_list, uint32_t revocation_list_size, uint32_t* p_quote_size) 50 | { 51 | sgx_status_t ret = sgx_get_quote_size(revocation_list_size ? revocation_list : NULL, p_quote_size); 52 | if(ret != SGX_SUCCESS) 53 | { 54 | snprintf(error_message, 256, "Failed to call sgx_get_quote_size. Error code: 0x%x\n", ret); 55 | error_status = 1; 56 | } 57 | } 58 | 59 | 60 | void get_quote(sgx_report_t report, 61 | uint16_t quote_type, 62 | sgx_spid_t spid, 63 | sgx_quote_nonce_t nonce, 64 | uint8_t* revocation_list, 65 | uint32_t revocation_list_size, 66 | char* p_quote, 67 | uint32_t* p_quote_size, 68 | sgx_report_t* p_qe_report) 69 | { 70 | memset(p_quote, 0, *p_quote_size); 71 | 72 | sgx_status_t ret = sgx_get_quote(&report, 73 | quote_type, 74 | &spid, 75 | &nonce, 76 | revocation_list_size ? revocation_list : NULL, 77 | revocation_list_size, 78 | p_qe_report, 79 | (sgx_quote_t *) p_quote, 80 | *p_quote_size); 81 | 82 | if(ret != SGX_SUCCESS) 83 | { 84 | snprintf(error_message, 256, "Failed to call sgx_get_quote. Error code: 0x%x\n", ret); 85 | error_status = 1; 86 | } 87 | } 88 | 89 | -------------------------------------------------------------------------------- /sgx/untrusted/attestation/attestation.i.template: -------------------------------------------------------------------------------- 1 | %module $(NAME)_swig 2 | 3 | %{ 4 | #define SWIG_PYTHON_STRICT_BYTE_CHAR 5 | 6 | #include "sgx_quote.h" 7 | #include "util.h" 8 | 9 | extern void get_extended_epid_group_id(uint32_t* p_group_id); 10 | extern void initialize_quoting_enclave(sgx_target_info_t* p_qe_target_info, sgx_epid_group_id_t* p_gid); 11 | extern void get_quote_size(uint8_t* revocation_list, uint32_t revocation_list_size, uint32_t* p_quote_size); 12 | 13 | extern void get_quote( 14 | sgx_report_t report, 15 | uint16_t quote_type, 16 | sgx_spid_t spid, 17 | sgx_quote_nonce_t nonce, 18 | uint8_t* revocation_list, 19 | uint32_t revocation_list_size, 20 | char* p_quote, 21 | uint32_t* p_quote_size, 22 | sgx_report_t* p_qe_report 23 | ); 24 | 25 | %} 26 | 27 | 28 | %include "cdata.i" 29 | %include "stdint.i" 30 | %include "typemaps.i" 31 | %include "cstring.i" 32 | 33 | %include "sgx_defs.h" 34 | 35 | %include "epid_group_id.i" 36 | %include "target_info.i" 37 | %include "report.i" 38 | %include "spid.i" 39 | %include "nonce.i" 40 | 41 | 42 | %apply uint32_t* OUTPUT { uint32_t* p_group_id }; 43 | extern void get_extended_epid_group_id(uint32_t* p_group_id); 44 | 45 | 46 | %apply sgx_target_info_t* p_target_info { sgx_target_info_t* p_qe_target_info }; 47 | %apply sgx_epid_group_id_t* p_epid_group_id { sgx_epid_group_id_t* p_gid }; 48 | extern void initialize_quoting_enclave(sgx_target_info_t* p_qe_target_info, sgx_epid_group_id_t* p_gid); 49 | 50 | 51 | %apply (char* STRING, size_t LENGTH) { (uint8_t* revocation_list, uint32_t revocation_list_size) }; 52 | %apply uint32_t* OUTPUT { uint32_t* p_quote_size}; 53 | extern void get_quote_size(uint8_t* revocation_list, uint32_t revocation_list_size, uint32_t* p_quote_size); 54 | 55 | 56 | %apply sgx_report_t report { sgx_report_t report }; 57 | %apply sgx_spid_t spid { sgx_spid_t spid }; 58 | %apply sgx_quote_nonce_t nonce { sgx_quote_nonce_t nonce }; 59 | %apply (char* STRING, size_t LENGTH) { (uint8_t* revocation_list, uint32_t revocation_list_size) }; 60 | %cstring_output_withsize(char* p_quote, uint32_t* p_quote_size); 61 | %apply sgx_report_t* p_report { sgx_report_t* p_qe_report }; 62 | 63 | extern void get_quote( 64 | sgx_report_t report, 65 | uint16_t quote_type, 66 | sgx_spid_t spid, 67 | sgx_quote_nonce_t nonce, 68 | uint8_t* revocation_list, 69 | uint32_t revocation_list_size, 70 | char* p_quote, 71 | uint32_t* p_quote_size, 72 | sgx_report_t* p_qe_report 73 | ); 74 | -------------------------------------------------------------------------------- /sgx/untrusted/attestation/attestation.py: -------------------------------------------------------------------------------- 1 | from sgx.untrusted.attestation.attestation_swig import * 2 | -------------------------------------------------------------------------------- /sgx/util.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | from sgx.exceptions import UnexpectedLengthError 4 | 5 | CONFIG_DIR = "/python-sgx" 6 | 7 | 8 | def get_challenger_public_key(): 9 | pubkey_path = os.path.join(CONFIG_DIR, "challenger_public.key") 10 | with open(pubkey_path) as pubkey_file: 11 | return bytes.fromhex(pubkey_file.read()) 12 | 13 | 14 | def check_msg(data, type_, length=None): 15 | if not isinstance(data, type_): 16 | raise TypeError("Received JSON is of type %r, expected type %r" % (type(data), type_)) 17 | 18 | if length and len(data) != length: 19 | raise UnexpectedLengthError("Received JSON %r has length %r, expected length %r" % (data, len(data), length)) 20 | -------------------------------------------------------------------------------- /tests/test_seal.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3-sgx 2 | 3 | import sys 4 | import traceback 5 | 6 | try: 7 | import sgx.trusted.sealing 8 | except ImportError as e: 9 | print(traceback.format_exc(), file=sys.stderr) 10 | exit() 11 | 12 | file_name = "tests/test.sealed" 13 | 14 | 15 | def seal(): 16 | with open(file_name, "bw+") as f: 17 | data = sgx.trusted.sealing.seal(secret=b"this is some secret text with a \0 (null byte)", 18 | plain_text=b"this is additional plaintext with a \0 (null byte)") 19 | print("data: %r" % data) 20 | print("data_len: %r" % len(data)) 21 | f.write(data) 22 | 23 | 24 | def unseal(): 25 | with open(file_name, "br") as f: 26 | data = f.read() 27 | print("data: %r" % data) 28 | secret, plain_text = sgx.trusted.sealing.unseal(data) 29 | print("secret: %r" % secret) 30 | print("plain text: %r" % plain_text) 31 | 32 | 33 | def exception(): 34 | sgx.trusted.sealing.seal(b"") 35 | 36 | 37 | def print_usage(): 38 | print("Usage: %s (seal|unseal)" % sys.argv[0]) 39 | 40 | 41 | def main(): 42 | if len(sys.argv) < 2: 43 | print_usage() 44 | return 45 | 46 | if sys.argv[1] == "seal": 47 | seal() 48 | elif sys.argv[1] == "unseal": 49 | unseal() 50 | elif sys.argv[1] == "exception": 51 | exception() 52 | else: 53 | print_usage() 54 | 55 | if __name__ == "__main__": 56 | try: 57 | main() 58 | except: 59 | # Without this graphene's pal does not return on exceptions 60 | print(traceback.format_exc(), file=sys.stderr) 61 | exit() 62 | -------------------------------------------------------------------------------- /tests/test_sealing_wrapper.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3-sgx 2 | 3 | import logging 4 | 5 | MANIFEST_PATH = "/python-sgx/sealing" 6 | 7 | # noinspection PyBroadException 8 | try: 9 | import sgx.trusted.sealing.wrapper as sealing_wrapper 10 | except: 11 | logging.exception('') 12 | exit() 13 | 14 | 15 | def test_append_to_file(): 16 | path = '/tests/unsealed' 17 | 18 | try: 19 | with open(path) as f: 20 | i = int(f.readlines()[-1]) 21 | except FileNotFoundError: 22 | logging.info("Creating file %r" % path) 23 | create_empty(path) 24 | i = 0 25 | except IndexError: 26 | i = 0 27 | 28 | logging.info("Appending %r to file %r", i + 1, path) 29 | with open(path, 'a') as f: 30 | f.write("%s\n" % (i+1)) 31 | 32 | 33 | def create_empty(path): 34 | open(path, 'bw').close() 35 | 36 | 37 | def main(): 38 | logging.basicConfig(level=logging.DEBUG) 39 | 40 | with sealing_wrapper.handle_sealing(MANIFEST_PATH): 41 | test_append_to_file() 42 | 43 | # XXX: For some reason the file modifications by the child are not synced with the parent, 44 | # therefore the sealing doesn't work with child processes. 45 | # 46 | # import sys 47 | # exit_code = os.spawnvp(os.P_WAIT, sys.argv[1], sys.argv[1:]) 48 | # if exit_code != 0: 49 | # logging.error("Command %r exited with code %r", " ".join(sys.argv[1:]), exit_code) 50 | 51 | 52 | if __name__ == '__main__': 53 | # noinspection PyBroadException 54 | try: 55 | main() 56 | except: 57 | # Without this graphene's pal does not return on exceptions 58 | logging.exception('') 59 | exit() 60 | -------------------------------------------------------------------------------- /uninstall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | sudo rm -rf /opt/python-sgx /var/lib/python-sgx /usr/local/bin/trusted-ra-manager /usr/local/bin/untrusted-ra-manager /usr/bin/graphene-pal 4 | -------------------------------------------------------------------------------- /utils/create_manifest.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import glob 4 | import os 5 | import argparse 6 | 7 | DIR = os.path.dirname(__file__) 8 | 9 | import sys 10 | sys.path.append(os.path.join(DIR, '../config')) 11 | from config import PYTHON_VERSION, LIBPROTOBUF_VERSION, DATA_DIR, CONFIG_DIR 12 | 13 | GRAPHENE_DIR = os.path.abspath(os.path.join(DIR, "../graphene/")) 14 | RUNTIME = os.path.join(GRAPHENE_DIR, "Runtime") 15 | TESTS_DIR = os.path.abspath(os.path.join(DIR, "../tests/")) 16 | 17 | 18 | def parse_args(): 19 | parser = argparse.ArgumentParser() 20 | parser.add_argument("PATH", nargs='?', default=".", help="Path to the manifest template or the directory containing the manifest templates") 21 | return parser.parse_args() 22 | 23 | 24 | def get_manifest_templates(path): 25 | manifests = glob.glob(path + "/*.manifest.template") 26 | if not manifests: 27 | sys.exit("Cannot find manifest templates in directory %r" % path) 28 | return manifests 29 | 30 | 31 | def main(): 32 | args = parse_args() 33 | path = os.path.abspath(args.PATH) 34 | 35 | if path.endswith(".manifest.template"): 36 | if not os.path.isfile(path): 37 | sys.exit("Cannot find file %r" % path) 38 | manifest_templates = [path] 39 | else: 40 | manifest_templates = get_manifest_templates(path) 41 | 42 | for manifest_template in manifest_templates: 43 | manifest = manifest_template[:-9] 44 | with open(manifest_template) as f_template: 45 | with open(manifest, "w+") as f_manifest: 46 | for line in f_template: 47 | line = line.replace("$(DATA_DIR)", DATA_DIR) 48 | line = line.replace("$(CONFIG_DIR)", CONFIG_DIR) 49 | line = line.replace("$(RUNTIME)", RUNTIME) 50 | line = line.replace("$(PYTHON_VERSION)", PYTHON_VERSION) 51 | line = line.replace("$(LIBPROTOBUF_VERSION)", LIBPROTOBUF_VERSION) 52 | line = line.replace("$(TESTS_DIR)", TESTS_DIR) 53 | f_manifest.write(line) 54 | 55 | 56 | if __name__ == "__main__": 57 | main() 58 | -------------------------------------------------------------------------------- /utils/sign_manifest.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import glob 4 | import os 5 | import subprocess 6 | import sys 7 | import argparse 8 | 9 | DIR = os.path.dirname(__file__) 10 | GRAPHENE_DIR = os.path.abspath(os.path.join(DIR, "../graphene/")) 11 | SGX_SIGN = os.path.join(GRAPHENE_DIR, "Pal/src/host/Linux-SGX/signer/pal-sgx-sign") 12 | SGX_GET_TOKEN = os.path.join(GRAPHENE_DIR, "Pal/src/host/Linux-SGX/signer/pal-sgx-get-token") 13 | LIBPAL = os.path.join(GRAPHENE_DIR, "Pal/src/libpal-enclave.so") 14 | KEY = os.path.join(GRAPHENE_DIR, "Pal/src/host/Linux-SGX/signer/enclave-key.pem") 15 | 16 | 17 | def execute(command): 18 | print("executing %r" % " ".join(command)) 19 | subprocess.check_call(command) 20 | 21 | 22 | def sign_manifest(manifest): 23 | output = manifest + ".sgx" 24 | command = [SGX_SIGN, 25 | "-libpal", LIBPAL, 26 | "-key", KEY, 27 | "-output", output, 28 | "-manifest", manifest 29 | ] 30 | execute(command) 31 | 32 | 33 | def create_token(manifest): 34 | token = manifest[:-9] + ".token" 35 | sig = manifest[:-9] + ".sig" 36 | command = [SGX_GET_TOKEN, 37 | "-output", token, 38 | "-sig", sig] 39 | execute(command) 40 | 41 | 42 | def get_manifests(path): 43 | manifests = glob.glob(path + "/*.manifest") 44 | if not manifests: 45 | sys.exit("Cannot find manifest in directory %r" % path) 46 | return manifests 47 | 48 | 49 | def parse_args(): 50 | parser = argparse.ArgumentParser() 51 | parser.add_argument("PATH", nargs='?', default=".", help="Path to the manifest file or the directory containing the manifest files") 52 | return parser.parse_args() 53 | 54 | 55 | def main(): 56 | args = parse_args() 57 | path = os.path.abspath(args.PATH) 58 | 59 | if path.endswith(".manifest"): 60 | if not os.path.isfile(path): 61 | sys.exit("Cannot find file %r" % path) 62 | manifests = [path] 63 | else: 64 | manifests = get_manifests(path) 65 | 66 | for manifest in manifests: 67 | sign_manifest(manifest) 68 | create_token(manifest) 69 | 70 | 71 | if __name__ == "__main__": 72 | main() 73 | --------------------------------------------------------------------------------