]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/BaseCryptLib/Pk/CryptX509.c
153e7106171eeaa83cce35e4814d6c054645deb6
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / Pk / CryptX509.c
1 /** @file
2 X.509 Certificate Handler Wrapper Implementation over OpenSSL.
3
4 Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "InternalCryptLib.h"
16 #include <openssl/x509.h>
17
18
19 /**
20 Construct a X509 object from DER-encoded certificate data.
21
22 If Cert is NULL, then return FALSE.
23 If SingleX509Cert is NULL, then return FALSE.
24
25 @param[in] Cert Pointer to the DER-encoded certificate data.
26 @param[in] CertSize The size of certificate data in bytes.
27 @param[out] SingleX509Cert The generated X509 object.
28
29 @retval TRUE The X509 object generation succeeded.
30 @retval FALSE The operation failed.
31
32 **/
33 BOOLEAN
34 EFIAPI
35 X509ConstructCertificate (
36 IN CONST UINT8 *Cert,
37 IN UINTN CertSize,
38 OUT UINT8 **SingleX509Cert
39 )
40 {
41 BIO *CertBio;
42 X509 *X509Cert;
43 BOOLEAN Status;
44
45 //
46 // Check input parameters.
47 //
48 if (Cert == NULL || SingleX509Cert == NULL || CertSize > INT_MAX) {
49 return FALSE;
50 }
51
52 Status = FALSE;
53
54 //
55 // Read DER-encoded X509 Certificate and Construct X509 object.
56 //
57 CertBio = BIO_new (BIO_s_mem ());
58 BIO_write (CertBio, Cert, (int) CertSize);
59 if (CertBio == NULL) {
60 goto _Exit;
61 }
62 X509Cert = d2i_X509_bio (CertBio, NULL);
63 if (X509Cert == NULL) {
64 goto _Exit;
65 }
66
67 *SingleX509Cert = (UINT8 *) X509Cert;
68 Status = TRUE;
69
70 _Exit:
71 //
72 // Release Resources.
73 //
74 BIO_free (CertBio);
75
76 return Status;
77 }
78
79 /**
80 Construct a X509 stack object from a list of DER-encoded certificate data.
81
82 If X509Stack is NULL, then return FALSE.
83
84 @param[in, out] X509Stack On input, pointer to an existing X509 stack object.
85 On output, pointer to the X509 stack object with new
86 inserted X509 certificate.
87 @param ... A list of DER-encoded single certificate data followed
88 by certificate size. A NULL terminates the list. The
89 pairs are the arguments to X509ConstructCertificate().
90
91 @retval TRUE The X509 stack construction succeeded.
92 @retval FALSE The construction operation failed.
93
94 **/
95 BOOLEAN
96 EFIAPI
97 X509ConstructCertificateStack (
98 IN OUT UINT8 **X509Stack,
99 ...
100 )
101 {
102 UINT8 *Cert;
103 UINTN CertSize;
104 X509 *X509Cert;
105 STACK_OF(X509) *CertStack;
106 BOOLEAN Status;
107 VA_LIST Args;
108 UINTN Index;
109
110 //
111 // Check input parameters.
112 //
113 if (X509Stack == NULL) {
114 return FALSE;
115 }
116
117 Status = FALSE;
118
119 //
120 // Initialize X509 stack object.
121 //
122 CertStack = (STACK_OF(X509) *) (*X509Stack);
123 if (CertStack == NULL) {
124 CertStack = sk_X509_new_null ();
125 if (CertStack == NULL) {
126 return Status;
127 }
128 }
129
130 VA_START (Args, X509Stack);
131
132 for (Index = 0; ; Index++) {
133 //
134 // If Cert is NULL, then it is the end of the list.
135 //
136 Cert = VA_ARG (Args, UINT8 *);
137 if (Cert == NULL) {
138 break;
139 }
140
141 CertSize = VA_ARG (Args, UINTN);
142
143 //
144 // Construct X509 Object from the given DER-encoded certificate data.
145 //
146 Status = X509ConstructCertificate (
147 (CONST UINT8 *) Cert,
148 CertSize,
149 (UINT8 **) &X509Cert
150 );
151 if (!Status) {
152 X509_free (X509Cert);
153 break;
154 }
155
156 //
157 // Insert the new X509 object into X509 stack object.
158 //
159 sk_X509_push (CertStack, X509Cert);
160 }
161
162 VA_END (Args);
163
164 if (!Status) {
165 sk_X509_pop_free (CertStack, X509_free);
166 } else {
167 *X509Stack = (UINT8 *) CertStack;
168 }
169
170 return Status;
171 }
172
173 /**
174 Release the specified X509 object.
175
176 If X509Cert is NULL, then return FALSE.
177
178 @param[in] X509Cert Pointer to the X509 object to be released.
179
180 **/
181 VOID
182 EFIAPI
183 X509Free (
184 IN VOID *X509Cert
185 )
186 {
187 //
188 // Check input parameters.
189 //
190 if (X509Cert == NULL) {
191 return;
192 }
193
194 //
195 // Free OpenSSL X509 object.
196 //
197 X509_free ((X509 *) X509Cert);
198 }
199
200 /**
201 Release the specified X509 stack object.
202
203 If X509Stack is NULL, then return FALSE.
204
205 @param[in] X509Stack Pointer to the X509 stack object to be released.
206
207 **/
208 VOID
209 EFIAPI
210 X509StackFree (
211 IN VOID *X509Stack
212 )
213 {
214 //
215 // Check input parameters.
216 //
217 if (X509Stack == NULL) {
218 return;
219 }
220
221 //
222 // Free OpenSSL X509 stack object.
223 //
224 sk_X509_pop_free ((STACK_OF(X509) *) X509Stack, X509_free);
225 }
226
227 /**
228 Retrieve the subject bytes from one X.509 certificate.
229
230 @param[in] Cert Pointer to the DER-encoded X509 certificate.
231 @param[in] CertSize Size of the X509 certificate in bytes.
232 @param[out] CertSubject Pointer to the retrieved certificate subject bytes.
233 @param[in, out] SubjectSize The size in bytes of the CertSubject buffer on input,
234 and the size of buffer returned CertSubject on output.
235
236 If Cert is NULL, then return FALSE.
237 If SubjectSize is NULL, then return FALSE.
238
239 @retval TRUE The certificate subject retrieved successfully.
240 @retval FALSE Invalid certificate, or the SubjectSize is too small for the result.
241 The SubjectSize will be updated with the required size.
242
243 **/
244 BOOLEAN
245 EFIAPI
246 X509GetSubjectName (
247 IN CONST UINT8 *Cert,
248 IN UINTN CertSize,
249 OUT UINT8 *CertSubject,
250 IN OUT UINTN *SubjectSize
251 )
252 {
253 BOOLEAN Status;
254 X509 *X509Cert;
255 X509_NAME *X509Name;
256
257 //
258 // Check input parameters.
259 //
260 if (Cert == NULL || SubjectSize == NULL) {
261 return FALSE;
262 }
263
264 Status = FALSE;
265 X509Cert = NULL;
266
267 //
268 // Read DER-encoded X509 Certificate and Construct X509 object.
269 //
270 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
271 if ((X509Cert == NULL) || (!Status)) {
272 goto _Exit;
273 }
274
275 //
276 // Retrieve subject name from certificate object.
277 //
278 X509Name = X509_get_subject_name (X509Cert);
279 if (*SubjectSize < (UINTN) X509Name->bytes->length) {
280 *SubjectSize = (UINTN) X509Name->bytes->length;
281 goto _Exit;
282 }
283 *SubjectSize = (UINTN) X509Name->bytes->length;
284 if (CertSubject != NULL) {
285 CopyMem (CertSubject, (UINT8 *)X509Name->bytes->data, *SubjectSize);
286 Status = TRUE;
287 }
288
289 _Exit:
290 //
291 // Release Resources.
292 //
293 X509_free (X509Cert);
294
295 return Status;
296 }
297
298 /**
299 Retrieve the RSA Public Key from one DER-encoded X509 certificate.
300
301 @param[in] Cert Pointer to the DER-encoded X509 certificate.
302 @param[in] CertSize Size of the X509 certificate in bytes.
303 @param[out] RsaContext Pointer to new-generated RSA context which contain the retrieved
304 RSA public key component. Use RsaFree() function to free the
305 resource.
306
307 If Cert is NULL, then return FALSE.
308 If RsaContext is NULL, then return FALSE.
309
310 @retval TRUE RSA Public Key was retrieved successfully.
311 @retval FALSE Fail to retrieve RSA public key from X509 certificate.
312
313 **/
314 BOOLEAN
315 EFIAPI
316 RsaGetPublicKeyFromX509 (
317 IN CONST UINT8 *Cert,
318 IN UINTN CertSize,
319 OUT VOID **RsaContext
320 )
321 {
322 BOOLEAN Status;
323 EVP_PKEY *Pkey;
324 X509 *X509Cert;
325
326 //
327 // Check input parameters.
328 //
329 if (Cert == NULL || RsaContext == NULL) {
330 return FALSE;
331 }
332
333 Status = FALSE;
334 Pkey = NULL;
335 X509Cert = NULL;
336
337 //
338 // Read DER-encoded X509 Certificate and Construct X509 object.
339 //
340 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
341 if ((X509Cert == NULL) || (!Status)) {
342 goto _Exit;
343 }
344
345 //
346 // Retrieve and check EVP_PKEY data from X509 Certificate.
347 //
348 Pkey = X509_get_pubkey (X509Cert);
349 if ((Pkey == NULL) || (Pkey->type != EVP_PKEY_RSA)) {
350 goto _Exit;
351 }
352
353 //
354 // Duplicate RSA Context from the retrieved EVP_PKEY.
355 //
356 if ((*RsaContext = RSAPublicKey_dup (Pkey->pkey.rsa)) != NULL) {
357 Status = TRUE;
358 }
359
360 _Exit:
361 //
362 // Release Resources.
363 //
364 X509_free (X509Cert);
365 EVP_PKEY_free (Pkey);
366
367 return Status;
368 }
369
370 /**
371 Verify one X509 certificate was issued by the trusted CA.
372
373 @param[in] Cert Pointer to the DER-encoded X509 certificate to be verified.
374 @param[in] CertSize Size of the X509 certificate in bytes.
375 @param[in] CACert Pointer to the DER-encoded trusted CA certificate.
376 @param[in] CACertSize Size of the CA Certificate in bytes.
377
378 If Cert is NULL, then return FALSE.
379 If CACert is NULL, then return FALSE.
380
381 @retval TRUE The certificate was issued by the trusted CA.
382 @retval FALSE Invalid certificate or the certificate was not issued by the given
383 trusted CA.
384
385 **/
386 BOOLEAN
387 EFIAPI
388 X509VerifyCert (
389 IN CONST UINT8 *Cert,
390 IN UINTN CertSize,
391 IN CONST UINT8 *CACert,
392 IN UINTN CACertSize
393 )
394 {
395 BOOLEAN Status;
396 X509 *X509Cert;
397 X509 *X509CACert;
398 X509_STORE *CertStore;
399 X509_STORE_CTX CertCtx;
400
401 //
402 // Check input parameters.
403 //
404 if (Cert == NULL || CACert == NULL) {
405 return FALSE;
406 }
407
408 Status = FALSE;
409 X509Cert = NULL;
410 X509CACert = NULL;
411 CertStore = NULL;
412
413 //
414 // Register & Initialize necessary digest algorithms for certificate verification.
415 //
416 EVP_add_digest (EVP_md5());
417 EVP_add_digest (EVP_sha1());
418 EVP_add_digest (EVP_sha256());
419
420 //
421 // Read DER-encoded certificate to be verified and Construct X509 object.
422 //
423 Status = X509ConstructCertificate (Cert, CertSize, (UINT8 **) &X509Cert);
424 if ((X509Cert == NULL) || (!Status)) {
425 goto _Exit;
426 }
427
428 //
429 // Read DER-encoded root certificate and Construct X509 object.
430 //
431 Status = X509ConstructCertificate (CACert, CACertSize, (UINT8 **) &X509CACert);
432 if ((X509CACert == NULL) || (!Status)) {
433 goto _Exit;
434 }
435
436 //
437 // Set up X509 Store for trusted certificate.
438 //
439 CertStore = X509_STORE_new ();
440 if (CertStore == NULL) {
441 goto _Exit;
442 }
443 if (!(X509_STORE_add_cert (CertStore, X509CACert))) {
444 goto _Exit;
445 }
446
447 //
448 // Set up X509_STORE_CTX for the subsequent verification operation.
449 //
450 if (!X509_STORE_CTX_init (&CertCtx, CertStore, X509Cert, NULL)) {
451 goto _Exit;
452 }
453
454 //
455 // X509 Certificate Verification.
456 //
457 Status = (BOOLEAN) X509_verify_cert (&CertCtx);
458 X509_STORE_CTX_cleanup (&CertCtx);
459
460 _Exit:
461 //
462 // Release Resources.
463 //
464 X509_free (X509Cert);
465 X509_free (X509CACert);
466 X509_STORE_free (CertStore);
467
468 return Status;
469 }