|
| 1 | +# OCI Request Signer for C |
| 2 | + |
| 3 | +This project provides a C implementation of [Oracle Cloud Infrastructure (OCI) request signature](https://docs.oracle.com/en-us/iaas/Content/API/Concepts/signingrequests.htm), suitable for use in embedded, kernel, or user-space applications. It includes a shared library and an example application demonstrating usage. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- Produces the HTTP `Authorization` header (key and value) for OCI requests, including the computed signature and all required metadata |
| 8 | +- OCI request signing for HTTP requests |
| 9 | +- No dynamic memory allocations |
| 10 | +- Support for DER format private keys |
| 11 | +- Default headers and automatic body hashing based on request method |
| 12 | +- Simple API for integration into C projects |
| 13 | +- Example usage and tests included |
| 14 | +- System header configurability via `OCI_SYSTEM_HEADER` |
| 15 | + |
| 16 | +## Building |
| 17 | + |
| 18 | +### Prerequisites |
| 19 | + |
| 20 | +- GCC or compatible C compiler |
| 21 | +- `pkg-config` utility |
| 22 | +- [OpenSSL](https://github.com/openssl/openssl) for testing and examples |
| 23 | +- [libcheck](https://github.com/libcheck/check) for testing |
| 24 | + |
| 25 | +### Build Instructions |
| 26 | + |
| 27 | +```sh |
| 28 | +sudo apt install libssl-dev check |
| 29 | +``` |
| 30 | + |
| 31 | +To build the shared library and example application, run: |
| 32 | + |
| 33 | +```sh |
| 34 | +make |
| 35 | +``` |
| 36 | + |
| 37 | +This will produce: |
| 38 | +- `libocisigner.so`: Shared library implementing OCI request signing |
| 39 | +- `example`: Example application using the library |
| 40 | + |
| 41 | +### Run Tests |
| 42 | + |
| 43 | +```sh |
| 44 | +make run-tests |
| 45 | +``` |
| 46 | + |
| 47 | +### Clean Build Artifacts |
| 48 | + |
| 49 | +```sh |
| 50 | +make clean |
| 51 | +``` |
| 52 | + |
| 53 | +## Usage |
| 54 | + |
| 55 | +### Private Key Format |
| 56 | + |
| 57 | +This library **only** accepts RSA private keys in DER format. The private key is stored in a dedicated binary data type (`oci_signer_binary_t`) to clearly indicate that it contains binary data rather than a string. |
| 58 | + |
| 59 | +If you have a PEM format key, you need to convert it to DER format before using it with this library: |
| 60 | + |
| 61 | +```sh |
| 62 | +# Convert PEM to DER format |
| 63 | +openssl rsa -in private_key.pem -outform DER -out private_key.der |
| 64 | +``` |
| 65 | + |
| 66 | +### Key ID Format |
| 67 | + |
| 68 | +The library supports two formats for the `key_id` parameter: |
| 69 | + |
| 70 | +1. **Standard format**: `tenancy/user/fingerprint` |
| 71 | + ``` |
| 72 | + ocid1.tenancy.oc1..aaaaaaaaba3pv6wkcr4jqae5f15p2b2m2yt2j6rx32uzr4h25vqstifsfdsq/ocid1.user.oc1..aaaaaaaat5nvwcna5j6aqzjcaty5eqbb6qt2jvpkanghtgdaqedqw3rynjq/20:3b:97:13:55:1c:5b:0d:d3:37:d8:50:4e:c5:3a:34 |
| 73 | + ``` |
| 74 | + |
| 75 | +2. **Session token format**: `ST$<user principal session token>` |
| 76 | + ``` |
| 77 | + ST$aaaaaaaa7tz3aaaaaaaaaymq2maaaaaaabfwiljtdnfgqaaaa |
| 78 | + ``` |
| 79 | + |
| 80 | +### System Header Configurability |
| 81 | + |
| 82 | +You can configure the system header used by the library by defining the macro `OCI_SYSTEM_HEADER` during compilation. This allows integration with custom or platform-specific headers as needed. |
| 83 | + |
| 84 | +Example: |
| 85 | +```sh |
| 86 | +gcc -DOCI_SYSTEM_HEADER='<your_header.h>' ... |
| 87 | +``` |
| 88 | + |
| 89 | +### Linking |
| 90 | + |
| 91 | +Include the header in your application: |
| 92 | + |
| 93 | +```c |
| 94 | +#include "oci_signer.h" |
| 95 | +``` |
| 96 | + |
| 97 | +Link against the shared library and OpenSSL: |
| 98 | + |
| 99 | +``` |
| 100 | +-L. -locisigner -lssl -lcrypto |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | +### Example Usage |
| 105 | + |
| 106 | +The main output of this library is the HTTP `Authorization` header, which you add to your request: |
| 107 | + |
| 108 | +```c |
| 109 | +oci_signer_header_t auth_header; |
| 110 | +// ... set up signer parameters ... |
| 111 | +oci_signer_sign(&signer_params, &auth_header, buffer_size); |
| 112 | +// Now add: |
| 113 | +// Header key: (char*)auth_header.key.data // will be "Authorization" |
| 114 | +// Header value: (char*)auth_header.value.data // contains the computed signature and metadata |
| 115 | +``` |
| 116 | +
|
| 117 | +See `example.c` for a complete usage demonstration, including examples of how to use custom crypto functions. |
| 118 | +
|
| 119 | +## Custom Crypto Implementation |
| 120 | +
|
| 121 | +For integration with custom environments (like Linux kernel modules), you can provide your own implementations of the required crypto functions: |
| 122 | +
|
| 123 | +1. Define `OCI_SYSTEM_HEADER` to include environment-specific headers instead of standard C library headers |
| 124 | +2. Provide custom implementations of the required crypto functions: |
| 125 | + - SHA256 hash function |
| 126 | + - RSA signing function |
| 127 | + - Base64 encoding function |
| 128 | +
|
| 129 | +## License |
| 130 | +
|
| 131 | +This project is licensed under the MIT License - see the LICENSE file for details. |
| 132 | +
|
| 133 | +## Acknowledgments |
| 134 | +
|
| 135 | +This implementation of computing the request authorization header is based on the [OCI Go SDK's HTTP signer](https://github.com/oracle/oci-go-sdk/blob/907df66346f4bd3b0e898b5c884422253e86cee3/common/http_signer.go#L244) |
0 commit comments