Back to index

lightning-sunbird  0.9+nobinonly
nsHttpDigestAuth.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Justin Bradford <jab@atdot.org> (original author of nsDigestAuth.h)
00025  *   An-Cheng Huang <pach@cs.cmu.edu>
00026  *   Darin Fisher <darin@netscape.com>
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 #ifndef nsDigestAuth_h__
00043 #define nsDigestAuth_h__
00044 
00045 #include "nsIHttpAuthenticator.h"
00046 #include "nsICryptoHash.h"
00047 #include "nsString.h"
00048 #include "nsCOMPtr.h"
00049 
00050 #define ALGO_SPECIFIED 0x01
00051 #define ALGO_MD5 0x02
00052 #define ALGO_MD5_SESS 0x04
00053 #define QOP_AUTH 0x01
00054 #define QOP_AUTH_INT 0x02
00055 
00056 #define DIGEST_LENGTH 16
00057 #define EXPANDED_DIGEST_LENGTH 32
00058 #define NONCE_COUNT_LENGTH 8
00059 
00060 //-----------------------------------------------------------------------------
00061 // nsHttpDigestAuth
00062 //-----------------------------------------------------------------------------
00063 
00064 class nsHttpDigestAuth : public nsIHttpAuthenticator
00065 {
00066   public:
00067     NS_DECL_ISUPPORTS
00068     NS_DECL_NSIHTTPAUTHENTICATOR
00069 
00070     nsHttpDigestAuth();
00071     ~nsHttpDigestAuth();
00072 
00073   protected:
00074     nsresult ExpandToHex(const char * digest, char * result);
00075 
00076     nsresult CalculateResponse(const char * ha1_digest,
00077                                const char * ha2_digest,
00078                                const nsAFlatCString & nonce,
00079                                PRUint16 qop,
00080                                const char * nonce_count,
00081                                const nsAFlatCString & cnonce,
00082                                char * result);
00083 
00084     nsresult CalculateHA1(const nsAFlatCString & username,
00085                           const nsAFlatCString & password,
00086                           const nsAFlatCString & realm,
00087                           PRUint16 algorithm,
00088                           const nsAFlatCString & nonce,
00089                           const nsAFlatCString & cnonce,
00090                           char * result);
00091 
00092     nsresult CalculateHA2(const nsAFlatCString & http_method,
00093                           const nsAFlatCString & http_uri_path,
00094                           PRUint16 qop,
00095                           const char * body_digest,
00096                           char * result);
00097 
00098     nsresult ParseChallenge(const char * challenge,
00099                             nsACString & realm,
00100                             nsACString & domain,
00101                             nsACString & nonce,
00102                             nsACString & opaque,
00103                             PRBool * stale,
00104                             PRUint16 * algorithm,
00105                             PRUint16 * qop);
00106 
00107     // result is in mHashBuf
00108     nsresult MD5Hash(const char *buf, PRUint32 len);
00109 
00110     nsresult GetMethodAndPath(nsIHttpChannel *, PRBool, nsCString &, nsCString &);
00111 
00112     // append the quoted version of value to aHeaderLine
00113     nsresult AppendQuotedString(const nsACString & value,
00114                                 nsACString & aHeaderLine);
00115 
00116   protected:
00117     nsCOMPtr<nsICryptoHash>        mVerifier;
00118     char                           mHashBuf[DIGEST_LENGTH];
00119     PRBool                         mGotVerifier;
00120 };
00121 
00122 #endif // nsHttpDigestAuth_h__