VirtualBox

source: vbox/trunk/include/iprt/crypto/pkix.h@ 56291

Last change on this file since 56291 was 56291, checked in by vboxsync, 9 years ago

include: Updated (C) year.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.1 KB
Line 
1/** @file
2 * IPRT - Public Key Infrastructure APIs.
3 */
4
5/*
6 * Copyright (C) 2006-2015 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef ___iprt_crypto_pkix_h
27#define ___iprt_crypto_pkix_h
28
29#include <iprt/asn1.h>
30
31
32RT_C_DECLS_BEGIN
33
34/** @defgroup grp_rt_crpkix RTCrPkix - Public Key Infrastructure APIs
35 * @ingroup grp_rt_crypto
36 * @{
37 */
38
39/**
40 * Verifies the signature (@a pSignatureValue) of the give data (@a pvData)
41 * using the specfied public key (@a pPublicKey) and algorithm.
42 *
43 * @returns IPRT status code.
44 * @param pAlgorithm The signature algorithm (digest w/ cipher).
45 * @param pParameters Parameter to the public key algorithm. Optional.
46 * @param pPublicKey The public key.
47 * @param pSignatureValue The signature value.
48 * @param pvData The signed data.
49 * @param cbData The amount of signed data.
50 * @param pErrInfo Where to return extended error info. Optional.
51 */
52RTDECL(int) RTCrPkixPubKeyVerifySignature(PCRTASN1OBJID pAlgorithm, PCRTASN1DYNTYPE pParameters, PCRTASN1BITSTRING pPublicKey,
53 PCRTASN1BITSTRING pSignatureValue, const void *pvData, size_t cbData,
54 PRTERRINFO pErrInfo);
55
56
57/**
58 * Gets the cipher OID matching the given signature algorithm.
59 *
60 * @returns Cipher OID string on success, NULL on failure.
61 * @param pAlgorithm The signature algorithm (digest w/ cipher).
62 */
63RTDECL(const char *) RTCrPkixGetCiperOidFromSignatureAlgorithm(PCRTASN1OBJID pAlgorithm);
64
65
66/** @name PKCS-1 Object Identifiers (OIDs)
67 * @{ */
68#define RTCR_PKCS1_OID "1.2.840.113549.1.1"
69#define RTCR_PKCS1_RSA_OID "1.2.840.113549.1.1.1"
70#define RTCR_PKCS1_MD2_WITH_RSA_OID "1.2.840.113549.1.1.2"
71#define RTCR_PKCS1_MD4_WITH_RSA_OID "1.2.840.113549.1.1.3"
72#define RTCR_PKCS1_MD5_WITH_RSA_OID "1.2.840.113549.1.1.4"
73#define RTCR_PKCS1_SHA1_WITH_RSA_OID "1.2.840.113549.1.1.5"
74#define RTCR_PKCS1_RSA_OAEP_ENCRYPTION_SET_OID "1.2.840.113549.1.1.6"
75#define RTCR_PKCS1_RSA_AES_OAEP_OID "1.2.840.113549.1.1.7"
76#define RTCR_PKCS1_MSGF1_OID "1.2.840.113549.1.1.8"
77#define RTCR_PKCS1_P_SPECIFIED_OID "1.2.840.113549.1.1.9"
78#define RTCR_PKCS1_RSASSA_PSS_OID "1.2.840.113549.1.1.10"
79#define RTCR_PKCS1_SHA256_WITH_RSA_OID "1.2.840.113549.1.1.11"
80#define RTCR_PKCS1_SHA384_WITH_RSA_OID "1.2.840.113549.1.1.12"
81#define RTCR_PKCS1_SHA512_WITH_RSA_OID "1.2.840.113549.1.1.13"
82#define RTCR_PKCS1_SHA224_WITH_RSA_OID "1.2.840.113549.1.1.14"
83/** @} */
84
85
86/**
87 * Public key signature scheme provider descriptor.
88 */
89typedef struct RTCRPKIXSIGNATUREDESC
90{
91 /** The signature scheme provider name. */
92 const char *pszName;
93 /** The object ID string. */
94 const char *pszObjId;
95 /** Pointer to a NULL terminated table of alias object IDs (optional). */
96 const char * const *papszObjIdAliases;
97 /** The size of the state. */
98 uint32_t cbState;
99 /** Reserved for future / explicit padding. */
100 uint32_t uReserved;
101 /** Provider specific field. This generally indicates the kind of padding
102 * scheme to employ with the given OID. */
103 uintptr_t uProviderSpecific;
104
105 /**
106 * Initializes the state of the signature scheme provider.
107 *
108 * Optional, RT_BZERO will be used if NULL.
109 *
110 * @returns IPRT status code.
111 * @param pDesc Pointer to this structure (for uProviderSpecific).
112 * @param pvState The opaque provider state.
113 * @param pvOpaque Opaque provider specific parameter.
114 * @param fSigning Set if a signing operation is going to be performed,
115 * clear if it is a verification. This is a fixed
116 * setting for the lifetime of the instance due to the
117 * algorithm requiring different keys.
118 * @param pKey The key to use (whether private or public depends on
119 * the operation).
120 * @param pParams Algorithm/key parameters, optional. Will be NULL if
121 * none.
122 */
123 DECLCALLBACKMEMBER(int, pfnInit)(struct RTCRPKIXSIGNATUREDESC const *pDesc, void *pvState, void *pvOpaque, bool fSigning,
124 PCRTASN1BITSTRING pKey, PCRTASN1DYNTYPE pParams);
125
126 /**
127 * Resets the state before performing another signing or verification.
128 *
129 * Optional. It is assumed that the provider does not have any state needing to
130 * be re-initialized if this method is not implemented.
131 *
132 * @returns IPRT status code.
133 * @param pDesc Pointer to this structure (for uProviderSpecific).
134 * @param pvState The opaque provider state.
135 * @param fSigning Exactly the same value as the init call.
136 */
137 DECLCALLBACKMEMBER(int, pfnReset)(struct RTCRPKIXSIGNATUREDESC const *pDesc, void *pvState, bool fSigning);
138
139 /**
140 * Deletes the provider state. Optional.
141 *
142 * The state will be securely wiped clean after the call, regardless of whether
143 * the method is implemented or not.
144 *
145 * @param pDesc Pointer to this structure (for uProviderSpecific).
146 * @param pvState The opaque provider state.
147 * @param fSigning Exactly the same value as the init call.
148 */
149 DECLCALLBACKMEMBER(void, pfnDelete)(struct RTCRPKIXSIGNATUREDESC const *pDesc, void *pvState, bool fSigning);
150
151 /**
152 * Verifies a signed message digest (fSigning = false).
153 *
154 * @returns IPRT status code.
155 * @retval VINF_SUCCESS if the signature checked out correctly.
156 * @retval VERR_PKIX_KEY wrong key or some other key issue.
157 *
158 * @param pDesc Pointer to this structure (for uProviderSpecific).
159 * @param pvState The opaque provider state.
160 * @param hDigest The handle to the digest. Call RTCrDigestFinal to
161 * complete and retreive the final hash value.
162 * @param pvSignature The signature to validate.
163 * @param cbSignature The size of the signature (in bytes).
164 */
165 DECLCALLBACKMEMBER(int, pfnVerify)(struct RTCRPKIXSIGNATUREDESC const *pDesc, void *pvState,
166 RTCRDIGEST hDigest, void const *pvSignature, size_t cbSignature);
167
168 /**
169 * Sign a message digest (fSigning = true).
170 *
171 * @returns IPRT status code.
172 * @retval VINF_SUCCESS on success.
173 * @retval VERR_PKIX_KEY wrong key or some other key issue.
174 * @retval VERR_BUFFER_OVERFLOW if the signature buffer is too small, the
175 * require buffer size will be available in @a *pcbSignature.
176 *
177 * @param pDesc Pointer to this structure (for uProviderSpecific).
178 * @param pvState The opaque provider state.
179 * @param hDigest The handle to the digest. Call RTCrDigestFinal to
180 * complete and retreive the final hash value.
181 * @param pvSignature The output signature buffer.
182 * @param pcbSignature On input the variable pointed to holds the size of
183 * the buffer @a pvSignature points to.
184 * On return the variable pointed to is set to the size
185 * of the returned signature, or the required size in
186 * case of VERR_BUFFER_OVERFLOW.
187 */
188 DECLCALLBACKMEMBER(int, pfnSign)(struct RTCRPKIXSIGNATUREDESC const *pDesc, void *pvState,
189 RTCRDIGEST hDigest, void *pvSignature, size_t *pcbSignature);
190
191} RTCRPKIXSIGNATUREDESC;
192/** Pointer to a public key signature scheme provider descriptor. */
193typedef RTCRPKIXSIGNATUREDESC const *PCRTCRPKIXSIGNATUREDESC;
194
195
196PCRTCRPKIXSIGNATUREDESC RTCrPkixSignatureFindByObjIdString(const char *pszObjId, void *ppvOpaque);
197PCRTCRPKIXSIGNATUREDESC RTCrPkixSignatureFindByObjId(PCRTASN1OBJID pObjId, void *ppvOpaque);
198RTDECL(int) RTCrPkixSignatureCreateByObjIdString(PRTCRPKIXSIGNATURE phSignature, const char *pszObjId, bool fSigning,
199 PCRTASN1BITSTRING pKey,PCRTASN1DYNTYPE pParams);
200RTDECL(int) RTCrPkixSignatureCreateByObjId(PRTCRPKIXSIGNATURE phSignature, PCRTASN1OBJID pObjId, bool fSigning,
201 PCRTASN1BITSTRING pKey, PCRTASN1DYNTYPE pParams);
202
203
204RTDECL(int) RTCrPkixSignatureCreate(PRTCRPKIXSIGNATURE phSignature, PCRTCRPKIXSIGNATUREDESC pDesc, void *pvOpaque,
205 bool fSigning, PCRTASN1BITSTRING pKey, PCRTASN1DYNTYPE pParams);
206RTDECL(uint32_t) RTCrPkixSignatureRetain(RTCRPKIXSIGNATURE hSignature);
207RTDECL(uint32_t) RTCrPkixSignatureRelease(RTCRPKIXSIGNATURE hSignature);
208RTDECL(int) RTCrPkixSignatureVerify(RTCRPKIXSIGNATURE hSignature, RTCRDIGEST hDigest,
209 void const *pvSignature, size_t cbSignature);
210RTDECL(int) RTCrPkixSignatureVerifyBitString(RTCRPKIXSIGNATURE hSignature, RTCRDIGEST hDigest, PCRTASN1BITSTRING pSignature);
211RTDECL(int) RTCrPkixSignatureVerifyOctetString(RTCRPKIXSIGNATURE hSignature, RTCRDIGEST hDigest, PCRTASN1OCTETSTRING pSignature);
212RTDECL(int) RTCrPkixSignatureSign(RTCRPKIXSIGNATURE hSignature, RTCRDIGEST hDigest,
213 void *pvSignature, size_t *pcbSignature);
214
215
216/**
217 * Public key encryption scheme provider descriptor.
218 *
219 * @todo This is just a sketch left over from when the signature code was
220 * chiseled out.
221 */
222typedef struct RTCRPKIXENCRYPTIONDESC
223{
224 /** The encryption scheme provider name. */
225 const char *pszName;
226 /** The object ID string. */
227 const char *pszObjId;
228 /** Pointer to a NULL terminated table of alias object IDs (optional). */
229 const char * const *papszObjIdAliases;
230 /** The size of the state. */
231 uint32_t cbState;
232 /** Reserved for future use / padding. */
233 uint32_t uReserved;
234 /** Provider specific field. */
235 uintptr_t uProviderSpecific;
236
237 /**
238 * Initializes the state for this encryption scheme.
239 *
240 * Optional, RT_BZERO will be used if NULL.
241 *
242 * @returns IPRT status code.
243 * @param pDesc Pointer to this structure (so uProviderSpecific can
244 * be read).
245 * @param pvState The opaque provider state.
246 * @param pvOpaque Opaque provider specific parameter.
247 * @param fEncrypt Set if the instance will be encrypting, clear if it
248 * will be decrypting. This aspect of the instance is
249 * immutable due to the algorithm requiring different
250 * keys for each of the operations.
251 * @param pKey The key to use (whether private or public depends on
252 * the operation type).
253 * @param pParams Algorithm/key parameters, optional. Will be NULL if
254 * none.
255 */
256 DECLCALLBACKMEMBER(int, pfnInit)(struct RTCRPKIXENCRYPTIONDESC const *pDesc, void *pvState, void *pvOpaque, bool fEncrypt,
257 PCRTASN1BITSTRING pKey, PCRTASN1DYNTYPE pParams);
258
259 /**
260 * Re-initializes the provider state.
261 *
262 * Optional. It is assumed that the provider does not have any state needing
263 * to be re-initialized if this method is not implemented. (Do not assume that
264 * a final encrypt/decrypt call has been made prior to this call.)
265 *
266 * @returns IPRT status code.
267 * @param pDesc Pointer to this structure (so uProviderSpecific can
268 * be read).
269 * @param pvState The opaque provider state.
270 * @param enmOperation Same as for the earlier pfnInit call.
271 */
272 DECLCALLBACKMEMBER(int, pfnReset)(struct RTCRPKIXENCRYPTIONDESC const *pDesc, void *pvState, bool fEncrypt);
273
274 /**
275 * Deletes the provider state. Optional.
276 *
277 * The state will be securely wiped clean after the call, regardless of whether
278 * the method is implemented or not.
279 *
280 * @param pDesc Pointer to this structure (so uProviderSpecific can
281 * be read).
282 * @param pvState The opaque provider state.
283 * @param enmOperation Same as for the earlier pfnInit call.
284 */
285 DECLCALLBACKMEMBER(void, pfnDelete)(struct RTCRPKIXENCRYPTIONDESC const *pDesc, void *pvState, bool fEncrypt);
286
287 /**
288 * Encrypt using the public key (fEncrypt = true).
289 *
290 * @returns IPRT status code.
291 * @retval VINF_SUCCESS on success.
292 * @retval VERR_PKIX_KEY wrong key or some other key issue.
293 * @retval VERR_BUFFER_OVERFLOW if the output buffer is too small, the require
294 * buffer size will be available in @a *pcbCiphertext. The caller can
295 * should retry the call with a larger buffer.
296 *
297 * @param pDesc Pointer to this structure (so uProviderSpecific can
298 * be read).
299 * @param pvState The opaque provider state.
300 * @param pvPlaintext The plaintext to encrypt.
301 * @param cbPlaintext The number of bytes of plaintext.
302 * @param pvCiphertext Where to return the ciphertext (if any).
303 * @param cbMaxCiphertext The size of the buffer @a pvCiphertext points to.
304 * @param pcbCiphertext Where to return the actual number of bytes of
305 * ciphertext returned.
306 * @param fFinal Whether this is the final call.
307 */
308 DECLCALLBACKMEMBER(int, pfnEncrypt)(struct RTCRPKIXENCRYPTIONDESC const *pDesc, void *pvState,
309 void const *pvPlaintext, size_t cbPlaintext,
310 void *pvCiphertext, size_t cbMaxCiphertext, size_t *pcbCiphertext, bool fFinal);
311
312 /**
313 * Calculate the output buffer size for the next pfnEncrypt call.
314 *
315 * @returns IPRT status code.
316 * @param pDesc Pointer to this structure (so uProviderSpecific can
317 * be read).
318 * @param pvState The opaque provider state.
319 * @param cbPlaintext The number of bytes of plaintext.
320 * @param pcbCiphertext Where to return the minimum buffer size. This may
321 * be larger than the actual number of bytes return.
322 * @param fFinal Whether this is the final call.
323 */
324 DECLCALLBACKMEMBER(int, pfnEncryptLength)(struct RTCRPKIXENCRYPTIONDESC const *pDesc, void *pvState,
325 size_t cbPlaintext, size_t *pcbCiphertext, bool fFinal);
326
327 /**
328 * Decrypt using the private key (fEncrypt = false).
329 *
330 * @returns IPRT status code.
331 * @retval VINF_SUCCESS on success.
332 * @retval VERR_PKIX_KEY wrong key or some other key issue.
333 * @retval VERR_BUFFER_OVERFLOW if the output buffer is too small, the require
334 * buffer size will be available in @a *pcbCiphertext. The caller can
335 * should retry the call with a larger buffer.
336 *
337 * @param pDesc Pointer to this structure (so uProviderSpecific can
338 * be read).
339 * @param pvState The opaque provider state.
340 * @param pvCiphertext The ciphertext to decrypt.
341 * @param cbCiphertext The number of bytes of ciphertext.
342 * @param pvPlaintext Where to return the plaintext (if any).
343 * @param cbMaxPlaintext The size of the buffer @a pvPlaintext points to.
344 * @param pcbPlaintext Where to return the actual number of bytes of
345 * plaintext returned.
346 * @param fFinal Whether this is the final call.
347 */
348 DECLCALLBACKMEMBER(int, pfnDecrypt)(struct RTCRPKIXENCRYPTIONDESC const *pDesc, void *pvState,
349 void const *pvCiphertext, size_t cbCiphertext,
350 void *pvPlaintext, size_t cbMaxPlaintext, size_t *pcbPlaintext, bool fFinal);
351
352 /**
353 * Calculate the output buffer size for the next pfnDecrypt call.
354 *
355 * @returns IPRT status code.
356 * @param pDesc Pointer to this structure (so uProviderSpecific can
357 * be read).
358 * @param pvState The opaque provider state.
359 * @param cbCiphertext The number of bytes of ciphertext.
360 * @param pcbPlaintext Where to return the minimum buffer size. This may
361 * be larger than the actual number of bytes return.
362 * @param fFinal Whether this is the final call.
363 */
364 DECLCALLBACKMEMBER(int, pfnDecryptLength)(struct RTCRPKIXENCRYPTIONDESC const *pDesc, void *pvState,
365 size_t cbCiphertext, size_t *pcbPlaintext, bool fFinal);
366} RTCRPKIXENCRYPTIONDESC;
367/** Pointer to a public key encryption schema provider descriptor. */
368typedef RTCRPKIXENCRYPTIONDESC const *PCRTCRPKIXENCRYPTIONDESC;
369
370
371PCRTCRPKIXENCRYPTIONDESC RTCrPkixEncryptionFindByObjIdString(const char *pszObjId, void *ppvOpaque);
372PCRTCRPKIXENCRYPTIONDESC RTCrPkixEncryptionFindByObjId(PCRTASN1OBJID pObjId, void *ppvOpaque);
373RTDECL(int) RTCrPkixEncryptionCreateByObjIdString(PRTCRPKIXENCRYPTION phEncryption, const char *pszObjId,
374 bool fEncrypt, PCRTASN1BITSTRING pKey,PCRTASN1DYNTYPE pParams);
375RTDECL(int) RTCrPkixEncryptionCreateByObjId(PRTCRPKIXENCRYPTION phEncryption, PCRTASN1OBJID pObjId, bool fEncrypt,
376 PCRTASN1BITSTRING pKey, PCRTASN1DYNTYPE pParams);
377
378
379RTDECL(int) RTCrPkixEncryptionCreate(PRTCRPKIXENCRYPTION phEncryption, PCRTCRPKIXENCRYPTIONDESC pDesc, void *pvOpaque,
380 bool fEncrypt, PCRTASN1BITSTRING pKey, PCRTASN1DYNTYPE pParams);
381RTDECL(int) RTCrPkixEncryptionReset(RTCRPKIXENCRYPTION hEncryption);
382RTDECL(uint32_t) RTCrPkixEncryptionRetain(RTCRPKIXENCRYPTION hEncryption);
383RTDECL(uint32_t) RTCrPkixEncryptionRelease(RTCRPKIXENCRYPTION hEncryption);
384
385
386/** @} */
387
388RT_C_DECLS_END
389
390#endif
391
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette