Movatterモバイル変換


[0]ホーム

URL:


PreviousNext JavaScript must be enabled to correctly display this content
  1. Security Developer’s Guide
  2. General Security
  3. Java Security Overview

Java Security Overview

Java security includes a large set of APIs, tools, and implementations of commonly-used security algorithms, mechanisms, and protocols. The Java security APIs span a wide range of areas, including cryptography, public key infrastructure, secure communication, authentication, and access control. Java security technology provides the developer with a comprehensive security framework for writing applications, and also provides the user or administrator with a set of tools to securely manage applications.

Introduction to Java Security

The JDK is designed with a strong emphasis on security. At its core, the Java language itself is type-safe and provides automatic garbage collection, enhancing the robustness of application code. A secure class loading and verification mechanism ensures that only legitimate Java code is executed. The Java security architecture includes a large set of application programming interfaces (APIs), tools, and implementations of commonly-used security algorithms, mechanisms, and protocols.

The Java security APIs span a wide range of areas. Cryptographic and public key infrastructure (PKI) interfaces provide the underlying basis for developing secure applications.

The APIs allow for multiple interoperable implementations of algorithms and other security services. Services are implemented inproviders, which are plugged into the JDK through a standard interface that makes it easy for applications to obtain security services without having to know anything about their implementations. This allows developers to focus on how to integrate security into their applications, rather than on how to actually implement complex security mechanisms.

The JDK includes a number of providers that implement a core set of security services. It also allows for additional custom providers to be installed. This enables developers to extend the platform with new security mechanisms.

The JDK is divided into modules. Modules that contain security APIs include the following:

Table 1-1 Modules That Contain Security APIs

ModuleDescription
java.baseDefines the foundational APIs ofJava SE. Contained packages includejava.security,javax.crypto,javax.net.ssl, andjavax.security.auth.
java.security.jgssDefines the Java binding of the IETF Generic Security Services API (GSS-API). This module also contains GSS-API mechanisms including Kerberos v5 and SPNEGO.
java.security.saslDefines Java support for the IETF Simple Authentication and Security Layer (SASL). This module also contains SASL mechanisms including DIGEST-MD5, CRAM-MD5, and NTLM,
java.smartcardioDefines the Java Smart Card I/O API.
java.xml.cryptoDefines the API for XML cryptography.
jdk.jartoolDefines APIs for signing JAR files.
jdk.security.authProvides implementations of thejavax.security.auth.* interfaces and various authentication modules.
jdk.security.jgssDefines Java extensions to the GSS-API and an implementation of the SASL GSS-API mechanism.

Java Language Security and Bytecode Verification

The Java language is designed to be type-safe and easy to use. It provides automatic memory management, garbage collection, and range-checking on arrays. This reduces the overall programming burden placed on developers, leading to fewer subtle programming errors and to safer, more robust code.

A compiler translates Java programs into a machine-independent bytecode representation. A bytecode verifier is invoked to ensure that only legitimate bytecodes are executed in the Java runtime. It checks that the bytecodes conform to the Java Language Specification and do not violate Java language rules or namespace restrictions. The verifier also checks for memory management violations, stack underflows or overflows, and illegal data typecasts. Once bytecodes have been verified, the Java runtime prepares them for execution.

In addition, the Java language defines different access modifiers that can be assigned to Java classes, methods, and fields, enabling developers to restrict access to their class implementations as appropriate. The language defines four distinct access levels:
  • private: Most restrictive modifier; access is not allowed outside the particular class in which the private member (a method, for example) is defined.

  • protected: Allows access to any subclass or to other classes within the same package.

  • Package-private: If not specified, then this is the default access level; allows access to classes within the same package.

  • public: No longer guarantees that the element is accessible everywhere; accessibility depends upon whether the package containing that element is exported by its defining module and whether that module is readable by the module containing the code that is attempting to access it.

Basic Security Architecture

The JDK defines a set of APIs spanning major security areas, including cryptography, public key infrastructure, authentication, and secure communication. The APIs allow developers to easily integrate security into their application code.

