1*12720SWyllys.Ingersoll@Sun.COM /* 2*12720SWyllys.Ingersoll@Sun.COM * CDDL HEADER START 3*12720SWyllys.Ingersoll@Sun.COM * 4*12720SWyllys.Ingersoll@Sun.COM * The contents of this file are subject to the terms of the 5*12720SWyllys.Ingersoll@Sun.COM * Common Development and Distribution License (the "License"). 6*12720SWyllys.Ingersoll@Sun.COM * You may not use this file except in compliance with the License. 7*12720SWyllys.Ingersoll@Sun.COM * 8*12720SWyllys.Ingersoll@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*12720SWyllys.Ingersoll@Sun.COM * or http://www.opensolaris.org/os/licensing. 10*12720SWyllys.Ingersoll@Sun.COM * See the License for the specific language governing permissions 11*12720SWyllys.Ingersoll@Sun.COM * and limitations under the License. 12*12720SWyllys.Ingersoll@Sun.COM * 13*12720SWyllys.Ingersoll@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each 14*12720SWyllys.Ingersoll@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*12720SWyllys.Ingersoll@Sun.COM * If applicable, add the following below this CDDL HEADER, with the 16*12720SWyllys.Ingersoll@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying 17*12720SWyllys.Ingersoll@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner] 18*12720SWyllys.Ingersoll@Sun.COM * 19*12720SWyllys.Ingersoll@Sun.COM * CDDL HEADER END 20*12720SWyllys.Ingersoll@Sun.COM */ 21*12720SWyllys.Ingersoll@Sun.COM 22*12720SWyllys.Ingersoll@Sun.COM /* 23*12720SWyllys.Ingersoll@Sun.COM * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 24*12720SWyllys.Ingersoll@Sun.COM */ 25*12720SWyllys.Ingersoll@Sun.COM 26*12720SWyllys.Ingersoll@Sun.COM /*-------------------------------------------------------------------------*/ 27*12720SWyllys.Ingersoll@Sun.COM /** 28*12720SWyllys.Ingersoll@Sun.COM * \file KMSAgentPKICommon.h 29*12720SWyllys.Ingersoll@Sun.COM * 30*12720SWyllys.Ingersoll@Sun.COM * X.509 Certificate and Private Key Support Interface 31*12720SWyllys.Ingersoll@Sun.COM * 32*12720SWyllys.Ingersoll@Sun.COM * This module provides simple interfaces to support SSL communication 33*12720SWyllys.Ingersoll@Sun.COM * for the KMS Agent enrollment protocol. Basic classes supporting 34*12720SWyllys.Ingersoll@Sun.COM * X.509 certificates, private key management are provided and hide 35*12720SWyllys.Ingersoll@Sun.COM * specific implementations from users of these classes. 36*12720SWyllys.Ingersoll@Sun.COM */ 37*12720SWyllys.Ingersoll@Sun.COM /*-------------------------------------------------------------------------*/ 38*12720SWyllys.Ingersoll@Sun.COM 39*12720SWyllys.Ingersoll@Sun.COM #ifndef K_KMSAgentPKICommon_h 40*12720SWyllys.Ingersoll@Sun.COM #define K_KMSAgentPKICommon_h 41*12720SWyllys.Ingersoll@Sun.COM 42*12720SWyllys.Ingersoll@Sun.COM #ifdef WIN32 43*12720SWyllys.Ingersoll@Sun.COM #pragma warning(disable: 4786) 44*12720SWyllys.Ingersoll@Sun.COM #endif 45*12720SWyllys.Ingersoll@Sun.COM 46*12720SWyllys.Ingersoll@Sun.COM #define MAX_CERT_SIZE 4096 47*12720SWyllys.Ingersoll@Sun.COM #define MAX_KEY_SIZE 4096 48*12720SWyllys.Ingersoll@Sun.COM 49*12720SWyllys.Ingersoll@Sun.COM #define DEFAULT_KEY_SIZE 2048 50*12720SWyllys.Ingersoll@Sun.COM 51*12720SWyllys.Ingersoll@Sun.COM #ifdef KMSUSERPKCS12 52*12720SWyllys.Ingersoll@Sun.COM enum EnumPKIFileFormat { FILE_FORMAT_DER, FILE_FORMAT_PEM, FILE_FORMAT_PKCS12 }; 53*12720SWyllys.Ingersoll@Sun.COM #else 54*12720SWyllys.Ingersoll@Sun.COM enum EnumPKIFileFormat { FILE_FORMAT_DER, FILE_FORMAT_PEM }; 55*12720SWyllys.Ingersoll@Sun.COM #endif 56*12720SWyllys.Ingersoll@Sun.COM 57*12720SWyllys.Ingersoll@Sun.COM /** 58*12720SWyllys.Ingersoll@Sun.COM * This class provides a simple interface for the management of 59*12720SWyllys.Ingersoll@Sun.COM * public keys. Simple load and store operations are provided for 60*12720SWyllys.Ingersoll@Sun.COM * storage and retrieval from memory buffers. 61*12720SWyllys.Ingersoll@Sun.COM */ 62*12720SWyllys.Ingersoll@Sun.COM class CPublicKey 63*12720SWyllys.Ingersoll@Sun.COM { 64*12720SWyllys.Ingersoll@Sun.COM 65*12720SWyllys.Ingersoll@Sun.COM public: 66*12720SWyllys.Ingersoll@Sun.COM 67*12720SWyllys.Ingersoll@Sun.COM CPublicKey(); 68*12720SWyllys.Ingersoll@Sun.COM 69*12720SWyllys.Ingersoll@Sun.COM /** 70*12720SWyllys.Ingersoll@Sun.COM * This method saves public key into a buffer, 71*12720SWyllys.Ingersoll@Sun.COM * it also returns the actual used buffer length. 72*12720SWyllys.Ingersoll@Sun.COM * @param i_pcBuffer Buffer to receive public key 73*12720SWyllys.Ingersoll@Sun.COM * @param i_iBufferLength length of the buffer provided 74*12720SWyllys.Ingersoll@Sun.COM * @param o_pActualLength actual length of the public key stored into the buffer 75*12720SWyllys.Ingersoll@Sun.COM * @param i_iFormat key format, @see EnumPKIFileFormat 76*12720SWyllys.Ingersoll@Sun.COM */ 77*12720SWyllys.Ingersoll@Sun.COM bool Save(unsigned char * const i_pcBuffer, 78*12720SWyllys.Ingersoll@Sun.COM int i_iBufferLength, 79*12720SWyllys.Ingersoll@Sun.COM int * const o_pActualLength, 80*12720SWyllys.Ingersoll@Sun.COM int i_iFormat); 81*12720SWyllys.Ingersoll@Sun.COM /** 82*12720SWyllys.Ingersoll@Sun.COM * This method loads the public key from a buffer 83*12720SWyllys.Ingersoll@Sun.COM * @param i_pcBuffer 84*12720SWyllys.Ingersoll@Sun.COM * @param i_iLength 85*12720SWyllys.Ingersoll@Sun.COM * @param i_iFormat one of the enums from EnumPKIFileFormat, 86*12720SWyllys.Ingersoll@Sun.COM * only FILE_FORMAT_PEM is supported. 87*12720SWyllys.Ingersoll@Sun.COM * @return true for success, false otherwise 88*12720SWyllys.Ingersoll@Sun.COM */ 89*12720SWyllys.Ingersoll@Sun.COM bool Load (unsigned char * const i_pcBuffer, 90*12720SWyllys.Ingersoll@Sun.COM int i_iLength, 91*12720SWyllys.Ingersoll@Sun.COM int i_iFormat); 92*12720SWyllys.Ingersoll@Sun.COM 93*12720SWyllys.Ingersoll@Sun.COM /** 94*12720SWyllys.Ingersoll@Sun.COM * use this object's public key to encrypt plaintext buffer 95*12720SWyllys.Ingersoll@Sun.COM */ 96*12720SWyllys.Ingersoll@Sun.COM bool Encrypt (int i_iLength, 97*12720SWyllys.Ingersoll@Sun.COM const unsigned char * const i_pcPlainText, 98*12720SWyllys.Ingersoll@Sun.COM unsigned char * const o_pcCypherText, 99*12720SWyllys.Ingersoll@Sun.COM int * const o_pActualLength); 100*12720SWyllys.Ingersoll@Sun.COM 101*12720SWyllys.Ingersoll@Sun.COM ~CPublicKey(); 102*12720SWyllys.Ingersoll@Sun.COM 103*12720SWyllys.Ingersoll@Sun.COM private: 104*12720SWyllys.Ingersoll@Sun.COM void *m_pPublicKeyImpl; 105*12720SWyllys.Ingersoll@Sun.COM }; 106*12720SWyllys.Ingersoll@Sun.COM 107*12720SWyllys.Ingersoll@Sun.COM /** 108*12720SWyllys.Ingersoll@Sun.COM * This class provides a simple interface for the management of 109*12720SWyllys.Ingersoll@Sun.COM * private keys. Simple load and store operations are provided for 110*12720SWyllys.Ingersoll@Sun.COM * storage and retrieval from memory buffers. 111*12720SWyllys.Ingersoll@Sun.COM * 112*12720SWyllys.Ingersoll@Sun.COM */ 113*12720SWyllys.Ingersoll@Sun.COM class CPrivateKey 114*12720SWyllys.Ingersoll@Sun.COM { 115*12720SWyllys.Ingersoll@Sun.COM 116*12720SWyllys.Ingersoll@Sun.COM public: 117*12720SWyllys.Ingersoll@Sun.COM 118*12720SWyllys.Ingersoll@Sun.COM CPrivateKey(); 119*12720SWyllys.Ingersoll@Sun.COM 120*12720SWyllys.Ingersoll@Sun.COM /** 121*12720SWyllys.Ingersoll@Sun.COM * Saves the private key to a memory buffer specified by 122*12720SWyllys.Ingersoll@Sun.COM * i_pcBuffer. Currently just the PEM format is supported. 123*12720SWyllys.Ingersoll@Sun.COM * Specification of a passphrase allows encryption of the private 124*12720SWyllys.Ingersoll@Sun.COM * key subject to the choice of the implementation. 125*12720SWyllys.Ingersoll@Sun.COM * 126*12720SWyllys.Ingersoll@Sun.COM * @param[in] i_pcBuffer 127*12720SWyllys.Ingersoll@Sun.COM * @param[in] i_iBufferLength 128*12720SWyllys.Ingersoll@Sun.COM * @param[out] o_pActualLength 129*12720SWyllys.Ingersoll@Sun.COM * @param[in] i_pPassphrase optional, if non-null the private key is 130*12720SWyllys.Ingersoll@Sun.COM * wrapped using this passphrase 131*12720SWyllys.Ingersoll@Sun.COM * @param[in] i_iFormat one of the enums from EnumPKIFileFormat, 132*12720SWyllys.Ingersoll@Sun.COM * only FILE_FORMAT_PEM is supported. 133*12720SWyllys.Ingersoll@Sun.COM * @return true for success, false otherwise 134*12720SWyllys.Ingersoll@Sun.COM */ 135*12720SWyllys.Ingersoll@Sun.COM bool Save( unsigned char * const i_pcBuffer, 136*12720SWyllys.Ingersoll@Sun.COM int i_iBufferLength, 137*12720SWyllys.Ingersoll@Sun.COM int * const o_pActualLength, 138*12720SWyllys.Ingersoll@Sun.COM const char * const i_pPassphrase, 139*12720SWyllys.Ingersoll@Sun.COM int i_iFormat ); 140*12720SWyllys.Ingersoll@Sun.COM 141*12720SWyllys.Ingersoll@Sun.COM /** 142*12720SWyllys.Ingersoll@Sun.COM * This method loads the private key from a buffer 143*12720SWyllys.Ingersoll@Sun.COM * @param i_pcBuffer 144*12720SWyllys.Ingersoll@Sun.COM * @param i_iLength 145*12720SWyllys.Ingersoll@Sun.COM * @param i_pPassphrase optional, if non-null the private key is 146*12720SWyllys.Ingersoll@Sun.COM * unwrapped using this passphrase 147*12720SWyllys.Ingersoll@Sun.COM * @param i_iFormat one of the enums from EnumPKIFileFormat, 148*12720SWyllys.Ingersoll@Sun.COM * only FILE_FORMAT_PEM is supported. 149*12720SWyllys.Ingersoll@Sun.COM * @return true for success, false otherwise 150*12720SWyllys.Ingersoll@Sun.COM */ 151*12720SWyllys.Ingersoll@Sun.COM bool Load(unsigned char * const i_pcBuffer, 152*12720SWyllys.Ingersoll@Sun.COM int i_iLength, 153*12720SWyllys.Ingersoll@Sun.COM const char * const i_pPassphrase, 154*12720SWyllys.Ingersoll@Sun.COM int i_iFormat); 155*12720SWyllys.Ingersoll@Sun.COM 156*12720SWyllys.Ingersoll@Sun.COM ~CPrivateKey(); 157*12720SWyllys.Ingersoll@Sun.COM 158*12720SWyllys.Ingersoll@Sun.COM #ifdef KMSUSERPKCS12 159*12720SWyllys.Ingersoll@Sun.COM void *GetNative(); 160*12720SWyllys.Ingersoll@Sun.COM void SetNative(void *); 161*12720SWyllys.Ingersoll@Sun.COM #endif 162*12720SWyllys.Ingersoll@Sun.COM private: 163*12720SWyllys.Ingersoll@Sun.COM void *m_pPKeyImpl; 164*12720SWyllys.Ingersoll@Sun.COM 165*12720SWyllys.Ingersoll@Sun.COM }; 166*12720SWyllys.Ingersoll@Sun.COM 167*12720SWyllys.Ingersoll@Sun.COM /** 168*12720SWyllys.Ingersoll@Sun.COM * This class provides a simple interface for managing X.509 169*12720SWyllys.Ingersoll@Sun.COM * certificates providing only simple load and save operations for 170*12720SWyllys.Ingersoll@Sun.COM * storage and retrieval. 171*12720SWyllys.Ingersoll@Sun.COM * 172*12720SWyllys.Ingersoll@Sun.COM */ 173*12720SWyllys.Ingersoll@Sun.COM class CCertificate 174*12720SWyllys.Ingersoll@Sun.COM { 175*12720SWyllys.Ingersoll@Sun.COM 176*12720SWyllys.Ingersoll@Sun.COM public: 177*12720SWyllys.Ingersoll@Sun.COM CCertificate(); 178*12720SWyllys.Ingersoll@Sun.COM 179*12720SWyllys.Ingersoll@Sun.COM ~CCertificate(); 180*12720SWyllys.Ingersoll@Sun.COM 181*12720SWyllys.Ingersoll@Sun.COM /** 182*12720SWyllys.Ingersoll@Sun.COM * save the certificate to the specified file name. Currently, 183*12720SWyllys.Ingersoll@Sun.COM * only FILE_FORMAT_PEM is supported. 184*12720SWyllys.Ingersoll@Sun.COM */ 185*12720SWyllys.Ingersoll@Sun.COM bool Save( const char * const i_pcFileName, 186*12720SWyllys.Ingersoll@Sun.COM int i_iFormat); 187*12720SWyllys.Ingersoll@Sun.COM 188*12720SWyllys.Ingersoll@Sun.COM /** 189*12720SWyllys.Ingersoll@Sun.COM * save the certificate to the specified buffer. Currently, only 190*12720SWyllys.Ingersoll@Sun.COM * FILE_FORMAT_PEM is supported. 191*12720SWyllys.Ingersoll@Sun.COM */ 192*12720SWyllys.Ingersoll@Sun.COM bool Save( unsigned char * const i_pcBuffer, 193*12720SWyllys.Ingersoll@Sun.COM int i_iBufferLength, 194*12720SWyllys.Ingersoll@Sun.COM int * const o_pActualLength, 195*12720SWyllys.Ingersoll@Sun.COM int i_iFormat); 196*12720SWyllys.Ingersoll@Sun.COM 197*12720SWyllys.Ingersoll@Sun.COM /** 198*12720SWyllys.Ingersoll@Sun.COM * load a certificate from the specified filename. Currently, 199*12720SWyllys.Ingersoll@Sun.COM * only FILE_FORMAT_PEM is supported. 200*12720SWyllys.Ingersoll@Sun.COM */ 201*12720SWyllys.Ingersoll@Sun.COM bool Load( const char * const i_pcFileName, 202*12720SWyllys.Ingersoll@Sun.COM int i_iFormat ); 203*12720SWyllys.Ingersoll@Sun.COM 204*12720SWyllys.Ingersoll@Sun.COM /** 205*12720SWyllys.Ingersoll@Sun.COM * load a certificate from the specified buffer. Currently, only 206*12720SWyllys.Ingersoll@Sun.COM * FILE_FORMAT_PEM is supported. 207*12720SWyllys.Ingersoll@Sun.COM */ 208*12720SWyllys.Ingersoll@Sun.COM bool Load( unsigned char * const i_pcBuffer, 209*12720SWyllys.Ingersoll@Sun.COM int i_iLength, 210*12720SWyllys.Ingersoll@Sun.COM int i_iFormat ); 211*12720SWyllys.Ingersoll@Sun.COM 212*12720SWyllys.Ingersoll@Sun.COM /** 213*12720SWyllys.Ingersoll@Sun.COM * prints the certificate to stdout 214*12720SWyllys.Ingersoll@Sun.COM */ 215*12720SWyllys.Ingersoll@Sun.COM bool Dump(); 216*12720SWyllys.Ingersoll@Sun.COM 217*12720SWyllys.Ingersoll@Sun.COM #ifdef KMSUSERPKCS12 218*12720SWyllys.Ingersoll@Sun.COM bool LoadPKCS12CertAndKey(char *filename, 219*12720SWyllys.Ingersoll@Sun.COM int i_iFormat, 220*12720SWyllys.Ingersoll@Sun.COM CPrivateKey *i_pPrivateKey, 221*12720SWyllys.Ingersoll@Sun.COM char *i_pPassphrase); 222*12720SWyllys.Ingersoll@Sun.COM 223*12720SWyllys.Ingersoll@Sun.COM bool SavePKCS12( 224*12720SWyllys.Ingersoll@Sun.COM unsigned char *i_pcBuffer, 225*12720SWyllys.Ingersoll@Sun.COM int i_iBufferLength, 226*12720SWyllys.Ingersoll@Sun.COM int *o_pActualLength, 227*12720SWyllys.Ingersoll@Sun.COM CPrivateKey* i_pPrivateKey, 228*12720SWyllys.Ingersoll@Sun.COM char* i_sPassphrase ); 229*12720SWyllys.Ingersoll@Sun.COM #endif 230*12720SWyllys.Ingersoll@Sun.COM 231*12720SWyllys.Ingersoll@Sun.COM private: 232*12720SWyllys.Ingersoll@Sun.COM /** 233*12720SWyllys.Ingersoll@Sun.COM * an opague pointer to implementation specific resources to be 234*12720SWyllys.Ingersoll@Sun.COM * freed by the Destructor. 235*12720SWyllys.Ingersoll@Sun.COM */ 236*12720SWyllys.Ingersoll@Sun.COM void *m_pCertImpl; 237*12720SWyllys.Ingersoll@Sun.COM #ifdef KMSUSERPKCS12 238*12720SWyllys.Ingersoll@Sun.COM /** 239*12720SWyllys.Ingersoll@Sun.COM * saves certificate to PKCS#12 memory BIO 240*12720SWyllys.Ingersoll@Sun.COM * @param i_pPrivateKey 241*12720SWyllys.Ingersoll@Sun.COM * @param i_sPassphrase 242*12720SWyllys.Ingersoll@Sun.COM * @return pointer to the Memory BIO 243*12720SWyllys.Ingersoll@Sun.COM */ 244*12720SWyllys.Ingersoll@Sun.COM void* SaveCertToPKCS12MemoryBIO( 245*12720SWyllys.Ingersoll@Sun.COM CPrivateKey* i_pPrivateKey, 246*12720SWyllys.Ingersoll@Sun.COM char *i_sPassphrase); 247*12720SWyllys.Ingersoll@Sun.COM #endif 248*12720SWyllys.Ingersoll@Sun.COM 249*12720SWyllys.Ingersoll@Sun.COM }; 250*12720SWyllys.Ingersoll@Sun.COM 251*12720SWyllys.Ingersoll@Sun.COM 252*12720SWyllys.Ingersoll@Sun.COM /** 253*12720SWyllys.Ingersoll@Sun.COM * This class provides a method for storing an X.509 certificate and 254*12720SWyllys.Ingersoll@Sun.COM * private key to a file. The private key is appended to the 255*12720SWyllys.Ingersoll@Sun.COM * certificate and optionally encrypted with the specified passphrase 256*12720SWyllys.Ingersoll@Sun.COM * for encoding and storage in PEM format. 257*12720SWyllys.Ingersoll@Sun.COM */ 258*12720SWyllys.Ingersoll@Sun.COM class CPKI 259*12720SWyllys.Ingersoll@Sun.COM { 260*12720SWyllys.Ingersoll@Sun.COM public: 261*12720SWyllys.Ingersoll@Sun.COM CPKI(); 262*12720SWyllys.Ingersoll@Sun.COM ~CPKI(); 263*12720SWyllys.Ingersoll@Sun.COM 264*12720SWyllys.Ingersoll@Sun.COM public: 265*12720SWyllys.Ingersoll@Sun.COM 266*12720SWyllys.Ingersoll@Sun.COM /** 267*12720SWyllys.Ingersoll@Sun.COM * exports a certificate and associated private key to the 268*12720SWyllys.Ingersoll@Sun.COM * specified file. 269*12720SWyllys.Ingersoll@Sun.COM * @param i_pCertificate a pointer to an instance of a certificate 270*12720SWyllys.Ingersoll@Sun.COM * @param i_pPrivateKey a pointer to an instance of a private key 271*12720SWyllys.Ingersoll@Sun.COM * @param i_pcFileName the name of the file to store the cert and private key 272*12720SWyllys.Ingersoll@Sun.COM * @param i_sPassphrase optional but when provided supplies a 273*12720SWyllys.Ingersoll@Sun.COM * pass phrase to use for encrypting the private key. The cipher 274*12720SWyllys.Ingersoll@Sun.COM * used for encryption is determined by the underlying implementation 275*12720SWyllys.Ingersoll@Sun.COM * which for the reference implementation uses triple DES by default. 276*12720SWyllys.Ingersoll@Sun.COM * @param i_eFileFormat the encoding format to use for the certificate and private key 277*12720SWyllys.Ingersoll@Sun.COM */ 278*12720SWyllys.Ingersoll@Sun.COM bool ExportCertAndKeyToFile( 279*12720SWyllys.Ingersoll@Sun.COM CCertificate* const i_pCertificate, 280*12720SWyllys.Ingersoll@Sun.COM CPrivateKey* const i_pPrivateKey, 281*12720SWyllys.Ingersoll@Sun.COM const char* const i_pcFileName, 282*12720SWyllys.Ingersoll@Sun.COM const char* const i_sPassphrase, 283*12720SWyllys.Ingersoll@Sun.COM EnumPKIFileFormat i_eFileFormat ); 284*12720SWyllys.Ingersoll@Sun.COM 285*12720SWyllys.Ingersoll@Sun.COM private: 286*12720SWyllys.Ingersoll@Sun.COM 287*12720SWyllys.Ingersoll@Sun.COM int m_iKeyLength; 288*12720SWyllys.Ingersoll@Sun.COM 289*12720SWyllys.Ingersoll@Sun.COM CCertificate *m_pCACertificate; 290*12720SWyllys.Ingersoll@Sun.COM CPrivateKey *m_pCAPrivateKey; 291*12720SWyllys.Ingersoll@Sun.COM }; 292*12720SWyllys.Ingersoll@Sun.COM 293*12720SWyllys.Ingersoll@Sun.COM #endif //K_KMSAgentPKICommon_h 294