github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/docs/Setup/ca-setup.md (about) 1 ## Fabric CA User's Guide 2 3 Fabric CA is a Certificate Authority for Hyperledger Fabric. 4 5 It provides features such as: 6 1) registration of identities, or connects to LDAP as the user registry; 7 2) issuance of Enrollment Certificates (ECerts); 8 3) issuance of Transaction Certificates (TCerts), providing both anonymity 9 and unlinkability when transacting on a Hyperledger Fabric blockchain; 10 4) certificate renewal and revocation. 11 12 Fabric CA consists of both a server and a client component as described 13 later in this document. 14 15 For developers interested in contributing to Fabric CA, see the 16 [Fabric CA repository](https://github.com/hyperledger/fabric-ca) for more 17 information. 18 19 ## Getting Started 20 21 ### Prerequisites 22 23 * Go 1.7+ installation or later 24 * **GOPATH** environment variable is set correctly 25 26 ### Install 27 28 To install the fabric-ca command: 29 30 ``` 31 # go get github.com/hyperledger/fabric-ca 32 ``` 33 34 ### The Fabric CA CLI 35 36 The following shows the fabric-ca CLI usage: 37 38 ``` 39 # fabric-ca 40 fabric-ca client - client related commands 41 fabric-ca server - server related commands 42 fabric-ca cfssl - all cfssl commands 43 44 For help, type "fabric-ca client", "fabric-ca server", or "fabric-ca cfssl". 45 ``` 46 47 The `fabric-ca server` and `fabric-ca client` commands are discussed below. 48 49 If you would like to enable debug-level logging (for server or client), 50 set the `FABRIC_CA_DEBUG` environment variable to `true`. 51 52 Since fabric-ca is built on top of [CFSSL](https://github.com/cloudflare/cfssl), 53 the `fabric-ca cfssl` commands are available but are not discussed in this document. 54 See [CFSSL](https://github.com/cloudflare/cfssl) for more information. 55 56 ## Fabric CA Server 57 58 This section describes the fabric-ca server. 59 60 You must initialize the Fabric CA server before starting it. 61 62 The fabric-ca server's home directory is determined as follows: 63 - if the `FABRIC_CA_HOME` environment variable is set, use its value; 64 - otherwise, if the `HOME` environment variable is set, use `$HOME/fabric-ca`; 65 - otherwise, use `/var/hyperledger/fabric/dev/fabric-ca'. 66 67 For the remainder of this server section, we assume that you have set the 68 `FABRIC_CA_HOME` environment variable to `$HOME/fabric-ca/server`. 69 70 #### Initializing the server 71 72 Initialize the Fabric CA server as follows: 73 74 ``` 75 # fabric-ca server init CSR-JSON-FILE 76 ``` 77 78 The following is a sample `CSR-JSON-FILE` which you can customize as desired. 79 The "CSR" stands for "Certificate Signing Request". 80 81 If you are going to connect to the fabric-ca server remotely over TLS, 82 replace "localhost" in the CSR-JSON-FILE below with the hostname where 83 you will be running your fabric-ca server. 84 85 ``` 86 { 87 "CN": "localhost", 88 "key": { "algo": "ecdsa", "size": 256 }, 89 "names": [ 90 { 91 "O": "Hyperledger Fabric", 92 "OU": "Fabric CA", 93 "L": "Raleigh", 94 "ST": "North Carolina", 95 "C": "US" 96 } 97 ] 98 } 99 ``` 100 101 All of the fields above pertain to the X.509 certificate which is generated 102 by the `fabric server init` command as follows: 103 104 <a name="csr-fields"/> 105 ###### CSR fields 106 107 - **CN** is the Common Name 108 - **keys** specifies the algorithm and key size as described below 109 - **O** is the organization name 110 - **OU** is the organization unit 111 - **L** is the location or city 112 - **ST** is the state 113 - **C** is the country 114 115 The `fabric-ca server init` command generates a self-signed X.509 certificate. 116 It stores the certificate in the `server-cert.pem` file and the key in the 117 `server-key.pem` file in the Fabric CA server's home directory. 118 119 ###### Algorithms and key sizes 120 121 The CSR-JSON-FILE can be customized to generate X.509 certificates and keys 122 that support both RSA and Elliptic Curve (ECDSA). The following setting is 123 an example of the implementation of Elliptic Curve Digital Signature 124 Algorithm (ECDSA) with curve `prime256v1` and signature algorithm 125 `ecdsa-with-SHA256`: 126 ``` 127 "key": { 128 "algo": "ecdsa" 129 "size": 256 130 } 131 ``` 132 The choice of algorithm and key size are based on security needs. 133 134 Elliptic Curve (ECDSA) offers the following key size options: 135 136 | size | ASN1 OID | Signature Algorithm | 137 |-------------|:-------------:|:-----:| 138 | 256 | prime256v1 | ecdsa-with-SHA256 | 139 | 384 | secp384r1 | ecdsa-with-SHA384 | 140 | 521 | secp521r1 | ecdsa-with-SHA512 | 141 142 RSA offers the following key size options: 143 144 | size | Modulus (bits)| Signature Algorithm | 145 |-------------|:-------------:|:-----:| 146 | 2048 | 2048 | sha256WithRSAEncryption | 147 | 4096 | 4096 | sha512WithRSAEncryption | 148 149 #### Starting the server 150 151 Create a file named `server-config.json` as shown below 152 in your fabric-ca server's home directory (e.g. *$HOME/fabric-ca/server*). 153 154 ``` 155 { 156 "tls_disable": false, 157 "ca_cert": "server-cert.pem", 158 "ca_key": "server-key.pem", 159 "driver":"sqlite3", 160 "data_source":"fabric-ca.db", 161 "user_registry": { "max_enrollments": 0 }, 162 "tls": { 163 "tls_cert": "server-cert.pem", 164 "tls_key": "server-key.pem" 165 }, 166 "users": { 167 "admin": { 168 "pass": "adminpw", 169 "type": "client", 170 "group": "bank_a", 171 "attrs": [ 172 {"name":"hf.Registrar.Roles","value":"client,peer,validator,auditor"}, 173 {"name":"hf.Registrar.DelegateRoles", "value": "client"} 174 ] 175 } 176 }, 177 "groups": { 178 "banks_and_institutions": { 179 "banks": ["bank_a", "bank_b", "bank_c"], 180 "institutions": ["institution_a"] 181 } 182 }, 183 "signing": { 184 "default": { 185 "usages": ["cert sign"], 186 "expiry": "8000h", 187 "ca_constraint": {"is_ca": true} 188 } 189 } 190 } 191 ``` 192 193 Now you may start the Fabric CA server as follows: 194 195 ``` 196 # cd $FABRIC_CA_HOME 197 # fabric-ca server start -address '0.0.0.0' -config server-config.json 198 ``` 199 200 To cause the fabric-ca server to listen on `http` rather than `https`, 201 set `tls_disable` to `true` in the `server-config.json` file. 202 203 To limit the number of times that the same secret (or password) can 204 be used for enrollment, set the `max_enrollments` in the `server-config.json` 205 file to the appropriate value. If you set the value to 1, the fabric-ca 206 server allows passwords to only be used once for a particular enrollment ID. 207 If you set the value to 0, the fabric-ca server places no limit on the number 208 of times that a secret can be reused for enrollment. 209 The default value is 0. 210 211 The fabric-ca server should now be listening on port 7054. 212 213 You may skip to the [Fabric CA Client](#fabric-ca-client) section 214 if you do not want to configure the fabric-ca server to run in a 215 cluster or to use LDAP. 216 217 #### Server database configuration 218 219 This section describes how to configure the fabric-ca server to connect 220 to Postgres or MySQL databases. The default database is SQLite and the 221 default database file is `fabric-ca.db` in the Fabric CA's home directory. 222 223 If you don't care about running the fabric-ca server in a cluster, you may 224 skip this section; otherwise, you must configure either Postgres or MySQL 225 as described below. 226 227 ###### Postgres 228 229 The following sample may be added to the `server-config.json` file in order 230 to connect to a Postgres database. Be sure to customize the various values 231 appropriately. 232 233 ``` 234 "driver":"postgres", 235 "data_source":"host=localhost port=5432 user=Username password=Password dbname=fabric-ca sslmode=verify-full", 236 ``` 237 238 Specifying *sslmode* enables SSL, and a value of *verify-full* means to verify 239 that the certificate presented by the postgres server was signed by a trusted CA 240 and that the postgres server's host name matches the one in the certificate. 241 242 We also need to set the TLS configuration in the fabric-ca server-config file. 243 If the database server requires client authentication, then a client cert and 244 key file needs to be provided. The following should be present in the fabric-ca 245 server config: 246 247 ``` 248 "tls":{ 249 ... 250 "db_client":{ 251 "ca_certfiles":["CA.pem"], 252 "client":[{"keyfile":"client-key.pem","certfile":"client-cert.pem"}] 253 } 254 }, 255 ``` 256 257 **ca_certfiles** - The names of the trusted root certificate files. 258 259 **certfile** - Client certificate file. 260 261 **keyfile** - Client key file. 262 263 ###### MySQL 264 265 The following sample may be added to the `server-config.json` file in order 266 to connect to a MySQL database. Be sure to customize the various values 267 appropriately. 268 269 ``` 270 ... 271 "driver":"mysql", 272 "data_source":"root:rootpw@tcp(localhost:3306)/fabric-ca?parseTime=true&tls=custom", 273 ... 274 ``` 275 276 If connecting over TLS to the MySQL server, the `tls.db_client` section is 277 also required as described in the **Postgres** section above. 278 279 #### LDAP 280 281 The fabric-ca server can be configured to read from an LDAP server. 282 283 In particular, the fabric-ca server may connect to an LDAP server to do the following: 284 285 * authenticate a user prior to enrollment, and 286 * retrieve a user's attribute values which are used for authorization. 287 288 In order to configure the fabric-ca server to connect to an LDAP server, add a section 289 of the following form to your fabric-ca server's configuration file: 290 291 ``` 292 { 293 "ldap": { 294 "url": "scheme://adminDN:pass@host[:port][/base]" 295 "userfilter": "filter" 296 } 297 ``` 298 299 where: 300 * `scheme` is one of *ldap* or *ldaps*; 301 * `adminDN` is the distinquished name of the admin user; 302 * `pass` is the password of the admin user; 303 * `host` is the hostname or IP address of the LDAP server; 304 * `port` is the optional port number, where default 389 for *ldap* and 636 for *ldaps*; 305 * `base` is the optional root of the LDAP tree to use for searches; 306 * `filter` is a filter to use when searching to convert a login user name to 307 a distinquished name. For example, a value of `(uid=%s)` searches for LDAP 308 entries with the value of a `uid` attribute whose value is the login user name. 309 Similarly, `(email=%s)` may be used to login with an email address. 310 311 The following is a sample configuration section for the default settings for the 312 OpenLDAP server whose docker image is at `https://github.com/osixia/docker-openldap`. 313 314 ``` 315 "ldap": { 316 "url": "ldap://cn=admin,dc=example,dc=org:admin@localhost:10389/dc=example,dc=org", 317 "userfilter": "(uid=%s)" 318 }, 319 ``` 320 321 See `FABRIC_CA/testdata/testconfig-ldap.json` for the complete configuration file with this section. 322 Also see `FABRIC_CA/scripts/run-ldap-tests` for a script which starts an OpenLDAP docker image, configures it, 323 runs the LDAP tests in FABRIC_CA/cli/server/ldap/ldap_test.go, and stops the OpenLDAP server. 324 325 ###### When LDAP is configured, enrollment works as follows: 326 327 * A fabric-ca client or client SDK sends an enrollment request with a basic authorization header. 328 * The fabric-ca server receives the enrollment request, decodes the user/pass in the authorization header, looks up the DN (Distinquished Name) associated with the user using the "userfilter" from the configuration file, and then attempts an LDAP bind with the user's password. If successful, the enrollment processing is authorized and can proceed. 329 330 ###### When LDAP is configured, attribute retrieval works as follows: 331 332 * A client SDK sends a request for a batch of tcerts **with one or more attributes** to the fabric-ca server. 333 * The fabric-ca server receives the tcert request and does as follows: 334 * extracts the enrollment ID from the token in the authorization header 335 (after validating the token); 336 * does an LDAP search/query to the LDAP server, requesting all of the 337 attribute names received in the tcert request; 338 * the attribute values are placed in the tcert as normal 339 340 #### Setting up a cluster of fabric-ca servers 341 342 You may use any IP sprayer to load balance to a cluster of fabric-ca 343 servers. This section provides an example of how to set up Haproxy 344 to route to a fabric-ca server cluster. Be sure to change 345 hostname and port to reflect the settings of your fabric-ca servers. 346 347 haproxy.conf 348 349 ``` 350 global 351 maxconn 4096 352 daemon 353 354 defaults 355 mode http 356 maxconn 2000 357 timeout connect 5000 358 timeout client 50000 359 timeout server 50000 360 361 listen http-in 362 bind *:7054 363 balance roundrobin 364 server server1 hostname1:port 365 server server2 hostname2:port 366 server server3 hostname3:port 367 ``` 368 369 <a name="fabric-ca-client"/> 370 ## Fabric CA Client 371 372 This section describes how to use the fabric-ca client. 373 374 The default fabric-ca client's home directory is `$HOME/fabric-ca`, but 375 this can be changed by setting the `FABRIC_CA_HOME` environment variable. 376 377 You must create a file named **client-config.json** in the fabric-ca 378 client's home directory. 379 The following is a sample client-config.json file: 380 381 ``` 382 { 383 "ca_certfiles":["server-cert.pem"], 384 "signing": { 385 "default": { 386 "usages": ["cert sign"], 387 "expiry": "8000h" 388 } 389 } 390 } 391 ``` 392 393 You must also copy the server's certificate into the client's home directory. 394 In the examples in this document, the server's certificate is at 395 `$HOME/fabric-ca/server/server-cert.pem`. The file name must 396 match the name in the *client-config.json* file. 397 398 <a name="EnrollBootstrap"/> 399 #### Enroll the bootstrap user 400 401 Unless the fabric-ca server is configured to use LDAP, it must 402 be configured with at least one pre-registered bootstrap user. 403 In the previous server-config.json in this document, that user 404 has an enrollment ID of *admin* with an enrollment secret of *adminpw*. 405 406 <a name="csr-admin"/> 407 First, create a CSR (Certificate Signing Request) JSON file similar to 408 the following. Customize it as desired. 409 410 ``` 411 { 412 "key": { "algo": "ecdsa", "size": 256 }, 413 "names": [ 414 { 415 "O": "Hyperledger Fabric", 416 "OU": "Fabric CA", 417 "L": "Raleigh", 418 "ST": "North Carolina", 419 "C": "US" 420 } 421 ] 422 } 423 ``` 424 425 See [CSR fields](#csr-fields) for a description of the fields in this file. 426 When enrolling, the CN (Common Name) field is automatically set to the enrollment ID 427 which is *admin* in this example, so it can be omitted from the csr.json file. 428 429 The following command enrolls the admin user and stores an enrollment certificate (ECert) 430 in the fabric-ca client's home directory. 431 432 ``` 433 # export FABRIC_CA_HOME=$HOME/fabric-ca/clients/admin 434 # fabric-ca client enroll -config client-config.json admin adminpw http://localhost:7054 csr.json 435 ``` 436 437 You should see a message similar to `[INFO] enrollment information was successfully stored in` 438 which indicates where the certificate and key files were stored. 439 440 The enrollment certificate is stored at `$FABRIC_CA_ENROLLMENT_DIR/cert.pem` by default, but a different path can be specified by setting the `FABRIC_CA_CERT_FILE` environment variable. 441 442 The enrollment key is stored at `$FABRIC_CA_ENROLLMENT_DIR/key.pem` by default, but a different 443 path can be specified by setting the `FABRIC_CA_KEY_FILE` environment variable. 444 445 If `FABRIC_CA_ENROLLMENT_DIR` is not set, the value of the `FABRIC_CA_HOME` 446 environment variable is used in its place. 447 448 #### Register a new identity 449 450 The user performing the register request must be currently enrolled, and 451 must also have the proper authority to register the type of user being 452 registered. 453 454 In particular, the invoker's identity must have been registered with the attribute 455 "hf.Registrar.Roles". This attribute specifies the types of identities 456 that the registrar is allowed to register. 457 458 For example, the attributes for a registrar might be as follows, indicating 459 that this registrar identity can register peer, application, and user identities. 460 461 ``` 462 "attrs": [{"name":"hf.Registrar.Roles", "value":"peer,app,user"}] 463 ``` 464 465 To register a new identity, you must first create a JSON file similar to the one below 466 which defines information for the identity being registered. 467 This is a sample of registration information for a peer. 468 469 ``` 470 { 471 "id": "peer1", 472 "type": "peer", 473 "group": "bank_a", 474 "attrs": [{"name":"SomeAttrName","value":"SomeAttrValue"}] 475 } 476 ``` 477 478 The **id** field is the enrollment ID of the identity. 479 480 The **type** field is the type of the identity: orderer, peer, app, or user. 481 482 The **group** field must be a valid group name as found in the *server-config.json* 483 file. 484 485 The **attrs** field is optional and is not required for a peer, but is shown 486 here as example of how you associate attributes with any identity. 487 488 Assuming you store the information above in a file named **register.json**, 489 the following command uses the **admin** user's credentials to 490 register the **peer1** identity. 491 492 ``` 493 # export FABRIC_CA_HOME=$HOME/fabric-ca/clients/admin 494 # cd $FABRIC_CA_HOME 495 # fabric-ca client register -config client-config.json register.json http://localhost:7054 496 ``` 497 498 The output of a successful *fabric-ca client register* command is a 499 password similar to `One time password: gHIexUckKpHz`. Make a note of 500 your password to use in the following section to enroll a peer. 501 502 #### Enrolling a peer identity 503 504 Now that you have successfully registered a peer identity, 505 you may now enroll the peer given the enrollment ID and secret 506 (i.e. the *password* from the previous section). 507 508 First, create a CSR (Certificate Signing Request) JSON file similar to 509 the one described in the [Enrolling the bootstrap user](#EnrollBootstrap) section. 510 Name the file *csr.json* for the following example. 511 512 This is similar to enrolling the bootstrap user except that 513 we also demonstrate how to use environment variables to place 514 the key and certificate files in a specific location. 515 The following example shows how to place them into a Hyperledger Fabric 516 MSP (Membership Service Provider) directory structure. 517 The *MSP_DIR* environment variable refers to the root 518 directory of MSP in Hyperledger Fabric and the $MSP_DIR/signcerts 519 and $MSP_DIR/keystore directories must exist. 520 521 Also note that you must replace *\<secret>* with the secret which was 522 returned from the registration in the previous section. 523 524 ``` 525 # export FABRIC_CA_CERT_FILE=$MSP_DIR/signcerts/peer.pem 526 # export FABRIC_CA_KEY_FILE=$MSP_DIR/keystore/key.pem 527 # fabric-ca client enroll -config client-config.json peer1 <secret> https://localhost:7054 csr.json 528 ``` 529 530 The peer.pem and key.pem files should now exist at the locations specified 531 by the environment variables. 532 533 #### Revoke a certificate or user 534 535 In order to revoke a certificate or user, the calling identity must have the 536 `hf.Revoker` attribute. 537 538 You may revoke a specific certificate by specifying its 539 AKI (Authority Key Identifier) and its serial number, as shown below. 540 541 ``` 542 fabric-ca client revoke -config client-config.json -aki xxx -serial yyy -reason "you're bad" https://localhost:7054 543 ``` 544 545 The following command disables a user's identity and also 546 revokes all of the certificates associated with the identity. All 547 future requests received by the fabric-ca server from this identity 548 will be rejected. 549 550 ``` 551 fabric-ca client revoke -config client-config.json https://localhost:7054 ENROLLMENT-ID -reason "you're really bad" 552 ``` 553 554 #### Enabling TLS for a fabric-ca client 555 556 This section describes in more detail how to configure TLS for a fabric-ca client. 557 558 The following sections may be configured in the `client-config.json`. 559 560 ``` 561 { 562 "ca_certfiles":["CA_root_cert.pem"], 563 "client":[{"keyfile":"client-key.pem","certfile":"client-cert.pem"}] 564 } 565 ``` 566 567 The **ca_certfiles** option is the set of root certificates trusted by the client. 568 This will typically just be the root fabric-ca server's certificate found in 569 the server's home directory in the **server-cert.pem** file. 570 571 The **client** option is required only if mutual TLS is configured on the server. 572 573 ## Appendix 574 575 ### Postgres SSL Configuration 576 577 **Basic instructions for configuring SSL on Postgres server:** 578 1. In postgresql.conf, uncomment SSL and set to "on" (SSL=on) 579 2. Place Certificate and Key files Postgress data directory. 580 581 Instructions for generating self-signed certificates for: 582 https://www.postgresql.org/docs/9.1/static/ssl-tcp.html 583 584 Note: Self-signed certificates are for testing purposes and should not be used 585 in a production environment 586 587 **Postgres Server - Require Client Certificates** 588 1. Place certificates of the certificate authorities (CAs) you trust in the file 589 root.crt in the Postgres data directory 590 2. In postgresql.conf, set "ssl_ca_file" to point to the root cert of client (CA cert) 591 3. Set the clientcert parameter to 1 on the appropriate hostssl line(s) in pg_hba.conf. 592 593 For more details on configuring SSL on the Postgres server, please refer to the 594 following Postgres documentation: https://www.postgresql.org/docs/9.4/static/libpq-ssl.html 595 596 597 ### MySQL SSL Configuration 598 **Basic instructions for configuring SSL on MySQL server:** 599 600 1. Open or create my.cnf file for the server. Add or un-comment the lines below 601 in [mysqld] section. These should point to the key and certificates for the 602 server, and the root CA cert. 603 604 Instruction on creating server and client side certs: 605 http://dev.mysql.com/doc/refman/5.7/en/creating-ssl-files-using-openssl.html 606 607 [mysqld] 608 ssl-ca=ca-cert.pem 609 ssl-cert=server-cert.pem 610 ssl-key=server-key.pem 611 612 Can run the following query to confirm SSL has been enabled. 613 614 mysql> SHOW GLOBAL VARIABLES LIKE 'have_%ssl'; 615 616 Should see: 617 618 ``` 619 +---------------+-------+ 620 | Variable_name | Value | 621 +---------------+-------+ 622 | have_openssl | YES | 623 | have_ssl | YES | 624 +---------------+-------+ 625 ``` 626 627 2. After the server-side SSL configuration is finished, the next step is to 628 create a user who has a privilege to access the MySQL server over SSL. For that, 629 log in to the MySQL server, and type: 630 631 mysql> GRANT ALL PRIVILEGES ON *.* TO 'ssluser'@'%' IDENTIFIED BY 'password' REQUIRE SSL; 632 mysql> FLUSH PRIVILEGES; 633 634 If you want to give a specific ip address from which the user will access the 635 server change the '%' to the specific ip address. 636 637 **MySQL Server - Require Client Certificates** 638 Options for secure connections are similar to those used on the server side. 639 640 - ssl-ca identifies the Certificate Authority (CA) certificate. This option, 641 if used, must specify the same certificate used by the server. 642 - ssl-cert identifies the client public key certificate. 643 - ssl-key identifies the client private key. 644 645 Suppose that you want to connect using an account that has no special encryption 646 requirements or was created using a GRANT statement that includes the REQUIRE SSL 647 option. As a recommended set of secure-connection options, start the MySQL 648 server with at least --ssl-cert and --ssl-key, and invoke the fabric-ca server with 649 **ca_certfiles** option set in the fabric-ca server file. 650 651 To require that a client certificate also be specified, create the account using 652 the REQUIRE X509 option. Then the client must also specify the proper client key 653 and certificate files or the MySQL server will reject the connection. CA cert, 654 client cert, and client key are all required for the fabric-ca server.