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