• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL project
3  * 2013.
4  */
5 /* ====================================================================
6  * Copyright (c) 2013 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    licensing@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #ifndef OPENSSL_HEADER_X509_INTERNAL_H
60 #define OPENSSL_HEADER_X509_INTERNAL_H
61 
62 #include <openssl/base.h>
63 #include <openssl/evp.h>
64 #include <openssl/x509.h>
65 
66 #include "../asn1/internal.h"
67 
68 #if defined(__cplusplus)
69 extern "C" {
70 #endif
71 
72 
73 /* Internal structures. */
74 
75 typedef struct X509_val_st {
76   ASN1_TIME *notBefore;
77   ASN1_TIME *notAfter;
78 } X509_VAL;
79 
80 DECLARE_ASN1_FUNCTIONS(X509_VAL)
81 
82 struct X509_pubkey_st {
83   X509_ALGOR *algor;
84   ASN1_BIT_STRING *public_key;
85   EVP_PKEY *pkey;
86 } /* X509_PUBKEY */;
87 
88 struct X509_name_entry_st {
89   ASN1_OBJECT *object;
90   ASN1_STRING *value;
91   int set;
92 } /* X509_NAME_ENTRY */;
93 
94 // we always keep X509_NAMEs in 2 forms.
95 struct X509_name_st {
96   STACK_OF(X509_NAME_ENTRY) *entries;
97   int modified;  // true if 'bytes' needs to be built
98   BUF_MEM *bytes;
99   // unsigned long hash; Keep the hash around for lookups
100   unsigned char *canon_enc;
101   int canon_enclen;
102 } /* X509_NAME */;
103 
104 struct x509_attributes_st {
105   ASN1_OBJECT *object;
106   STACK_OF(ASN1_TYPE) *set;
107 } /* X509_ATTRIBUTE */;
108 
109 typedef struct x509_cert_aux_st {
110   STACK_OF(ASN1_OBJECT) *trust;   // trusted uses
111   STACK_OF(ASN1_OBJECT) *reject;  // rejected uses
112   ASN1_UTF8STRING *alias;         // "friendly name"
113   ASN1_OCTET_STRING *keyid;       // key id of private key
114 } X509_CERT_AUX;
115 
116 DECLARE_ASN1_FUNCTIONS(X509_CERT_AUX)
117 
118 struct X509_extension_st {
119   ASN1_OBJECT *object;
120   ASN1_BOOLEAN critical;
121   ASN1_OCTET_STRING *value;
122 } /* X509_EXTENSION */;
123 
124 typedef struct {
125   ASN1_INTEGER *version;  // [ 0 ] default of v1
126   ASN1_INTEGER *serialNumber;
127   X509_ALGOR *signature;
128   X509_NAME *issuer;
129   X509_VAL *validity;
130   X509_NAME *subject;
131   X509_PUBKEY *key;
132   ASN1_BIT_STRING *issuerUID;            // [ 1 ] optional in v2
133   ASN1_BIT_STRING *subjectUID;           // [ 2 ] optional in v2
134   STACK_OF(X509_EXTENSION) *extensions;  // [ 3 ] optional in v3
135   ASN1_ENCODING enc;
136 } X509_CINF;
137 
138 DECLARE_ASN1_FUNCTIONS(X509_CINF)
139 
140 struct x509_st {
141   X509_CINF *cert_info;
142   X509_ALGOR *sig_alg;
143   ASN1_BIT_STRING *signature;
144   CRYPTO_refcount_t references;
145   CRYPTO_EX_DATA ex_data;
146   // These contain copies of various extension values
147   long ex_pathlen;
148   long ex_pcpathlen;
149   unsigned long ex_flags;
150   unsigned long ex_kusage;
151   unsigned long ex_xkusage;
152   unsigned long ex_nscert;
153   ASN1_OCTET_STRING *skid;
154   AUTHORITY_KEYID *akid;
155   X509_POLICY_CACHE *policy_cache;
156   STACK_OF(DIST_POINT) *crldp;
157   STACK_OF(GENERAL_NAME) *altname;
158   NAME_CONSTRAINTS *nc;
159   unsigned char cert_hash[SHA256_DIGEST_LENGTH];
160   X509_CERT_AUX *aux;
161   CRYPTO_BUFFER *buf;
162   CRYPTO_MUTEX lock;
163 } /* X509 */;
164 
165 typedef struct {
166   ASN1_ENCODING enc;
167   ASN1_INTEGER *version;
168   X509_NAME *subject;
169   X509_PUBKEY *pubkey;
170   //  d=2 hl=2 l=  0 cons: cont: 00
171   STACK_OF(X509_ATTRIBUTE) *attributes;  // [ 0 ]
172 } X509_REQ_INFO;
173 
174 DECLARE_ASN1_FUNCTIONS(X509_REQ_INFO)
175 
176 struct X509_req_st {
177   X509_REQ_INFO *req_info;
178   X509_ALGOR *sig_alg;
179   ASN1_BIT_STRING *signature;
180   CRYPTO_refcount_t references;
181 } /* X509_REQ */;
182 
183 struct x509_revoked_st {
184   ASN1_INTEGER *serialNumber;
185   ASN1_TIME *revocationDate;
186   STACK_OF(X509_EXTENSION) /* optional */ *extensions;
187   // Set up if indirect CRL
188   STACK_OF(GENERAL_NAME) *issuer;
189   // Revocation reason
190   int reason;
191 } /* X509_REVOKED */;
192 
193 typedef struct {
194   ASN1_INTEGER *version;
195   X509_ALGOR *sig_alg;
196   X509_NAME *issuer;
197   ASN1_TIME *lastUpdate;
198   ASN1_TIME *nextUpdate;
199   STACK_OF(X509_REVOKED) *revoked;
200   STACK_OF(X509_EXTENSION) /* [0] */ *extensions;
201   ASN1_ENCODING enc;
202 } X509_CRL_INFO;
203 
204 DECLARE_ASN1_FUNCTIONS(X509_CRL_INFO)
205 
206 struct X509_crl_st {
207   // actual signature
208   X509_CRL_INFO *crl;
209   X509_ALGOR *sig_alg;
210   ASN1_BIT_STRING *signature;
211   CRYPTO_refcount_t references;
212   int flags;
213   // Copies of various extensions
214   AUTHORITY_KEYID *akid;
215   ISSUING_DIST_POINT *idp;
216   // Convenient breakdown of IDP
217   int idp_flags;
218   int idp_reasons;
219   // CRL and base CRL numbers for delta processing
220   ASN1_INTEGER *crl_number;
221   ASN1_INTEGER *base_crl_number;
222   unsigned char crl_hash[SHA256_DIGEST_LENGTH];
223   STACK_OF(GENERAL_NAMES) *issuers;
224 } /* X509_CRL */;
225 
226 struct X509_VERIFY_PARAM_st {
227   char *name;
228   time_t check_time;                // Time to use
229   unsigned long inh_flags;          // Inheritance flags
230   unsigned long flags;              // Various verify flags
231   int purpose;                      // purpose to check untrusted certificates
232   int trust;                        // trust setting to check
233   int depth;                        // Verify depth
234   STACK_OF(ASN1_OBJECT) *policies;  // Permissible policies
235   // The following fields specify acceptable peer identities.
236   STACK_OF(OPENSSL_STRING) *hosts;  // Set of acceptable names
237   unsigned int hostflags;           // Flags to control matching features
238   char *peername;                   // Matching hostname in peer certificate
239   char *email;                      // If not NULL email address to match
240   size_t emaillen;
241   unsigned char *ip;     // If not NULL IP address to match
242   size_t iplen;          // Length of IP address
243   unsigned char poison;  // Fail all verifications at name checking
244 } /* X509_VERIFY_PARAM */;
245 
246 struct x509_object_st {
247   // one of the above types
248   int type;
249   union {
250     char *ptr;
251     X509 *x509;
252     X509_CRL *crl;
253     EVP_PKEY *pkey;
254   } data;
255 } /* X509_OBJECT */;
256 
257 // This is a static that defines the function interface
258 struct x509_lookup_method_st {
259   const char *name;
260   int (*new_item)(X509_LOOKUP *ctx);
261   void (*free)(X509_LOOKUP *ctx);
262   int (*init)(X509_LOOKUP *ctx);
263   int (*shutdown)(X509_LOOKUP *ctx);
264   int (*ctrl)(X509_LOOKUP *ctx, int cmd, const char *argc, long argl,
265               char **ret);
266   int (*get_by_subject)(X509_LOOKUP *ctx, int type, X509_NAME *name,
267                         X509_OBJECT *ret);
268   int (*get_by_issuer_serial)(X509_LOOKUP *ctx, int type, X509_NAME *name,
269                               ASN1_INTEGER *serial, X509_OBJECT *ret);
270   int (*get_by_fingerprint)(X509_LOOKUP *ctx, int type, unsigned char *bytes,
271                             int len, X509_OBJECT *ret);
272   int (*get_by_alias)(X509_LOOKUP *ctx, int type, char *str, int len,
273                       X509_OBJECT *ret);
274 } /* X509_LOOKUP_METHOD */;
275 
276 // This is used to hold everything.  It is used for all certificate
277 // validation.  Once we have a certificate chain, the 'verify'
278 // function is then called to actually check the cert chain.
279 struct x509_store_st {
280   // The following is a cache of trusted certs
281   int cache;                    // if true, stash any hits
282   STACK_OF(X509_OBJECT) *objs;  // Cache of all objects
283   CRYPTO_MUTEX objs_lock;
284 
285   // These are external lookup methods
286   STACK_OF(X509_LOOKUP) *get_cert_methods;
287 
288   X509_VERIFY_PARAM *param;
289 
290   // Callbacks for various operations
291   X509_STORE_CTX_verify_fn verify;          // called to verify a certificate
292   X509_STORE_CTX_verify_cb verify_cb;       // error callback
293   X509_STORE_CTX_get_issuer_fn get_issuer;  // get issuers cert from ctx
294   X509_STORE_CTX_check_issued_fn check_issued;  // check issued
295   X509_STORE_CTX_check_revocation_fn
296       check_revocation;                   // Check revocation status of chain
297   X509_STORE_CTX_get_crl_fn get_crl;      // retrieve CRL
298   X509_STORE_CTX_check_crl_fn check_crl;  // Check CRL validity
299   X509_STORE_CTX_cert_crl_fn cert_crl;    // Check certificate against CRL
300   X509_STORE_CTX_lookup_certs_fn lookup_certs;
301   X509_STORE_CTX_lookup_crls_fn lookup_crls;
302   X509_STORE_CTX_cleanup_fn cleanup;
303 
304   CRYPTO_refcount_t references;
305 } /* X509_STORE */;
306 
307 
308 // This is the functions plus an instance of the local variables.
309 struct x509_lookup_st {
310   int init;                    // have we been started
311   int skip;                    // don't use us.
312   X509_LOOKUP_METHOD *method;  // the functions
313   char *method_data;           // method data
314 
315   X509_STORE *store_ctx;  // who owns us
316 } /* X509_LOOKUP */;
317 
318 // This is a used when verifying cert chains.  Since the
319 // gathering of the cert chain can take some time (and have to be
320 // 'retried', this needs to be kept and passed around.
321 struct x509_store_ctx_st {
322   X509_STORE *ctx;
323 
324   // The following are set by the caller
325   X509 *cert;                 // The cert to check
326   STACK_OF(X509) *untrusted;  // chain of X509s - untrusted - passed in
327   STACK_OF(X509_CRL) *crls;   // set of CRLs passed in
328 
329   X509_VERIFY_PARAM *param;
330   void *other_ctx;  // Other info for use with get_issuer()
331 
332   // Callbacks for various operations
333   X509_STORE_CTX_verify_fn verify;          // called to verify a certificate
334   X509_STORE_CTX_verify_cb verify_cb;       // error callback
335   X509_STORE_CTX_get_issuer_fn get_issuer;  // get issuers cert from ctx
336   X509_STORE_CTX_check_issued_fn check_issued;  // check issued
337   X509_STORE_CTX_check_revocation_fn
338       check_revocation;                   // Check revocation status of chain
339   X509_STORE_CTX_get_crl_fn get_crl;      // retrieve CRL
340   X509_STORE_CTX_check_crl_fn check_crl;  // Check CRL validity
341   X509_STORE_CTX_cert_crl_fn cert_crl;    // Check certificate against CRL
342   X509_STORE_CTX_check_policy_fn check_policy;
343   X509_STORE_CTX_lookup_certs_fn lookup_certs;
344   X509_STORE_CTX_lookup_crls_fn lookup_crls;
345   X509_STORE_CTX_cleanup_fn cleanup;
346 
347   // The following is built up
348   int valid;               // if 0, rebuild chain
349   int last_untrusted;      // index of last untrusted cert
350   STACK_OF(X509) *chain;   // chain of X509s - built up and trusted
351   X509_POLICY_TREE *tree;  // Valid policy tree
352 
353   int explicit_policy;  // Require explicit policy value
354 
355   // When something goes wrong, this is why
356   int error_depth;
357   int error;
358   X509 *current_cert;
359   X509 *current_issuer;   // cert currently being tested as valid issuer
360   X509_CRL *current_crl;  // current CRL
361 
362   int current_crl_score;         // score of current CRL
363   unsigned int current_reasons;  // Reason mask
364 
365   X509_STORE_CTX *parent;  // For CRL path validation: parent context
366 
367   CRYPTO_EX_DATA ex_data;
368 } /* X509_STORE_CTX */;
369 
370 ASN1_TYPE *ASN1_generate_v3(const char *str, X509V3_CTX *cnf);
371 
372 int X509_CERT_AUX_print(BIO *bp, X509_CERT_AUX *x, int indent);
373 
374 
375 /* RSA-PSS functions. */
376 
377 /* x509_rsa_pss_to_ctx configures |ctx| for an RSA-PSS operation based on
378  * signature algorithm parameters in |sigalg| (which must have type
379  * |NID_rsassaPss|) and key |pkey|. It returns one on success and zero on
380  * error. */
381 int x509_rsa_pss_to_ctx(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg,
382                         EVP_PKEY *pkey);
383 
384 /* x509_rsa_pss_to_ctx sets |algor| to the signature algorithm parameters for
385  * |ctx|, which must have been configured for an RSA-PSS signing operation. It
386  * returns one on success and zero on error. */
387 int x509_rsa_ctx_to_pss(EVP_MD_CTX *ctx, X509_ALGOR *algor);
388 
389 /* x509_print_rsa_pss_params prints a human-readable representation of RSA-PSS
390  * parameters in |sigalg| to |bp|. It returns one on success and zero on
391  * error. */
392 int x509_print_rsa_pss_params(BIO *bp, const X509_ALGOR *sigalg, int indent,
393                               ASN1_PCTX *pctx);
394 
395 
396 /* Signature algorithm functions. */
397 
398 /* x509_digest_sign_algorithm encodes the signing parameters of |ctx| as an
399  * AlgorithmIdentifer and saves the result in |algor|. It returns one on
400  * success, or zero on error. */
401 int x509_digest_sign_algorithm(EVP_MD_CTX *ctx, X509_ALGOR *algor);
402 
403 /* x509_digest_verify_init sets up |ctx| for a signature verification operation
404  * with public key |pkey| and parameters from |algor|. The |ctx| argument must
405  * have been initialised with |EVP_MD_CTX_init|. It returns one on success, or
406  * zero on error. */
407 int x509_digest_verify_init(EVP_MD_CTX *ctx, const X509_ALGOR *sigalg,
408                             EVP_PKEY *pkey);
409 
410 
411 #if defined(__cplusplus)
412 }  /* extern C */
413 #endif
414 
415 #endif  /* OPENSSL_HEADER_X509_INTERNAL_H */
416