xref: /onnv-gate/usr/src/lib/libkmsagent/common/KMSAgentPKIKey.cpp (revision 12720:3db6e0082404)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /**
27  * \file KMSAgentPKIKey.cpp
28  */
29 #include <stdio.h>
30 
31 #include "SYSCommon.h"
32 #include "KMSAgentPKICommon.h"
33 #include "KMSAgentPKIimpl.h"
34 
35 ///////////////////////////////////////////////////////////////////////////////////////
36 // public key methods
37 ///////////////////////////////////////////////////////////////////////////////////////
CPublicKey()38 CPublicKey::CPublicKey()
39 {
40    m_pPublicKeyImpl = InitializePKeyImpl();
41 
42    FATAL_ASSERT( m_pPublicKeyImpl != NULL );
43 
44 }
45 
46 /**
47  * This method saves public key into a buffer,
48  * it also returns the actual used buffer length.
49  * @param i_pcBuffer Buffer to receive public key
50  * @param i_iBufferLength length of the buffer provided
51  * @param o_pActualLength actual length of the public key stored into the buffer
52  * @param i_iFormat key format, @see EnumPKIFileFormat
53  */
Save(unsigned char * const i_pcBuffer,int i_iBufferLength,int * const o_pActualLength,int i_iFormat)54 bool CPublicKey::Save(  unsigned char * const      i_pcBuffer,
55                         int                        i_iBufferLength,
56                         int * const                o_pActualLength,
57                         int                        i_iFormat )
58 {
59    return SavePublicKeyToBuffer( m_pPublicKeyImpl,
60                                  i_pcBuffer,
61                                  i_iBufferLength,
62                                  o_pActualLength,
63                                  i_iFormat );
64 }
65 
Load(unsigned char * const i_pcBuffer,int i_iLength,int i_iFormat)66 bool CPublicKey::Load(unsigned char * const i_pcBuffer,
67                        int                   i_iLength,
68                        int                   i_iFormat)
69 {
70    return LoadPublicKeyFromBuffer( m_pPublicKeyImpl,
71                                     i_pcBuffer,
72                                     i_iLength,
73                                     i_iFormat );
74 }
75 
Encrypt(int i_iLength,const unsigned char * const i_pcPlainText,unsigned char * const o_pcCypherText,int * const o_pActualLength)76 bool CPublicKey::Encrypt (int i_iLength,
77                   const unsigned char * const i_pcPlainText,
78                   unsigned char * const o_pcCypherText,
79                   int * const o_pActualLength)
80 {
81     return PublicKeyEncrypt(i_iLength,i_pcPlainText,o_pcCypherText,o_pActualLength, m_pPublicKeyImpl );
82 }
83 
~CPublicKey()84 CPublicKey::~CPublicKey()
85 {
86    if(m_pPublicKeyImpl != NULL)
87    {
88       FinalizePKeyImpl( m_pPublicKeyImpl );
89    }
90 }
91 
92 ///////////////////////////////////////////////////////////////////////////////////////
93 // private key methods
94 ///////////////////////////////////////////////////////////////////////////////////////
95 
CPrivateKey()96 CPrivateKey::CPrivateKey()
97 {
98    m_pPKeyImpl = InitializePKeyImpl();
99 
100    FATAL_ASSERT( m_pPKeyImpl != NULL );
101 
102 }
103 
104 /**
105  * This method saves private key into a buffer,
106  * it also returns the actual used buffer length.
107  */
Save(unsigned char * const i_pcBuffer,int i_iBufferLength,int * const o_pActualLength,const char * const i_pPassphrase,int i_iFormat)108 bool CPrivateKey::Save( unsigned char * const      i_pcBuffer,
109                         int                        i_iBufferLength,
110                         int * const                o_pActualLength,
111                         const char * const         i_pPassphrase,
112                         int                        i_iFormat )
113 {
114    return SavePrivateKeyToBuffer(m_pPKeyImpl,
115                                  i_pcBuffer,
116                                  i_iBufferLength,
117                                  o_pActualLength,
118                                  i_pPassphrase,
119                                  i_iFormat );
120 }
121 
Load(unsigned char * const i_pcBuffer,int i_iLength,const char * const i_pPassphrase,int i_iFormat)122 bool CPrivateKey::Load(unsigned char * const i_pcBuffer,
123                        int                   i_iLength,
124                        const char * const    i_pPassphrase,
125                        int                   i_iFormat)
126 {
127    return LoadPrivateKeyFromBuffer( m_pPKeyImpl,
128                                     i_pcBuffer,
129                                     i_iLength,
130                                     i_pPassphrase,
131                                     i_iFormat );
132 }
133 
~CPrivateKey()134 CPrivateKey::~CPrivateKey()
135 {
136    if(m_pPKeyImpl != NULL)
137    {
138       FinalizePKeyImpl( m_pPKeyImpl );
139    }
140 }
141 #ifdef KMSUSERPKCS12
142 void
GetNative()143 *CPrivateKey::GetNative()
144 {
145 	return GetPKey(m_pPKeyImpl);
146 }
147 void
SetNative(void * pKey)148 CPrivateKey::SetNative(void *pKey)
149 {
150 	SetPKey(m_pPKeyImpl, pKey);
151 	return;
152 }
153 #endif
154