xref: /onnv-gate/usr/src/lib/libkmsagent/common/KMSAgentPKIKey.cpp (revision 12720:3db6e0082404)
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  * \file KMSAgentPKIKey.cpp
28*12720SWyllys.Ingersoll@Sun.COM  */
29*12720SWyllys.Ingersoll@Sun.COM #include <stdio.h>
30*12720SWyllys.Ingersoll@Sun.COM 
31*12720SWyllys.Ingersoll@Sun.COM #include "SYSCommon.h"
32*12720SWyllys.Ingersoll@Sun.COM #include "KMSAgentPKICommon.h"
33*12720SWyllys.Ingersoll@Sun.COM #include "KMSAgentPKIimpl.h"
34*12720SWyllys.Ingersoll@Sun.COM 
35*12720SWyllys.Ingersoll@Sun.COM ///////////////////////////////////////////////////////////////////////////////////////
36*12720SWyllys.Ingersoll@Sun.COM // public key methods
37*12720SWyllys.Ingersoll@Sun.COM ///////////////////////////////////////////////////////////////////////////////////////
CPublicKey()38*12720SWyllys.Ingersoll@Sun.COM CPublicKey::CPublicKey()
39*12720SWyllys.Ingersoll@Sun.COM {
40*12720SWyllys.Ingersoll@Sun.COM    m_pPublicKeyImpl = InitializePKeyImpl();
41*12720SWyllys.Ingersoll@Sun.COM 
42*12720SWyllys.Ingersoll@Sun.COM    FATAL_ASSERT( m_pPublicKeyImpl != NULL );
43*12720SWyllys.Ingersoll@Sun.COM 
44*12720SWyllys.Ingersoll@Sun.COM }
45*12720SWyllys.Ingersoll@Sun.COM 
46*12720SWyllys.Ingersoll@Sun.COM /**
47*12720SWyllys.Ingersoll@Sun.COM  * This method saves public key into a buffer,
48*12720SWyllys.Ingersoll@Sun.COM  * it also returns the actual used buffer length.
49*12720SWyllys.Ingersoll@Sun.COM  * @param i_pcBuffer Buffer to receive public key
50*12720SWyllys.Ingersoll@Sun.COM  * @param i_iBufferLength length of the buffer provided
51*12720SWyllys.Ingersoll@Sun.COM  * @param o_pActualLength actual length of the public key stored into the buffer
52*12720SWyllys.Ingersoll@Sun.COM  * @param i_iFormat key format, @see EnumPKIFileFormat
53*12720SWyllys.Ingersoll@Sun.COM  */
Save(unsigned char * const i_pcBuffer,int i_iBufferLength,int * const o_pActualLength,int i_iFormat)54*12720SWyllys.Ingersoll@Sun.COM bool CPublicKey::Save(  unsigned char * const      i_pcBuffer,
55*12720SWyllys.Ingersoll@Sun.COM                         int                        i_iBufferLength,
56*12720SWyllys.Ingersoll@Sun.COM                         int * const                o_pActualLength,
57*12720SWyllys.Ingersoll@Sun.COM                         int                        i_iFormat )
58*12720SWyllys.Ingersoll@Sun.COM {
59*12720SWyllys.Ingersoll@Sun.COM    return SavePublicKeyToBuffer( m_pPublicKeyImpl,
60*12720SWyllys.Ingersoll@Sun.COM                                  i_pcBuffer,
61*12720SWyllys.Ingersoll@Sun.COM                                  i_iBufferLength,
62*12720SWyllys.Ingersoll@Sun.COM                                  o_pActualLength,
63*12720SWyllys.Ingersoll@Sun.COM                                  i_iFormat );
64*12720SWyllys.Ingersoll@Sun.COM }
65*12720SWyllys.Ingersoll@Sun.COM 
Load(unsigned char * const i_pcBuffer,int i_iLength,int i_iFormat)66*12720SWyllys.Ingersoll@Sun.COM bool CPublicKey::Load(unsigned char * const i_pcBuffer,
67*12720SWyllys.Ingersoll@Sun.COM                        int                   i_iLength,
68*12720SWyllys.Ingersoll@Sun.COM                        int                   i_iFormat)
69*12720SWyllys.Ingersoll@Sun.COM {
70*12720SWyllys.Ingersoll@Sun.COM    return LoadPublicKeyFromBuffer( m_pPublicKeyImpl,
71*12720SWyllys.Ingersoll@Sun.COM                                     i_pcBuffer,
72*12720SWyllys.Ingersoll@Sun.COM                                     i_iLength,
73*12720SWyllys.Ingersoll@Sun.COM                                     i_iFormat );
74*12720SWyllys.Ingersoll@Sun.COM }
75*12720SWyllys.Ingersoll@Sun.COM 
Encrypt(int i_iLength,const unsigned char * const i_pcPlainText,unsigned char * const o_pcCypherText,int * const o_pActualLength)76*12720SWyllys.Ingersoll@Sun.COM bool CPublicKey::Encrypt (int i_iLength,
77*12720SWyllys.Ingersoll@Sun.COM                   const unsigned char * const i_pcPlainText,
78*12720SWyllys.Ingersoll@Sun.COM                   unsigned char * const o_pcCypherText,
79*12720SWyllys.Ingersoll@Sun.COM                   int * const o_pActualLength)
80*12720SWyllys.Ingersoll@Sun.COM {
81*12720SWyllys.Ingersoll@Sun.COM     return PublicKeyEncrypt(i_iLength,i_pcPlainText,o_pcCypherText,o_pActualLength, m_pPublicKeyImpl );
82*12720SWyllys.Ingersoll@Sun.COM }
83*12720SWyllys.Ingersoll@Sun.COM 
~CPublicKey()84*12720SWyllys.Ingersoll@Sun.COM CPublicKey::~CPublicKey()
85*12720SWyllys.Ingersoll@Sun.COM {
86*12720SWyllys.Ingersoll@Sun.COM    if(m_pPublicKeyImpl != NULL)
87*12720SWyllys.Ingersoll@Sun.COM    {
88*12720SWyllys.Ingersoll@Sun.COM       FinalizePKeyImpl( m_pPublicKeyImpl );
89*12720SWyllys.Ingersoll@Sun.COM    }
90*12720SWyllys.Ingersoll@Sun.COM }
91*12720SWyllys.Ingersoll@Sun.COM 
92*12720SWyllys.Ingersoll@Sun.COM ///////////////////////////////////////////////////////////////////////////////////////
93*12720SWyllys.Ingersoll@Sun.COM // private key methods
94*12720SWyllys.Ingersoll@Sun.COM ///////////////////////////////////////////////////////////////////////////////////////
95*12720SWyllys.Ingersoll@Sun.COM 
CPrivateKey()96*12720SWyllys.Ingersoll@Sun.COM CPrivateKey::CPrivateKey()
97*12720SWyllys.Ingersoll@Sun.COM {
98*12720SWyllys.Ingersoll@Sun.COM    m_pPKeyImpl = InitializePKeyImpl();
99*12720SWyllys.Ingersoll@Sun.COM 
100*12720SWyllys.Ingersoll@Sun.COM    FATAL_ASSERT( m_pPKeyImpl != NULL );
101*12720SWyllys.Ingersoll@Sun.COM 
102*12720SWyllys.Ingersoll@Sun.COM }
103*12720SWyllys.Ingersoll@Sun.COM 
104*12720SWyllys.Ingersoll@Sun.COM /**
105*12720SWyllys.Ingersoll@Sun.COM  * This method saves private key into a buffer,
106*12720SWyllys.Ingersoll@Sun.COM  * it also returns the actual used buffer length.
107*12720SWyllys.Ingersoll@Sun.COM  */
Save(unsigned char * const i_pcBuffer,int i_iBufferLength,int * const o_pActualLength,const char * const i_pPassphrase,int i_iFormat)108*12720SWyllys.Ingersoll@Sun.COM bool CPrivateKey::Save( unsigned char * const      i_pcBuffer,
109*12720SWyllys.Ingersoll@Sun.COM                         int                        i_iBufferLength,
110*12720SWyllys.Ingersoll@Sun.COM                         int * const                o_pActualLength,
111*12720SWyllys.Ingersoll@Sun.COM                         const char * const         i_pPassphrase,
112*12720SWyllys.Ingersoll@Sun.COM                         int                        i_iFormat )
113*12720SWyllys.Ingersoll@Sun.COM {
114*12720SWyllys.Ingersoll@Sun.COM    return SavePrivateKeyToBuffer(m_pPKeyImpl,
115*12720SWyllys.Ingersoll@Sun.COM                                  i_pcBuffer,
116*12720SWyllys.Ingersoll@Sun.COM                                  i_iBufferLength,
117*12720SWyllys.Ingersoll@Sun.COM                                  o_pActualLength,
118*12720SWyllys.Ingersoll@Sun.COM                                  i_pPassphrase,
119*12720SWyllys.Ingersoll@Sun.COM                                  i_iFormat );
120*12720SWyllys.Ingersoll@Sun.COM }
121*12720SWyllys.Ingersoll@Sun.COM 
Load(unsigned char * const i_pcBuffer,int i_iLength,const char * const i_pPassphrase,int i_iFormat)122*12720SWyllys.Ingersoll@Sun.COM bool CPrivateKey::Load(unsigned char * const i_pcBuffer,
123*12720SWyllys.Ingersoll@Sun.COM                        int                   i_iLength,
124*12720SWyllys.Ingersoll@Sun.COM                        const char * const    i_pPassphrase,
125*12720SWyllys.Ingersoll@Sun.COM                        int                   i_iFormat)
126*12720SWyllys.Ingersoll@Sun.COM {
127*12720SWyllys.Ingersoll@Sun.COM    return LoadPrivateKeyFromBuffer( m_pPKeyImpl,
128*12720SWyllys.Ingersoll@Sun.COM                                     i_pcBuffer,
129*12720SWyllys.Ingersoll@Sun.COM                                     i_iLength,
130*12720SWyllys.Ingersoll@Sun.COM                                     i_pPassphrase,
131*12720SWyllys.Ingersoll@Sun.COM                                     i_iFormat );
132*12720SWyllys.Ingersoll@Sun.COM }
133*12720SWyllys.Ingersoll@Sun.COM 
~CPrivateKey()134*12720SWyllys.Ingersoll@Sun.COM CPrivateKey::~CPrivateKey()
135*12720SWyllys.Ingersoll@Sun.COM {
136*12720SWyllys.Ingersoll@Sun.COM    if(m_pPKeyImpl != NULL)
137*12720SWyllys.Ingersoll@Sun.COM    {
138*12720SWyllys.Ingersoll@Sun.COM       FinalizePKeyImpl( m_pPKeyImpl );
139*12720SWyllys.Ingersoll@Sun.COM    }
140*12720SWyllys.Ingersoll@Sun.COM }
141*12720SWyllys.Ingersoll@Sun.COM #ifdef KMSUSERPKCS12
142*12720SWyllys.Ingersoll@Sun.COM void
GetNative()143*12720SWyllys.Ingersoll@Sun.COM *CPrivateKey::GetNative()
144*12720SWyllys.Ingersoll@Sun.COM {
145*12720SWyllys.Ingersoll@Sun.COM 	return GetPKey(m_pPKeyImpl);
146*12720SWyllys.Ingersoll@Sun.COM }
147*12720SWyllys.Ingersoll@Sun.COM void
SetNative(void * pKey)148*12720SWyllys.Ingersoll@Sun.COM CPrivateKey::SetNative(void *pKey)
149*12720SWyllys.Ingersoll@Sun.COM {
150*12720SWyllys.Ingersoll@Sun.COM 	SetPKey(m_pPKeyImpl, pKey);
151*12720SWyllys.Ingersoll@Sun.COM 	return;
152*12720SWyllys.Ingersoll@Sun.COM }
153*12720SWyllys.Ingersoll@Sun.COM #endif
154