1 | /* $Id: pkix-signature-builtin.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Crypto - Public Key Signature Schemas, Built-in providers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 | #include <iprt/crypto/pkix.h>
|
---|
33 |
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 |
|
---|
37 | #ifdef IPRT_WITH_OPENSSL
|
---|
38 | # include "internal/iprt-openssl.h"
|
---|
39 | # include <openssl/evp.h>
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include "pkix-signature-builtin.h"
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Global Variables *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 | /**
|
---|
49 | * Array of built in message digest vtables.
|
---|
50 | */
|
---|
51 | static PCRTCRPKIXSIGNATUREDESC const g_apPkixSignatureDescriptors[] =
|
---|
52 | {
|
---|
53 | &g_rtCrPkixSigningHashWithRsaDesc,
|
---|
54 | };
|
---|
55 |
|
---|
56 |
|
---|
57 |
|
---|
58 | PCRTCRPKIXSIGNATUREDESC RTCrPkixSignatureFindByObjIdString(const char *pszObjId, void **ppvOpaque)
|
---|
59 | {
|
---|
60 | if (ppvOpaque)
|
---|
61 | *ppvOpaque = NULL;
|
---|
62 |
|
---|
63 | /*
|
---|
64 | * Primary OIDs.
|
---|
65 | */
|
---|
66 | uint32_t i = RT_ELEMENTS(g_apPkixSignatureDescriptors);
|
---|
67 | while (i-- > 0)
|
---|
68 | if (strcmp(g_apPkixSignatureDescriptors[i]->pszObjId, pszObjId) == 0)
|
---|
69 | return g_apPkixSignatureDescriptors[i];
|
---|
70 |
|
---|
71 | /*
|
---|
72 | * Alias OIDs.
|
---|
73 | */
|
---|
74 | i = RT_ELEMENTS(g_apPkixSignatureDescriptors);
|
---|
75 | while (i-- > 0)
|
---|
76 | {
|
---|
77 | const char * const *ppszAliases = g_apPkixSignatureDescriptors[i]->papszObjIdAliases;
|
---|
78 | if (ppszAliases)
|
---|
79 | for (; *ppszAliases; ppszAliases++)
|
---|
80 | if (strcmp(*ppszAliases, pszObjId) == 0)
|
---|
81 | return g_apPkixSignatureDescriptors[i];
|
---|
82 | }
|
---|
83 |
|
---|
84 | #if 0//def IPRT_WITH_OPENSSL
|
---|
85 | /*
|
---|
86 | * Try EVP and see if it knows the algorithm.
|
---|
87 | */
|
---|
88 | if (ppvOpaque)
|
---|
89 | {
|
---|
90 | rtCrOpenSslInit();
|
---|
91 | int iAlgoNid = OBJ_txt2nid(pszObjId);
|
---|
92 | if (iAlgoNid != NID_undef)
|
---|
93 | {
|
---|
94 | const char *pszAlogSn = OBJ_nid2sn(iAlgoNid);
|
---|
95 | const EVP_MD *pEvpMdType = EVP_get_digestbyname(pszAlogSn);
|
---|
96 | if (pEvpMdType)
|
---|
97 | {
|
---|
98 | /*
|
---|
99 | * Return the OpenSSL provider descriptor and the EVP_MD address.
|
---|
100 | */
|
---|
101 | Assert(pEvpMdType->md_size);
|
---|
102 | *ppvOpaque = (void *)pEvpMdType;
|
---|
103 | return &g_rtCrPkixSignatureOpenSslDesc;
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | #endif
|
---|
108 | return NULL;
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | PCRTCRPKIXSIGNATUREDESC RTCrPkixSignatureFindByObjId(PCRTASN1OBJID pObjId, void **ppvOpaque)
|
---|
113 | {
|
---|
114 | return RTCrPkixSignatureFindByObjIdString(pObjId->szObjId, ppvOpaque);
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | RTDECL(int) RTCrPkixSignatureCreateByObjIdString(PRTCRPKIXSIGNATURE phSignature, const char *pszObjId, bool fSigning,
|
---|
119 | PCRTASN1BITSTRING pKey,PCRTASN1DYNTYPE pParams)
|
---|
120 | {
|
---|
121 | void *pvOpaque;
|
---|
122 | PCRTCRPKIXSIGNATUREDESC pDesc = RTCrPkixSignatureFindByObjIdString(pszObjId, &pvOpaque);
|
---|
123 | if (pDesc)
|
---|
124 | return RTCrPkixSignatureCreate(phSignature, pDesc, pvOpaque, fSigning, pKey, pParams);
|
---|
125 | return VERR_NOT_FOUND;
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | RTDECL(int) RTCrPkixSignatureCreateByObjId(PRTCRPKIXSIGNATURE phSignature, PCRTASN1OBJID pObjId, bool fSigning,
|
---|
130 | PCRTASN1BITSTRING pKey, PCRTASN1DYNTYPE pParams)
|
---|
131 | {
|
---|
132 | void *pvOpaque;
|
---|
133 | PCRTCRPKIXSIGNATUREDESC pDesc = RTCrPkixSignatureFindByObjId(pObjId, &pvOpaque);
|
---|
134 | if (pDesc)
|
---|
135 | return RTCrPkixSignatureCreate(phSignature, pDesc, pvOpaque, fSigning, pKey, pParams);
|
---|
136 | return VERR_NOT_FOUND;
|
---|
137 | }
|
---|
138 |
|
---|