The APIs are designed around the following principles:

Implementation independence
Applications do not need to implement security themselves. Rather, they can request security services from the JDK. Security services are implemented in providers (see the sectionSecurity Providers), which are plugged into the JDK via a standard interface. An application may rely on multiple independent providers for security functionality.
Implementation interoperability

Providers are interoperable across applications. Specifically, an application is not bound to a specific provider if it does not rely on default values from the provider.

Algorithm extensibility
The JDK includes a number of built-in providers that implement a basic set of security services that are widely used today. However, some applications may rely on emerging standards not yet implemented, or on proprietary services. The JDK supports the installation of custom providers that implement such services.

Security Providers

Thejava.security.Provider class encapsulates the notion of a security provider in the Java platform. It specifies the provider's name and lists the security services it implements. Multiple providers may be configured at the same time and are listed in order of preference. When a security service is requested, the highest priority provider that implements that service is selected.

Applications rely on the relevantgetInstance method to request a security service from an underlying provider.

For example, message digest creation represents one type of service available from providers. To request an implementation of a specific message digest algorithm, call the methodjava.security.MessageDigest.getInstance. The following statement requests a SHA-256 message digest implementation without specifying a provider name:

    MessageDigest md = MessageDigest.getInstance("SHA-256");

The following figure illustrates how this statement obtains a SHA-256 message digest implementation. The providers are searched in preference order, and the implementation from the first provider supplying that particular algorithm,ProviderB, is returned.

Figure 1-1 Request SHA-256 Message Digest Implementation Without Specifying Provider


Description of Figure 1-1 follows
Description of "Figure 1-1 Request SHA-256 Message Digest Implementation Without Specifying Provider"

You can optionally request an implementation from a specific provider by specifying the provider's name. The following statement requests a SHA-256 message digest implementation from a specific provider,ProviderC:

    MessageDigest md = MessageDigest.getInstance("SHA-256", "ProviderC");

The following figure illustrates how this statement requests a SHA-256 message digest implementation from a specific provider,ProviderC. In this case, the implementation from that provider is returned, even though a provider with a higher preference order,ProviderB, also supplies a SHA-256 implementation.

Figure 1-2 Request SHA-256 Message Digest Implementation from Specific Provider


Description of Figure 1-2 follows
Description of "Figure 1-2 Request SHA-256 Message Digest Implementation from Specific Provider"

For more information about cryptographic services, such as message digest algorithms, see the sectionJava Cryptography.

Oracle's implementation of the Java platform includes a number of built-in default providers that implement a basic set of security services that can be used by applications. Note that other vendor implementations of the Java platform may include different sets of providers that encapsulate vendor-specific sets of security services. The term built-in default providers refers to the providers available in Oracle's implementation.

Java Cryptography

The Java cryptography architecture is a framework for accessing and developing cryptographic functionality for the Java platform.

It includes APIs for a large variety of cryptographic services, including the following:

  • Message digest algorithms
  • Digital signature algorithms
  • Symmetric bulk and stream encryption
  • Asymmetric encryption
  • Password-based encryption (PBE)
  • Elliptic Curve Cryptography (ECC)
  • Key agreement algorithms
  • Key Derivation Functions (KDFs)
  • Key generators
  • Key Encapsulation Mechanisms (KEMs)
  • Message Authentication Codes (MACs)
  • Secure Random Number Generators
For historical (export control) reasons, the cryptography APIs are organized into two distinct packages:
  • Thejava.security andjava.security.* packages contains classes that arenot subject to export controls (likeSignature andMessageDigest)
  • Thejavax.crypto package contains classes that are subject to export controls (likeCipher,KeyAgreement, andKEM)

The cryptographic interfaces are provider-based, allowing for multiple and interoperable cryptography implementations. Some providers may perform cryptographic operations in software; others may perform the operations on a hardware token (for example, on a smart card device or on a hardware cryptographic accelerator). Providers that implement export-controlled services must be digitally signed by a certificate issued by the Oracle JCE Certificate Authority.

