| 1 |
x509_openssl |
| 2 |
============= |
| 3 |
|
| 4 |
## Overview |
| 5 |
|
| 6 |
x509_openssl provides several utility functions. These are: |
| 7 |
- a utility function that imports into a SSL context a pair of x509 certificate/private key |
| 8 |
- a utility function that imports from a null terminated string all the certificates in a SSL_CTX*. |
| 9 |
|
| 10 |
## References |
| 11 |
|
| 12 |
[OpenSSL](https://www.openssl.org) |
| 13 |
|
| 14 |
```c |
| 15 |
int x509_openssl_add_credentials(SSL_CTX* ssl_ctx, const char* x509certificate, const char* x509privatekey); |
| 16 |
int x509_openssl_add_certificates(SSL_CTX, ssl_ctx, const char* certificates); |
| 17 |
int x509_openssl_add_ecc_credentials(SSL_CTX* ssl_ctx, const char* ecc_alias_cert, const char* ecc_alias_key); |
| 18 |
``` |
| 19 |
|
| 20 |
### x509_openssl_add_credentials |
| 21 |
```c |
| 22 |
int x509_openssl_add_credentials(SSL_CTX* ssl_ctx, const char* x509certificate, const char* x509privatekey); |
| 23 |
``` |
| 24 |
|
| 25 |
x509_openssl_add_credentials loads a x509 certificate and a x509 private key into a SSL context. |
| 26 |
|
| 27 |
**SRS_X509_OPENSSL_02_001: [** If any argument is NULL then x509_openssl_add_credentials shall fail and return a non-zero value. **]** |
| 28 |
|
| 29 |
**SRS_X509_OPENSSL_02_002: [** x509_openssl_add_credentials shall use BIO_new_mem_buf to create a memory BIO from the x509 certificate. **]** |
| 30 |
|
| 31 |
**SRS_X509_OPENSSL_02_003: [** x509_openssl_add_credentials shall use PEM_read_bio_X509 to read the x509 certificate. **]** |
| 32 |
|
| 33 |
**SRS_X509_OPENSSL_02_004: [** x509_openssl_add_credentials shall use BIO_new_mem_buf to create a memory BIO from the x509 privatekey. **]** |
| 34 |
|
| 35 |
**SRS_X509_OPENSSL_02_005: [** x509_openssl_add_credentials shall use PEM_read_bio_RSAPrivateKey to read the x509 private key. **]** |
| 36 |
|
| 37 |
**SRS_X509_OPENSSL_02_006: [** x509_openssl_add_credentials shall use SSL_CTX_use_certificate to load the certicate into the SSL context. **]** |
| 38 |
|
| 39 |
**SRS_X509_OPENSSL_02_007: [** x509_openssl_add_credentials shall use SSL_CTX_use_RSAPrivateKey to load the private key into the SSL context. **]** |
| 40 |
|
| 41 |
**SRS_X509_OPENSSL_02_008: [** If no error occurs, then x509_openssl_add_credentials shall succeed and return 0. **]** |
| 42 |
|
| 43 |
**SRS_X509_OPENSSL_02_009: [** Otherwise x509_openssl_add_credentials shall fail and return a non-zero number. **]** |
| 44 |
|
| 45 |
|
| 46 |
### x509_openssl_add_certificates |
| 47 |
```c |
| 48 |
int x509_openssl_add_certificates(SSL_CTX* ssl_ctx, const char* certificates); |
| 49 |
``` |
| 50 |
|
| 51 |
`x509_openssl_add_certificates` adds all the certificates in `certificates` to `ssl_ctx`. |
| 52 |
|
| 53 |
**SRS_X509_OPENSSL_02_010: [** If `ssl_ctx` is `NULL` then `x509_openssl_add_certificates` shall fail and return a non-zero value. **]** |
| 54 |
|
| 55 |
**SRS_X509_OPENSSL_02_011: [** If `certificates` is `NULL` then `x509_openssl_add_certificates` shall fail and return a non-zero value. **]** |
| 56 |
|
| 57 |
**SRS_X509_OPENSSL_02_012: [** `x509_openssl_add_certificates` shall get the memory BIO method function by calling `BIO_s_mem`. **]** |
| 58 |
|
| 59 |
**SRS_X509_OPENSSL_02_013: [** `x509_openssl_add_certificates` shall create a new memory BIO by calling `BIO_new`. **]** |
| 60 |
|
| 61 |
**SRS_X509_OPENSSL_02_014: [** `x509_openssl_add_certificates` shall load `certificates` into the memory BIO by a call to `BIO_puts`. **]** |
| 62 |
|
| 63 |
**SRS_X509_OPENSSL_02_015: [** `x509_openssl_add_certificates` shall retrieve each certificate by a call to `PEM_read_bio_X509`. **]** |
| 64 |
|
| 65 |
**SRS_X509_OPENSSL_02_016: [** `x509_openssl_add_certificates` shall add the certificate to the store by a call to `X509_STORE_add_cert`. **]** |
| 66 |
|
| 67 |
**SRS_X509_OPENSSL_02_017: [** If `X509_STORE_add_cert` returns with error and that error is `X509_R_CERT_ALREADY_IN_HASH_TABLE` then `x509_openssl_add_certificates` shall ignore it as the certificate is already in the store. **]** |
| 68 |
|
| 69 |
**SRS_X509_OPENSSL_02_018: [** In case of any failure `x509_openssl_add_certificates` shall fail and return a non-zero value. **]** |
| 70 |
|
| 71 |
**SRS_X509_OPENSSL_02_019: [** Otherwise, `x509_openssl_add_certificates` shall succeed and return 0. **]** |
| 72 |
|
| 73 |
### x509_openssl_add_ecc_credentials |
| 74 |
|
| 75 |
```c |
| 76 |
int x509_openssl_add_ecc_credentials(SSL_CTX* ssl_ctx, const char* ecc_alias_cert, const char* ecc_alias_key); |
| 77 |
``` |
| 78 |
|
| 79 |
**SRS_X509_OPENSSL_07_001: [** If `ssl_ctx`, `ecc_alias_cert`, or `ecc_alias_key` are NULL, `x509_openssl_add_ecc_credentials` shall return a non-zero value. **]** |
| 80 |
|
| 81 |
**SRS_X509_OPENSSL_07_002: [** `x509_openssl_add_ecc_credentials` shall get the memory BIO method function. **]** |
| 82 |
|
| 83 |
**SRS_X509_OPENSSL_07_003: [** `x509_openssl_add_ecc_credentials` shall generate a EVP_PKEY by calling `PEM_read_bio_PrivateKey`. **]** |
| 84 |
|
| 85 |
**SRS_X509_OPENSSL_07_004: [** `x509_openssl_add_ecc_credentials` shall import the certification using by the EVP_PKEY. **]** |
| 86 |
|
| 87 |
**SRS_X509_OPENSSL_07_005: [** `x509_openssl_add_ecc_credentials` shall load the cert chain by calling PEM_read_bio_X509_AUX and SSL_CTX_use_certification. **]** |
| 88 |
|
| 89 |
**SRS_X509_OPENSSL_07_006: [** If successful `x509_openssl_add_ecc_credentials` shall to import each certificate in the cert chain. **]** |
| 90 |
|
| 91 |
**SRS_X509_OPENSSL_07_007: [** If any failure is encountered `x509_openssl_add_ecc_credentials` shall return a non-zero value. **]** |
| 92 |
|