3bc50dee2d525e3ed2d4e079a37d77ba5809596a
[framework/web/wrt-commons.git] / modules / vcore / src / vcore / CertificateLoader.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16 #include <dpl/assert.h>
17 #include <openssl/x509v3.h>
18 #include <dpl/log/log.h>
19 #include <dpl/noncopyable.h>
20 #include <openssl/ecdsa.h>
21 #include <openssl/evp.h>
22
23 #include "Base64.h"
24 #include "CertificateLoader.h"
25 #include "SSLContainers.h"
26
27 namespace {
28 const int MIN_RSA_KEY_LENGTH = 1024;
29 //const char *OID_CURVE_SECP256R1 = "urn:oid:1.2.840.10045.3.1.7";
30 } // namespace anonymous
31
32 namespace ValidationCore {
33 //// COMPARATOR CLASS START ////
34
35 //class CertificateLoaderECDSA : public CertificateLoader::CertificateLoaderComparator, DPL::Noncopyable {
36 //public:
37 //    CertificateLoaderECDSA(const std::string &publicKey)
38 //      : m_ecPublicKey(NULL)
39 //      , m_searchKey(NULL)
40 //    {
41 //        m_bnCtx = BN_CTX_new(); // if fails we can continue anyway
42 //        m_tmpPoint = BN_new();  // if fails we can continue anyway
43 //        m_initialized = CertificateLoader::convertBase64NodeToBigNum(publicKey, &m_searchKey);
44 //
45 //        if(!m_initialized)
46 //            LogError("Init failed!");
47 //    }
48 //
49 //    virtual bool compare(X509 *x509cert){
50 //        if(!m_initialized)
51 //            return false;
52 //
53 //        EVP_PKEY_free(m_ecPublicKey);
54 //
55 //        m_ecPublicKey = X509_get_pubkey(x509cert);
56 //
57 //        if(m_ecPublicKey == NULL)
58 //            return false;
59 //
60 //        if(m_ecPublicKey->type != EVP_PKEY_EC){
61 //            LogError("ecPublicKey has wrong type!");
62 //            return false;
63 //        }
64 //
65 //        // Pointer to internal data of ecPublicKey. Do not free!
66 //        EC_KEY *eckey = m_ecPublicKey->pkey.ec;
67 //
68 //        const EC_POINT *ecpoint = EC_KEY_get0_public_key(eckey);
69 //        const EC_GROUP *ecgroup = EC_KEY_get0_group(eckey);
70 //
71 //        m_tmpPoint = EC_POINT_point2bn(ecgroup, ecpoint, POINT_CONVERSION_UNCOMPRESSED, m_tmpPoint, m_bnCtx);
72 //
73 //        if(BN_cmp(m_tmpPoint, m_searchKey) == 0)
74 //            return true;
75 //
76 //        return false;
77 //    }
78 //
79 //    ~CertificateLoaderECDSA(){
80 //        BN_CTX_free(m_bnCtx);
81 //        EVP_PKEY_free(m_ecPublicKey);
82 //        BN_free(m_searchKey);
83 //        BN_free(m_tmpPoint);
84 //    }
85 //
86 //private:
87 //    bool        m_initialized;
88 //    EVP_PKEY   *m_ecPublicKey;
89 //    BN_CTX     *m_bnCtx;
90 //    BIGNUM     *m_searchKey;
91 //    BIGNUM     *m_tmpPoint;
92 //};
93
94 ///// COMPARETORS CLASS END /////
95
96 //// COMPARATOR RSA CLASS START ////
97
98 //class CertificateLoaderRSA : public CertificateLoader::CertificateLoaderComparator, DPL::Noncopyable {
99 //public:
100 //    CertificateLoaderRSA(const std::string &m_modulus,const std::string &m_exponent )
101 //      : m_rsaPublicKey(NULL)
102 //      , m_modulus_bn(NULL)
103 //      , m_exponent_bn(NULL)
104 //    {
105 //
106 //        m_initialized_modulus = CertificateLoader::convertBase64NodeToBigNum(m_modulus, &m_modulus_bn);
107 //        m_initialized_exponent = CertificateLoader::convertBase64NodeToBigNum(m_exponent, &m_exponent_bn);
108 //
109 //        if(!m_initialized_modulus || !m_initialized_exponent)
110 //            LogError("Init failed!");
111 //    }
112 //
113 //    virtual bool compare(X509 *x509cert){
114 //
115 //        if(!m_initialized_modulus || !m_initialized_exponent)
116 //            return false;
117 //
118 //        EVP_PKEY_free(m_rsaPublicKey);
119 //        m_rsaPublicKey = X509_get_pubkey(x509cert);
120 //
121 //        if(m_rsaPublicKey == NULL)
122 //            return false;
123 //
124 //        if(m_rsaPublicKey->type != EVP_PKEY_RSA){
125 //            LogInfo("rsaPublicKey has wrong type!");
126 //            return false;
127 //        }
128 //
129 //        RSA *rsa = NULL;
130 //        rsa = m_rsaPublicKey->pkey.rsa;
131 //
132 //        if (BN_cmp(m_modulus_bn, rsa->n) == 0 &&
133 //            BN_cmp(m_exponent_bn, rsa->e) == 0 ){
134 //            LogError ("Compare TRUE");
135 //            return true;
136 //        }
137 //        return false;
138 //    }
139 //
140 //    ~CertificateLoaderRSA(){
141 //        EVP_PKEY_free(m_rsaPublicKey);
142 //        BN_free(m_modulus_bn);
143 //        BN_free(m_exponent_bn);
144 //
145 //    }
146 //
147 //private:
148 //    bool        m_initialized_modulus;
149 //    bool        m_initialized_exponent;
150 //    EVP_PKEY   *m_rsaPublicKey;
151 //    BIGNUM     *m_modulus_bn;
152 //    BIGNUM     *m_exponent_bn;
153 //};
154
155 ///// COMPARETORS RSA CLASS END /////
156
157 CertificateLoader::CertificateLoaderResult CertificateLoader::
158     loadCertificateBasedOnExponentAndModulus(const std::string &m_modulus,
159         const std::string &m_exponent)
160 {
161     (void) m_modulus;
162     (void) m_exponent;
163     LogError("Not implemented.");
164     return UNKNOWN_ERROR;
165     //    if (m_exponent.empty() || m_modulus.empty())
166     //        return WRONG_ARGUMENTS;
167     //
168     //    CertificateLoaderRSA comparator(m_modulus,m_exponent);
169     //
170     //    CertificateLoaderResult result = NO_ERROR;
171     //    for(int i=0; storeId[i]; ++i){
172     //        result = loadCertificate(std::string(storeId[i]), &comparator);
173     //
174     //        if(result == ERR_NO_MORE_CERTIFICATES)
175     //            continue;
176     //
177     //        return result;
178     //    }
179     //
180     //    return result;
181 }
182
183 CertificateLoader::CertificateLoaderResult CertificateLoader::loadCertificate(
184         const std::string &storageName,
185         CertificateLoader::CertificateLoaderComparator *cmp)
186 {
187     (void) storageName;
188     (void) cmp;
189     LogError("Not Implemented");
190     return UNKNOWN_ERROR;
191     //    long int result = OPERATION_SUCCESS;
192     //
193     //    char storeId[CERTMGR_MAX_PLUGIN_ID_SIZE];
194     //    char type[CERTMGR_MAX_CERT_TYPE_SIZE];
195     //    certmgr_cert_id certId;
196     //    certmgr_ctx context;
197     //    certmgr_mem_buff certRetrieved;
198     //    unsigned char buffer[CERTMGR_MAX_BUFFER_SIZE];
199     //
200     //    certmgr_cert_descriptor descriptor;
201     //
202     //    certRetrieved.data = buffer;
203     //    certRetrieved.firstFree = 0;
204     //    certRetrieved.size = CERTMGR_MAX_BUFFER_SIZE;
205     //    certId.storeId = storeId;
206     //    certId.type = type;
207     //
208     //    CERTMGR_INIT_CONTEXT((&context), (sizeof(context)))
209     //
210     //    strncpy(context.storeId, storageName.c_str(), storageName.size());
211     //
212     //    for(certRetrieved.firstFree = 0;
213     //        OPERATION_SUCCESS == (result = certmgr_retrieve_certificate_from_store(&context, &certRetrieved, &certId));
214     //        certRetrieved.firstFree = 0)
215     //    {
216     //
217     //        if(OPERATION_SUCCESS!=certmgr_extract_certificate_data(&certRetrieved, &descriptor)){
218     //            LogError("Extracting Certificate Data failed \n");
219     //            continue;
220     //        }
221     //
222     //        const unsigned char *ptr = certRetrieved.data;
223     //
224     //        X509 *x509cert = d2i_X509(NULL, &ptr, certRetrieved.size);
225     //        if(x509cert == NULL){
226     //            certmgr_release_certificate_data(&descriptor);
227     //            LogError("Error extracting certificate (d2i_X509).");
228     //            return UNKNOWN_ERROR;
229     //        }
230     //
231     //        LogDebug("The subject of this certificate is " << descriptor.mandatory.subject);
232     //        if(cmp->compare(x509cert)){
233     //            LogDebug("Found match. Coping bytes: " << certRetrieved.size);
234     //            m_certificatePtr = CertificatePtr(new Certificate(certRetrieved));
235     //            certmgr_release_certificate_data(&descriptor);
236     //            X509_free(x509cert);
237     //            break;
238     //        }
239     //
240     //        LogDebug("Release");
241     //        X509_free(x509cert);
242     //        certmgr_release_certificate_data(&descriptor);
243     //    }
244     //
245     //    if(ERR_NO_MORE_CERTIFICATES == result){
246     //        LogError("Certificates for given DN not found\n");
247     //        return CERTIFICATE_NOT_FOUND;
248     //    }
249     //
250     //    if(result!= OPERATION_SUCCESS){
251     //        LogError("Certificate Manager Error\n");
252     //        return UNKNOWN_ERROR;
253     //    }
254     //
255     //    LogDebug("Exit");
256     //    return NO_ERROR;
257 }
258
259 // TODO
260 CertificateLoader::CertificateLoaderResult CertificateLoader::
261     loadCertificateBasedOnSubjectName(const std::string &subjectName)
262 {
263     (void) subjectName;
264     LogError("Not implemented.");
265     return UNKNOWN_ERROR;
266     //    if(subjectName.empty())
267     //    {
268     //        return WRONG_ARGUMENTS;
269     //    }
270     //
271     //    long int result = OPERATION_SUCCESS;
272     //
273     //    char storeId[CERTMGR_MAX_PLUGIN_ID_SIZE];
274     //    char type[CERTMGR_MAX_CERT_TYPE_SIZE];
275     //    certmgr_cert_id certId;
276     //    certmgr_ctx context;
277     //    certmgr_mem_buff certRetrieved;
278     //    unsigned char buffer[CERTMGR_MAX_BUFFER_SIZE];
279     //
280     //    certmgr_cert_descriptor descriptor;
281     //
282     //    certRetrieved.data = buffer;
283     //    certRetrieved.firstFree = 0;
284     //    certRetrieved.size = CERTMGR_MAX_BUFFER_SIZE;
285     //    certId.storeId = storeId;
286     //    certId.type = type;
287     //
288     //    CERTMGR_INIT_CONTEXT((&context), (sizeof(context)))
289     //
290     //    for(certRetrieved.firstFree = 0;
291     //        OPERATION_SUCCESS == (result = certmgr_retrieve_certificate_from_store(&context, &certRetrieved, &certId));
292     //        certRetrieved.firstFree = 0)
293     //    {
294     //
295     //        if(OPERATION_SUCCESS!=certmgr_extract_certificate_data(&certRetrieved, &descriptor)){
296     //            LogError("Extracting Certificate Data failed \n");
297     //            continue;
298     //        }
299     //
300     //        if(!strcmp(subjectName.c_str(), descriptor.mandatory.subject)){
301     //            LogDebug("The subject of this certificate is " << descriptor.mandatory.subject);
302     //            m_certificatePtr = CertificatePtr(new Certificate(certRetrieved));
303     //            certmgr_release_certificate_data(&descriptor);
304     //            break;
305     //        }
306     //        LogDebug("Release");
307     //        certmgr_release_certificate_data(&descriptor);
308     //    }
309     //
310     //    if(ERR_NO_MORE_CERTIFICATES == result) {
311     //        LogError("Certificates for given DN not found\n");
312     //        return CERTIFICATE_NOT_FOUND;
313     //    }
314     //    if(result!= OPERATION_SUCCESS){
315     //        LogError("Certificate Manager Error\n");
316     //        return UNKNOWN_ERROR;
317     //    }
318     //    LogDebug("Exit");
319     //    return NO_ERROR;
320 }
321
322 // KW CertificateLoader::CertificateLoaderResult CertificateLoader::loadCertificateBasedOnIssuerName(const std::string &issuerName, const std::string &serialNumber)
323 // KW {
324 // KW     if(issuerName.empty() || serialNumber.empty())
325 // KW     {
326 // KW         return WRONG_ARGUMENTS;
327 // KW     }
328 // KW
329 // KW     if(m_cmBuff.data){
330 // KW         delete[] m_cmBuff.data;
331 // KW         memset(&m_cmBuff, 0, sizeof(certmgr_mem_buff));
332 // KW     }
333 // KW
334 // KW     LogDebug("IssuerName: " << issuerName << " serialNumber: " << serialNumber);
335 // KW
336 // KW     //used to check status of retrieved certificate
337 // KW     long int result = OPERATION_SUCCESS;
338 // KW     char storeId[CERTMGR_MAX_PLUGIN_ID_SIZE];
339 // KW     char type[CERTMGR_MAX_CERT_TYPE_SIZE];
340 // KW     certmgr_cert_id certId;
341 // KW     certmgr_ctx context;
342 // KW     certmgr_mem_buff certRetrieved;
343 // KW     unsigned char buffer[CERTMGR_MAX_BUFFER_SIZE];
344 // KW
345 // KW     certmgr_cert_descriptor descriptor;
346 // KW
347 // KW     certRetrieved.data = buffer;
348 // KW     certRetrieved.firstFree = 0;
349 // KW     certRetrieved.size = CERTMGR_MAX_BUFFER_SIZE;
350 // KW     certId.storeId = storeId;
351 // KW     certId.type = type;
352 // KW
353 // KW     CERTMGR_INIT_CONTEXT((&context), (sizeof(context)))
354 // KW
355 // KW     for(certRetrieved.firstFree = 0;
356 // KW         OPERATION_SUCCESS == (result = certmgr_retrieve_certificate_from_store(&context, &certRetrieved, &certId));
357 // KW         certRetrieved.firstFree = 0)
358 // KW     {
359 // KW
360 // KW         LogDebug("Extracting certificate from CertMgr");
361 // KW
362 // KW         if( OPERATION_SUCCESS != certmgr_extract_certificate_data(&certRetrieved, &descriptor) ){
363 // KW             LogError("Extracting Certificate Data failed \n");
364 // KW             continue;
365 // KW         }
366 // KW
367 // KW         LogDebug("Issuer: " << descriptor.mandatory.issuer);
368 // KW
369 // KW         const unsigned char *ptr = certRetrieved.data;
370 // KW         char *tmp;
371 // KW
372 // KW         X509 *x509cert = d2i_X509(NULL, &ptr, certRetrieved.size);
373 // KW         std::string serialNO = std::string(tmp = i2s_ASN1_INTEGER(NULL, X509_get_serialNumber(x509cert)));
374 // KW         OPENSSL_free(tmp);
375 // KW         X509_free(x509cert);
376 // KW
377 // KW         LogInfo("Certificate number found: " << serialNO);
378 // KW         LogInfo("Certificate number looking for: " << serialNumber);
379 // KW
380 // KW         if(!strcmp(issuerName.c_str(), descriptor.mandatory.issuer)
381 // KW               && serialNumber == serialNO)
382 // KW         {
383 // KW             LogError("The issuer of this certificate is " << descriptor.mandatory.issuer);
384 // KW
385 // KW             m_cmBuff.data = new unsigned char[certRetrieved.size];
386 // KW             m_cmBuff.firstFree = m_cmBuff.size = certRetrieved.size;
387 // KW             memcpy(m_cmBuff.data, certRetrieved.data, certRetrieved.size);
388 // KW             certmgr_release_certificate_data(&descriptor);
389 // KW             break;
390 // KW         }
391 // KW         certmgr_release_certificate_data(&descriptor);
392 // KW     }
393 // KW
394 // KW     if(ERR_NO_MORE_CERTIFICATES == result) {
395 // KW         LogError("Certificates not found");
396 // KW         return CERTIFICATE_NOT_FOUND;
397 // KW     }
398 // KW     if(result != OPERATION_SUCCESS){
399 // KW         LogError("Certificate Manager Error");
400 // KW         return UNKNOWN_ERROR;
401 // KW     }
402 // KW     return NO_ERROR;
403 // KW }
404
405 CertificateLoader::CertificateLoaderResult CertificateLoader::
406     loadCertificateWithECKEY(const std::string &curveName,
407         const std::string &publicKey)
408 {
409     (void) curveName;
410     (void) publicKey;
411     LogError("Not implemented.");
412     return UNKNOWN_ERROR;
413     //    if(curveName != OID_CURVE_SECP256R1){
414     //        LogError("Found field id: " << curveName << " Expected: " << OID_CURVE_SECP256R1);
415     //        return UNSUPPORTED_CERTIFICATE_FIELD;
416     //    }
417     //
418     //    CertificateLoaderECDSA comparator(publicKey);
419     //
420     //    CertificateLoaderResult result = NO_ERROR;
421     //    for(int i=0; storeId[i]; ++i){
422     //        result = loadCertificate(std::string(storeId[i]), &comparator);
423     //
424     //        if(result == ERR_NO_MORE_CERTIFICATES)
425     //            continue;
426     //
427     //        return result;
428     //    }
429     //
430     //    return result;
431 }
432
433 CertificateLoader::CertificateLoaderResult CertificateLoader::
434     loadCertificateFromRawData(const std::string &rawData)
435 {
436     Try {
437         Base64Decoder base;
438         base.reset();
439         base.append(rawData);
440         if (!base.finalize()) {
441             LogWarning("Certificate format is broken.");
442             return UNKNOWN_ERROR;
443         }
444         std::string derCert = base.get();
445         m_certificatePtr = CertificatePtr(new Certificate(derCert));
446     } Catch(Certificate::Exception::Base) {
447         LogWarning("Error reading certificate by openssl.");
448         return UNKNOWN_ERROR;
449     }
450
451     // Check the key length if sig algorithm is RSA
452     EVP_PKEY *pKey = X509_get_pubkey(m_certificatePtr->getX509());
453
454     if (pKey->type == EVP_PKEY_RSA) {
455         RSA* pRSA = pKey->pkey.rsa;
456
457         if (pRSA) {
458             int keyLength = RSA_size(pRSA);
459
460             // key Length (modulus) is in bytes
461             keyLength <<= 3;
462             LogDebug("RSA key length: " << keyLength << " bits");
463
464             if (keyLength < MIN_RSA_KEY_LENGTH) {
465                 LogError(
466                     "RSA key too short!" << "Has only " << keyLength << " bits");
467                 return CERTIFICATE_SECURITY_ERROR;
468             }
469         }
470     }
471
472     return NO_ERROR;
473 }
474
475 // DEPRACETED FUNCTION
476 //CertificateLoader::CertificateLoaderResult CertificateLoader::loadCertificateFromRawData(const std::string &rawData)
477 //{
478 //    certmgr_mem_buff cmBuff = {0,0,0};
479 //
480 //    long int size;
481 //    cmBuff.data = certmgr_util_base64_decode(const_cast<void*>(static_cast<const void*>(rawData.c_str())), rawData.size(), &size);
482 //
483 //    cmBuff.firstFree = cmBuff.size = size;
484 //
485 //    certmgr_cert_descriptor descriptor;
486 //
487 //    long int result = certmgr_extract_certificate_data(&cmBuff, &descriptor);
488 //
489 //    if (result != OPERATION_SUCCESS)
490 //    {
491 //        LogError("Unable to load certificate");
492 //        return UNKNOWN_ERROR;
493 //    }
494 //
495 //    certmgr_release_certificate_data(&descriptor);
496 //
497 //    m_certificatePtr = CertificatePtr(new Certificate(cmBuff));
498 //
499 //    // we have to use temp pointer cause d2i_x509 modifies its input
500 //    const unsigned char* tmpPtr = cmBuff.data;
501 //    X509* pCertificate = d2i_X509(NULL, &tmpPtr, cmBuff.size);
502 //
503 //    if (pCertificate)
504 //    {
505 //        SSLSmartContainer<X509> pX509(pCertificate);
506 //
507 //        // Check the key length if sig algorithm is RSA
508 //        EVP_PKEY *pKey = X509_get_pubkey(pX509);
509 //
510 //        if (pKey->type == EVP_PKEY_RSA)
511 //        {
512 //            RSA* pRSA = pKey->pkey.rsa;
513 //
514 //            if (pRSA)
515 //            {
516 //                int keyLength = RSA_size(pRSA);
517 //
518 //                // key Length (modulus) is in bytes
519 //                keyLength <<= 3;
520 //                LogDebug("RSA key length: " << keyLength << " bits");
521 //
522 //                if (keyLength < MIN_RSA_KEY_LENGTH)
523 //                {
524 //                    LogError("RSA key too short!" << "Has only " << keyLength << " bits");
525 //                    return CERTIFICATE_SECURITY_ERROR;
526 //                }
527 //            }
528 //        }
529 //    }
530 //
531 //    return NO_ERROR;
532 //}
533
534 CertificateLoader::CertificateLoaderResult CertificateLoader::
535     loadCertificateBasedOnDSAComponents(const std::string& strP,
536         const std::string& strQ,
537         const std::string& strG,
538         const std::string& strY,
539         const std::string& strJ,
540         const std::string& strSeed,
541         const std::string& strPGenCounter)
542 {
543     (void) strP;
544     (void) strQ;
545     (void) strG;
546     (void) strY;
547     (void) strJ;
548     (void) strSeed;
549     (void) strPGenCounter;
550     LogError("Not implemented.");
551     return UNKNOWN_ERROR;
552     //    (void)strY;
553     //    (void)strJ;
554     //    (void)strSeed;
555     //    (void)strPGenCounter;
556     //
557     //    long int result = UNKNOWN_ERROR;
558     //
559     //    char storeId[CERTMGR_MAX_PLUGIN_ID_SIZE];
560     //    char type[CERTMGR_MAX_CERT_TYPE_SIZE];
561     //    certmgr_cert_id certId;
562     //    certmgr_ctx context;
563     //    certmgr_mem_buff certRetrieved;
564     //
565     //    unsigned char buffer[CERTMGR_MAX_BUFFER_SIZE];
566     //
567     //    certmgr_cert_descriptor descriptor;
568     //
569     //    certRetrieved.data = buffer;
570     //    certRetrieved.firstFree = 0;
571     //    certRetrieved.size = CERTMGR_MAX_BUFFER_SIZE;
572     //    certId.storeId = storeId;
573     //    certId.type = type;
574     //
575     //    CERTMGR_INIT_CONTEXT((&context), (sizeof(context)))
576     //    std::string strStoreType("Operator");
577     //    strncpy(context.storeId, strStoreType.c_str(),  strStoreType.length());
578     //
579     //    for (certRetrieved.firstFree = 0;
580     //      OPERATION_SUCCESS == (result = certmgr_retrieve_certificate_from_store(&context, &certRetrieved, &certId));
581     //      certRetrieved.firstFree = 0)
582     //    {
583     //
584     //        if (OPERATION_SUCCESS != certmgr_extract_certificate_data(&certRetrieved, &descriptor))
585     //        {
586     //            LogDebug("unable to retrieve cert from storage");
587     //            continue;
588     //        }
589     //
590     //        X509* pCertificate = d2i_X509(NULL, (const unsigned char**) &(certRetrieved.data), certRetrieved.size);
591     //
592     //        if (pCertificate)
593     //        {
594     //            EVP_PKEY *pKey = X509_get_pubkey(pCertificate);
595     //
596     //            if (pKey->type == EVP_PKEY_DSA)
597     //            {
598     //                DSA* pDSA = pKey->pkey.dsa;
599     //
600     //                if (pDSA)
601     //                {
602     //                    BIGNUM *pDSApBigNum = NULL, *pDSAqBigNum = NULL, *pDSAgBigNum = NULL;
603     //
604     //                    convertBase64NodeToBigNum(strP, &pDSApBigNum);
605     //                    convertBase64NodeToBigNum(strQ, &pDSAqBigNum);
606     //                    convertBase64NodeToBigNum(strG, &pDSAgBigNum);
607     //
608     //                    if (pDSApBigNum && pDSAqBigNum && pDSAgBigNum &&
609     //                      BN_cmp(pDSApBigNum, pDSA->p) == 0 &&
610     //                      BN_cmp(pDSAqBigNum, pDSA->q) == 0 &&
611     //                      BN_cmp(pDSAgBigNum, pDSA->g) == 0)
612     //                    {
613     //                        LogInfo("DSA Certificate found");
614     //                        /* TODO load this certificate to m_cmBuff value */
615     //                        LogError("Not implemented!");
616     //
617     //                        EVP_PKEY_free(pKey);
618     //                        X509_free(pCertificate);
619     //
620     //                        BN_free(pDSApBigNum);
621     //                        BN_free(pDSAqBigNum);
622     //                        BN_free(pDSAgBigNum);
623     //
624     //                        certmgr_release_certificate_data(&descriptor);
625     //                        return NO_ERROR;
626     //                    }
627     //
628     //                    if (pDSApBigNum)
629     //                    {
630     //                        BN_free(pDSApBigNum);
631     //                    }
632     //                    if (pDSAqBigNum)
633     //                    {
634     //                        BN_free(pDSAqBigNum);
635     //                    }
636     //                    if (pDSAgBigNum)
637     //                    {
638     //                        BN_free(pDSAgBigNum);
639     //                    }
640     //
641     //                }
642     //                EVP_PKEY_free(pKey);
643     //            }
644     //            X509_free(pCertificate);
645     //        }
646     //        else
647     //            LogError("Unable to load certificate");
648     //
649     //        certmgr_release_certificate_data(&descriptor);
650     //    }
651     //
652     //    LogError("No DSA certificate with given parameters");
653     //
654     //    return CERTIFICATE_NOT_FOUND;
655 }
656
657 bool CertificateLoader::convertBase64NodeToBigNum(const std::string& strNode,
658         BIGNUM** ppBigNum)
659 {
660     (void) strNode;
661     (void) ppBigNum;
662     LogError("Not implemented.");
663     return false;
664     //    if (!ppBigNum || *ppBigNum != NULL)
665     //    {
666     //        LogError("Ptr variable not initialized properly!");
667     //        return false;
668     //    }
669     //
670     //    // decode base64 to binary
671     //    long int binBuffLength = 0;
672     //    unsigned char* binBuff = NULL;
673     //
674     //    binBuff = certmgr_util_base64_decode(const_cast<char*> (strNode.c_str()), strNode.length(), &binBuffLength);
675     //
676     //    if (!binBuff)
677     //    {
678     //        LogError("base64 decode failed");
679     //        return false;
680     //    }
681     //
682     //    // convert binary to bignum
683     //    *ppBigNum = BN_bin2bn(binBuff, binBuffLength, *ppBigNum);
684     //
685     //    free(binBuff);
686     //
687     //    if (!(*ppBigNum))
688     //    {
689     //        LogError("Conversion from node to bignum failed");
690     //        return false;
691     //    }
692     //
693     //    return true;
694 }
695
696 // KW bool CertificateLoader::convertBigNumToBase64Node(const BIGNUM* pBigNum, std::string& strNode)
697 // KW {
698 // KW     if (!pBigNum)
699 // KW     {
700 // KW         LogError("null ptr");
701 // KW         return false;
702 // KW     }
703 // KW
704 // KW     int nNumLength = BN_num_bytes(pBigNum);
705 // KW     unsigned char* buffer = new unsigned char[nNumLength + 1];
706 // KW
707 // KW     // convert bignum to binary format
708 // KW     if (BN_bn2bin(pBigNum, buffer) < 0)
709 // KW     {
710 // KW         LogError("Conversion from bignum to binary failed");
711 // KW         delete []buffer;
712 // KW         return false;
713 // KW     }
714 // KW
715 // KW     char* pBase64Node = NULL;
716 // KW     unsigned long int buffLen = 0;
717 // KW     certmgr_util_base64_encode(buffer, (unsigned long int) nNumLength, &pBase64Node, &buffLen);
718 // KW
719 // KW     strNode.assign(pBase64Node, buffLen);
720 // KW
721 // KW     delete []buffer;
722 // KW     return true;
723 // KW }
724 } // namespace ValidationCore
725