The Java platform includes built-in providers for many of the most commonly used cryptographic algorithms, including the RSA, DSA, and ECDSA signature algorithms, the AES encryption algorithm, the SHA-2 message digest algorithms, and the Diffie-Hellman (DH) and Elliptic Curve Diffie-Hellman (ECDH) key agreement algorithms. Most of the built-in providers implement cryptographic algorithms in Java code.

The Java platform also includes a built-in provider that acts as a bridge to a native PKCS#11 (v2.x) token. This provider, namedSunPKCS11, allows Java applications to seamlessly access cryptographic services located on PKCS#11-compliant tokens.

On Windows, the Java platform includes a built-in provider that acts as a bridge to the native Microsoft CryptoAPI. This provider, namedSunMSCAPI, allows Java applications to seamlessly access cryptographic services on Windows through the CryptoAPI.

Public Key Infrastructure

Public Key Infrastructure (PKI) is a term used for a framework that enables secure exchange of information based on public key cryptography. It allows identities (of people, organizations, etc.) to be bound to digital certificates and provides a means of verifying the authenticity of certificates. PKI encompasses keys, certificates, public key encryption, and trusted Certification Authorities (CAs) who generate and digitally sign certificates.

The Java platform includes APIs and provider support for X.509 digital certificates and Certificate Revocation Lists (CRLs), as well as PKIX-compliant certification path building and validation. The classes related to PKI are located in thejava.security andjava.security.cert packages.

Key and Certificate Storage

The Java platform provides for long-term persistent storage of cryptographic keys and certificates via key and certificate stores. Specifically, thejava.security.KeyStore class represents akey store, a secure repository of cryptographic keys and/or trusted certificates (to be used, for example, during certification path validation), and thejava.security.cert.CertStore class represents acertificate store, a public and potentially vast repository of unrelated and typically untrusted certificates. ACertStore may also store CRLs.

KeyStore andCertStore implementations are distinguished by types. The Java platform includes the standard PKCS11 and PKCS12 key store types (whose implementations are compliant with the corresponding PKCS specifications from the Internet Engineering Task Force (IETF)). It also contains a proprietary file-based key store type called JKS (which stands for Java Key Store), and a type called DKS (Domain Key Store) which is a collection of keystores that are presented as a single logical keystore.

The Java platform includes a special built-in key store,cacerts, that contains a number of certificates for well-known, trusted CAs. The keytool utility is able to list the certificates included incacerts. Seekeytool inJava Development Kit Tool Specifications.

The SunPKCS11 provider mentioned in the sectionJava Cryptography includes a PKCS11KeyStore implementation. This means that keys and certificates residing in secure hardware (such as a smart card) can be accessed and used by Java applications via theKeyStore API. Note that smart card keys may not be permitted to leave the device. In such cases, thejava.security.Key object returned by theKeyStore API may simply be a reference to the key (that is, it would not contain the actual key material). Such aKey object can only be used to perform cryptographic operations on the device where the actual key resides.

The Java platform also includes an LDAP certificate store type (for accessing certificates stored in an LDAP directory), as well as an in-memory Collection certificate store type (for accessing certificates managed in ajava.util.Collection object).

The Java platform supports native Microsoft Windows keystore types. See the algorithm names for theKeyStore engine class inThe SunMSCAPI Provider. The Java platform also includes aKeyStore implementation that proivdes access to the macOS Keychain. See the algorithm names for theKeyStore engine class inThe Apple Provider.

Public Key Infrastructure Tools

