2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
61 #include <openssl/bio.h>
62 #include <openssl/objects.h>
63 #include <openssl/evp.h>
64 #include <openssl/x509.h>
65 #include <openssl/pem.h>
67 static int ssl_set_cert(CERT
*c
, X509
*x509
);
68 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
);
69 #ifndef OPENSSL_NO_TLSEXT
70 static int ssl_set_authz(CERT
*c
, unsigned char *authz
,
73 int SSL_use_certificate(SSL
*ssl
, X509
*x
)
77 SSLerr(SSL_F_SSL_USE_CERTIFICATE
,ERR_R_PASSED_NULL_PARAMETER
);
80 if (!ssl_cert_inst(&ssl
->cert
))
82 SSLerr(SSL_F_SSL_USE_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
85 return(ssl_set_cert(ssl
->cert
,x
));
88 #ifndef OPENSSL_NO_STDIO
89 int SSL_use_certificate_file(SSL
*ssl
, const char *file
, int type
)
96 in
=BIO_new(BIO_s_file_internal());
99 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
,ERR_R_BUF_LIB
);
103 if (BIO_read_filename(in
,file
) <= 0)
105 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
,ERR_R_SYS_LIB
);
108 if (type
== SSL_FILETYPE_ASN1
)
111 x
=d2i_X509_bio(in
,NULL
);
113 else if (type
== SSL_FILETYPE_PEM
)
116 x
=PEM_read_bio_X509(in
,NULL
,ssl
->ctx
->default_passwd_callback
,ssl
->ctx
->default_passwd_callback_userdata
);
120 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
,SSL_R_BAD_SSL_FILETYPE
);
126 SSLerr(SSL_F_SSL_USE_CERTIFICATE_FILE
,j
);
130 ret
=SSL_use_certificate(ssl
,x
);
132 if (x
!= NULL
) X509_free(x
);
133 if (in
!= NULL
) BIO_free(in
);
138 int SSL_use_certificate_ASN1(SSL
*ssl
, const unsigned char *d
, int len
)
143 x
=d2i_X509(NULL
,&d
,(long)len
);
146 SSLerr(SSL_F_SSL_USE_CERTIFICATE_ASN1
,ERR_R_ASN1_LIB
);
150 ret
=SSL_use_certificate(ssl
,x
);
155 #ifndef OPENSSL_NO_RSA
156 int SSL_use_RSAPrivateKey(SSL
*ssl
, RSA
*rsa
)
163 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
,ERR_R_PASSED_NULL_PARAMETER
);
166 if (!ssl_cert_inst(&ssl
->cert
))
168 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
,ERR_R_MALLOC_FAILURE
);
171 if ((pkey
=EVP_PKEY_new()) == NULL
)
173 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY
,ERR_R_EVP_LIB
);
178 EVP_PKEY_assign_RSA(pkey
,rsa
);
180 ret
=ssl_set_pkey(ssl
->cert
,pkey
);
186 static int ssl_set_pkey(CERT
*c
, EVP_PKEY
*pkey
)
189 /* Special case for DH: check two DH certificate types for a match.
190 * This means for DH certificates we must set the certificate first.
192 if (pkey
->type
== EVP_PKEY_DH
)
196 x
= c
->pkeys
[SSL_PKEY_DH_RSA
].x509
;
197 if (x
&& X509_check_private_key(x
, pkey
))
199 x
= c
->pkeys
[SSL_PKEY_DH_DSA
].x509
;
200 if (i
== -1 && x
&& X509_check_private_key(x
, pkey
))
205 i
=ssl_cert_type(NULL
,pkey
);
208 SSLerr(SSL_F_SSL_SET_PKEY
,SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
212 if (c
->pkeys
[i
].x509
!= NULL
)
215 pktmp
= X509_get_pubkey(c
->pkeys
[i
].x509
);
216 EVP_PKEY_copy_parameters(pktmp
,pkey
);
217 EVP_PKEY_free(pktmp
);
220 #ifndef OPENSSL_NO_RSA
221 /* Don't check the public/private key, this is mostly
222 * for smart cards. */
223 if ((pkey
->type
== EVP_PKEY_RSA
) &&
224 (RSA_flags(pkey
->pkey
.rsa
) & RSA_METHOD_FLAG_NO_CHECK
))
228 if (!X509_check_private_key(c
->pkeys
[i
].x509
,pkey
))
230 X509_free(c
->pkeys
[i
].x509
);
231 c
->pkeys
[i
].x509
= NULL
;
236 if (c
->pkeys
[i
].privatekey
!= NULL
)
237 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
238 CRYPTO_add(&pkey
->references
,1,CRYPTO_LOCK_EVP_PKEY
);
239 c
->pkeys
[i
].privatekey
=pkey
;
240 c
->key
= &(c
->pkeys
[i
]);
246 #ifndef OPENSSL_NO_RSA
247 #ifndef OPENSSL_NO_STDIO
248 int SSL_use_RSAPrivateKey_file(SSL
*ssl
, const char *file
, int type
)
254 in
=BIO_new(BIO_s_file_internal());
257 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
,ERR_R_BUF_LIB
);
261 if (BIO_read_filename(in
,file
) <= 0)
263 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
,ERR_R_SYS_LIB
);
266 if (type
== SSL_FILETYPE_ASN1
)
269 rsa
=d2i_RSAPrivateKey_bio(in
,NULL
);
271 else if (type
== SSL_FILETYPE_PEM
)
274 rsa
=PEM_read_bio_RSAPrivateKey(in
,NULL
,
275 ssl
->ctx
->default_passwd_callback
,ssl
->ctx
->default_passwd_callback_userdata
);
279 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
,SSL_R_BAD_SSL_FILETYPE
);
284 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_FILE
,j
);
287 ret
=SSL_use_RSAPrivateKey(ssl
,rsa
);
290 if (in
!= NULL
) BIO_free(in
);
295 int SSL_use_RSAPrivateKey_ASN1(SSL
*ssl
, unsigned char *d
, long len
)
298 const unsigned char *p
;
302 if ((rsa
=d2i_RSAPrivateKey(NULL
,&p
,(long)len
)) == NULL
)
304 SSLerr(SSL_F_SSL_USE_RSAPRIVATEKEY_ASN1
,ERR_R_ASN1_LIB
);
308 ret
=SSL_use_RSAPrivateKey(ssl
,rsa
);
312 #endif /* !OPENSSL_NO_RSA */
314 int SSL_use_PrivateKey(SSL
*ssl
, EVP_PKEY
*pkey
)
320 SSLerr(SSL_F_SSL_USE_PRIVATEKEY
,ERR_R_PASSED_NULL_PARAMETER
);
323 if (!ssl_cert_inst(&ssl
->cert
))
325 SSLerr(SSL_F_SSL_USE_PRIVATEKEY
,ERR_R_MALLOC_FAILURE
);
328 ret
=ssl_set_pkey(ssl
->cert
,pkey
);
332 #ifndef OPENSSL_NO_STDIO
333 int SSL_use_PrivateKey_file(SSL
*ssl
, const char *file
, int type
)
339 in
=BIO_new(BIO_s_file_internal());
342 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
,ERR_R_BUF_LIB
);
346 if (BIO_read_filename(in
,file
) <= 0)
348 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
,ERR_R_SYS_LIB
);
351 if (type
== SSL_FILETYPE_PEM
)
354 pkey
=PEM_read_bio_PrivateKey(in
,NULL
,
355 ssl
->ctx
->default_passwd_callback
,ssl
->ctx
->default_passwd_callback_userdata
);
357 else if (type
== SSL_FILETYPE_ASN1
)
360 pkey
= d2i_PrivateKey_bio(in
,NULL
);
364 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
,SSL_R_BAD_SSL_FILETYPE
);
369 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_FILE
,j
);
372 ret
=SSL_use_PrivateKey(ssl
,pkey
);
375 if (in
!= NULL
) BIO_free(in
);
380 int SSL_use_PrivateKey_ASN1(int type
, SSL
*ssl
, const unsigned char *d
, long len
)
383 const unsigned char *p
;
387 if ((pkey
=d2i_PrivateKey(type
,NULL
,&p
,(long)len
)) == NULL
)
389 SSLerr(SSL_F_SSL_USE_PRIVATEKEY_ASN1
,ERR_R_ASN1_LIB
);
393 ret
=SSL_use_PrivateKey(ssl
,pkey
);
398 int SSL_CTX_use_certificate(SSL_CTX
*ctx
, X509
*x
)
402 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
,ERR_R_PASSED_NULL_PARAMETER
);
405 if (!ssl_cert_inst(&ctx
->cert
))
407 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE
,ERR_R_MALLOC_FAILURE
);
410 return(ssl_set_cert(ctx
->cert
, x
));
413 static int ssl_set_cert(CERT
*c
, X509
*x
)
418 pkey
=X509_get_pubkey(x
);
421 SSLerr(SSL_F_SSL_SET_CERT
,SSL_R_X509_LIB
);
425 i
=ssl_cert_type(x
,pkey
);
428 SSLerr(SSL_F_SSL_SET_CERT
,SSL_R_UNKNOWN_CERTIFICATE_TYPE
);
433 if (c
->pkeys
[i
].privatekey
!= NULL
)
435 EVP_PKEY_copy_parameters(pkey
,c
->pkeys
[i
].privatekey
);
438 #ifndef OPENSSL_NO_RSA
439 /* Don't check the public/private key, this is mostly
440 * for smart cards. */
441 if ((c
->pkeys
[i
].privatekey
->type
== EVP_PKEY_RSA
) &&
442 (RSA_flags(c
->pkeys
[i
].privatekey
->pkey
.rsa
) &
443 RSA_METHOD_FLAG_NO_CHECK
))
446 #endif /* OPENSSL_NO_RSA */
447 if (!X509_check_private_key(x
,c
->pkeys
[i
].privatekey
))
449 /* don't fail for a cert/key mismatch, just free
450 * current private key (when switching to a different
451 * cert & key, first this function should be used,
452 * then ssl_set_pkey */
453 EVP_PKEY_free(c
->pkeys
[i
].privatekey
);
454 c
->pkeys
[i
].privatekey
=NULL
;
455 /* clear error queue */
462 if (c
->pkeys
[i
].x509
!= NULL
)
463 X509_free(c
->pkeys
[i
].x509
);
464 CRYPTO_add(&x
->references
,1,CRYPTO_LOCK_X509
);
466 c
->key
= &(c
->pkeys
[i
]);
472 #ifndef OPENSSL_NO_STDIO
473 int SSL_CTX_use_certificate_file(SSL_CTX
*ctx
, const char *file
, int type
)
480 in
=BIO_new(BIO_s_file_internal());
483 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,ERR_R_BUF_LIB
);
487 if (BIO_read_filename(in
,file
) <= 0)
489 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,ERR_R_SYS_LIB
);
492 if (type
== SSL_FILETYPE_ASN1
)
495 x
=d2i_X509_bio(in
,NULL
);
497 else if (type
== SSL_FILETYPE_PEM
)
500 x
=PEM_read_bio_X509(in
,NULL
,ctx
->default_passwd_callback
,ctx
->default_passwd_callback_userdata
);
504 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,SSL_R_BAD_SSL_FILETYPE
);
510 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_FILE
,j
);
514 ret
=SSL_CTX_use_certificate(ctx
,x
);
516 if (x
!= NULL
) X509_free(x
);
517 if (in
!= NULL
) BIO_free(in
);
522 int SSL_CTX_use_certificate_ASN1(SSL_CTX
*ctx
, int len
, const unsigned char *d
)
527 x
=d2i_X509(NULL
,&d
,(long)len
);
530 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_ASN1
,ERR_R_ASN1_LIB
);
534 ret
=SSL_CTX_use_certificate(ctx
,x
);
539 #ifndef OPENSSL_NO_RSA
540 int SSL_CTX_use_RSAPrivateKey(SSL_CTX
*ctx
, RSA
*rsa
)
547 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
,ERR_R_PASSED_NULL_PARAMETER
);
550 if (!ssl_cert_inst(&ctx
->cert
))
552 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
,ERR_R_MALLOC_FAILURE
);
555 if ((pkey
=EVP_PKEY_new()) == NULL
)
557 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY
,ERR_R_EVP_LIB
);
562 EVP_PKEY_assign_RSA(pkey
,rsa
);
564 ret
=ssl_set_pkey(ctx
->cert
, pkey
);
569 #ifndef OPENSSL_NO_STDIO
570 int SSL_CTX_use_RSAPrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
576 in
=BIO_new(BIO_s_file_internal());
579 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
,ERR_R_BUF_LIB
);
583 if (BIO_read_filename(in
,file
) <= 0)
585 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
,ERR_R_SYS_LIB
);
588 if (type
== SSL_FILETYPE_ASN1
)
591 rsa
=d2i_RSAPrivateKey_bio(in
,NULL
);
593 else if (type
== SSL_FILETYPE_PEM
)
596 rsa
=PEM_read_bio_RSAPrivateKey(in
,NULL
,
597 ctx
->default_passwd_callback
,ctx
->default_passwd_callback_userdata
);
601 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
,SSL_R_BAD_SSL_FILETYPE
);
606 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_FILE
,j
);
609 ret
=SSL_CTX_use_RSAPrivateKey(ctx
,rsa
);
612 if (in
!= NULL
) BIO_free(in
);
617 int SSL_CTX_use_RSAPrivateKey_ASN1(SSL_CTX
*ctx
, const unsigned char *d
, long len
)
620 const unsigned char *p
;
624 if ((rsa
=d2i_RSAPrivateKey(NULL
,&p
,(long)len
)) == NULL
)
626 SSLerr(SSL_F_SSL_CTX_USE_RSAPRIVATEKEY_ASN1
,ERR_R_ASN1_LIB
);
630 ret
=SSL_CTX_use_RSAPrivateKey(ctx
,rsa
);
634 #endif /* !OPENSSL_NO_RSA */
636 int SSL_CTX_use_PrivateKey(SSL_CTX
*ctx
, EVP_PKEY
*pkey
)
640 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY
,ERR_R_PASSED_NULL_PARAMETER
);
643 if (!ssl_cert_inst(&ctx
->cert
))
645 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY
,ERR_R_MALLOC_FAILURE
);
648 return(ssl_set_pkey(ctx
->cert
,pkey
));
651 #ifndef OPENSSL_NO_STDIO
652 int SSL_CTX_use_PrivateKey_file(SSL_CTX
*ctx
, const char *file
, int type
)
658 in
=BIO_new(BIO_s_file_internal());
661 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
,ERR_R_BUF_LIB
);
665 if (BIO_read_filename(in
,file
) <= 0)
667 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
,ERR_R_SYS_LIB
);
670 if (type
== SSL_FILETYPE_PEM
)
673 pkey
=PEM_read_bio_PrivateKey(in
,NULL
,
674 ctx
->default_passwd_callback
,ctx
->default_passwd_callback_userdata
);
676 else if (type
== SSL_FILETYPE_ASN1
)
679 pkey
= d2i_PrivateKey_bio(in
,NULL
);
683 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
,SSL_R_BAD_SSL_FILETYPE
);
688 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_FILE
,j
);
691 ret
=SSL_CTX_use_PrivateKey(ctx
,pkey
);
694 if (in
!= NULL
) BIO_free(in
);
699 int SSL_CTX_use_PrivateKey_ASN1(int type
, SSL_CTX
*ctx
, const unsigned char *d
,
703 const unsigned char *p
;
707 if ((pkey
=d2i_PrivateKey(type
,NULL
,&p
,(long)len
)) == NULL
)
709 SSLerr(SSL_F_SSL_CTX_USE_PRIVATEKEY_ASN1
,ERR_R_ASN1_LIB
);
713 ret
=SSL_CTX_use_PrivateKey(ctx
,pkey
);
719 #ifndef OPENSSL_NO_STDIO
720 /* Read a file that contains our certificate in "PEM" format,
721 * possibly followed by a sequence of CA certificates that should be
722 * sent to the peer in the Certificate message.
724 int SSL_CTX_use_certificate_chain_file(SSL_CTX
*ctx
, const char *file
)
730 ERR_clear_error(); /* clear error stack for SSL_CTX_use_certificate() */
732 in
= BIO_new(BIO_s_file_internal());
735 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
,ERR_R_BUF_LIB
);
739 if (BIO_read_filename(in
,file
) <= 0)
741 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
,ERR_R_SYS_LIB
);
745 x
=PEM_read_bio_X509_AUX(in
,NULL
,ctx
->default_passwd_callback
,
746 ctx
->default_passwd_callback_userdata
);
749 SSLerr(SSL_F_SSL_CTX_USE_CERTIFICATE_CHAIN_FILE
,ERR_R_PEM_LIB
);
753 ret
= SSL_CTX_use_certificate(ctx
, x
);
755 if (ERR_peek_error() != 0)
756 ret
= 0; /* Key/certificate mismatch doesn't imply ret==0 ... */
759 /* If we could set up our certificate, now proceed to
760 * the CA certificates.
766 if (ctx
->extra_certs
!= NULL
)
768 sk_X509_pop_free(ctx
->extra_certs
, X509_free
);
769 ctx
->extra_certs
= NULL
;
772 while ((ca
= PEM_read_bio_X509(in
, NULL
,
773 ctx
->default_passwd_callback
,
774 ctx
->default_passwd_callback_userdata
))
777 r
= SSL_CTX_add_extra_chain_cert(ctx
, ca
);
784 /* Note that we must not free r if it was successfully
785 * added to the chain (while we must free the main
786 * certificate, since its reference count is increased
787 * by SSL_CTX_use_certificate). */
789 /* When the while loop ends, it's usually just EOF. */
790 err
= ERR_peek_last_error();
791 if (ERR_GET_LIB(err
) == ERR_LIB_PEM
&& ERR_GET_REASON(err
) == PEM_R_NO_START_LINE
)
794 ret
= 0; /* some real error */
798 if (x
!= NULL
) X509_free(x
);
799 if (in
!= NULL
) BIO_free(in
);
804 #ifndef OPENSSL_NO_TLSEXT
805 /* authz_validate returns true iff authz is well formed, i.e. that it meets the
806 * wire format as documented in the CERT_PKEY structure and that there are no
807 * duplicate entries. */
808 static char authz_validate(const unsigned char *authz
, size_t length
)
810 unsigned char types_seen_bitmap
[32];
815 memset(types_seen_bitmap
, 0, sizeof(types_seen_bitmap
));
819 unsigned char type
, byte
, bit
;
830 if (types_seen_bitmap
[byte
] & (1 << bit
))
832 types_seen_bitmap
[byte
] |= (1 << bit
);
836 len
= ((unsigned short) authz
[0]) << 8 |
837 ((unsigned short) authz
[1]);
849 static int serverinfo_find_extension(const unsigned char *serverinfo
,
850 size_t serverinfo_length
,
851 unsigned short extension_type
,
852 const unsigned char **extension_data
,
853 unsigned short *extension_length
)
855 *extension_data
= NULL
;
856 *extension_length
= 0;
857 if (serverinfo
== NULL
|| serverinfo_length
== 0)
861 unsigned short type
= 0; /* uint16 */
862 unsigned short len
= 0; /* uint16 */
864 /* end of serverinfo */
865 if (serverinfo_length
== 0)
866 return -1; /* Extension not found */
868 /* read 2-byte type field */
869 if (serverinfo_length
< 2)
870 return 0; /* Error */
871 type
= (serverinfo
[0] << 8) + serverinfo
[1];
873 serverinfo_length
-= 2;
875 /* read 2-byte len field */
876 if (serverinfo_length
< 2)
877 return 0; /* Error */
878 len
= (serverinfo
[0] << 8) + serverinfo
[1];
880 serverinfo_length
-= 2;
882 if (len
> serverinfo_length
)
883 return 0; /* Error */
885 if (type
== extension_type
)
887 *extension_data
= serverinfo
;
888 *extension_length
= len
;
889 return 1; /* Success */
893 serverinfo_length
-= len
;
895 return 0; /* Error */
898 static int serverinfo_srv_first_cb(SSL
*s
, unsigned short ext_type
,
899 const unsigned char *in
,
900 unsigned short inlen
, int *al
,
905 *al
= SSL_AD_DECODE_ERROR
;
911 static int serverinfo_srv_second_cb(SSL
*s
, unsigned short ext_type
,
912 const unsigned char **out
, unsigned short *outlen
,
915 const unsigned char *serverinfo
= NULL
;
916 size_t serverinfo_length
= 0;
918 /* Is there serverinfo data for the chosen server cert? */
919 if ((ssl_get_server_cert_serverinfo(s
, &serverinfo
,
920 &serverinfo_length
)) != 0)
922 /* Find the relevant extension from the serverinfo */
923 int retval
= serverinfo_find_extension(serverinfo
, serverinfo_length
,
924 ext_type
, out
, outlen
);
926 return 0; /* Error */
928 return -1; /* No extension found, don't send extension */
929 return 1; /* Send extension */
931 return -1; /* No serverinfo data found, don't send extension */
934 /* With a NULL context, this function just checks that the serverinfo data
935 parses correctly. With a non-NULL context, it registers callbacks for
936 the included extensions. */
937 static int serverinfo_process_buffer(const unsigned char *serverinfo
,
938 size_t serverinfo_length
, SSL_CTX
*ctx
)
940 if (serverinfo
== NULL
|| serverinfo_length
== 0)
944 unsigned short ext_type
= 0; /* uint16 */
945 unsigned short len
= 0; /* uint16 */
947 /* end of serverinfo */
948 if (serverinfo_length
== 0)
951 /* read 2-byte type field */
952 if (serverinfo_length
< 2)
954 /* FIXME: check for types we understand explicitly? */
956 /* Register callbacks for extensions */
957 ext_type
= (serverinfo
[0] << 8) + serverinfo
[1];
958 if (ctx
&& !SSL_CTX_set_custom_srv_ext(ctx
, ext_type
,
959 serverinfo_srv_first_cb
,
960 serverinfo_srv_second_cb
, NULL
))
964 serverinfo_length
-= 2;
966 /* read 2-byte len field */
967 if (serverinfo_length
< 2)
969 len
= (serverinfo
[0] << 8) + serverinfo
[1];
971 serverinfo_length
-= 2;
973 if (len
> serverinfo_length
)
977 serverinfo_length
-= len
;
981 static const unsigned char *authz_find_data(const unsigned char *authz
,
983 unsigned char data_type
,
986 if (authz
== NULL
) return NULL
;
987 if (!authz_validate(authz
, authz_length
))
989 SSLerr(SSL_F_AUTHZ_FIND_DATA
,SSL_R_INVALID_AUTHZ_DATA
);
1003 /* We've validated the authz data, so we don't have to
1004 * check again that we have enough bytes left. */
1005 len
= ((unsigned short) authz
[0]) << 8 |
1006 ((unsigned short) authz
[1]);
1009 if (type
== data_type
)
1015 authz_length
-= len
;
1021 static int ssl_set_authz(CERT
*c
, unsigned char *authz
, size_t authz_length
)
1023 CERT_PKEY
*current_key
= c
->key
;
1024 if (current_key
== NULL
)
1026 if (!authz_validate(authz
, authz_length
))
1028 SSLerr(SSL_F_SSL_SET_AUTHZ
,SSL_R_INVALID_AUTHZ_DATA
);
1031 current_key
->authz
= OPENSSL_realloc(current_key
->authz
, authz_length
);
1032 if (current_key
->authz
== NULL
)
1034 SSLerr(SSL_F_SSL_SET_AUTHZ
,ERR_R_MALLOC_FAILURE
);
1037 current_key
->authz_length
= authz_length
;
1038 memcpy(current_key
->authz
, authz
, authz_length
);
1042 int SSL_CTX_use_authz(SSL_CTX
*ctx
, unsigned char *authz
,
1043 size_t authz_length
)
1047 SSLerr(SSL_F_SSL_CTX_USE_AUTHZ
,ERR_R_PASSED_NULL_PARAMETER
);
1050 if (!ssl_cert_inst(&ctx
->cert
))
1052 SSLerr(SSL_F_SSL_CTX_USE_AUTHZ
,ERR_R_MALLOC_FAILURE
);
1055 return ssl_set_authz(ctx
->cert
, authz
, authz_length
);
1058 int SSL_CTX_use_serverinfo(SSL_CTX
*ctx
, const unsigned char *serverinfo
,
1059 size_t serverinfo_length
)
1061 if (ctx
== NULL
|| serverinfo
== NULL
|| serverinfo_length
== 0)
1063 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
,ERR_R_PASSED_NULL_PARAMETER
);
1066 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, NULL
))
1068 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
,SSL_R_INVALID_SERVERINFO_DATA
);
1071 if (!ssl_cert_inst(&ctx
->cert
))
1073 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
,ERR_R_MALLOC_FAILURE
);
1076 if (ctx
->cert
->key
== NULL
)
1078 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
,ERR_R_INTERNAL_ERROR
);
1081 ctx
->cert
->key
->serverinfo
= OPENSSL_realloc(ctx
->cert
->key
->serverinfo
,
1083 if (ctx
->cert
->key
->serverinfo
== NULL
)
1085 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
,ERR_R_MALLOC_FAILURE
);
1088 memcpy(ctx
->cert
->key
->serverinfo
, serverinfo
, serverinfo_length
);
1089 ctx
->cert
->key
->serverinfo_length
= serverinfo_length
;
1091 /* Now that the serverinfo is validated and stored, go ahead and
1092 * register callbacks. */
1093 if (!serverinfo_process_buffer(serverinfo
, serverinfo_length
, ctx
))
1095 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO
,SSL_R_INVALID_SERVERINFO_DATA
);
1101 int SSL_use_authz(SSL
*ssl
, unsigned char *authz
, size_t authz_length
)
1105 SSLerr(SSL_F_SSL_USE_AUTHZ
,ERR_R_PASSED_NULL_PARAMETER
);
1108 if (!ssl_cert_inst(&ssl
->cert
))
1110 SSLerr(SSL_F_SSL_USE_AUTHZ
,ERR_R_MALLOC_FAILURE
);
1113 return ssl_set_authz(ssl
->cert
, authz
, authz_length
);
1116 const unsigned char *SSL_CTX_get_authz_data(SSL_CTX
*ctx
, unsigned char type
,
1117 size_t *data_length
)
1119 CERT_PKEY
*current_key
;
1121 if (ctx
->cert
== NULL
)
1123 current_key
= ctx
->cert
->key
;
1124 if (current_key
->authz
== NULL
)
1126 return authz_find_data(current_key
->authz
,
1127 current_key
->authz_length
, type
, data_length
);
1130 #ifndef OPENSSL_NO_STDIO
1131 /* read_authz returns a newly allocated buffer with authz data */
1132 static unsigned char *read_authz(const char *file
, size_t *authz_length
)
1134 BIO
*authz_in
= NULL
;
1135 unsigned char *authz
= NULL
;
1136 /* Allow authzs up to 64KB. */
1137 static const size_t authz_limit
= 65536;
1139 unsigned char *ret
= NULL
;
1141 authz_in
= BIO_new(BIO_s_file_internal());
1142 if (authz_in
== NULL
)
1144 SSLerr(SSL_F_READ_AUTHZ
,ERR_R_BUF_LIB
);
1148 if (BIO_read_filename(authz_in
,file
) <= 0)
1150 SSLerr(SSL_F_READ_AUTHZ
,ERR_R_SYS_LIB
);
1154 authz
= OPENSSL_malloc(authz_limit
);
1155 read_length
= BIO_read(authz_in
, authz
, authz_limit
);
1156 if (read_length
== authz_limit
|| read_length
<= 0)
1158 SSLerr(SSL_F_READ_AUTHZ
,SSL_R_AUTHZ_DATA_TOO_LARGE
);
1159 OPENSSL_free(authz
);
1162 *authz_length
= read_length
;
1165 if (authz_in
!= NULL
) BIO_free(authz_in
);
1169 int SSL_CTX_use_authz_file(SSL_CTX
*ctx
, const char *file
)
1171 unsigned char *authz
= NULL
;
1172 size_t authz_length
= 0;
1175 authz
= read_authz(file
, &authz_length
);
1179 ret
= SSL_CTX_use_authz(ctx
, authz
, authz_length
);
1180 /* SSL_CTX_use_authz makes a local copy of the authz. */
1181 OPENSSL_free(authz
);
1185 int SSL_use_authz_file(SSL
*ssl
, const char *file
)
1187 unsigned char *authz
= NULL
;
1188 size_t authz_length
= 0;
1191 authz
= read_authz(file
, &authz_length
);
1195 ret
= SSL_use_authz(ssl
, authz
, authz_length
);
1196 /* SSL_use_authz makes a local copy of the authz. */
1197 OPENSSL_free(authz
);
1201 int SSL_CTX_use_serverinfo_file(SSL_CTX
*ctx
, const char *file
)
1203 unsigned char *serverinfo
= NULL
;
1204 size_t serverinfo_length
= 0;
1205 unsigned char* extension
= 0;
1206 long extension_length
= 0;
1208 char* header
= NULL
;
1211 size_t num_extensions
= 0;
1213 if (ctx
== NULL
|| file
== NULL
)
1215 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
,ERR_R_PASSED_NULL_PARAMETER
);
1219 bin
= BIO_new(BIO_s_file_internal());
1222 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_BUF_LIB
);
1225 if (BIO_read_filename(bin
, file
) <= 0)
1227 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_SYS_LIB
);
1231 for (num_extensions
=0;; num_extensions
++)
1233 if (PEM_read_bio(bin
, &name
, &header
, &extension
, &extension_length
) == 0)
1235 /* There must be at least one extension in this file */
1236 if (num_extensions
== 0)
1238 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_PEM_LIB
);
1241 else /* End of file, we're done */
1244 /* Check that the decoded PEM data is plausible (valid length field) */
1245 if (extension_length
< 4 || (extension
[2] << 8) + extension
[3] != extension_length
- 4)
1247 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_PEM_LIB
);
1250 /* Append the decoded extension to the serverinfo buffer */
1251 serverinfo
= OPENSSL_realloc(serverinfo
, serverinfo_length
+ extension_length
);
1252 if (serverinfo
== NULL
)
1254 SSLerr(SSL_F_SSL_CTX_USE_SERVERINFO_FILE
, ERR_R_MALLOC_FAILURE
);
1257 memcpy(serverinfo
+ serverinfo_length
, extension
, extension_length
);
1258 serverinfo_length
+= extension_length
;
1260 OPENSSL_free(name
); name
= NULL
;
1261 OPENSSL_free(header
); header
= NULL
;
1262 OPENSSL_free(extension
); extension
= NULL
;
1265 ret
= SSL_CTX_use_serverinfo(ctx
, serverinfo
, serverinfo_length
);
1267 /* SSL_CTX_use_serverinfo makes a local copy of the serverinfo. */
1269 OPENSSL_free(header
);
1270 OPENSSL_free(extension
);
1271 OPENSSL_free(serverinfo
);
1276 #endif /* OPENSSL_NO_STDIO */
1277 #endif /* OPENSSL_NO_TLSEXT */