github.com/jlmucb/cloudproxy@v0.0.0-20170830161738-b5aa0b619bc4/go/apps/simpleexample/SimpleClientCpp/junkyard/helpers.h.old (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 <errno.h>
    31  
    32  #include <openssl/ssl.h>
    33  #include <openssl/rsa.h>
    34  #include <openssl/x509.h>
    35  #include <openssl/x509v3.h>
    36  #include <openssl/bn.h>
    37  
    38  #include "messages.pb.h"
    39  
    40  #include <string>
    41  #include <memory>
    42  
    43  using std::string;
    44  
    45  #define AESBLKSIZE 16
    46  
    47  #ifndef byte
    48  typedef unsigned char byte;
    49  typedef long long unsigned int64;
    50  #endif
    51  
    52  void PrintBytes(int n, byte* in);
    53  bool ReadFile(string& file_name, string* out);
    54  bool WriteFile(string& file_name, string& in);
    55  
    56  bool SerializePrivateKey(string& key_type, EVP_PKEY* key, string* out_buf);
    57  bool DeserializePrivateKey(string& in_buf, string* key_type, EVP_PKEY** key);
    58  
    59  EVP_PKEY* GenerateKey(string& keyType, int keySize);
    60  bool GenerateX509CertificateRequest(string& key_type, string& common_name,
    61              EVP_PKEY* subjectKey, bool sign_request, X509_REQ* req);
    62  bool SignX509Certificate(EVP_PKEY* signingKey, bool f_isCa, bool f_canSign,
    63                           string& signing_issuer,string& keyUsage,
    64                           string& extendedKeyUsage,
    65                           int64 duration, EVP_PKEY* signedKey,
    66                           X509_REQ* req, bool verify_req_sig, X509* cert);
    67  bool VerifyX509CertificateChain(X509* cacert, X509* cert);
    68  
    69  BIGNUM* bin_to_BN(int len, byte* buf);
    70  string* BN_to_bin(BIGNUM& n);
    71  bool BN_to_string(BIGNUM& n, string* out);
    72  
    73  void XorBlocks(int size, byte* in1, byte* in2, byte* out);
    74  bool AesCtrCrypt(int key_size_bits, byte* key, int size,
    75                   byte* in, byte* out);
    76  bool AesCFBEncrypt(byte* key, int in_size, byte* in, int iv_size, byte* iv,
    77                     int* out_size, byte* out);
    78  bool AesCFBDecrypt(byte* key, int in_size, byte* in, int iv_size, byte* iv,
    79                     int* out_size, byte* out);
    80  
    81  #define SSL_NO_SERVER_VERIFY_NO_CLIENT_AUTH 0
    82  #define SSL_NO_SERVER_VERIFY_NO_CLIENT_VERIFY 1
    83  #define SSL_SERVER_VERIFY_NO_CLIENT_VERIFY 2
    84  #define SSL_SERVER_VERIFY_CLIENT_VERIFY 3
    85  
    86  int SslMessageRead(SSL* ssl, int size, byte* buf);
    87  int SslMessageWrite(SSL* ssl, int size, byte* buf);
    88  int SslRead(SSL* ssl, int size, byte* buf);
    89  int SslWrite(SSL* ssl, int size, byte* buf);
    90  
    91  class SslChannel {
    92  private:
    93    bool server_role_;
    94    int fd_;
    95    SSL_CTX *ssl_ctx_;
    96    SSL* ssl_;
    97    X509* peer_cert_;
    98    X509_STORE *store_;
    99    EVP_PKEY* private_key_;
   100  public:
   101    SslChannel();
   102    ~SslChannel();
   103  
   104    int CreateClientSocket(string& addr, string& port);
   105    int CreateServerSocket(string& addr, string& port);
   106    bool InitClientSslChannel(string& network, string& address, string& port,
   107                                  X509* caCert, X509* programCert,
   108                                  string& keyType, EVP_PKEY* key,
   109                                  int verify = SSL_SERVER_VERIFY_CLIENT_VERIFY);
   110    bool InitServerSslChannel(string& network, string& address, string& port,
   111                                  X509* caCert, X509* programCert,
   112                                  string& keyType, EVP_PKEY* key,
   113                                  int verify = SSL_SERVER_VERIFY_CLIENT_VERIFY);
   114    bool ServerLoop(void(*Handle)(SslChannel*,  SSL*, int));
   115    void Close();
   116    SSL* GetSslChannel() {return ssl_;};
   117  
   118    X509* GetPeerCert();
   119  };
   120  
   121  char ValueToHex(byte x);
   122  byte HexToValue(char x);
   123  
   124  string* ByteToHexLeftToRight(int, byte*);
   125  string* ByteToHexRightToLeft(int, byte*);
   126  int HexToByteLeftToRight(char*, int, byte*);
   127  int HexToByteRightToLeft(char*, int, byte*);
   128  
   129  #endif
   130