There are two built-in tools for working with keys, certificates, and key stores:

  • keytool creates and manages key stores. Use it to perform the following tasks:

    • Create public/private key pairs
    • Display, import, and export X.509 v1, v2, and v3 certificates stored as files
    • Create X.509 certificates
    • Issue certificate (PKCS#10) requests to be sent to CAs
    • Create certificates based on certificate requests
    • Import certificate replies (obtained from the CAs sent certificate requests)
    • Designate public key certificates as trusted
    • Accept a password and store it securely as a secret key
  • jarsigner signs JAR files and verifies signatures on signed JAR files. The Java ARchive (JAR) file format enables the bundling of multiple files into a single file. Typically, a JAR file contains the class files and auxiliary resources associated with applets and applications.

To digitally sign code, perform the following:
  1. Usekeytool to generate or import appropriate keys and certificates into your key store (if they are not there already).

  2. Use thejar tool to package the code in a JAR file.

  3. Use thejarsigner tool (or thejdk.security.jarsigner API) to sign the JAR file. Thejarsigner tool accesses a key store to find any keys and certificates needed to sign a JAR file or to verify the signature of a signed JAR file.

    Note:

    jarsigner can optionally generate signatures that include a timestamp. Systems that verify JAR file signatures can check the timestamp and accept a JAR file that was signed while the signing certificate was valid rather than requiring the certificate to be current. (Certificates typically expire annually, and it is not reasonable to expect JAR file creators to re-sign deployed JAR files annually.)

Seekeytool andjarsigner inJava Development Kit Tool Specifications.

Authentication

Authentication is the process of determining the identity of a user. In the context of the Java runtime environment, it is the process of identifying the user of an executing Java program. In certain cases, this process may rely on the services described in the sectionJava Cryptography.

The Java platform provides APIs that enable an application to perform user authentication via pluggable login modules. Applications call into theLoginContext class (in thejavax.security.auth.login package), which in turn references a configuration. The configuration specifies which login module (an implementation of thejavax.security.auth.spi.LoginModule interface) is to be used to perform the actual authentication.

Since applications solely talk to the standardLoginContext API, they can remain independent from the underlying plug-in modules. New or updated modules can be plugged in for an application without having to modify the application itself. The following figure illustrates the independence between applications and underlying login modules:

Figure 1-3 Authentication Login Modules Plugging into the Authentication Framework

Description of Figure 1-3 follows
Description of "Figure 1-3 Authentication Login Modules Plugging into the Authentication Framework"

It is important to note that although login modules are pluggable components that can be configured into the Java platform, they are not plugged in via security providers. Therefore, they do not follow the provider searching model as described in the sectionSecurity Providers. Instead, as is shown inFigure 1-3, login modules are administered by their own unique configuration.

The Java platform provides the following built-in login modules, all in thecom.sun.security.auth.module package:

  • JndiLoginModule for username/password authentication using LDAP or NIS databases
  • KeyStoreLoginModule for logging into any type of key store, including a PKCS#11 token key store
  • Krb5LoginModule for authentication using Kerberos protocols
  • LdapLoginModule for LDAP-based authentication
  • NTLoginModule for authentication using a user's Windows NT security information
  • UnixLoginModule for authentication using a user's UNIXPrincipal information

Authentication can also be achieved during the process of establishing a secure communication channel between two peers. The Java platform provides implementations of a number of standard communication protocols, which are discussed in the sectionSecure Communication.

Secure Communication

The data that travels across a network can be accessed by someone who is not the intended recipient. When the data includes private information, such as passwords and credit card numbers, steps must be taken to make the data unintelligible to unauthorized parties. It is also important to ensure that you are sending the data to the appropriate party, and that the data has not been modified, either intentionally or unintentionally, during transport.

Cryptography forms the basis required for secure communication; see the sectionJava Cryptography. The Java platform also provides API support and provider implementations for a number of standard secure communication protocols.

TLS and DTLS Protocols

Transport Layer Security (TLS) and its predecessor, Secure Sockets Layer (SSL), are cryptographic protocols which provide a secure channel between two communication peers. TLS uses a combination of cryptographic processes by providing authentication, confidentiality and integrity properties for communication over a untrusted or potential hostile network. TLS runs over a reliable, stream-oriented transport channel, typically Transport Control Protocol (TCP). TLS is application protocol independent. Higher-level protocols, for example Hypertext Transfer Protocol (HTTP), can layer on top of TLS transparently.

The Datagram Transport Layer Security (DTLS) protocols are based on the stream-oriented TLS protocols and are intended to provider similar security properties for datagram transport, like User Datagram Protocol (UDP), which does not provide reliable or in-order delivery of data.

The JDK provides APIs and an implementation of the SSL, TLS, and DTLS protocols that includes functionality for data encryption, message integrity, and server and client authentication. Applications can use (D)TLS to provide for the secure passage of data between two peers over any application protocol, such as HTTP on top of TCP/IP.

Thejavax.net.ssl.SSLSocket class represents a network socket that encapsulates TLS support on top of a normal stream socket (java.net.Socket). Some applications might want to use alternate data transport abstractions (for example, New-I/O); thejavax.net.ssl.SSLEngine class is available to produce and consume TLS/DTLS packets.

The JDK also includes APIs that support the notion of pluggable (provider-based) key managers and trust managers.A key manager is encapsulated by thejavax.net.ssl.KeyManager class, and manages the keys used to perform authentication. Atrust manager is encapsulated by theTrustManager class (in the same package), and makes decisions about who to trust based on certificates in the key store it manages.

The JDK includes a built-in provider that implements the SSL/TLS/DTLS protocols:

Simple Authentication and Security Layer (SASL)

Simple Authentication and Security Layer (SASL) is an Internet standard that specifies a protocol for authentication and optional establishment of a security layer between client and server applications. SASL defines how authentication data is to be exchanged, but does not itself specify the contents of that data. It is a framework into which specific authentication mechanisms that specify the contents and semantics of the authentication data can fit. There are a number of standard SASL mechanisms defined by the Internet community for various security levels and deployment scenarios.

The Java SASL API, which is in thejava.security.sasl module, defines classes and interfaces for applications that use SASL mechanisms. It is defined to be mechanism-neutral; an application that uses the API need not be hardwired into using any particular SASL mechanism. Applications can select the mechanism to use based on desired security features. The API supports both client and server applications. Thejavax.security.sasl.Sasl class is used to createSaslClient andSaslServer objects.

SASL mechanism implementations are supplied in provider packages. Each provider may support one or more SASL mechanisms and is registered and invoked via the standard provider architecture.

The Java platform includes a built-in provider that implements the following SASL mechanisms:

  • CRAM-MD5, DIGEST-MD5, EXTERNAL, GSSAPI, NTLM, and PLAIN client mechanisms
  • CRAM-MD5, DIGEST-MD5, GSSAPI, and NTLM server mechanisms

Generic Security Service API and Kerberos

The Java platform contains an API with the Java language bindings for the Generic Security Service Application Programming Interface (GSS-API), which is in thejava.security.jgss module. GSS-API offers application programmers uniform access to security services atop a variety of underlying security mechanisms. The Java GSS-API currently requires use of a Kerberos v5 mechanism, and the Java platform includes a built-in implementation of this mechanism. At this time, it is not possible to plug in additional mechanisms.

Note:

TheKrb5LoginModule mentioned in the sectionAuthentication can be used in conjunction with the GSS Kerberos mechanism.

The Java platform also includes a built-in implementation of the Simple and Protected GSS-API Negotiation Mechanism (SPNEGO) GSS-API mechanism.

Before two applications can use GSS-API to securely exchange messages between them, they must establish a joint security context. The context encapsulates shared state information that might include, for example, cryptographic keys. Both applications create and use anorg.ietf.jgss.GSSContext object to establish and maintain the shared information that makes up the security context. Once a security context has been established, it can be used to prepare secure messages for exchange.

The Java GSS APIs are in theorg.ietf.jgss package. The Java platform also defines basic Kerberos classes, likeKerberosPrincipal,KerberosTicket,KerberosKey, andKeyTab, which are located in thejavax.security.auth.kerberos package.

XML Signature

The Java XML Digital Signature API is a standard Java API for generating and validating XML Signatures.

XML Signatures can be applied to data of any type, XML or binary (seeXML Signature Syntax and Processing). The resulting signature is represented in XML. An XML Signature can be used to secure your data and provide data integrity, message authentication, and signer authentication.

The API is designed to support all of the required or recommended features of the W3C Recommendation for XML-Signature Syntax and Processing. The API is extensible and pluggable and is based on the Java Cryptography Service Provider Architecture.

The Java XML Digital Signature API, which is in thejava.xml.crypto module, consists of six packages:

  • javax.xml.crypto
  • javax.xml.crypto.dsig
  • javax.xml.crypto.dsig.keyinfo
  • javax.xml.crypto.dsig.spec
  • javax.xml.crypto.dom
  • javax.xml.crypto.dsig.dom

Java API for XML Processing (JAXP)

Java API for XML Processing (JAXP) is for processing XML data using Java applications. It includes support for Simple API for XML (SAX), Document Object Models (DOM) and Streaming API for XML (StAX) parsers, XML Schema Validation, and Extensible Stylesheet Language Transformations (XSLT). In addition, JAXP provides secure processing features that can help safeguard your applications and system from XML-related attacks. See theJava API for XML Processing (JAXP) Security Guide.

Note:

Secure Coding Guidelines for Java SE contains additional recommendations that can help defend against XML-related attacks.

Security Tools Summary

The following tables describe Java security and Kerberos-related tools.

Table 1-2 Java Security Tools

ToolUsage
jarCreates Java Archive (JAR) files
jarsignerSigns and verifies signatures on JAR files
keytoolCreates and manages key stores

There are also three Kerberos-related tools that are shipped with the JDK for Windows. Equivalent functionality is provided in tools of the same name that are automatically part of Linux and macOS.

Table 1-3 Kerberos-related Tools

ToolUsage
kinitObtains and caches Kerberos ticket-granting tickets
klistLists entries in the local Kerberos credentials cache and key table
ktabManages the names and service keys stored in the local Kerberos key table

The JAR Signing and Verification Tool

Thejarsigner tool can be used to digitally sign Java archives (JAR files), and to verify such signatures. This tool depends on the keystore that is managed bykeytool.

Note:

You can also use thejdk.security.jarsigner API to sign JAR files.

The Key and Certificate Management Tool

keytool is a key and certificate management utility. It enables users to administer their own public/private key pairs and associated certificates for use in self-authentication (where the user authenticates himself/herself to other users/services) or data integrity and authentication services, using digital signatures. The authentication information includes both a sequence (chain) of X.509 certificates, and an associated private key, which can be referenced by a so-called "alias". This tool also manages certificates (that are "trusted" by the user), which are stored in the same database as the authentication information, and can be referenced by an "alias".

keytool stores the keys and certificates in a so-calledkeystore. The default keystore implementation implements the keystore as a file. It protects private keys with a password.

The chains of X.509 certificates are provided by organizations called Certification Authorities, or CAs. Identities (including CAs) use their private keys to authenticate their association with objects (such as with channels which are secured using SSL), with archives of code they signed, or (for CAs) with X.509 certificates they have issued. As a bootstrapping tool, certificates generated using the-gencert option may be used until a Certification Authority returns a certificate chain.

The private keys in this database are always stored in encrypted form, to make it difficult to disclose these private keys inappropriately. A password is required to access or modify the database. These private keys are encrypted using the "password", which should be several words long. If the password is lost, those authentication keys cannot be recovered.

In fact, each private key in the keystore can be protected using its own individual password, which may or may not be the same as the password that protects the keystore's overall integrity.

This tool is (currently) intended to be used from the command line, where one simply typeskeytool as a shell prompt.keytool is a script that executes the appropriate Java classes and is built together with the SDK.

The command line options for each command may be provided in any order. Typing an incorrect option or typingkeytool -help will cause the tool's usage to be summarized on the output device (such as a shell window).

Built-In Providers

TheJava SE implementation from Oracle includes a number of built-in provider packages. SeeJDK Providers Documentation.


[8]ページ先頭

©2009-2025 Movatter.jp