github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/src/tpm2/openssl_helpers.h (about) 1 // Copyright 2015 Google Corporation, All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // http://www.apache.org/licenses/LICENSE-2.0 7 // or in the the file LICENSE-2.0.txt in the top level sourcedirectory 8 // Unless required by applicable law or agreed to in writing, software 9 // distributed under the License is distributed on an "AS IS" BASIS, 10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 // See the License for the specific language governing permissions and 12 // limitations under the License 13 // 14 // Portions of this code were derived TPM2.0-TSS published 15 // by Intel under the license set forth in intel_license.txt 16 // and downloaded on or about August 6, 2015. 17 // File: openssl_helpers.cc 18 19 // standard buffer size 20 21 #ifndef __OPENSSL_HELPERS__ 22 #define __OPENSSL_HELPERS__ 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <sys/types.h> 26 #include <sys/stat.h> 27 #include <fcntl.h> 28 #include <unistd.h> 29 #include <string.h> 30 #include <tpm20.h> 31 #include <tpm2_lib.h> 32 #include <errno.h> 33 34 #include <tpm2.pb.h> 35 #include <openssl/rsa.h> 36 #include <openssl/x509.h> 37 #include <openssl/x509v3.h> 38 39 #include <string> 40 using std::string; 41 42 bool GenerateX509CertificateRequest(x509_cert_request_parameters_message& params, 43 bool sign_request, X509_REQ* req); 44 bool GetPublicRsaParametersFromSSLKey(RSA& rsa, rsa_public_key_message* key_msg); 45 bool GetPrivateRsaParametersFromSSLKey(RSA& rsa, 46 rsa_private_key_message* key_msg); 47 bool SignX509Certificate(RSA* signing_key, bool isCa, 48 signing_instructions_message& signing_instructions, 49 EVP_PKEY* signedKey, 50 X509_REQ* req, bool verify_req_sig, X509* cert); 51 bool VerifyX509CertificateChain(certificate_chain_message& chain); 52 bool GetCertificateRequestParametersFromX509(X509_REQ& x509_req, 53 cert_parameters_message* cert_params); 54 bool GetCertificateParametersFromX509(X509& x509_cert, 55 cert_parameters_message* cert_params); 56 bool GetPublicRsaKeyFromParameters(const rsa_public_key_message& key_msg, 57 RSA* rsa); 58 bool GetPrivateRsaKeyFromParameters(const rsa_private_key_message& key_msg, 59 RSA* rsa); 60 61 void print_internal_private_key(RSA& key); 62 void print_cert_request_message(x509_cert_request_parameters_message& 63 req_message); 64 65 BIGNUM* bin_to_BN(int len, byte* buf); 66 string* BN_to_bin(BIGNUM& n); 67 68 void XorBlocks(int size, byte* in1, byte* in2, byte* out); 69 bool AesCtrCrypt(int key_size_bits, byte* key, int size, 70 byte* in, byte* out); 71 bool KDFa(uint16_t hashAlg, string& key, string& label, string& contextU, 72 string& contextV, int bits, int out_size, byte* out); 73 bool AesCFBEncrypt(byte* key, int in_size, byte* in, int iv_size, byte* iv, 74 int* out_size, byte* out); 75 bool AesCFBDecrypt(byte* key, int in_size, byte* in, int iv_size, byte* iv, 76 int* out_size, byte* out); 77 int SizeHash(TPM_ALG_ID hash); 78 #endif 79