aboutsummaryrefslogtreecommitdiffstats
path: root/libparc/parc/security/parc_CertificateFactory.c
blob: 52cef4afff120114f80c7b0fead564b3374f6634 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * parc_CertificateFactory.c
 * PARC Library
 */

#include <config.h>

#include <parc/security/parc_CertificateFactory.h>
#include <parc/security/parc_X509Certificate.h>

#include <parc/algol/parc_Object.h>

struct parc_certificate_factory {
    PARCCertificateType type;
    PARCContainerEncoding encoding;
};

parcObject_ExtendPARCObject(PARCCertificateFactory, NULL, NULL, NULL, NULL, NULL, NULL, NULL);

parcObject_ImplementAcquire(parcCertificateFactory, PARCCertificateFactory);
parcObject_ImplementRelease(parcCertificateFactory, PARCCertificateFactory);

PARCCertificateFactory *
parcCertificateFactory_Create(PARCCertificateType type, PARCContainerEncoding encoding)
{
    PARCCertificateFactory *factory = parcObject_CreateInstance(PARCCertificateFactory);
    factory->type = type;
    factory->encoding = encoding;
    return factory;
}

PARCCertificate *
parcCertificateFactory_CreateCertificateFromFile(PARCCertificateFactory *factory, const char *filename, const char *password __attribute__((unused)))
{
    if (factory->type == PARCCertificateType_X509 && factory->encoding == PARCContainerEncoding_PEM) {
        PARCX509Certificate *certificate = parcX509Certificate_CreateFromPEMFile(filename);
        PARCCertificate *wrapper = parcCertificate_CreateFromInstance(PARCX509CertificateInterface, certificate);
        return wrapper;
    }

    // Unsupported configuration
    return NULL;
}

PARCCertificate *
parcCertificateFactory_CreateCertificateFromBuffer(PARCCertificateFactory *factory, PARCBuffer *buffer)
{
    if (factory->type == PARCCertificateType_X509 && factory->encoding == PARCContainerEncoding_DER) {
        PARCX509Certificate *certificate = parcX509Certificate_CreateFromDERBuffer(buffer);

        // This may fail.
        if (certificate == NULL) {
            return NULL;
        }

        PARCCertificate *wrapper = parcCertificate_CreateFromInstance(PARCX509CertificateInterface, certificate);
        return wrapper;
    }

    // Unsupported configuration
    return NULL;
}

PARCCertificate *
parcCertificateFactory_CreateSelfSignedCertificate(PARCCertificateFactory *factory, PARCBuffer **privateKey,
                                                   char *subjectName, PARCSigningAlgorithm signAlgo,
                                                   size_t keyLength, size_t valdityDays)
{
    if (factory->type == PARCCertificateType_X509 && factory->encoding == PARCContainerEncoding_DER) {
      PARCX509Certificate *certificate = NULL;
      switch (signAlgo)
      {
        case PARCSigningAlgorithm_RSA:
          certificate = parcX509Certificate_CreateSelfSignedCertificate(privateKey, subjectName, (int) keyLength, valdityDays, PARCKeyType_RSA);
          break;
        case PARCSigningAlgorithm_ECDSA:
          certificate = parcX509Certificate_CreateSelfSignedCertificate(privateKey, subjectName, (int) keyLength, valdityDays, PARCKeyType_EC);
          break;
      }

        // This may fail.
        if (certificate == NULL) {
            return NULL;
        }

        PARCCertificate *wrapper = parcCertificate_CreateFromInstance(PARCX509CertificateInterface, certificate);
        return wrapper;
    }

    // Unsupported configuration
    return NULL;
}